From b4356270de7e02392e5e9e84161a892c0de0c597 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Mon, 8 Apr 2019 14:48:59 +0100 Subject: [PATCH 1/3] Test by parsing README --- test.js | 73 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 15 deletions(-) mode change 100644 => 100755 test.js diff --git a/test.js b/test.js old mode 100644 new mode 100755 index 08fbf52..c0f9337 --- a/test.js +++ b/test.js @@ -1,15 +1,58 @@ -var beautifier = require("."); - -console.log(beautifier( - { - str: "Hello World", - num: 42, - smallarray: [ 1, 2, 3, "foo", {} ], - smallobject: { foo: "bar", bar: 42 }, - bigarray: [ 1, 2, 3, "foo", { foo: "bar", bar: 42, arr: [ 1, 2, 3, "foo", {} ] } ], - bigobject: { foo: [ 1, 2, 3, "foo", {} ], bar: 42, a: {b: { c: 42 }}, foobar: "FooBar" } - }, - null, - 2, - process.argv[2] ? parseInt(process.argv[2], 10): 80 -)); +#!/usr/bin/env node + +var beautify = require("."); +var fs = require('fs'); +var assert = require('assert'); + +var obj = { str: "Hello World", num: 42, smallarray: [ 1, 2, 3, "foo", {} ], smallobject: { foo: "bar", bar: 42 }, bigarray: [ 1, 2, 3, "foo", { foo: "bar", bar: 42, arr: [ 1, 2, 3, "foo", {} ] } ], bigobject: { foo: [ 1, 2, 3, "foo", {} ], bar: 42, a: {b: { c: 42 }}, foobar: "FooBar" } }; + +var readmeArray = fs.readFileSync('README.md', 'utf-8').split('\n'); + +const STATE_NONE = 0; +const STATE_CODE = 1; +const STATE_JSON = 2; + +var state = 0; +var currentOutput = ""; +var currentCode = []; + +for (line of readmeArray) { + switch (state) { + case STATE_NONE: + if (line == '```js') { + state = STATE_CODE; + } + if (line == '```json') { + state = STATE_JSON; + } + break; + case STATE_CODE: + if (line == '```') { + state = STATE_NONE; + } else { + var matches = line.match(/console.log\((.*)\)/); + + if (matches) { + currentCode.push(matches[1]); + } + } + break; + case STATE_JSON: + if (line == '```') { + // Check data + for (i of currentCode) { + console.log('Testing: ' + i); + assert.strictEqual(currentOutput, eval(i) + '\n'); + } + + currentOutput = ""; + currentCode = []; + state = STATE_NONE; + } else { + currentOutput += line + '\n'; + } + break; + } +} + +console.log('Done'); From f6a64f28a4e3affb178abcd535091e5d304d6248 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Mon, 8 Apr 2019 14:49:19 +0100 Subject: [PATCH 2/3] Test that we JSON.parse() back to obj --- test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test.js b/test.js index c0f9337..21d2d4c 100755 --- a/test.js +++ b/test.js @@ -43,6 +43,7 @@ for (line of readmeArray) { for (i of currentCode) { console.log('Testing: ' + i); assert.strictEqual(currentOutput, eval(i) + '\n'); + assert.deepEqual(obj, JSON.parse(currentOutput)); } currentOutput = ""; From 6806f05d66b76347cf52269c60b3695338f7c82e Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Mon, 8 Apr 2019 14:55:41 +0100 Subject: [PATCH 3/3] Description --- test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test.js b/test.js index 21d2d4c..49e6d94 100755 --- a/test.js +++ b/test.js @@ -1,5 +1,9 @@ #!/usr/bin/env node +/** + * Highly hacky test script that parses json and js code examples in README to look for test cases + */ + var beautify = require("."); var fs = require('fs'); var assert = require('assert');