@@ -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
73108end
0 commit comments