|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import SpecBuilder from "../../../../src/OpenSpec/SpecBuilder"; |
| 4 | +import MixedSpec from "../../../../src/OpenSpec/Spec/Type/MixedSpec"; |
| 5 | +import TypeSpec from "../../../../src/OpenSpec/Spec/Type/TypeSpec"; |
| 6 | +import ParseSpecException from "../../../../src/OpenSpec/ParseSpecException"; |
| 7 | +import SpecLibrary from "../../../../src/OpenSpec/SpecLibrary"; |
| 8 | +import ArraySpec from "../../../../src/OpenSpec/Spec/Type/ArraySpec"; |
| 9 | + |
| 10 | + |
| 11 | +function getSpecInstance() |
| 12 | +{ |
| 13 | + let specData = { type: 'mixed', options: [] }; |
| 14 | + let spec = SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 15 | + |
| 16 | + return spec; |
| 17 | +} |
| 18 | + |
| 19 | +// @todo find a better way to compare arrays |
| 20 | +const equal_arrays = function(array1, array2) { |
| 21 | + if (array1.length !== array2.length) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + for (let i = 0; i < array1.length; i++) { |
| 26 | + if (array1[i] instanceof Array && array2[i] instanceof Array) { |
| 27 | + if (!equal_arrays(array1[i], array2[i])) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + } else if (array1[i] !== array2[i]) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return true; |
| 36 | +}; |
| 37 | + |
| 38 | +function array_unique(array) { |
| 39 | + return array.filter((elem, index, array) => { return array.indexOf(elem) === index; }); |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +describe('MixedParsingTest', function() { |
| 44 | + |
| 45 | + it('testParseSpecResult', function() { |
| 46 | + let spec = getSpecInstance(); |
| 47 | + |
| 48 | + assert(spec instanceof MixedSpec); |
| 49 | + }); |
| 50 | + |
| 51 | + it('testSpecCorrectTypeName', function() { |
| 52 | + let spec = getSpecInstance(); |
| 53 | + |
| 54 | + assert(spec.getTypeName() === 'mixed'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('testSpecRequiredFields', function() { |
| 58 | + let spec = getSpecInstance(); |
| 59 | + |
| 60 | + let fields = spec.getRequiredFields(); |
| 61 | + fields.sort(); |
| 62 | + assert(equal_arrays(fields, ['options', 'type'])); |
| 63 | + }); |
| 64 | + |
| 65 | + it('testSpecOptionalFields', function() { |
| 66 | + let spec = getSpecInstance(); |
| 67 | + |
| 68 | + let fields = spec.getOptionalFields(); |
| 69 | + fields.sort(); |
| 70 | + assert(equal_arrays(fields, [])); |
| 71 | + }); |
| 72 | + |
| 73 | + it('testSpecAllFields', function() { |
| 74 | + let spec = getSpecInstance(); |
| 75 | + |
| 76 | + let reqFields = spec.getRequiredFields(); |
| 77 | + let optFields = spec.getOptionalFields(); |
| 78 | + let allFieldsCalculated = array_unique([...reqFields, ...optFields]); |
| 79 | + allFieldsCalculated.sort(); |
| 80 | + |
| 81 | + let allFields = spec.getAllFields(); |
| 82 | + allFields.sort(); |
| 83 | + |
| 84 | + assert(equal_arrays(allFieldsCalculated, allFields)); |
| 85 | + }); |
| 86 | + |
| 87 | + it('testMissingRequiredFields', function() { |
| 88 | + let specData = { type: 'mixed' }; |
| 89 | + |
| 90 | + let exception = null; |
| 91 | + try { |
| 92 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 93 | + } catch (ex) { |
| 94 | + |
| 95 | + if (!(ex instanceof ParseSpecException)) { |
| 96 | + throw new Error('Unexpected exception.'); |
| 97 | + } |
| 98 | + |
| 99 | + exception = ex; |
| 100 | + } |
| 101 | + |
| 102 | + assert(exception.containsError(ParseSpecException.CODE_MISSING_REQUIRED_FIELD)); |
| 103 | + }); |
| 104 | + |
| 105 | + it('testUnexpectedFields', function() { |
| 106 | + let specData = { |
| 107 | + type: 'mixed', |
| 108 | + this_is_an_unexpected_field: 1234, |
| 109 | + and_this_is_other: ['a', 'b'] |
| 110 | + }; |
| 111 | + |
| 112 | + let exception = null; |
| 113 | + try { |
| 114 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 115 | + } catch (ex) { |
| 116 | + |
| 117 | + if (!(ex instanceof ParseSpecException)) { |
| 118 | + throw new Error('Unexpected exception.'); |
| 119 | + } |
| 120 | + |
| 121 | + exception = ex; |
| 122 | + } |
| 123 | + |
| 124 | + assert(exception.containsError(ParseSpecException.CODE_UNEXPECTED_FIELDS)); |
| 125 | + }); |
| 126 | + |
| 127 | + it('testFieldItemsOfInvalidType', function() { |
| 128 | + let specData = { type: 'mixed', options: 'this is a string' }; |
| 129 | + |
| 130 | + let exception = null; |
| 131 | + try { |
| 132 | + SpecBuilder.getInstance().build(specData, new SpecLibrary()); |
| 133 | + } catch (ex) { |
| 134 | + |
| 135 | + if (!(ex instanceof ParseSpecException)) { |
| 136 | + throw new Error('Unexpected exception.'); |
| 137 | + } |
| 138 | + |
| 139 | + exception = ex; |
| 140 | + } |
| 141 | + |
| 142 | + assert(exception.containsError(ParseSpecException.CODE_ARRAY_EXPECTED)); |
| 143 | + }); |
| 144 | + |
| 145 | +}); |
0 commit comments