Skip to content

Commit ad8ab30

Browse files
committed
Add setup and setup_all
1 parent 5f27907 commit ad8ab30

File tree

8 files changed

+220
-39
lines changed

8 files changed

+220
-39
lines changed

lib/elixir_script_test/test.ex

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,44 @@ defmodule ElixirScript.Test do
22
@doc false
33
defmacro __using__(_opts) do
44
quote do
5-
import unquote(__MODULE__), only: [test: 2, test: 3]
5+
import unquote(__MODULE__), only: [test: 2, test: 3, setup: 1, setup: 2, setup_all: 1, setup_all: 2]
66
import ExUnit.Assertions
77

88
def __elixir_script_test_module__(), do: true
99
end
1010
end
1111

12+
defmacro setup_all(context \\ quote(do: _), contents) do
13+
do_setup(context, contents, :__elixirscript_test_setup_all)
14+
end
15+
16+
defmacro setup(context \\ quote(do: _), contents) do
17+
do_setup(context, contents, :__elixirscript_test_setup)
18+
end
19+
20+
defp do_setup(context, contents, name) do
21+
contents =
22+
case contents do
23+
[do: block] ->
24+
quote do
25+
unquote(block)
26+
end
27+
_ ->
28+
quote do
29+
try(unquote(contents))
30+
end
31+
end
32+
33+
context = Macro.escape(context)
34+
contents = Macro.escape(contents, unquote: true)
35+
36+
quote bind_quoted: [context: context, contents: contents, name: name] do
37+
def unquote(name)(unquote(context)) do
38+
unquote(contents)
39+
end
40+
end
41+
end
42+
1243
defmacro test(message, context \\ quote(do: _), contents) do
1344
contents =
1445
case contents do
@@ -30,7 +61,7 @@ defmodule ElixirScript.Test do
3061
|> String.replace(" ", "_")
3162
|> String.replace(~r/[^A-Za-z0-9]/, "")
3263

33-
name = String.to_atom("__test_#{name}")
64+
name = String.to_atom("__elixirscript_test_case_#{name}")
3465

3566
quote bind_quoted: [context: context, contents: contents, message: message, name: name] do
3667
def unquote(name)(unquote(context)) do
@@ -56,9 +87,7 @@ defmodule ElixirScript.Test do
5687
|> Path.join("Elixir.*.js")
5788
|> Path.wildcard()
5889

59-
test_script_path = Path.join([:code.priv_dir(:elixir_script), "testrunner", "index.js"])
60-
61-
{_, exit_status} = System.cmd "node", [test_script_path] ++ js_files, into: IO.stream(:stdio, :line)
90+
exit_status = node_test_runner(js_files)
6291

6392
# Delete directory at the end
6493
File.rm_rf!(output)
@@ -70,4 +99,10 @@ defmodule ElixirScript.Test do
7099
:error
71100
end
72101
end
102+
103+
defp node_test_runner(js_files) do
104+
test_script_path = Path.join([:code.priv_dir(:elixir_script), "testrunner", "index.js"])
105+
{_, exit_status} = System.cmd "node", [test_script_path] ++ js_files, into: IO.stream(:stdio, :line)
106+
exit_status
107+
end
73108
end

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
},
1212
"scripts": {
1313
"lint": "eslint src/javascript/lib/**/*.js src/javascript/tests/**/*.js",
14-
"lint:fix":
15-
"eslint src/javascript/lib/**/*.js src/javascript/tests/**/*.js --fix",
16-
"build": "rollup -c rollup.config.js",
14+
"lint:fix": "eslint src/javascript/lib/**/*.js src/javascript/tests/**/*.js --fix",
15+
"build": "node rollup.config.js",
1716
"clean": "rm -rf priv/build",
1817
"test": "nyc ava src/javascript/tests"
1918
},
@@ -24,6 +23,7 @@
2423
"author": "",
2524
"license": "MIT",
2625
"dependencies": {
26+
"chalk": "^2.3.0",
2727
"erlang-types": "^1.0.1",
2828
"grapheme-splitter": "^1.0.2",
2929
"rollup-plugin-commonjs": "^8.2.1",
@@ -45,7 +45,9 @@
4545
"rollup-plugin-node-resolve": "^3.0.0"
4646
},
4747
"ava": {
48-
"require": ["babel-register"],
48+
"require": [
49+
"babel-register"
50+
],
4951
"babel": {
5052
"babelrc": true
5153
}

priv/testrunner/testRunner.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Colors from './colors.js';
2+
import Vendor from './vendor.build.js';
23

34
async function start(files) {
45
const results = {
@@ -17,11 +18,47 @@ async function start(files) {
1718
return results;
1819
}
1920

21+
function runSetup(mod, name, incomingContext = new Map()) {
22+
if (mod.default[name]) {
23+
const result = mod.default[name](incomingContext);
24+
25+
return resolveContext(result, incomingContext);
26+
}
27+
28+
return incomingContext;
29+
}
30+
31+
function resolveContext(context, parentContext) {
32+
if (context === Symbol.for('ok')) {
33+
return parentContext;
34+
} else if (context instanceof Vendor.ErlangTypes.Tuple && context.get(0) === Symbol.for('ok')) {
35+
return resolveContext(context.get(1), parentContext);
36+
} else if (context instanceof Map) {
37+
return new Map([...parentContext, ...context]);
38+
} else if (Array.isArray(context)) {
39+
return mergeContextKeywordList(context, parentContext);
40+
}
41+
42+
throw new Error('Invalid context');
43+
}
44+
45+
function mergeContextKeywordList(context, parentContext) {
46+
const newContext = new Map([...parentContext]);
47+
48+
for (const entry of context) {
49+
newContext.set(entry.get(0), entry.get(1));
50+
}
51+
52+
return newContext;
53+
}
54+
2055
function runTests(mod, results) {
21-
const context = [];
56+
const contextSetupAll = runSetup(mod, '__elixirscript_test_setup_all');
2257

2358
for (const key of Object.keys(mod.default)) {
24-
if (key.startsWith('__test_')) {
59+
const context = runSetup(mod, '__elixirscript_test_setup', contextSetupAll);
60+
61+
if (key.startsWith('__elixirscript_test_case')) {
2562
results.tests++;
2663
const test = mod.default[key](context);
2764
try {
@@ -54,7 +91,7 @@ function handleError(e, test, results, mod) {
5491
printErrorLine(right, 'right');
5592
}
5693
} else {
57-
console.error(e.message);
94+
console.log(e);
5895
}
5996
}
6097

priv/testrunner/vendor.build.js

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

priv/testrunner/vendor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ErlangTypes from 'erlang-types';
2+
import Chalk from 'chalk';
3+
4+
export default {
5+
ErlangTypes,
6+
Chalk,
7+
};

rollup.config.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
1-
import nodeResolve from 'rollup-plugin-node-resolve';
2-
import commonjs from 'rollup-plugin-commonjs';
3-
import babel from 'rollup-plugin-babel';
4-
import minify from 'rollup-plugin-babel-minify';
1+
const rollup = require('rollup');
2+
const babel = require('rollup-plugin-babel');
3+
const nodeResolve = require('rollup-plugin-node-resolve');
4+
const commonjs = require('rollup-plugin-commonjs');
5+
const minify = require('rollup-plugin-babel-minify');
56

6-
export default {
7-
input: 'src/javascript/elixir.js',
8-
name: 'ElixirScript',
9-
plugins: [
10-
nodeResolve({
11-
jsnext: true,
12-
main: true,
13-
}),
14-
commonjs(),
15-
babel({
16-
babelrc: false,
17-
}),
18-
minify({
19-
keepFnName: true,
20-
keepClassName: true,
21-
}),
22-
],
23-
output: [
24-
{ file: 'priv/build/iife/ElixirScript.Core.js', format: 'iife' },
25-
{ file: 'priv/build/es/ElixirScript.Core.js', format: 'es', sourcemap: 'inline' },
26-
],
27-
};
7+
const plugins = [
8+
nodeResolve({
9+
jsnext: true,
10+
main: true,
11+
}),
12+
commonjs(),
13+
babel({
14+
babelrc: false,
15+
}),
16+
minify({
17+
keepFnName: true,
18+
keepClassName: true,
19+
}),
20+
];
21+
22+
rollup
23+
.rollup({
24+
input: 'src/javascript/elixir.js',
25+
output: 'priv/build/es/ElixirScript.Core.js',
26+
sourcemap: 'inline',
27+
format: 'es',
28+
plugins,
29+
})
30+
.then((bundle) => {
31+
bundle.write({
32+
format: 'es',
33+
file: 'priv/build/es/ElixirScript.Core.js',
34+
});
35+
});
36+
37+
rollup
38+
.rollup({
39+
input: 'priv/testrunner/vendor.js',
40+
output: 'priv/testrunner/vendor.build.js',
41+
format: 'es',
42+
plugins,
43+
})
44+
.then((bundle) => {
45+
bundle.write({
46+
format: 'es',
47+
file: 'priv/testrunner/vendor.build.js',
48+
});
49+
});

test_elixir_script/integration_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
defmodule ElixirScript.Test.Test do
1+
defmodule ElixirScript.Integration.Test do
22
use ElixirScript.Test
33

4+
setup do
5+
[item: true]
6+
end
7+
48
test "Something" do
59
assert {:ok, 2} = {:ok, 1}
610
end

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,14 @@ chalk@^2.1.0:
12641264
escape-string-regexp "^1.0.5"
12651265
supports-color "^4.0.0"
12661266

1267+
chalk@^2.3.0:
1268+
version "2.3.0"
1269+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
1270+
dependencies:
1271+
ansi-styles "^3.1.0"
1272+
escape-string-regexp "^1.0.5"
1273+
supports-color "^4.0.0"
1274+
12671275
chokidar@^1.4.2:
12681276
version "1.7.0"
12691277
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468"

0 commit comments

Comments
 (0)