Skip to content

Commit 97a906b

Browse files
committed
Seperated tests into separate files by translation feature
1 parent a381829 commit 97a906b

14 files changed

Lines changed: 584 additions & 535 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule ExToJS.Translator.Assignment.Test do
2+
use ExUnit.Case
3+
import ExToJS.TestHelper
4+
5+
test "translate assignment" do
6+
ex_ast = quote do: a = 1
7+
js_code = "let a = 1;"
8+
9+
assert_translation(ex_ast, js_code)
10+
11+
ex_ast = quote do: a = :atom
12+
js_code = "let a = Symbol('atom');"
13+
14+
assert_translation(ex_ast, js_code)
15+
16+
ex_ast = quote do: {a, b} = {1, 2}
17+
js_code = "let [a,b] = [1,2];"
18+
19+
assert_translation(ex_ast, js_code)
20+
end
21+
end

test/translator/atom_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule ExToJS.Translator.Atom.Test do
2+
use ExUnit.Case
3+
import ExToJS.TestHelper
4+
5+
test "translate atom" do
6+
ex_ast = quote do: :atom
7+
assert_translation(ex_ast, "Symbol('atom')")
8+
end
9+
end

test/translator/case_test.exs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule ExToJS.Translator.Case.Test do
2+
use ExUnit.Case
3+
import ExToJS.TestHelper
4+
5+
test "translate case" do
6+
ex_ast = quote do
7+
case data do
8+
:ok -> value
9+
:error -> nil
10+
end
11+
end
12+
13+
js_code = """
14+
if(data == Symbol('ok')){
15+
value
16+
}else if(data == Symbol('error')){
17+
null
18+
}
19+
"""
20+
21+
assert_translation(ex_ast, js_code)
22+
23+
ex_ast = quote do
24+
case data do
25+
false -> value = 13
26+
true -> true
27+
end
28+
end
29+
30+
js_code = """
31+
if(data == false){
32+
let value = 13;
33+
}else if(data == true){
34+
true
35+
}
36+
"""
37+
38+
assert_translation(ex_ast, js_code)
39+
40+
41+
42+
ex_ast = quote do
43+
case data do
44+
false -> value = 13
45+
_ -> true
46+
end
47+
end
48+
49+
js_code = """
50+
if(data == false){
51+
let value = 13;
52+
}else{
53+
true
54+
}
55+
"""
56+
57+
assert_translation(ex_ast, js_code)
58+
end
59+
end

test/translator/cond_test.exs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
defmodule ExToJS.Translator.Cond.Test do
2+
use ExUnit.Case
3+
import ExToJS.TestHelper
4+
5+
test "translate cond" do
6+
ex_ast = quote do
7+
cond do
8+
1 + 1 == 1 ->
9+
"This will never match"
10+
2 * 2 != 4 ->
11+
"Nor this"
12+
true ->
13+
"This will"
14+
end
15+
end
16+
17+
js_code = """
18+
if(1 + 1 == 1){
19+
'This will never match'
20+
}else if(2 * 2 != 4){
21+
'Nor this'
22+
}else{
23+
'This will'
24+
}
25+
"""
26+
27+
assert_translation(ex_ast, js_code)
28+
29+
ex_ast = quote do
30+
cond do
31+
1 + 1 == 1 ->
32+
a = 1
33+
"This will never match"
34+
2 * 2 != 4 ->
35+
a = 2
36+
"Nor this"
37+
true ->
38+
a = 3
39+
"This will"
40+
end
41+
end
42+
43+
js_code = """
44+
if(1 + 1 == 1){
45+
let a = 1;
46+
'This will never match'
47+
}else if(2 * 2 != 4){
48+
let a = 2;
49+
'Nor this'
50+
}else{
51+
let a = 3;
52+
'This will'
53+
}
54+
"""
55+
56+
assert_translation(ex_ast, js_code)
57+
end
58+
end

test/translator/defmodule_test.exs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
defmodule ExToJS.Translator.Defmodule.Test do
2+
use ExUnit.Case
3+
import ExToJS.TestHelper
4+
5+
test "translate defmodules" do
6+
ex_ast = quote do
7+
defmodule Elephant do
8+
end
9+
end
10+
11+
assert_translation(ex_ast, "")
12+
13+
ex_ast = quote do
14+
defmodule Elephant do
15+
def something() do
16+
end
17+
18+
defp something_else() do
19+
end
20+
end
21+
end
22+
23+
js_code = """
24+
export function something(){
25+
return null;
26+
}
27+
28+
function something_else(){
29+
return null;
30+
}
31+
"""
32+
33+
assert_translation(ex_ast, js_code)
34+
35+
ex_ast = quote do
36+
defmodule Elephant do
37+
alias Icabod.Crane
38+
39+
def something() do
40+
end
41+
42+
defp something_else() do
43+
end
44+
end
45+
end
46+
47+
js_code = """
48+
import * as Crane from 'icabod/crane';
49+
50+
export function something(){
51+
return null;
52+
}
53+
54+
function something_else(){
55+
return null;
56+
}
57+
"""
58+
59+
assert_translation(ex_ast, js_code)
60+
end
61+
end

0 commit comments

Comments
 (0)