forked from edgurgel/solid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.exs
214 lines (168 loc) · 5.95 KB
/
context_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
defmodule Solid.ContextTest do
use ExUnit.Case, async: true
alias Solid.Context
describe "get_in/3" do
test "counter_vars scope only" do
context = %Context{counter_vars: %{"x" => 1}}
assert Context.get_in(context, ["x"], [:counter_vars]) == {:ok, 1}
end
test "vars scope only" do
context = %Context{vars: %{"x" => 1}}
assert Context.get_in(context, ["x"], [:vars]) == {:ok, 1}
end
test "var scope with false value" do
context = %Context{vars: %{"x" => false}}
assert Context.get_in(context, ["x"], [:vars]) == {:ok, false}
end
test "var scope with nil value" do
context = %Context{vars: %{"x" => nil}}
assert Context.get_in(context, ["x"], [:vars]) == {:ok, nil}
end
test "iteration_vars scope only" do
context = %Context{iteration_vars: %{"x" => 1}}
assert Context.get_in(context, ["x"], [:iteration_vars]) == {:ok, 1}
end
test "nested access" do
context = %Context{vars: %{"x" => %{"y" => 1}}}
assert Context.get_in(context, ["x", "y"], [:vars]) == {:ok, 1}
end
test "nested access string" do
context = %Context{vars: %{"x" => "y"}}
assert Context.get_in(context, ["x", "y"], [:vars]) == {:error, {:not_found, ["x", "y"]}}
end
test "nested access nil" do
context = %Context{vars: %{"x" => 1}}
assert Context.get_in(context, ["x", "y"], [:vars]) == {:error, {:not_found, ["x", "y"]}}
end
test "counter_vars & vars scopes with both keys existing" do
context = %Context{vars: %{"x" => 1}, counter_vars: %{"x" => 2}}
assert Context.get_in(context, ["x"], [:vars, :counter_vars]) == {:ok, 1}
end
test "counter_vars & vars scopes with counter_vars key existing" do
context = %Context{counter_vars: %{"x" => 2}}
assert Context.get_in(context, ["x"], [:vars, :counter_vars]) == {:ok, 2}
end
test "list access" do
context = %Context{vars: %{"x" => ["a", "b", "c"]}}
assert Context.get_in(context, ["x", 1], [:vars]) == {:ok, "b"}
end
test "list size" do
context = %Context{vars: %{"x" => ["a", "b", "c"]}}
assert Context.get_in(context, ["x", "size"], [:vars]) == {:ok, 3}
end
test "map size" do
context = %Context{vars: %{"x" => %{"a" => 1, "b" => 2}}}
assert Context.get_in(context, ["x", "size"], [:vars]) == {:ok, 2}
end
test "map size key" do
context = %Context{vars: %{"x" => %{"a" => 1, "b" => 2, "size" => 42}}}
assert Context.get_in(context, ["x", "size"], [:vars]) == {:ok, 42}
end
end
describe "run_cycle/2" do
test "first run" do
cycle = [values: ["one", "two", "three"]]
context = %Context{cycle_state: %{}}
new_context = %Context{
cycle_state: %{
["one", "two", "three"] => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "one"}
end
test "second run" do
cycle = [values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
["one", "two", "three"] => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
["one", "two", "three"] => {1, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "two"}
end
test "third run" do
cycle = [values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
["one", "two", "three"] => {1, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
["one", "two", "three"] => {2, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "three"}
end
test "fourth run - loops back" do
cycle = [values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
["one", "two", "three"] => {2, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
["one", "two", "three"] => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "one"}
end
test "named first run" do
cycle = [name: "first", values: ["one", "two", "three"]]
context = %Context{cycle_state: %{}}
new_context = %Context{
cycle_state: %{
"first" => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "one"}
end
test "named second run" do
cycle = [name: "first", values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
"first" => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
"first" => {1, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "two"}
end
test "named third run" do
cycle = [name: "first", values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
"first" => {1, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
"first" => {2, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "three"}
end
test "named fourth run - loops back" do
cycle = [name: "first", values: ["one", "two", "three"]]
context = %Context{
cycle_state: %{
"first" => {2, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
new_context = %Context{
cycle_state: %{
"first" => {0, %{0 => "one", 1 => "two", 2 => "three"}}
}
}
assert Context.run_cycle(context, cycle) == {new_context, "one"}
end
end
end