trxl 0.1.5 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -7
- data/VERSION +1 -1
- data/lib/trxl.rb +1 -1
- data/lib/trxl/trxl.rb +181 -135
- data/lib/trxl/trxl_grammar.rb +1354 -1078
- data/lib/trxl/trxl_grammar.treetop +103 -82
- data/spec/trxl/conditionals_spec.rb +32 -18
- data/spec/trxl/ranges_spec.rb +13 -30
- data/spec/trxl/require_spec.rb +11 -11
- data/spec/trxl/stdlib_spec.rb +243 -52
- data/spec/trxl/strings_spec.rb +20 -0
- data/trxl.gemspec +34 -57
- metadata +40 -37
- data/.gitignore +0 -24
@@ -3,11 +3,11 @@ require 'spec_helper'
|
|
3
3
|
describe "For conditional evaluation, the language" do
|
4
4
|
|
5
5
|
include Trxl::SpecHelper
|
6
|
-
|
6
|
+
|
7
7
|
before(:each) do
|
8
8
|
@parser = Trxl::Calculator.new
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should be able to evaluate conditional 'if' expressions without elsif and else branches" do
|
12
12
|
program = "test = fun(x) { if(x) TRUE end };"
|
13
13
|
eval(program + "test(TRUE)").should be_true
|
@@ -23,7 +23,7 @@ describe "For conditional evaluation, the language" do
|
|
23
23
|
end
|
24
24
|
PROGRAM
|
25
25
|
eval(program).should be_true
|
26
|
-
|
26
|
+
|
27
27
|
program = <<-PROGRAM
|
28
28
|
if(FALSE)
|
29
29
|
TRUE
|
@@ -32,7 +32,7 @@ describe "For conditional evaluation, the language" do
|
|
32
32
|
end
|
33
33
|
PROGRAM
|
34
34
|
eval(program).should be_false
|
35
|
-
|
35
|
+
|
36
36
|
program = <<-PROGRAM
|
37
37
|
if(NULL)
|
38
38
|
TRUE
|
@@ -41,7 +41,7 @@ describe "For conditional evaluation, the language" do
|
|
41
41
|
end
|
42
42
|
PROGRAM
|
43
43
|
eval(program).should be_false
|
44
|
-
|
44
|
+
|
45
45
|
program = <<-PROGRAM
|
46
46
|
if(NULL != NULL)
|
47
47
|
TRUE
|
@@ -200,7 +200,7 @@ describe "For conditional evaluation, the language" do
|
|
200
200
|
PROGRAM
|
201
201
|
eval(program).should == "else branch"
|
202
202
|
end
|
203
|
-
|
203
|
+
|
204
204
|
it "should be able to evaluate conditional 'if' expressions with multiple elsif and an else branch" do
|
205
205
|
program = <<-PROGRAM
|
206
206
|
foo = fun() {
|
@@ -217,7 +217,7 @@ describe "For conditional evaluation, the language" do
|
|
217
217
|
foo();
|
218
218
|
PROGRAM
|
219
219
|
eval(program).should == 0
|
220
|
-
|
220
|
+
|
221
221
|
program = <<-PROGRAM
|
222
222
|
foo = fun() {
|
223
223
|
if(2 == 1)
|
@@ -233,7 +233,7 @@ describe "For conditional evaluation, the language" do
|
|
233
233
|
foo();
|
234
234
|
PROGRAM
|
235
235
|
eval(program).should == 1
|
236
|
-
|
236
|
+
|
237
237
|
program = <<-PROGRAM
|
238
238
|
foo = fun() {
|
239
239
|
if(2 == 0)
|
@@ -249,7 +249,7 @@ describe "For conditional evaluation, the language" do
|
|
249
249
|
foo();
|
250
250
|
PROGRAM
|
251
251
|
eval(program).should == 2
|
252
|
-
|
252
|
+
|
253
253
|
program = <<-PROGRAM
|
254
254
|
foo = fun() {
|
255
255
|
if(2 == 0)
|
@@ -265,7 +265,7 @@ describe "For conditional evaluation, the language" do
|
|
265
265
|
foo();
|
266
266
|
PROGRAM
|
267
267
|
eval(program).should == 3
|
268
|
-
|
268
|
+
|
269
269
|
program = <<-PROGRAM
|
270
270
|
foo = fun(value) {
|
271
271
|
if(value <= 5000)
|
@@ -284,9 +284,9 @@ describe "For conditional evaluation, the language" do
|
|
284
284
|
};
|
285
285
|
foo(13000);
|
286
286
|
PROGRAM
|
287
|
-
eval(program).should == 15.92
|
287
|
+
eval(program).should == 15.92
|
288
288
|
end
|
289
|
-
|
289
|
+
|
290
290
|
it "should allow an arbitrary number of statements inside if/elsif/else expressions" do
|
291
291
|
program = <<-PROGRAM
|
292
292
|
foo = fun() {
|
@@ -306,7 +306,7 @@ describe "For conditional evaluation, the language" do
|
|
306
306
|
};
|
307
307
|
foo();
|
308
308
|
PROGRAM
|
309
|
-
eval(program).should == 0
|
309
|
+
eval(program).should == 0
|
310
310
|
|
311
311
|
program = <<-PROGRAM
|
312
312
|
foo = fun() {
|
@@ -326,7 +326,7 @@ describe "For conditional evaluation, the language" do
|
|
326
326
|
};
|
327
327
|
foo();
|
328
328
|
PROGRAM
|
329
|
-
eval(program).should == 1
|
329
|
+
eval(program).should == 1
|
330
330
|
|
331
331
|
program = <<-PROGRAM
|
332
332
|
foo = fun() {
|
@@ -346,7 +346,7 @@ describe "For conditional evaluation, the language" do
|
|
346
346
|
};
|
347
347
|
foo();
|
348
348
|
PROGRAM
|
349
|
-
eval(program).should == 2
|
349
|
+
eval(program).should == 2
|
350
350
|
|
351
351
|
program = <<-PROGRAM
|
352
352
|
foo = fun() {
|
@@ -366,9 +366,9 @@ describe "For conditional evaluation, the language" do
|
|
366
366
|
};
|
367
367
|
foo();
|
368
368
|
PROGRAM
|
369
|
-
eval(program).should == 3
|
369
|
+
eval(program).should == 3
|
370
370
|
end
|
371
|
-
|
371
|
+
|
372
372
|
it "should allow arbitrary expressions as conditional expression" do
|
373
373
|
program = "test = fun(x) {if(x == 0) TRUE else FALSE end};"
|
374
374
|
program << "test(0)"
|
@@ -432,6 +432,20 @@ describe "For conditional evaluation, the language" do
|
|
432
432
|
foo("hello");
|
433
433
|
PROGRAM
|
434
434
|
eval(program).should == "something else"
|
435
|
+
|
436
|
+
program = <<-PROGRAM
|
437
|
+
foo = fun(x) {
|
438
|
+
case x
|
439
|
+
when NULL then "nil"
|
440
|
+
when 2 then "two"
|
441
|
+
when 3 then "three"
|
442
|
+
else FALSE
|
443
|
+
end
|
444
|
+
};
|
445
|
+
foo(NULL);
|
446
|
+
PROGRAM
|
447
|
+
eval(program).should == "nil"
|
448
|
+
|
435
449
|
end
|
436
450
|
|
437
451
|
it "should allow multiple statements in each branch of a 'case' expression" do
|
@@ -450,5 +464,5 @@ describe "For conditional evaluation, the language" do
|
|
450
464
|
PROGRAM
|
451
465
|
eval(program).should == [ 1, "one" ]
|
452
466
|
end
|
453
|
-
|
467
|
+
|
454
468
|
end
|
data/spec/trxl/ranges_spec.rb
CHANGED
@@ -3,24 +3,24 @@ require 'spec_helper'
|
|
3
3
|
describe "When working with Ranges, the Trxl::Calculator" do
|
4
4
|
|
5
5
|
include Trxl::SpecHelper
|
6
|
-
|
6
|
+
|
7
7
|
before(:each) do
|
8
8
|
@parser = Trxl::Calculator.new
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should return arrays for range literal expressions" do
|
12
12
|
eval("0..0").should == [ 0 ]
|
13
13
|
eval("0..-1").should == [ ]
|
14
14
|
eval("0...0").should == [ ]
|
15
15
|
eval("0...-1").should == [ ]
|
16
|
-
|
16
|
+
|
17
17
|
eval("1..5").should == [ 1, 2, 3, 4, 5 ]
|
18
18
|
eval("1 .. 5").should == [ 1, 2, 3, 4, 5 ]
|
19
19
|
eval("a = 1; b = 5; a..b").should == [ 1, 2, 3, 4, 5 ]
|
20
20
|
eval("1...5").should == [ 1, 2, 3, 4 ]
|
21
21
|
eval("1 ... 5").should == [ 1, 2, 3, 4 ]
|
22
22
|
eval("a = 1; b = 5; a...b").should == [ 1, 2, 3, 4 ]
|
23
|
-
|
23
|
+
|
24
24
|
eval("\"a\"..\"a\"").should == [ "a" ]
|
25
25
|
eval("\"a\"..\"c\"").should == [ "a", "b", "c" ]
|
26
26
|
eval("\"a\" .. \"c\"").should == [ "a", "b", "c" ]
|
@@ -32,23 +32,26 @@ describe "When working with Ranges, the Trxl::Calculator" do
|
|
32
32
|
eval("'a' .. 'c'").should == [ "a", "b", "c" ]
|
33
33
|
eval("'a'...'c'").should == [ "a", "b" ]
|
34
34
|
eval("'a' ... 'c'").should == [ "a", "b" ]
|
35
|
-
|
35
|
+
|
36
36
|
eval("'aa'..'aa'").should == [ "aa" ]
|
37
37
|
eval("'aa'..'ac'").should == [ "aa", "ab", "ac" ]
|
38
38
|
eval("'aa'...'ac'").should == [ "aa", "ab" ]
|
39
39
|
|
40
|
+
eval("'a'.. 'ac'").should == %w[a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac]
|
41
|
+
eval("'a'...'ac'").should == %w[a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab]
|
42
|
+
|
40
43
|
eval("1..5 == [ 1, 2, 3, 4, 5 ]").should be_true
|
41
44
|
eval("1...5 == [ 1, 2, 3, 4 ]").should be_true
|
42
|
-
|
45
|
+
|
43
46
|
eval("SIZE(1..5)").should == 5
|
44
|
-
|
47
|
+
|
45
48
|
eval("a..b == [ 1, 2 ]", { :a => 1, :b => 2 }).should be_true
|
46
49
|
eval("a..c", { :a => 1, :b => nil, :c => 2 }).should == [ 1, 2]
|
47
|
-
|
50
|
+
|
48
51
|
lambda { eval("a..b", { :a => nil, :b => nil }) }.should raise_error
|
49
|
-
|
52
|
+
|
50
53
|
end
|
51
|
-
|
54
|
+
|
52
55
|
it "should store ranges as arrays" do
|
53
56
|
eval("a = 1..5; a;").should == [ 1, 2, 3, 4, 5 ]
|
54
57
|
eval("a = 1 .. 5; a;").should == [ 1, 2, 3, 4, 5 ]
|
@@ -59,23 +62,3 @@ describe "When working with Ranges, the Trxl::Calculator" do
|
|
59
62
|
end
|
60
63
|
|
61
64
|
end
|
62
|
-
|
63
|
-
describe "When working with Strings, the Trxl::Calculator" do
|
64
|
-
|
65
|
-
include Trxl::SpecHelper
|
66
|
-
|
67
|
-
before(:each) do
|
68
|
-
@parser = Trxl::Calculator.new
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should evaluate string literals to strings using double quotes (\"\")" do
|
72
|
-
eval("\"Test String\"").should == "Test String"
|
73
|
-
eval("s = \"Test String\"; s;").should == "Test String"
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should evaluate string literals to strings using single quotes ('')" do
|
77
|
-
eval("'Test String'").should == "Test String"
|
78
|
-
eval("s = 'Test String'; s;").should == "Test String"
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
data/spec/trxl/require_spec.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "For defining Trxl::StdLib, the Trxl::Calculator" do
|
4
|
-
|
4
|
+
|
5
5
|
include Trxl::SpecHelper
|
6
|
-
|
6
|
+
|
7
7
|
before(:each) do
|
8
8
|
@parser = Trxl::Calculator.new
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should be able to require trxl code snippets and merge them into the current env" do
|
12
12
|
program = "require 'stdlib'"
|
13
13
|
env_after_require = eval(program)
|
14
14
|
env_after_require.should have_key(:foreach_in)
|
15
|
-
env_after_require.should have_key(:
|
15
|
+
env_after_require.should have_key(:__foreach_in__)
|
16
16
|
env_after_require.should have_key(:inject)
|
17
|
-
env_after_require.should have_key(:
|
17
|
+
env_after_require.should have_key(:__inject__)
|
18
18
|
env_after_require.should have_key(:map)
|
19
19
|
env_after_require.should have_key(:ratio)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should be able to require scoped trxl code snippets and merge them into the current env" do
|
23
23
|
program = "require 'stdlib/foreach_in'"
|
24
24
|
env_after_require = eval(program)
|
25
25
|
env_after_require.should have_key(:foreach_in)
|
26
|
-
|
26
|
+
|
27
27
|
program = "require 'stdlib/inject'"
|
28
28
|
env_after_require = eval(program)
|
29
29
|
env_after_require.should have_key(:inject)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
it "should be able to require scoped trxl code snippets and merge them into the current env" do
|
33
33
|
program = "require 'stdlib/foreach_in'"
|
34
34
|
env_after_require = eval(program)
|
35
35
|
env_after_require.should have_key(:foreach_in)
|
36
|
-
|
36
|
+
|
37
37
|
program = "require 'stdlib/inject'"
|
38
38
|
env_after_require = eval(program)
|
39
39
|
env_after_require.should have_key(:inject)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "should ignore a require statement if the library has already been loaded" do
|
43
43
|
program = "require 'stdlib/inject'"
|
44
44
|
program = "require 'stdlib/map'"
|
@@ -46,5 +46,5 @@ describe "For defining Trxl::StdLib, the Trxl::Calculator" do
|
|
46
46
|
env_after_require.should have_key(:inject)
|
47
47
|
env_after_require.should have_key(:map)
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
end
|
data/spec/trxl/stdlib_spec.rb
CHANGED
@@ -3,15 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe "The Trxl::StdLib" do
|
4
4
|
|
5
5
|
include Trxl::SpecHelper
|
6
|
-
|
6
|
+
|
7
7
|
before(:each) do
|
8
8
|
@parser = Trxl::Calculator.new
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should be able to parse the whole Trxl::StdLib code" do
|
12
12
|
lambda { parse(Trxl::Calculator.stdlib) }.should_not raise_error
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
|
+
it "should define a foreach function that does not invoke the body function for empty enumerables" do
|
16
|
+
program = <<-PROGRAM
|
17
|
+
require 'stdlib/foreach_in';
|
18
|
+
test = [];
|
19
|
+
foreach_in([], fun(i) { test << i });
|
20
|
+
test;
|
21
|
+
PROGRAM
|
22
|
+
eval(program).should == []
|
23
|
+
end
|
24
|
+
|
15
25
|
it "should define a foreach function with read access to the outer env" do
|
16
26
|
program = <<-PROGRAM
|
17
27
|
require 'stdlib/foreach_in';
|
@@ -19,20 +29,40 @@ describe "The Trxl::StdLib" do
|
|
19
29
|
PROGRAM
|
20
30
|
eval(program).should == 3
|
21
31
|
end
|
22
|
-
|
32
|
+
|
23
33
|
it "should define a foreach function with write access to the outer env" do
|
24
34
|
program = <<-PROGRAM
|
25
35
|
require 'stdlib/foreach_in';
|
26
36
|
sum = 0;
|
27
37
|
a = 1..10;
|
28
|
-
foreach_in(a, fun(e) {
|
29
|
-
sum = sum + e
|
38
|
+
foreach_in(a, fun(e) {
|
39
|
+
sum = sum + e
|
30
40
|
});
|
31
41
|
sum;
|
32
42
|
PROGRAM
|
33
43
|
eval(program).should == 55
|
34
44
|
end
|
35
45
|
|
46
|
+
it "should define an inject function that returns the original memo for empty enumerables" do
|
47
|
+
program = <<-PROGRAM
|
48
|
+
require 'stdlib/inject';
|
49
|
+
inject(0, [], fun(memo, val) { memo + val });
|
50
|
+
PROGRAM
|
51
|
+
eval(program).should == 0
|
52
|
+
|
53
|
+
program = <<-PROGRAM
|
54
|
+
require 'stdlib/inject';
|
55
|
+
inject([], [], fun(memo, val) { memo << val });
|
56
|
+
PROGRAM
|
57
|
+
eval(program).should == []
|
58
|
+
|
59
|
+
program = <<-PROGRAM
|
60
|
+
require 'stdlib/inject';
|
61
|
+
inject({}, [], fun(memo, val) { memo[val] });
|
62
|
+
PROGRAM
|
63
|
+
eval(program).should == {}
|
64
|
+
end
|
65
|
+
|
36
66
|
it "should define an inject function with read access to the outer env" do
|
37
67
|
program = <<-PROGRAM
|
38
68
|
require 'stdlib/inject';
|
@@ -48,12 +78,12 @@ describe "The Trxl::StdLib" do
|
|
48
78
|
base = 0;
|
49
79
|
positives = inject(0, values, fun(memo, val) {
|
50
80
|
if(ENV[val] == "yes")
|
51
|
-
base = base + 1;
|
81
|
+
base = base + 1;
|
52
82
|
memo + 1
|
53
|
-
elsif(ENV[val] == "no")
|
54
|
-
base = base + 1;
|
83
|
+
elsif(ENV[val] == "no")
|
84
|
+
base = base + 1;
|
55
85
|
memo
|
56
|
-
else
|
86
|
+
else
|
57
87
|
memo
|
58
88
|
end
|
59
89
|
});
|
@@ -61,7 +91,7 @@ describe "The Trxl::StdLib" do
|
|
61
91
|
ROUND((ROUND(positives, 10) / base) * 100, 2)
|
62
92
|
else
|
63
93
|
0
|
64
|
-
end
|
94
|
+
end
|
65
95
|
};
|
66
96
|
ratio('a'..'c');
|
67
97
|
PROGRAM
|
@@ -124,34 +154,34 @@ describe "The Trxl::StdLib" do
|
|
124
154
|
|
125
155
|
it "should define a 'ratio' function for any enumerable collection" do
|
126
156
|
env = {
|
127
|
-
:a => "Ja",
|
157
|
+
:a => "Ja",
|
128
158
|
:b => "Ja",
|
129
159
|
:c => "Ja",
|
130
160
|
:d => "Ja"
|
131
161
|
}
|
132
162
|
req_stmt = "require 'stdlib/ratio';"
|
133
163
|
eval("#{req_stmt} ratio('a'..'d', 'Ja', 'keine Angabe');", env).should == 100
|
134
|
-
|
164
|
+
|
135
165
|
env = {
|
136
|
-
:a => "Ja",
|
166
|
+
:a => "Ja",
|
137
167
|
:b => "Ja",
|
138
168
|
:c => "Nein",
|
139
169
|
:d => "Nein"
|
140
170
|
}
|
141
171
|
req_stmt = "require 'stdlib/ratio';"
|
142
172
|
eval("#{req_stmt} ratio('a'..'d', 'Ja', 'keine Angabe');", env).should == 50
|
143
|
-
|
173
|
+
|
144
174
|
env = {
|
145
|
-
:a => "Nein",
|
175
|
+
:a => "Nein",
|
146
176
|
:b => "Nein",
|
147
177
|
:c => "Nein",
|
148
178
|
:d => "Nein"
|
149
179
|
}
|
150
180
|
req_stmt = "require 'stdlib/ratio';"
|
151
181
|
eval("#{req_stmt} ratio('a'..'d', 'Ja', 'keine Angabe');", env).should == 0
|
152
|
-
|
182
|
+
|
153
183
|
env = {
|
154
|
-
:a => "Ja",
|
184
|
+
:a => "Ja",
|
155
185
|
:b => "Nein",
|
156
186
|
:c => "Nein",
|
157
187
|
:d => "Nein",
|
@@ -159,13 +189,13 @@ describe "The Trxl::StdLib" do
|
|
159
189
|
}
|
160
190
|
req_stmt = "require 'stdlib/ratio';"
|
161
191
|
eval("#{req_stmt} ratio('a'..'e', 'Ja', 'keine Angabe');", env).should == 25
|
162
|
-
|
192
|
+
|
163
193
|
env = {
|
164
194
|
:a => "keine Angabe"
|
165
195
|
}
|
166
196
|
req_stmt = "require 'stdlib/ratio';"
|
167
197
|
eval("#{req_stmt} ratio(['a'], 'Ja', 'keine Angabe');", env).should be_nil
|
168
|
-
|
198
|
+
|
169
199
|
env = {
|
170
200
|
:a => "keine Angabe",
|
171
201
|
:b => "keine Angabe",
|
@@ -173,14 +203,14 @@ describe "The Trxl::StdLib" do
|
|
173
203
|
}
|
174
204
|
req_stmt = "require 'stdlib/ratio';"
|
175
205
|
eval("#{req_stmt} ratio('a'..'c', 'Ja', 'keine Angabe');", env).should be_nil
|
176
|
-
|
206
|
+
|
177
207
|
env = {
|
178
208
|
:a => "Ja",
|
179
209
|
:b => "keine Angabe"
|
180
210
|
}
|
181
211
|
req_stmt = "require 'stdlib/ratio';"
|
182
212
|
eval("#{req_stmt} ratio('a'..'b', 'Ja', 'keine Angabe');", env).should == 100
|
183
|
-
|
213
|
+
|
184
214
|
env = {
|
185
215
|
:a => "Nein",
|
186
216
|
:b => "keine Angabe"
|
@@ -208,20 +238,20 @@ describe "The Trxl::StdLib" do
|
|
208
238
|
in_groups_of(2, 1..4, fun(group) { SUM(group) });
|
209
239
|
PROGRAM
|
210
240
|
eval(program).should == [ 3, 7 ]
|
211
|
-
|
241
|
+
|
212
242
|
program = <<-PROGRAM
|
213
243
|
require 'stdlib/in_groups_of';
|
214
244
|
in_groups_of(2, 'a'..'d', fun(group) { group });
|
215
245
|
PROGRAM
|
216
246
|
eval(program).should == [ [ 'a', 'b' ], [ 'c', 'd' ] ]
|
217
|
-
|
247
|
+
|
218
248
|
env = Trxl::Environment.new({
|
219
|
-
:a => {
|
220
|
-
1 => "Building 1",
|
221
|
-
2 => "Building 2",
|
222
|
-
3 => "Building 3"
|
249
|
+
:a => {
|
250
|
+
1 => "Building 1",
|
251
|
+
2 => "Building 2",
|
252
|
+
3 => "Building 3"
|
223
253
|
},
|
224
|
-
:b => {
|
254
|
+
:b => {
|
225
255
|
1 => "Feuerwehrgebäude",
|
226
256
|
2 => "Schulgebäude",
|
227
257
|
3 => "Wohngebäude"
|
@@ -244,14 +274,14 @@ describe "The Trxl::StdLib" do
|
|
244
274
|
:r => { 1 => 100, 2 => 200, 3 => 300 },
|
245
275
|
:s => { 1 => 100, 2 => 200, 3 => 300 },
|
246
276
|
:t => { 1 => 100, 2 => 200, 3 => 300 },
|
247
|
-
:u => {
|
277
|
+
:u => {
|
248
278
|
1 => [ 15, 15, 15 ],
|
249
279
|
1 => [ 20, 20, 20 ],
|
250
280
|
1 => [ 30, 30, 30 ]
|
251
281
|
}
|
252
282
|
})
|
253
283
|
end
|
254
|
-
|
284
|
+
|
255
285
|
it "should allow recursive functions as accumulator in 'in_groups_of' function calls" do
|
256
286
|
program = <<-PROGRAM
|
257
287
|
require 'stdlib/inject';
|
@@ -264,15 +294,155 @@ describe "The Trxl::StdLib" do
|
|
264
294
|
PROGRAM
|
265
295
|
eval(program).should == [ 3, 7 ]
|
266
296
|
end
|
267
|
-
|
297
|
+
|
298
|
+
it "should define a 'hash_values' function" do
|
299
|
+
program = <<-PROGRAM
|
300
|
+
require 'stdlib/hash_values';
|
301
|
+
hash_values(hash);
|
302
|
+
PROGRAM
|
303
|
+
|
304
|
+
eval(program, :hash => {}).should == []
|
305
|
+
|
306
|
+
eval(program, {
|
307
|
+
:hash => {
|
308
|
+
1 => 100,
|
309
|
+
2 => 200,
|
310
|
+
3 => 300
|
311
|
+
}
|
312
|
+
}).should == [ 100, 200, 300 ]
|
313
|
+
|
314
|
+
eval(program, {
|
315
|
+
:hash => {
|
316
|
+
1 => [ 100, 100, 100 ],
|
317
|
+
2 => [ 200, 200, 200 ],
|
318
|
+
3 => [ 300, 300, 300 ]
|
319
|
+
}
|
320
|
+
}).should == [
|
321
|
+
[ 100, 100, 100 ],
|
322
|
+
[ 200, 200, 200 ],
|
323
|
+
[ 300, 300, 300 ],
|
324
|
+
]
|
325
|
+
end
|
326
|
+
|
327
|
+
it "should define a 'hash_range_values' function" do
|
328
|
+
program = <<-PROGRAM
|
329
|
+
require 'stdlib/hash_range_values';
|
330
|
+
hash_range_values('a'..'b');
|
331
|
+
PROGRAM
|
332
|
+
|
333
|
+
eval(program, {
|
334
|
+
:a => { 1 => 100 },
|
335
|
+
:b => { 2 => 200 }
|
336
|
+
}).should == [
|
337
|
+
[100],
|
338
|
+
[200]
|
339
|
+
]
|
340
|
+
|
341
|
+
eval(program, {
|
342
|
+
:a => { 1 => [ 100, 100, 100 ], 2 => [ 200, 200, 200 ] },
|
343
|
+
:b => { 3 => [ 300, 300, 300 ], 4 => [ 400, 400, 400 ] }
|
344
|
+
}).should == [
|
345
|
+
[ [100, 100, 100], [ 200, 200, 200 ] ],
|
346
|
+
[ [300, 300, 300], [ 400, 400, 400 ] ]
|
347
|
+
]
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should define a 'hash_value_sum' function" do
|
351
|
+
|
352
|
+
program = <<-PROGRAM
|
353
|
+
require 'stdlib/hash_value_sum';
|
354
|
+
hash_value_sum(hash);
|
355
|
+
PROGRAM
|
356
|
+
|
357
|
+
eval(program, :hash => {}).should == 0
|
358
|
+
|
359
|
+
eval(program, {
|
360
|
+
:hash => {
|
361
|
+
1 => 100,
|
362
|
+
2 => 200,
|
363
|
+
3 => 300
|
364
|
+
}
|
365
|
+
}).should == 600
|
366
|
+
|
367
|
+
eval(program, {
|
368
|
+
:hash => {
|
369
|
+
1 => [ 100, 100, 100 ],
|
370
|
+
2 => [ 200, 200, 200 ],
|
371
|
+
3 => [ 300, 300, 300 ]
|
372
|
+
}
|
373
|
+
}).should == 1800
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should define a 'avg_hash_value_sum' function" do
|
377
|
+
|
378
|
+
program = <<-PROGRAM
|
379
|
+
require 'stdlib/avg_hash_value_sum';
|
380
|
+
avg_hash_value_sum(hash);
|
381
|
+
PROGRAM
|
382
|
+
|
383
|
+
eval(program, :hash => {}).should == 0
|
384
|
+
|
385
|
+
eval(program, {
|
386
|
+
:hash => {
|
387
|
+
1 => 100,
|
388
|
+
2 => 200,
|
389
|
+
3 => 300
|
390
|
+
}
|
391
|
+
}).should == 200
|
392
|
+
|
393
|
+
eval(program, {
|
394
|
+
:hash => {
|
395
|
+
1 => [ 100, 110, 120 ],
|
396
|
+
2 => [ 200, 210, 220 ],
|
397
|
+
3 => [ 300, 310, 320 ]
|
398
|
+
}
|
399
|
+
}).should == 630
|
400
|
+
end
|
401
|
+
|
402
|
+
it "should define a 'hash_range_value_sum' function" do
|
403
|
+
|
404
|
+
program = <<-PROGRAM
|
405
|
+
require 'stdlib/hash_range_value_sum';
|
406
|
+
hash_range_value_sum('a'..'b');
|
407
|
+
PROGRAM
|
408
|
+
|
409
|
+
eval(program, {
|
410
|
+
:a => { 1 => 100 },
|
411
|
+
:b => { 2 => 200 }
|
412
|
+
}).should == 300
|
413
|
+
|
414
|
+
eval(program, {
|
415
|
+
:a => { 1 => [ 100, 100, 100 ] },
|
416
|
+
:b => { 2 => [ 200, 200, 200 ] }
|
417
|
+
}).should == 900
|
418
|
+
end
|
419
|
+
|
420
|
+
it "should define a 'avg_hash_range_value_sum' function" do
|
421
|
+
|
422
|
+
program = <<-PROGRAM
|
423
|
+
require 'stdlib/avg_hash_range_value_sum';
|
424
|
+
avg_hash_range_value_sum('a'..'b');
|
425
|
+
PROGRAM
|
426
|
+
|
427
|
+
eval(program, {
|
428
|
+
:a => { 1 => 100 },
|
429
|
+
:b => { 2 => 200 }
|
430
|
+
}).should == 300
|
431
|
+
|
432
|
+
eval(program, {
|
433
|
+
:a => { 1 => [ 100, 110, 120 ] },
|
434
|
+
:b => { 2 => [ 200, 210, 220 ] }
|
435
|
+
}).should == 320
|
436
|
+
end
|
437
|
+
|
268
438
|
it "should define a 'sum_of_type' function" do
|
269
439
|
env = {
|
270
440
|
:types => {
|
271
|
-
1 => "Feuerwehrgebäude",
|
272
|
-
2 => "Schulgebäude",
|
441
|
+
1 => "Feuerwehrgebäude",
|
442
|
+
2 => "Schulgebäude",
|
273
443
|
3 => "Sonstige",
|
274
444
|
},
|
275
|
-
:values => {
|
445
|
+
:values => {
|
276
446
|
1 => [ 100, 100, 100 ],
|
277
447
|
2 => [ 200, 200, 200 ],
|
278
448
|
3 => [ 300, 300, 300 ]
|
@@ -283,15 +453,15 @@ describe "The Trxl::StdLib" do
|
|
283
453
|
eval("#{req_stmt} sum_of_type('Schulgebäude', types, values);", env).should == 600
|
284
454
|
eval("#{req_stmt} sum_of_type('Sonstige', types, values);", env).should == 900
|
285
455
|
end
|
286
|
-
|
456
|
+
|
287
457
|
it "should define a 'avg_sum_of_type' function" do
|
288
458
|
env = {
|
289
459
|
:types => {
|
290
|
-
1 => "Feuerwehrgebäude",
|
291
|
-
2 => "Schulgebäude",
|
460
|
+
1 => "Feuerwehrgebäude",
|
461
|
+
2 => "Schulgebäude",
|
292
462
|
3 => "Sonstige",
|
293
463
|
},
|
294
|
-
:values => {
|
464
|
+
:values => {
|
295
465
|
1 => [ 100, 100, 100 ],
|
296
466
|
2 => [ 200, 200, 200 ],
|
297
467
|
3 => [ 300, 300, 300 ]
|
@@ -302,20 +472,20 @@ describe "The Trxl::StdLib" do
|
|
302
472
|
eval("#{req_stmt} avg_sum_of_type('Schulgebäude', types, values);", env).should == 200
|
303
473
|
eval("#{req_stmt} avg_sum_of_type('Sonstige', types, values);", env).should == 300
|
304
474
|
end
|
305
|
-
|
475
|
+
|
306
476
|
it "should define a 'total_range_sum_of_type' function" do
|
307
477
|
env = {
|
308
478
|
:types => {
|
309
|
-
1 => "Feuerwehrgebäude",
|
310
|
-
2 => "Schulgebäude",
|
479
|
+
1 => "Feuerwehrgebäude",
|
480
|
+
2 => "Schulgebäude",
|
311
481
|
3 => "Sonstige",
|
312
482
|
},
|
313
|
-
:a => {
|
483
|
+
:a => {
|
314
484
|
1 => [ 100, 100, 100 ],
|
315
485
|
2 => [ 200, 200, 200 ],
|
316
486
|
3 => [ 300, 300, 300 ]
|
317
487
|
},
|
318
|
-
:b => {
|
488
|
+
:b => {
|
319
489
|
1 => [ 100, 100, 100 ],
|
320
490
|
2 => [ 200, 200, 200 ],
|
321
491
|
3 => [ 300, 300, 300 ]
|
@@ -326,20 +496,20 @@ describe "The Trxl::StdLib" do
|
|
326
496
|
eval("#{req_stmt} total_range_sum_of_type('Schulgebäude', types, 'a'..'b');", env).should == 1200
|
327
497
|
eval("#{req_stmt} total_range_sum_of_type('Sonstige', types, 'a'..'b');", env).should == 1800
|
328
498
|
end
|
329
|
-
|
499
|
+
|
330
500
|
it "should define a 'avg_range_sum_of_type' function" do
|
331
501
|
env = {
|
332
502
|
:types => {
|
333
|
-
1 => "Feuerwehrgebäude",
|
334
|
-
2 => "Schulgebäude",
|
503
|
+
1 => "Feuerwehrgebäude",
|
504
|
+
2 => "Schulgebäude",
|
335
505
|
3 => "Sonstige",
|
336
506
|
},
|
337
|
-
:a => {
|
507
|
+
:a => {
|
338
508
|
1 => [ 100, 100, 100 ],
|
339
509
|
2 => [ 200, 200, 200 ],
|
340
510
|
3 => [ 300, 300, 300 ]
|
341
511
|
},
|
342
|
-
:b => {
|
512
|
+
:b => {
|
343
513
|
1 => [ 100, 100, 100 ],
|
344
514
|
2 => [ 200, 200, 200 ],
|
345
515
|
3 => [ 300, 300, 300 ]
|
@@ -350,7 +520,28 @@ describe "The Trxl::StdLib" do
|
|
350
520
|
eval("#{req_stmt} avg_range_sum_of_type('Schulgebäude', types, 'a'..'b');", env).should == 400
|
351
521
|
eval("#{req_stmt} avg_range_sum_of_type('Sonstige', types, 'a'..'b');", env).should == 600
|
352
522
|
end
|
353
|
-
|
523
|
+
|
524
|
+
it "should define a 'avg_range_sum' function" do
|
525
|
+
|
526
|
+
program = <<-PROGRAM
|
527
|
+
require 'stdlib/avg_range_sum';
|
528
|
+
avg_range_sum('a'..'c');
|
529
|
+
PROGRAM
|
530
|
+
|
531
|
+
eval(program, {
|
532
|
+
:a => [],
|
533
|
+
:b => [],
|
534
|
+
:c => []
|
535
|
+
}).should == 0
|
536
|
+
|
537
|
+
eval(program, {
|
538
|
+
:a => [100, 200, 300],
|
539
|
+
:b => [400, 500, 600],
|
540
|
+
:c => [700, 800, 900]
|
541
|
+
}).should == 1500
|
542
|
+
|
543
|
+
end
|
544
|
+
|
354
545
|
it "should define a 'year_from_date' function" do
|
355
546
|
req_stmt = "require 'stdlib/year_from_date';"
|
356
547
|
eval("#{req_stmt} year_from_date('01/1999');").should == 1999
|
@@ -358,7 +549,7 @@ describe "The Trxl::StdLib" do
|
|
358
549
|
eval("#{req_stmt} year_from_date('12/2008');").should == 2008
|
359
550
|
eval("#{req_stmt} year_from_date('12/2008');").should == 2008
|
360
551
|
end
|
361
|
-
|
552
|
+
|
362
553
|
it "should define a 'month_from_date' function" do
|
363
554
|
req_stmt = "require 'stdlib/month_from_date';"
|
364
555
|
eval("#{req_stmt} month_from_date('01/1999');").should == 1
|
@@ -366,5 +557,5 @@ describe "The Trxl::StdLib" do
|
|
366
557
|
eval("#{req_stmt} month_from_date('1/2008');").should == 1
|
367
558
|
eval("#{req_stmt} month_from_date('12/2008');").should == 12
|
368
559
|
end
|
369
|
-
|
560
|
+
|
370
561
|
end
|