|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import { parseValue } from '../../language/parser'; |
| 5 | +import { print } from '../../language/printer'; |
| 6 | + |
| 7 | +import { sortValueNode } from '../sortValueNode'; |
| 8 | + |
| 9 | +describe('sortValueNode', () => { |
| 10 | + function expectSortedValue(source: string) { |
| 11 | + return expect(print(sortValueNode(parseValue(source)))); |
| 12 | + } |
| 13 | + |
| 14 | + it('do not change non-object values', () => { |
| 15 | + expectSortedValue('1').to.equal('1'); |
| 16 | + expectSortedValue('3.14').to.equal('3.14'); |
| 17 | + expectSortedValue('null').to.equal('null'); |
| 18 | + expectSortedValue('true').to.equal('true'); |
| 19 | + expectSortedValue('false').to.equal('false'); |
| 20 | + expectSortedValue('"cba"').to.equal('"cba"'); |
| 21 | + expectSortedValue('"""cba"""').to.equal('"""cba"""'); |
| 22 | + expectSortedValue('[1, 3.14, null, false, "cba"]').to.equal( |
| 23 | + '[1, 3.14, null, false, "cba"]', |
| 24 | + ); |
| 25 | + expectSortedValue('[[1, 3.14, null, false, "cba"]]').to.equal( |
| 26 | + '[[1, 3.14, null, false, "cba"]]', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('sort input object fields', () => { |
| 31 | + expectSortedValue('{ b: 2, a: 1 }').to.equal('{a: 1, b: 2}'); |
| 32 | + expectSortedValue('{ a: { c: 3, b: 2 } }').to.equal('{a: {b: 2, c: 3}}'); |
| 33 | + expectSortedValue('[{ b: 2, a: 1 }, { d: 4, c: 3}]').to.equal( |
| 34 | + '[{a: 1, b: 2}, {c: 3, d: 4}]', |
| 35 | + ); |
| 36 | + expectSortedValue( |
| 37 | + '{ b: { g: 7, f: 6 }, c: 3 , a: { d: 4, e: 5 } }', |
| 38 | + ).to.equal('{a: {d: 4, e: 5}, b: {f: 6, g: 7}, c: 3}'); |
| 39 | + }); |
| 40 | +}); |
0 commit comments