Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 63 additions & 15 deletions test.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
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

/**
* 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');

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');
assert.deepEqual(obj, JSON.parse(currentOutput));
}

currentOutput = "";
currentCode = [];
state = STATE_NONE;
} else {
currentOutput += line + '\n';
}
break;
}
}

console.log('Done');