|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +// @ts-nocheck |
| 16 | +/* eslint-disable no-undef */ |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const fs = require('fs'); |
| 20 | +const Decorators = require('./Decorators'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Load a test node from disk |
| 24 | + * @param {string} name the name of the file to load |
| 25 | + * @returns {object} the node |
| 26 | + */ |
| 27 | +function loadNode(name) { |
| 28 | + return JSON.parse(fs.readFileSync( __dirname + `/../test/data/decorators/${name}`, 'utf-8')); |
| 29 | +} |
| 30 | + |
| 31 | +describe('decorators', () => { |
| 32 | + it('handles string decorator', () => { |
| 33 | + const decorators = new Decorators( loadNode('string.json')); |
| 34 | + expect(decorators.getDecoratorValue( 'Test', 'type')).toBe('value'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('handles boolean decorator', () => { |
| 38 | + const decorators = new Decorators( loadNode('boolean.json')); |
| 39 | + expect(decorators.getDecoratorValue( 'Test', 'type')).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('handles number decorator', () => { |
| 43 | + const decorators = new Decorators( loadNode('number.json')); |
| 44 | + expect(decorators.getDecoratorValue( 'Test', 'type')).toBe(3.14); |
| 45 | + }); |
| 46 | + |
| 47 | + it('handles identifier decorator', () => { |
| 48 | + const decorators = new Decorators( loadNode('identifier.json')); |
| 49 | + expect(decorators.getDecoratorValue( 'Test', 'type')).toBe('typeIdentifier'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles getting a value for a missing decorator', () => { |
| 53 | + const decorators = new Decorators( loadNode('string.json')); |
| 54 | + expect(decorators.getDecoratorValue( 'Missing', 'type')).toBe(null); |
| 55 | + }); |
| 56 | + |
| 57 | + it('handles getting a missing value for a decorator', () => { |
| 58 | + const decorators = new Decorators( loadNode('string.json')); |
| 59 | + expect(decorators.getDecoratorValue( 'Test', 'missing')).toBe(undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + it('handles mutiple args decorator', () => { |
| 63 | + const decorators = new Decorators( loadNode('multi.json')); |
| 64 | + expect(decorators.getDecoratorValue( 'Test', 'type')).toBe('value'); |
| 65 | + expect(decorators.getDecoratorValue( 'Test', 'second')).toBe('value2'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('hasDecorator', () => { |
| 69 | + const decorators = new Decorators( loadNode('multi.json')); |
| 70 | + expect(decorators.hasDecorator( 'Test')).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('getArguments', () => { |
| 74 | + const decorators = new Decorators( loadNode('multi.json')); |
| 75 | + expect(decorators.getArguments( 'Test')).toStrictEqual({second: 'value2', type: 'value'}); |
| 76 | + }); |
| 77 | + |
| 78 | + it('fails with odd number of arguments', () => { |
| 79 | + expect(() => new Decorators( loadNode('invalid-odd.json'))).toThrow('Arguments must be [name, value] pairs'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('fails with argument names that are not strings', () => { |
| 83 | + expect(() => new Decorators( loadNode('invalid-arg-name.json'))).toThrow('Argument names must be strings'); |
| 84 | + }); |
| 85 | + |
| 86 | +}); |
0 commit comments