From 6270bdb8a983b0e45c6312b6cbf68552f2e9bb86 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Fri, 26 Jan 2018 09:52:08 +0700 Subject: [PATCH] Use ava for tests --- package.json | 5 ++++- test.js | 28 ++++++++++++++++++++++++++++ test.sh | 9 --------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 test.js delete mode 100755 test.sh diff --git a/package.json b/package.json index 7db5482..fda9073 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "index.js" ], "scripts": { - "test": "bash test.sh" + "test": "ava" }, "keywords": [ "json", @@ -25,5 +25,8 @@ "cardinal": "^1.0.0", "get-stdin": "^5.0.1", "meow": "^4.0.0" + }, + "devDependencies": { + "ava": "^0.24.0" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..123ad3f --- /dev/null +++ b/test.js @@ -0,0 +1,28 @@ +'use strict' +const test = require('ava') +const {execSync} = require('child_process') + +function fx(json, code = '') { + const output = execSync(`echo '${JSON.stringify(json)}' | node index.js ${code}`).toString('utf8') + return JSON.parse(output) +} + +test('pass', t => { + t.deepEqual(fx([{"greeting": "hello world"}]), [{"greeting": "hello world"}]) +}) + +test('anon func', t => { + t.deepEqual(fx({"key": "value"}, "'x => x.key'"), 'value') +}) + +test('this bind', t => { + t.deepEqual(fx([1, 2, 3, 4, 5], "'this.map(x => x * this.length)'"), [5, 10, 15, 20, 25]) +}) + +test('generator', t => { + t.deepEqual(fx([1, 2, 3, 4, 5], "'for (let i of this) if (i % 2 == 0) yield i'"), [2, 4]) +}) + +test('chain', t => { + t.deepEqual(fx({"items": ["foo", "bar"]}, "'this.items' 'yield* this' 'x => x[1]'"), 'bar') +}) diff --git a/test.sh b/test.sh deleted file mode 100755 index 2c7274d..0000000 --- a/test.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -euxo pipefail -readonly fx='node index.js' - -echo '[{"greeting": "hello world"}]' | $fx -echo '{"key": "value"}' | $fx "x => x.key" -echo '[1, 2, 3, 4, 5]' | $fx "this.map(x => x * this.length)" -echo '[1, 2, 3, 4, 5]' | $fx "for (let i of this) if (i % 2 == 0) yield i" -echo '{"items": ["foo", "bar"]}' | $fx "this.items" "yield* this; yield 'baz'"