|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder"; |
| 4 | +import ObjectSpec from "../../../../src/OpenSpec/Spec/Type/ObjectSpec"; |
| 5 | +import TypeSpec from "../../../../src/OpenSpec/Spec/Type/TypeSpec"; |
| 6 | +import ParseSpecException from "../../../../src/OpenSpec/ParseSpecException"; |
| 7 | +import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary"; |
| 8 | + |
| 9 | + |
| 10 | +function getSpecInstance() |
| 11 | +{ |
| 12 | + let specData = { |
| 13 | + type: 'object', |
| 14 | + fields: { |
| 15 | + field1: { type: 'string' }, |
| 16 | + field2: { type: 'string' }, |
| 17 | + }, |
| 18 | + requiredFields: [ 'field1' ], |
| 19 | + extensible: true |
| 20 | + }; |
| 21 | + |
| 22 | + return SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 23 | +} |
| 24 | + |
| 25 | +// @todo find a better way to compare arrays |
| 26 | +const equal_arrays = function(array1, array2) { |
| 27 | + if (array1.length !== array2.length) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + for (let i = 0; i < array1.length; i++) { |
| 32 | + if (array1[i] instanceof Array && array2[i] instanceof Array) { |
| 33 | + if (!equal_arrays(array1[i], array2[i])) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + } else if (array1[i] !== array2[i]) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return true; |
| 42 | +}; |
| 43 | + |
| 44 | +function array_unique(array) { |
| 45 | + return array.filter((elem, index, array) => { return array.indexOf(elem) === index; }); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +describe('ObjectParsingTest', function() { |
| 50 | + |
| 51 | + it('testParseSpecResult', function() { |
| 52 | + let spec = getSpecInstance(); |
| 53 | + assert(spec instanceof ObjectSpec); |
| 54 | + }); |
| 55 | + |
| 56 | + it('testSpecCorrectTypeName', function() { |
| 57 | + let spec = getSpecInstance(); |
| 58 | + assert(spec.getTypeName() === 'object'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('testSpecRequiredFields', function() { |
| 62 | + let spec = getSpecInstance(); |
| 63 | + |
| 64 | + let fields = spec.getRequiredFields(); |
| 65 | + fields.sort(); |
| 66 | + assert(equal_arrays(fields, ['type'])); |
| 67 | + }); |
| 68 | + |
| 69 | + it('testSpecOptionalFields', function() { |
| 70 | + let spec = getSpecInstance(); |
| 71 | + |
| 72 | + let fields = spec.getOptionalFields(); |
| 73 | + fields.sort(); |
| 74 | + assert(equal_arrays(fields, ['extensible', 'extensionFields', 'fields', 'requiredFields'])); |
| 75 | + }); |
| 76 | + |
| 77 | + it('testSpecAllFields', function() { |
| 78 | + let spec = getSpecInstance(); |
| 79 | + |
| 80 | + let reqFields = spec.getRequiredFields(); |
| 81 | + let optFields = spec.getOptionalFields(); |
| 82 | + let allFieldsCalculated = array_unique([...reqFields, ...optFields]); |
| 83 | + allFieldsCalculated.sort(); |
| 84 | + |
| 85 | + let allFields = spec.getAllFields(); |
| 86 | + allFields.sort(); |
| 87 | + |
| 88 | + assert(equal_arrays(allFieldsCalculated, allFields)); |
| 89 | + }); |
| 90 | + |
| 91 | + it('testUnexpectedFields', function() { |
| 92 | + let specData = { |
| 93 | + type: 'object', |
| 94 | + this_is_an_unexpected_field: 1234, |
| 95 | + and_this_is_other: ['a', 'b'] |
| 96 | + }; |
| 97 | + |
| 98 | + let exception = null; |
| 99 | + try { |
| 100 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 101 | + } catch (ex) { |
| 102 | + |
| 103 | + if (!(ex instanceof ParseSpecException)) { |
| 104 | + throw new Error('Unexpected exception.'); |
| 105 | + } |
| 106 | + |
| 107 | + exception = ex; |
| 108 | + } |
| 109 | + |
| 110 | + assert(exception.containsError(ParseSpecException.CODE_UNEXPECTED_FIELDS)); |
| 111 | + }); |
| 112 | + |
| 113 | + it('testFieldFieldsOfInvalidType', function() { |
| 114 | + let specData = { type: 'object', fields: 'this is a string' }; |
| 115 | + |
| 116 | + let exception = null; |
| 117 | + try { |
| 118 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 119 | + } catch (ex) { |
| 120 | + |
| 121 | + if (!(ex instanceof ParseSpecException)) { |
| 122 | + throw new Error('Unexpected exception.'); |
| 123 | + } |
| 124 | + |
| 125 | + exception = ex; |
| 126 | + } |
| 127 | + |
| 128 | + assert(exception.containsError(ParseSpecException.CODE_ARRAY_EXPECTED)); |
| 129 | + }); |
| 130 | + |
| 131 | + it('testFieldRequiredFieldsOfInvalidType', function() { |
| 132 | + let specData = { type: 'object', fields: [], requiredFields: 'An array should be here' }; |
| 133 | + |
| 134 | + let exception = null; |
| 135 | + try { |
| 136 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 137 | + } catch (ex) { |
| 138 | + |
| 139 | + if (!(ex instanceof ParseSpecException)) { |
| 140 | + throw new Error('Unexpected exception.'); |
| 141 | + } |
| 142 | + |
| 143 | + exception = ex; |
| 144 | + } |
| 145 | + |
| 146 | + assert(exception.containsError(ParseSpecException.CODE_ARRAY_EXPECTED)); |
| 147 | + }); |
| 148 | + |
| 149 | + it('testFieldRequiredFieldsWithInvalidItems', function() { |
| 150 | + let specData = { type: 'object', fields: [], requiredFields: ['correct1', 'correct2', 1234, 'correct3'] }; |
| 151 | + |
| 152 | + let exception = null; |
| 153 | + try { |
| 154 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 155 | + } catch (ex) { |
| 156 | + |
| 157 | + if (!(ex instanceof ParseSpecException)) { |
| 158 | + throw new Error('Unexpected exception.'); |
| 159 | + } |
| 160 | + |
| 161 | + exception = ex; |
| 162 | + } |
| 163 | + |
| 164 | + assert(exception.containsError(ParseSpecException.CODE_STRING_EXPECTED)); |
| 165 | + }); |
| 166 | + |
| 167 | + it('testFieldExtensibleOfInvalidType', function() { |
| 168 | + let specData = { type: 'object', extensible: 'this is a string' }; |
| 169 | + |
| 170 | + let exception = null; |
| 171 | + try { |
| 172 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 173 | + } catch (ex) { |
| 174 | + |
| 175 | + if (!(ex instanceof ParseSpecException)) { |
| 176 | + throw new Error('Unexpected exception.'); |
| 177 | + } |
| 178 | + |
| 179 | + exception = ex; |
| 180 | + } |
| 181 | + |
| 182 | + assert(exception.containsError(ParseSpecException.CODE_INVALID_SPEC_DATA)); |
| 183 | + }); |
| 184 | + |
| 185 | + it('testFieldExtensionFieldsOfInvalidType', function() { |
| 186 | + let specData = { type: 'object', extensible: true, extensionFields: 'this is a string' }; |
| 187 | + |
| 188 | + let exception = null; |
| 189 | + try { |
| 190 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 191 | + } catch (ex) { |
| 192 | + |
| 193 | + if (!(ex instanceof ParseSpecException)) { |
| 194 | + throw new Error('Unexpected exception.'); |
| 195 | + } |
| 196 | + |
| 197 | + exception = ex; |
| 198 | + } |
| 199 | + |
| 200 | + assert(exception.containsError(ParseSpecException.CODE_ARRAY_EXPECTED)); |
| 201 | + }); |
| 202 | + |
| 203 | + it('testExtensionFieldsWithoutExtensible', function() { |
| 204 | + let specData = { type: 'object', extensionFields: { type: 'string' } }; |
| 205 | + |
| 206 | + let exception = null; |
| 207 | + try { |
| 208 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 209 | + } catch (ex) { |
| 210 | + |
| 211 | + if (!(ex instanceof ParseSpecException)) { |
| 212 | + throw new Error('Unexpected exception.'); |
| 213 | + } |
| 214 | + |
| 215 | + exception = ex; |
| 216 | + } |
| 217 | + |
| 218 | + assert(exception.containsError(ParseSpecException.CODE_EXTENSIBLE_EXPECTED)); |
| 219 | + }); |
| 220 | + |
| 221 | + it('testExtensionFieldsWithExtensibleFalse', function() { |
| 222 | + let specData = { type: 'object', extensible: false, extensionFields: { type: 'string' } }; |
| 223 | + |
| 224 | + let exception = null; |
| 225 | + try { |
| 226 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 227 | + } catch (ex) { |
| 228 | + |
| 229 | + if (!(ex instanceof ParseSpecException)) { |
| 230 | + throw new Error('Unexpected exception.'); |
| 231 | + } |
| 232 | + |
| 233 | + exception = ex; |
| 234 | + } |
| 235 | + |
| 236 | + assert(exception.containsError(ParseSpecException.CODE_EXTENSIBLE_EXPECTED)); |
| 237 | + }); |
| 238 | + |
| 239 | + it('testValidObjectWithNoFieldSpecs', function() { |
| 240 | + let specData = { type: 'object' }; |
| 241 | + |
| 242 | + let errors; |
| 243 | + try { |
| 244 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 245 | + errors = []; |
| 246 | + } catch (ex) { |
| 247 | + |
| 248 | + if (!(ex instanceof ParseSpecException)) { |
| 249 | + throw new Error('Unexpected exception.'); |
| 250 | + } |
| 251 | + |
| 252 | + errors = ex._errors; // @todo ->getErrors(); |
| 253 | + } |
| 254 | + |
| 255 | + assert(errors.length === 0, "Spec for object with no field specs not validates as expected."); |
| 256 | + }); |
| 257 | + |
| 258 | +}); |
0 commit comments