|
| 1 | +import { IterableUtil } from './IterableUtil'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for IterableUtil class. |
| 5 | + * |
| 6 | + * Note: The current implementation of readBoolean has a limitation - it doesn't actually |
| 7 | + * validate that the value is a boolean. It returns any truthy value as-is, or false for falsy values. |
| 8 | + * This behavior is tested below, but the implementation may need to be updated to properly |
| 9 | + * validate boolean types as suggested in the TODO comment in the source code. |
| 10 | + */ |
| 11 | +describe('IterableUtil', () => { |
| 12 | + describe('readBoolean', () => { |
| 13 | + it('should return true when the key exists and value is true', () => { |
| 14 | + // GIVEN a dictionary with a true boolean value |
| 15 | + const dict = { testKey: true }; |
| 16 | + |
| 17 | + // WHEN reading the boolean value |
| 18 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 19 | + |
| 20 | + // THEN it should return true |
| 21 | + expect(result).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return false when the key exists and value is false', () => { |
| 25 | + // GIVEN a dictionary with a false boolean value |
| 26 | + const dict = { testKey: false }; |
| 27 | + |
| 28 | + // WHEN reading the boolean value |
| 29 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 30 | + |
| 31 | + // THEN it should return false |
| 32 | + expect(result).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return false when the key does not exist', () => { |
| 36 | + // GIVEN a dictionary without the key |
| 37 | + const dict = { otherKey: true }; |
| 38 | + |
| 39 | + // WHEN reading a non-existent key |
| 40 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 41 | + |
| 42 | + // THEN it should return false |
| 43 | + expect(result).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return false when the key exists but value is undefined', () => { |
| 47 | + // GIVEN a dictionary with undefined value |
| 48 | + const dict = { testKey: undefined }; |
| 49 | + |
| 50 | + // WHEN reading the boolean value |
| 51 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 52 | + |
| 53 | + // THEN it should return false |
| 54 | + expect(result).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return false when the key exists but value is null', () => { |
| 58 | + // GIVEN a dictionary with null value |
| 59 | + const dict = { testKey: null }; |
| 60 | + |
| 61 | + // WHEN reading the boolean value |
| 62 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 63 | + |
| 64 | + // THEN it should return false |
| 65 | + expect(result).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return false when the key exists but value is 0', () => { |
| 69 | + // GIVEN a dictionary with 0 value |
| 70 | + const dict = { testKey: 0 }; |
| 71 | + |
| 72 | + // WHEN reading the boolean value |
| 73 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 74 | + |
| 75 | + // THEN it should return false |
| 76 | + expect(result).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return false when the key exists but value is empty string', () => { |
| 80 | + // GIVEN a dictionary with empty string value |
| 81 | + const dict = { testKey: '' }; |
| 82 | + |
| 83 | + // WHEN reading the boolean value |
| 84 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 85 | + |
| 86 | + // THEN it should return false |
| 87 | + expect(result).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return false when the key exists but value is NaN', () => { |
| 91 | + // GIVEN a dictionary with NaN value |
| 92 | + const dict = { testKey: NaN }; |
| 93 | + |
| 94 | + // WHEN reading the boolean value |
| 95 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 96 | + |
| 97 | + // THEN it should return false |
| 98 | + expect(result).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + // TODO: Verify that we want this to return a string instead of a boolean |
| 102 | + it('should return truthy string as boolean when key exists', () => { |
| 103 | + // GIVEN a dictionary with truthy string value |
| 104 | + const dict = { testKey: 'true' }; |
| 105 | + |
| 106 | + // WHEN reading the boolean value |
| 107 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 108 | + |
| 109 | + // THEN it should return the string cast to boolean (truthy) |
| 110 | + expect(result).toBe('true'); |
| 111 | + }); |
| 112 | + |
| 113 | + // TODO: Verify that we want this to return a number instead of a boolean |
| 114 | + it('should return truthy number as boolean when key exists', () => { |
| 115 | + // GIVEN a dictionary with truthy number value |
| 116 | + const dict = { testKey: 1 }; |
| 117 | + |
| 118 | + // WHEN reading the boolean value |
| 119 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 120 | + |
| 121 | + // THEN it should return the number cast to boolean (truthy) |
| 122 | + expect(result).toBe(1); |
| 123 | + }); |
| 124 | + |
| 125 | + // TODO: Verify that we want this to return an object instead of a boolean |
| 126 | + it('should return truthy object as boolean when key exists', () => { |
| 127 | + // GIVEN a dictionary with truthy object value |
| 128 | + const dict = { testKey: {} }; |
| 129 | + |
| 130 | + // WHEN reading the boolean value |
| 131 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 132 | + |
| 133 | + // THEN it should return the object cast to boolean (truthy) |
| 134 | + expect(result).toEqual({}); |
| 135 | + }); |
| 136 | + |
| 137 | + // TODO: Verify that we want this to return an array instead of a boolean |
| 138 | + it('should return truthy array as boolean when key exists', () => { |
| 139 | + // GIVEN a dictionary with truthy array value |
| 140 | + const dict = { testKey: [] }; |
| 141 | + |
| 142 | + // WHEN reading the boolean value |
| 143 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 144 | + |
| 145 | + // THEN it should return the array cast to boolean (truthy) |
| 146 | + expect(result).toEqual([]); |
| 147 | + }); |
| 148 | + |
| 149 | + // TODO: Verify that we want this to return a function instead of a boolean |
| 150 | + it('should return truthy function as boolean when key exists', () => { |
| 151 | + // GIVEN a dictionary with truthy function value |
| 152 | + const dict = { testKey: () => {} }; |
| 153 | + |
| 154 | + // WHEN reading the boolean value |
| 155 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 156 | + |
| 157 | + // THEN it should return the function cast to boolean (truthy) |
| 158 | + expect(result).toBeInstanceOf(Function); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should handle empty dictionary', () => { |
| 162 | + // GIVEN an empty dictionary |
| 163 | + const dict = {}; |
| 164 | + |
| 165 | + // WHEN reading a key from empty dictionary |
| 166 | + const result = IterableUtil.readBoolean(dict, 'testKey'); |
| 167 | + |
| 168 | + // THEN it should return false |
| 169 | + expect(result).toBe(false); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should handle dictionary with multiple keys', () => { |
| 173 | + // GIVEN a dictionary with multiple keys |
| 174 | + const dict = { |
| 175 | + key1: true, |
| 176 | + key2: false, |
| 177 | + key3: 'string', |
| 178 | + key4: 123 |
| 179 | + }; |
| 180 | + |
| 181 | + // WHEN reading different keys |
| 182 | + const result1 = IterableUtil.readBoolean(dict, 'key1'); |
| 183 | + const result2 = IterableUtil.readBoolean(dict, 'key2'); |
| 184 | + const result3 = IterableUtil.readBoolean(dict, 'key3'); |
| 185 | + const result4 = IterableUtil.readBoolean(dict, 'key4'); |
| 186 | + const result5 = IterableUtil.readBoolean(dict, 'nonExistentKey'); |
| 187 | + |
| 188 | + // THEN it should return correct values |
| 189 | + expect(result1).toBe(true); |
| 190 | + expect(result2).toBe(false); |
| 191 | + expect(result3).toBe('string'); // truthy string is returned as-is |
| 192 | + expect(result4).toBe(123); // truthy number is returned as-is |
| 193 | + expect(result5).toBe(false); // key doesn't exist |
| 194 | + }); |
| 195 | + |
| 196 | + it('should handle special boolean values', () => { |
| 197 | + // GIVEN a dictionary with special boolean values |
| 198 | + const dict = { |
| 199 | + trueValue: true, |
| 200 | + falseValue: false |
| 201 | + }; |
| 202 | + |
| 203 | + // WHEN reading boolean values |
| 204 | + const trueResult = IterableUtil.readBoolean(dict, 'trueValue'); |
| 205 | + const falseResult = IterableUtil.readBoolean(dict, 'falseValue'); |
| 206 | + |
| 207 | + // THEN it should return the actual boolean values |
| 208 | + expect(trueResult).toBe(true); |
| 209 | + expect(falseResult).toBe(false); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should handle case sensitivity in keys', () => { |
| 213 | + // GIVEN a dictionary with case-sensitive keys |
| 214 | + const dict = { TestKey: true, testkey: false }; |
| 215 | + |
| 216 | + // WHEN reading with different case |
| 217 | + const result1 = IterableUtil.readBoolean(dict, 'TestKey'); |
| 218 | + const result2 = IterableUtil.readBoolean(dict, 'testkey'); |
| 219 | + const result3 = IterableUtil.readBoolean(dict, 'TESTKEY'); |
| 220 | + |
| 221 | + // THEN it should be case sensitive |
| 222 | + expect(result1).toBe(true); |
| 223 | + expect(result2).toBe(false); |
| 224 | + expect(result3).toBe(false); // key doesn't exist |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
0 commit comments