forked from surface-ui/surface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface_test.exs
146 lines (120 loc) · 3.81 KB
/
surface_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
defmodule SurfaceTest do
use Surface.ConnCase
doctest Surface, import: true
import Surface
describe "quote_surface" do
test "generate AST" do
[tag] =
quote_surface line: 1 do
~F"<div>content</div>"
end
assert %Surface.AST.Tag{
element: "div",
children: [%Surface.AST.Literal{value: "content"}],
meta: %{line: 1}
} = tag
end
test "using heredocs" do
[tag | _] =
quote_surface line: 1 do
~F"""
<div>content</div>
"""
end
assert %Surface.AST.Tag{
element: "div",
children: [%Surface.AST.Literal{value: "content"}],
meta: %{line: 1}
} = tag
end
end
describe "components" do
test "retrieve all components" do
list = components()
# We cannot assert if it retrieves deps' components from here since this project cannot
# depend on a another project that depends on surface itself. So we just make sure it
# retrieves, at least, the list of components of this project.
assert SurfaceWeb.Components.FakeComponentInWebNamespace in list
assert Surface.Components.Form in list
assert Enum not in list
end
test "retrieve only components in the web namespace" do
list = components(only_web_namespace: true)
assert SurfaceWeb.Components.FakeComponentInWebNamespace in list
assert Surface.Components.Form not in list
assert Enum not in list
end
test "retrieve only components in the current project" do
list = components(only_current_project: true)
assert SurfaceWeb.Components.FakeComponentInWebNamespace in list
assert Surface.Components.Form in list
assert Enum not in list
# We cannot test `only_current_project: false` from here since this project cannot
# depend on a another project that depends on surface itself. So we just make sure it
# retrieves, at least, the list of components of this project.
list = components(only_current_project: false)
assert SurfaceWeb.Components.FakeComponentInWebNamespace in list
assert Surface.Components.Form in list
assert Enum not in list
end
end
test "raise error when trying to unquote an undefined variable" do
message = """
code.ex:2: undefined variable "content".
Available variable: "message"
"""
assert_raise(CompileError, message, fn ->
quote_surface line: 1, file: "code.ex" do
~F"""
<div>
{^content}
</div>
"""
end
end)
end
test "raise error when trying to unquote expressions that are not variables" do
message = """
code.ex:2: cannot unquote `to_string(:abc)`.
The expression to be unquoted must be written as `^var`, where `var` is an existing variable.
"""
assert_raise(CompileError, message, fn ->
quote_surface line: 1, file: "code.ex" do
~F"""
<div>
{^to_string(:abc)}
</div>
"""
end
end)
end
test "raise error when not using the ~F sigil" do
code = """
quote_surface do
"\""
<div>
{^to_string(:abc)}
</div>
"\""
end
"""
message = "code.exs:1: the code to be quoted must be wrapped in a `~F` sigil."
assert_raise(CompileError, message, fn ->
{{:module, _, _, _}, _} = Code.eval_string(code, [], %{__ENV__ | file: "code.exs", line: 1})
end)
end
test "raise error when using {^...} outside `quote_surface`" do
message = "code:2: cannot use tagged expression {^var} outside `quote_surface`"
code =
quote do
~F"""
<div>
{^content}
</div>
"""
end
assert_raise(CompileError, message, fn ->
compile_surface(code)
end)
end
end