|
| 1 | +import { TypedMap, YAMLValue } from './temp_lib/index'; |
| 2 | + |
| 3 | +export function testYAMLMethods(): void { |
| 4 | + let val: YAMLValue; |
| 5 | + |
| 6 | + val = YAMLValue.newNull(); |
| 7 | + assert(val.isNull()); |
| 8 | + |
| 9 | + val = YAMLValue.newBool(true); |
| 10 | + assert(val.isBool()); |
| 11 | + assert(val.toBool() == true); |
| 12 | + |
| 13 | + val = YAMLValue.newI64(1); |
| 14 | + assert(val.isNumber()); |
| 15 | + assert(val.toNumber() == '1'); |
| 16 | + assert(val.toI64() == 1); |
| 17 | + |
| 18 | + val = YAMLValue.newU64(1); |
| 19 | + assert(val.isNumber()); |
| 20 | + assert(val.toNumber() == '1'); |
| 21 | + assert(val.toU64() == 1); |
| 22 | + |
| 23 | + val = YAMLValue.newF64(1.2); |
| 24 | + assert(val.isNumber()); |
| 25 | + assert(val.toNumber() == '1.2'); |
| 26 | + assert(val.toF64() == 1.2); |
| 27 | + |
| 28 | + val = YAMLValue.newString('a'); |
| 29 | + assert(val.isString()); |
| 30 | + assert(val.toString() == 'a'); |
| 31 | + |
| 32 | + val = YAMLValue.newArray([YAMLValue.newU64(1)]); |
| 33 | + assert(val.isArray()); |
| 34 | + assert(val.toArray().length == 1); |
| 35 | + assert(val.toArray()[0].toNumber() == '1'); |
| 36 | + |
| 37 | + let obj: TypedMap<YAMLValue, YAMLValue> = new TypedMap(); |
| 38 | + obj.set(YAMLValue.newString('a'), YAMLValue.newString('b')); |
| 39 | + |
| 40 | + val = YAMLValue.newObject(obj); |
| 41 | + assert(val.isObject()); |
| 42 | + assert(val.toObject().entries.length == 1); |
| 43 | + assert(val.toObject().mustGet(YAMLValue.newString('a')).toString() == 'b'); |
| 44 | + |
| 45 | + val = YAMLValue.newTagged('a', YAMLValue.newString('b')); |
| 46 | + assert(val.isTagged()); |
| 47 | + assert((val.toTagged().tag = 'a')); |
| 48 | + assert(val.toTagged().value.toString() == 'b'); |
| 49 | +} |
| 50 | + |
| 51 | +export function testYAMLEqNeOverloads(): void { |
| 52 | + assert(YAMLValue.newBool(true) == YAMLValue.newBool(true)); |
| 53 | + assert(!(YAMLValue.newBool(true) != YAMLValue.newBool(true))); |
| 54 | + assert(YAMLValue.newBool(true) != YAMLValue.newBool(false)); |
| 55 | + |
| 56 | + assert(YAMLValue.newU64(1) == YAMLValue.newU64(1)); |
| 57 | + assert(!(YAMLValue.newU64(1) != YAMLValue.newU64(1))); |
| 58 | + assert(YAMLValue.newU64(1) != YAMLValue.newU64(2)); |
| 59 | + |
| 60 | + assert(YAMLValue.newString('a') == YAMLValue.newString('a')); |
| 61 | + assert(!(YAMLValue.newString('a') != YAMLValue.newString('a'))); |
| 62 | + assert(YAMLValue.newString('a') != YAMLValue.newString('b')); |
| 63 | + |
| 64 | + assert(YAMLValue.newArray([YAMLValue.newU64(1)]) == YAMLValue.newArray([YAMLValue.newU64(1)])); |
| 65 | + assert(!(YAMLValue.newArray([YAMLValue.newU64(1)]) != YAMLValue.newArray([YAMLValue.newU64(1)]))); |
| 66 | + assert( |
| 67 | + YAMLValue.newArray([YAMLValue.newU64(1)]) != |
| 68 | + YAMLValue.newArray([YAMLValue.newU64(1), YAMLValue.newU64(2)]), |
| 69 | + ); |
| 70 | + assert(YAMLValue.newArray([YAMLValue.newU64(1)]) != YAMLValue.newArray([YAMLValue.newU64(2)])); |
| 71 | + |
| 72 | + const objA: TypedMap<YAMLValue, YAMLValue> = new TypedMap(); |
| 73 | + objA.set(YAMLValue.newString('a'), YAMLValue.newString('b')); |
| 74 | + |
| 75 | + const objB: TypedMap<YAMLValue, YAMLValue> = new TypedMap(); |
| 76 | + objB.set(YAMLValue.newString('a'), YAMLValue.newString('b')); |
| 77 | + |
| 78 | + assert(YAMLValue.newObject(objA) == YAMLValue.newObject(objB)); |
| 79 | + assert(!(YAMLValue.newObject(objA) != YAMLValue.newObject(objB))); |
| 80 | + |
| 81 | + objA.set(YAMLValue.newString('c'), YAMLValue.newString('d')); |
| 82 | + assert(YAMLValue.newObject(objA) != YAMLValue.newObject(objB)); |
| 83 | + |
| 84 | + objB.set(YAMLValue.newString('c'), YAMLValue.newString('e')); |
| 85 | + assert(YAMLValue.newObject(objA) != YAMLValue.newObject(objB)); |
| 86 | + |
| 87 | + assert( |
| 88 | + YAMLValue.newTagged('a', YAMLValue.newString('b')) == |
| 89 | + YAMLValue.newTagged('a', YAMLValue.newString('b')), |
| 90 | + ); |
| 91 | + assert( |
| 92 | + !( |
| 93 | + YAMLValue.newTagged('a', YAMLValue.newString('b')) != |
| 94 | + YAMLValue.newTagged('a', YAMLValue.newString('b')) |
| 95 | + ), |
| 96 | + ); |
| 97 | + assert( |
| 98 | + YAMLValue.newTagged('a', YAMLValue.newString('b')) != |
| 99 | + YAMLValue.newTagged('c', YAMLValue.newString('d')), |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +export function testYAMLIndexOverload(): void { |
| 104 | + const objA: TypedMap<YAMLValue, YAMLValue> = new TypedMap(); |
| 105 | + const objB: TypedMap<YAMLValue, YAMLValue> = new TypedMap(); |
| 106 | + |
| 107 | + objB.set(YAMLValue.newString('b'), YAMLValue.newString('c')); |
| 108 | + objA.set(YAMLValue.newString('a'), YAMLValue.newArray([YAMLValue.newObject(objB)])); |
| 109 | + |
| 110 | + assert(YAMLValue.newObject(objA)['a']['0']['b'].toString() == 'c'); |
| 111 | +} |
| 112 | + |
| 113 | +export function testComplexYAMLParsing(): void { |
| 114 | + const metadata = new TypedMap<YAMLValue, YAMLValue>(); |
| 115 | + metadata.set( |
| 116 | + YAMLValue.newString('created_at'), |
| 117 | + YAMLValue.newTagged('timestamp', YAMLValue.newString('2025-01-01')), |
| 118 | + ); |
| 119 | + metadata.set(YAMLValue.newString('author'), YAMLValue.newNull()); |
| 120 | + |
| 121 | + const limits = new TypedMap<YAMLValue, YAMLValue>(); |
| 122 | + limits.set(YAMLValue.newString('max_retry'), YAMLValue.newI64(3)); |
| 123 | + limits.set(YAMLValue.newString('timeout'), YAMLValue.newI64(1000)); |
| 124 | + |
| 125 | + const settings = new TypedMap<YAMLValue, YAMLValue>(); |
| 126 | + settings.set(YAMLValue.newString('debug'), YAMLValue.newBool(true)); |
| 127 | + settings.set(YAMLValue.newString('mode'), YAMLValue.newString('production')); |
| 128 | + settings.set(YAMLValue.newString('limits'), YAMLValue.newObject(limits)); |
| 129 | + |
| 130 | + const tags = [ |
| 131 | + YAMLValue.newString('important'), |
| 132 | + YAMLValue.newString('test'), |
| 133 | + YAMLValue.newString('v1'), |
| 134 | + ]; |
| 135 | + |
| 136 | + const item1 = new TypedMap<YAMLValue, YAMLValue>(); |
| 137 | + item1.set(YAMLValue.newString('name'), YAMLValue.newString('item1')); |
| 138 | + item1.set(YAMLValue.newString('value'), YAMLValue.newI64(100)); |
| 139 | + |
| 140 | + const item2 = new TypedMap<YAMLValue, YAMLValue>(); |
| 141 | + item2.set(YAMLValue.newString('name'), YAMLValue.newString('item2')); |
| 142 | + item2.set(YAMLValue.newString('value'), YAMLValue.newI64(200)); |
| 143 | + |
| 144 | + const nestedArray = [YAMLValue.newObject(item1), YAMLValue.newObject(item2)]; |
| 145 | + |
| 146 | + // Create the root object |
| 147 | + const root = new TypedMap<YAMLValue, YAMLValue>(); |
| 148 | + root.set(YAMLValue.newString('name'), YAMLValue.newString('Test Config')); |
| 149 | + root.set(YAMLValue.newString('version'), YAMLValue.newF64(1.0)); |
| 150 | + root.set(YAMLValue.newString('enabled'), YAMLValue.newBool(true)); |
| 151 | + root.set(YAMLValue.newString('count'), YAMLValue.newI64(42)); |
| 152 | + root.set(YAMLValue.newString('pi'), YAMLValue.newF64(3.14159)); |
| 153 | + root.set(YAMLValue.newString('settings'), YAMLValue.newObject(settings)); |
| 154 | + root.set(YAMLValue.newString('tags'), YAMLValue.newArray(tags)); |
| 155 | + root.set(YAMLValue.newString('nested_array'), YAMLValue.newArray(nestedArray)); |
| 156 | + root.set(YAMLValue.newString('metadata'), YAMLValue.newObject(metadata)); |
| 157 | + |
| 158 | + const obj = YAMLValue.newObject(root); |
| 159 | + |
| 160 | + // Now validate the structure |
| 161 | + assert(obj.isObject()); |
| 162 | + const parsed = obj.toObject(); |
| 163 | + |
| 164 | + // Validate simple fields |
| 165 | + assert(parsed.mustGet(YAMLValue.newString('name')).toString() == 'Test Config'); |
| 166 | + assert(parsed.mustGet(YAMLValue.newString('version')).toF64() == 1.0); |
| 167 | + assert(parsed.mustGet(YAMLValue.newString('enabled')).toBool() == true); |
| 168 | + assert(parsed.mustGet(YAMLValue.newString('count')).toI64() == 42); |
| 169 | + assert(parsed.mustGet(YAMLValue.newString('pi')).toF64() == 3.14159); |
| 170 | + |
| 171 | + // Validate nested object (settings) |
| 172 | + const parsedSettings = parsed.mustGet(YAMLValue.newString('settings')).toObject(); |
| 173 | + assert(parsedSettings.mustGet(YAMLValue.newString('debug')).toBool() == true); |
| 174 | + assert(parsedSettings.mustGet(YAMLValue.newString('mode')).toString() == 'production'); |
| 175 | + |
| 176 | + // Validate deeply nested object (settings.limits) |
| 177 | + const parsedLimits = parsedSettings.mustGet(YAMLValue.newString('limits')).toObject(); |
| 178 | + assert(parsedLimits.mustGet(YAMLValue.newString('max_retry')).toI64() == 3); |
| 179 | + assert(parsedLimits.mustGet(YAMLValue.newString('timeout')).toI64() == 1000); |
| 180 | + |
| 181 | + // Validate array of strings (tags) |
| 182 | + const parsedTags = parsed.mustGet(YAMLValue.newString('tags')).toArray(); |
| 183 | + assert(parsedTags.length == 3); |
| 184 | + assert(parsedTags[0].toString() == 'important'); |
| 185 | + assert(parsedTags[1].toString() == 'test'); |
| 186 | + assert(parsedTags[2].toString() == 'v1'); |
| 187 | + |
| 188 | + // Validate array of objects (nested_array) |
| 189 | + const parsedNestedArray = parsed.mustGet(YAMLValue.newString('nested_array')).toArray(); |
| 190 | + assert(parsedNestedArray.length == 2); |
| 191 | + |
| 192 | + const parsedItem1 = parsedNestedArray[0].toObject(); |
| 193 | + assert(parsedItem1.mustGet(YAMLValue.newString('name')).toString() == 'item1'); |
| 194 | + assert(parsedItem1.mustGet(YAMLValue.newString('value')).toI64() == 100); |
| 195 | + |
| 196 | + const parsedItem2 = parsedNestedArray[1].toObject(); |
| 197 | + assert(parsedItem2.mustGet(YAMLValue.newString('name')).toString() == 'item2'); |
| 198 | + assert(parsedItem2.mustGet(YAMLValue.newString('value')).toI64() == 200); |
| 199 | + |
| 200 | + // Validate tagged value and null |
| 201 | + const parsedMetadata = parsed.mustGet(YAMLValue.newString('metadata')).toObject(); |
| 202 | + assert(parsedMetadata.mustGet(YAMLValue.newString('created_at')).isTagged()); |
| 203 | + const parsedCreatedAt = parsedMetadata.mustGet(YAMLValue.newString('created_at')).toTagged(); |
| 204 | + assert(parsedCreatedAt.tag == 'timestamp'); |
| 205 | + assert(parsedCreatedAt.value.toString() == '2025-01-01'); |
| 206 | + assert(parsedMetadata.mustGet(YAMLValue.newString('author')).isNull()); |
| 207 | +} |
0 commit comments