|
| 1 | +const path = require('path'); |
| 2 | +const {readFileSync} = require('fs'); |
| 3 | +const globby = require('globby'); |
| 4 | + |
| 5 | +const {StatsSerializeStream} = require('../lib/statsUtils'); |
| 6 | + |
| 7 | +describe('StatsSerializeStream', () => { |
| 8 | + it('should properly stringify primitives', function () { |
| 9 | + expectProperJson(0); |
| 10 | + expectProperJson(1); |
| 11 | + expectProperJson(-1); |
| 12 | + expectProperJson(42.42); |
| 13 | + expectProperJson(-42.42); |
| 14 | + expectProperJson(false); |
| 15 | + expectProperJson(true); |
| 16 | + expectProperJson(null); |
| 17 | + expectProperJson(null); |
| 18 | + expectProperJson(''); |
| 19 | + expectProperJson('"'); |
| 20 | + expectProperJson('foo bar'); |
| 21 | + expectProperJson('"foo bar"'); |
| 22 | + expectProperJson('Вива Лас-Вегас!'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should properly stringify simple arrays', function () { |
| 26 | + expectProperJson([]); |
| 27 | + expectProperJson([1, undefined, 2]); |
| 28 | + // eslint-disable-next-line |
| 29 | + expectProperJson([1, , 2]); |
| 30 | + expectProperJson([false, 'f\'o"o', -1, 42.42]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should properly stringify objects', function () { |
| 34 | + expectProperJson({}); |
| 35 | + expectProperJson({a: 1, 'foo-bar': null, undef: undefined, '"Гусь!"': true}); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should properly stringify complex structures', function () { |
| 39 | + expectProperJson({ |
| 40 | + foo: [], |
| 41 | + bar: { |
| 42 | + baz: [ |
| 43 | + 1, |
| 44 | + {a: 1, b: ['foo', 'bar'], c: []}, |
| 45 | + 'foo', |
| 46 | + {a: 1, b: undefined, c: [{d: true}]}, |
| 47 | + null, |
| 48 | + undefined |
| 49 | + ] |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + globby.sync('stats/**/*.json', {cwd: __dirname}).forEach(filepath => { |
| 55 | + it(`should properly stringify JSON from "${filepath}"`, function () { |
| 56 | + const content = readFileSync(path.resolve(__dirname, filepath), 'utf8'); |
| 57 | + const json = JSON.parse(content); |
| 58 | + expectProperJson(json); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +async function expectProperJson(json) { |
| 64 | + expect(await stringify(json)).to.equal(JSON.stringify(json, null, 2)); |
| 65 | +} |
| 66 | + |
| 67 | +async function stringify(json) { |
| 68 | + return new Promise((resolve, reject) => { |
| 69 | + let result = ''; |
| 70 | + |
| 71 | + new StatsSerializeStream(json) |
| 72 | + .on('data', chunk => result += chunk) |
| 73 | + .on('end', () => resolve(result)) |
| 74 | + .on('error', reject); |
| 75 | + }); |
| 76 | +} |
0 commit comments