q-language 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/.gemtest +0 -0
  2. data/Rakefile +10 -0
  3. data/lib/q-language.rb +9 -0
  4. data/lib/q-language/device.rb +166 -0
  5. data/lib/q-language/environment.rb +358 -0
  6. data/lib/q-language/methods/array.rb +523 -0
  7. data/lib/q-language/methods/block.rb +26 -0
  8. data/lib/q-language/methods/class.rb +24 -0
  9. data/lib/q-language/methods/dynamic.rb +82 -0
  10. data/lib/q-language/methods/false.rb +47 -0
  11. data/lib/q-language/methods/hash.rb +351 -0
  12. data/lib/q-language/methods/implicit.rb +345 -0
  13. data/lib/q-language/methods/module.rb +39 -0
  14. data/lib/q-language/methods/nil.rb +47 -0
  15. data/lib/q-language/methods/number.rb +118 -0
  16. data/lib/q-language/methods/object.rb +155 -0
  17. data/lib/q-language/methods/string.rb +157 -0
  18. data/lib/q-language/methods/time.rb +110 -0
  19. data/lib/q-language/methods/token.rb +72 -0
  20. data/lib/q-language/methods/true.rb +14 -0
  21. data/lib/q-language/node.rb +45 -0
  22. data/lib/q-language/object.rb +125 -0
  23. data/lib/q-language/parser.rb +104 -0
  24. data/lib/q-language/writer.rb +90 -0
  25. data/test/methods/test_array.rb +191 -0
  26. data/test/methods/test_block.rb +66 -0
  27. data/test/methods/test_class.rb +34 -0
  28. data/test/methods/test_dynamic.rb +158 -0
  29. data/test/methods/test_false.rb +60 -0
  30. data/test/methods/test_hash.rb +10 -0
  31. data/test/methods/test_implicit.rb +332 -0
  32. data/test/methods/test_module.rb +55 -0
  33. data/test/methods/test_nil.rb +60 -0
  34. data/test/methods/test_number.rb +10 -0
  35. data/test/methods/test_object.rb +157 -0
  36. data/test/methods/test_string.rb +271 -0
  37. data/test/methods/test_time.rb +181 -0
  38. data/test/methods/test_token.rb +92 -0
  39. data/test/methods/test_true.rb +16 -0
  40. data/test/test.rb +23 -0
  41. metadata +103 -0
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require_relative '../test.rb'
7
+
8
+ class TestModule < TestQ
9
+ def test_def_returns_self
10
+ returns :m, "{ :m module def Token(foo) { Number(614) } }"
11
+ end
12
+
13
+ def test_def_defines_method
14
+ script = "{ module def Token(foo) { Number(614) } }"
15
+ object = Object.new.extend(Q_Device.new(script).execute.return_value)
16
+
17
+ assert_equal(614, object.foo)
18
+ end
19
+
20
+ def test_def_defines_method_where_self_is_the_caller
21
+ script = "{ module def Token(foo) { self } }"
22
+ object = Object.new.extend(Q_Device.new(script).execute.return_value)
23
+
24
+ assert_equal(object, object.foo)
25
+ end
26
+
27
+ def test_include_returns_self
28
+ returns :m, "{ :m module include module }"
29
+ end
30
+
31
+ def test_include_includes_module
32
+ script = "{ module include { module def Token(foo) { Number(614) } } }"
33
+ object = Object.new.extend(Q_Device.new(script).execute.return_value)
34
+
35
+ assert_equal(614, object.foo)
36
+ end
37
+
38
+ def test_include_does_not_include_class
39
+ script = "{ module include { class def Token(foo) { Number(614) } } }"
40
+ object = Object.new.extend(Q_Device.new(script).execute.return_value)
41
+
42
+ assert_raises(NoMethodError) { object.foo }
43
+ end
44
+
45
+ def test_include_avoids_cyclic_include_error
46
+ script = "{ :m module include { module include :m } }"
47
+ Q_Device.new(script).execute
48
+
49
+ # No error means it passed
50
+ end
51
+
52
+ def test_module_p_returns_self
53
+ returns :m, "{ :m module module? }"
54
+ end
55
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require_relative '../test.rb'
7
+
8
+ class TestNil < TestQ
9
+ def test_and_returns_nil
10
+ eq nil, "{ nil and Token(a) }"
11
+ end
12
+
13
+ def test_choose_returns_object_2
14
+ eq :b, "{ choose nil Token(a) Token(b) }"
15
+ end
16
+
17
+ def test_if_returns_nil
18
+ eq nil, "{ if nil { Token(a) } }"
19
+ end
20
+
21
+ def test_nil_p_returns_true
22
+ eq true, "{ nil nil? }"
23
+ end
24
+
25
+ def test_not_returns_true
26
+ eq true, "{ not nil }"
27
+ end
28
+
29
+ def test_or_returns_object
30
+ eq :a, "{ nil or Token(a) }"
31
+ end
32
+
33
+ def test_stringify_returns_nil_string
34
+ eq "nil", "{ nil stringify }"
35
+ end
36
+
37
+ def test_tally_returns_zero
38
+ eq 0, "{ nil tally }"
39
+ end
40
+
41
+ def test_unless_returns_block_return_value
42
+ eq :a, "{ unless nil { Token(a) } }"
43
+ end
44
+
45
+ def test_unless_calls_block
46
+ set? :value, 614, "{ unless nil { :value Number(614) } }"
47
+ end
48
+
49
+ def test_xor_false_returns_false
50
+ eq false, "{ nil xor false }"
51
+ end
52
+
53
+ def test_xor_nil_returns_false
54
+ eq false, "{ nil xor nil }"
55
+ end
56
+
57
+ def test_xor_true_returns_true
58
+ eq true, "{ nil xor true }"
59
+ end
60
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require_relative '../test.rb'
7
+
8
+ class TestNumber < TestQ
9
+
10
+ end
@@ -0,0 +1,157 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require_relative '../test.rb'
7
+
8
+ class TestObject < TestQ
9
+ def test_and_returns_argument
10
+ eq :a, "{ new and Token(a) }"
11
+ end
12
+
13
+ def test_array_p_returns_false
14
+ eq false, "{ new array? }"
15
+ end
16
+
17
+ def test_block_p_returns_false
18
+ eq false, "{ new block? }"
19
+ end
20
+
21
+ def test_break
22
+ end
23
+
24
+ def test_choose_returns_object_1
25
+ eq :a, "{ new choose Token(a) Token(b) }"
26
+ end
27
+
28
+ def test_class_p_returns_false
29
+ eq false, "{ new class? }"
30
+ end
31
+
32
+ def test_dup_returns_a_duplicate
33
+ script = "{ :o new dup }"
34
+ env = Q_Device.new(script).execute
35
+
36
+ assert_instance_of(env.return_value.class, env.return_value)
37
+ end
38
+
39
+ def test_dup_returns_self_when_duplication_is_disallowed
40
+ eq true, "{ true dup }"
41
+ end
42
+
43
+ def test_dynamic_p_returns_false
44
+ eq false, "{ true dynamic? }"
45
+ end
46
+
47
+ def test_eq_p_returns_true_when_object_is_eq
48
+ eq true, "{ String(foo) eq? String(foo) }"
49
+ end
50
+
51
+ def test_eq_p_returns_false_when_object_is_not_eq
52
+ eq false, "{ new eq? new }"
53
+ end
54
+
55
+ def test_eqq_p_returns_true_when_object_is_eqq
56
+ eq true, "{ :o new eqq? :o }"
57
+ end
58
+
59
+ def test_eqq_p_returns_false_when_object_is_not_eqq
60
+ eq false, "{ new eqq? new }"
61
+ end
62
+
63
+ def test_false_p_returns_false
64
+ eq false, "{ new false? }"
65
+ end
66
+
67
+ def test_hash_p_returns_false
68
+ eq false, "{ new hash? }"
69
+ end
70
+
71
+ def test_if_returns_block_return_value
72
+ eq :a, "{ if new { Token(a) } }"
73
+ end
74
+
75
+ def test_if_calls_block
76
+ set? :value, :a, "{ if new { :value Token(a) Token(b) } }"
77
+ end
78
+
79
+ def test_if_skips_the_rest_of_the_block
80
+ set? :value, nil, "{ if new { } :value Token(a) }"
81
+ end
82
+
83
+ def test_jump
84
+ end
85
+
86
+ def test_module_p_returns_false
87
+ eq false, "{ new module? }"
88
+ end
89
+
90
+ def test_neq_p_returns_false_when_object_is_eq
91
+ eq false, "{ String(foo) neq? String(foo) }"
92
+ end
93
+
94
+ def test_neq_p_returns_true_when_object_is_not_eq
95
+ eq true, "{ new neq? new }"
96
+ end
97
+
98
+ def test_nil_p_returns_false
99
+ eq false, "{ new nil? }"
100
+ end
101
+
102
+ def test_not_returns_false
103
+ eq false, "{ not new }"
104
+ end
105
+
106
+ def test_number_p_returns_false
107
+ eq false, "{ new number? }"
108
+ end
109
+
110
+ def test_or_returns_self
111
+ eq true, "{ true or Token(a) }"
112
+ end
113
+
114
+ def test_same_p_returns_true_when_object_is_same
115
+ eq true, "{ :o new same? :o }"
116
+ end
117
+
118
+ def test_same_p_returns_true_when_object_is_not_same
119
+ eq false, "{ String(foo) same? String(foo) }"
120
+ end
121
+
122
+ def test_string_p_returns_false
123
+ eq false, "{ new string? }"
124
+ end
125
+
126
+ def test_tally_returns_one
127
+ eq 1, "{ new tally }"
128
+ end
129
+
130
+ def test_time_p_returns_false
131
+ eq false, "{ new time? }"
132
+ end
133
+
134
+ def test_token_p_returns_false
135
+ eq false, "{ new token? }"
136
+ end
137
+
138
+ def test_false_p_returns_false
139
+ eq false, "{ new false? }"
140
+ end
141
+
142
+ def test_unless_returns_nil
143
+ eq nil, "{ unless new { Token(a) } }"
144
+ end
145
+
146
+ def test_use_returns_self
147
+ returns :o, "{ use :o new { Token(a) } }"
148
+ end
149
+
150
+ def test_use_calls_block
151
+ set? :value, 614, "{ use new { :value Number(614) } }"
152
+ end
153
+
154
+ def test_use_calls_block_with_self_as_argument
155
+ set? :value, 614, "{ use Number(614) :arg { :value :arg } }"
156
+ end
157
+ end
@@ -0,0 +1,271 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require_relative '../test.rb'
7
+
8
+ class TestString < TestQ
9
+ def test_add_returns_string_concatenation
10
+ eq 'abcdef', "{ String(abc) add String(def) }"
11
+ end
12
+
13
+ def test_add_all_returns_string_concatenation
14
+ eq 'abcdefghi', "{ String(abc) String(def) String(ghi) add_all }"
15
+ end
16
+
17
+ def test_at_n_returns_nth_character
18
+ eq ?b, "{ String(abc) at Number(1) }"
19
+ end
20
+
21
+ def test_at_outside_of_range_returns_nil
22
+ eq nil, "{ String(abc) at Number(100) }"
23
+ end
24
+
25
+ def test_between_p_returns_true_when_equal_to_lowest_string
26
+ eq true, "{ :s1 between? :s1 :s3 }", s1: ?a, s2: ?b, s3: ?c
27
+ end
28
+
29
+ def test_between_p_returns_true_when_equal_to_highest_string
30
+ eq true, "{ :s3 between? :s1 :s3 }", s1: ?a, s2: ?b, s3: ?c
31
+ end
32
+
33
+ def test_between_p_returns_false_when_lower_than_lowest_string
34
+ eq false, "{ :s1 between? :s2 :s3 }", s1: ?a, s2: ?b, s3: ?c
35
+ end
36
+
37
+ def test_between_p_returns_false_when_higher_than_highest_string
38
+ eq false, "{ :s3 between? :s1 :s2 }", s1: ?a, s2: ?b, s3: ?c
39
+ end
40
+
41
+ def test_between_p_returns_true_when_between_highest_and_lowest_strings
42
+ eq true, "{ :s2 between? :s1 :s3 }", s1: ?a, s2: ?b, s3: ?c
43
+ end
44
+
45
+ def test_capitalize_returns_capitalized_string
46
+ eq 'Abc', "{ String(abc) capitalize }"
47
+ end
48
+
49
+ def test_capitalize_bang_returns_capitalized_string
50
+ eq 'Abc', "{ String(abc) capitalize! }"
51
+ end
52
+
53
+ def test_capitalize_bang_returns_same_string
54
+ returns :s, "{ :s String(abc) capitalize! }"
55
+ end
56
+
57
+ def test_clear_returns_empty_string
58
+ eq '', "{ String(abc) clear }"
59
+ end
60
+
61
+ def test_clear_returns_same_string
62
+ returns :s, "{ :s String(abc) clear }"
63
+ end
64
+
65
+ def test_downcase_returns_lowercase_string
66
+ eq 'abc', "{ String(AbC) downcase }"
67
+ end
68
+
69
+ def test_downcase_bang_returns_lowercase_string
70
+ eq 'abc', "{ String(AbC) downcase! }"
71
+ end
72
+
73
+ def test_downcase_bang_returns_same_string
74
+ returns :s, "{ :s String(AbC) downcase! }"
75
+ end
76
+
77
+ def test_each_returns_self
78
+ returns :s, "{ :s String(abc) each { } }"
79
+ end
80
+
81
+ def test_each_calls_block_once_per_character
82
+ eq 3, "{ String(abc) each { :a push i } :a elect length }", a: []
83
+ end
84
+
85
+ def test_each_terminates_upon_break
86
+ eq 1, "{ String(abc) each { :a push i break_if { true } } :a elect length }", a: []
87
+ end
88
+
89
+ def test_each_continues_upon_jump
90
+ eq 3, "{ String(abc) each { :a push i jump_if { true } :a elect push i } :a elect length }", a: []
91
+ end
92
+
93
+ def test_each_terminates_upon_break
94
+ eq 1, "{ String(abc) each { :a push i break_if { true } } :a elect length }", a: []
95
+ end
96
+
97
+ def test_each_calls_block_with_each_character_as_argument
98
+ eq [?a, ?b, ?c], "{ String(abc) each :c { :a push :c } :a elect }", a: []
99
+ end
100
+
101
+ def test_each_line_returns_self
102
+ returns :s, "{ :s String(abc) each_line { } }"
103
+ end
104
+
105
+ def test_each_calls_block_once_per_line
106
+ eq 3, "{ String(a\nb\nc) each_line { :a push i } :a elect length }", a: []
107
+ end
108
+
109
+ def test_each_line_continues_upon_jump
110
+ eq 3, "{ String(a\nb\nc) each_line { :a push i jump_if { true } :a elect push i } :a elect length }", a: []
111
+ end
112
+
113
+ def test_each_line_terminates_upon_break
114
+ eq 1, "{ String(a\nb\nc) each_line { :a push i break_if { true } } :a elect length }", a: []
115
+ end
116
+
117
+ def test_each_line_calls_block_with_each_line_as_argument
118
+ eq [?a, ?b, ?c], "{ String(a\nb\nc) each_line :l { :a push { :l strip } } :a elect }", a: []
119
+ end
120
+
121
+ def test_empty_p_returns_true_for_empty_string
122
+ eq true, "{ String() empty? }"
123
+ end
124
+
125
+ def test_empty_p_returns_false_for_nonempty_string
126
+ eq false, "{ String(abc) empty? }"
127
+ end
128
+
129
+ def test_end_with_p_returns_true_when_ends_with_string
130
+ eq true, "{ String(abc) end_with? String(bc) }"
131
+ end
132
+
133
+ def test_end_with_p_returns_false_when_does_not_end_with_string
134
+ eq false, "{ String(abc) end_with? String(ab) }"
135
+ end
136
+
137
+ def test_include_p_returns_true_when_includes_string
138
+ eq true, "{ String(abc) include? String(bc) }"
139
+ end
140
+
141
+ def test_include_p_returns_false_when_does_not_include_string
142
+ eq false, "{ String(abc) include? String(cb) }"
143
+ end
144
+
145
+ def test_index_returns_index_of_string
146
+ eq 1, "{ String(abc) index String(b) }"
147
+ end
148
+
149
+ def test_index_returns_nil_when_no_such_string
150
+ eq nil, "{ String(abc) index String(d) }"
151
+ end
152
+
153
+ def test_length_returns_string_length
154
+ eq 3, "{ String(abc) length }"
155
+ end
156
+
157
+ def test_next_returns_next_string
158
+ eq 'abd', "{ String(abc) next }"
159
+ end
160
+
161
+ def test_next_bang_returns_next_string
162
+ eq 'abd', "{ String(abc) next! }"
163
+ end
164
+
165
+ def test_next_bang_returns_same_string
166
+ returns :s, "{ :s String(abc) next! }"
167
+ end
168
+
169
+ def test_partition_returns_string_partitioned_into_array
170
+ eq ['', ?a, 'bc'], "{ String(abc) partition String(a) }"
171
+ eq [?a, ?b, ?c], "{ String(abc) partition String(b) }"
172
+ eq ['ab', ?c, ''], "{ String(abc) partition String(c) }"
173
+ eq ['abc', '', ''], "{ String(abc) partition String(d) }"
174
+ end
175
+
176
+ def test_push_returns_string_concatenation
177
+ eq 'abcdef', "{ String(abc) push String(def) }"
178
+ end
179
+
180
+ def test_push_returns_same_string
181
+ returns :s, "{ :s String(abc) push String(def) }"
182
+ end
183
+
184
+ def test_push_all_returns_string_concatenation
185
+ eq 'abcdefghi', "{ String(abc) String(def) String(ghi) push_all }"
186
+ end
187
+
188
+ def test_push_all_returns_same_string
189
+ returns :s, "{ :s String(abc) String(def) String(ghi) push_all }"
190
+ end
191
+
192
+ def test_replace_returns_new_string_value
193
+ eq 'def', "{ String(abc) replace String(def) }"
194
+ end
195
+
196
+ def test_replace_returns_same_string
197
+ returns :s, "{ :s String(abc) replace String(def) }"
198
+ end
199
+
200
+ def test_reverse_returns_reversed_string
201
+ eq 'cba', "{ String(abc) reverse }"
202
+ end
203
+
204
+ def test_reverse_bang_returns_reversed_string
205
+ eq 'cba', "{ String(abc) reverse! }"
206
+ end
207
+
208
+ def test_reverse_bang_returns_same_string
209
+ returns :s, "{ :s String(abc) reverse! }"
210
+ end
211
+
212
+ def test_split_returns_string_split_into_array
213
+ eq [?a, ?c], "{ String(abc) split String(b) }"
214
+ end
215
+
216
+ def test_start_with_p_returns_true_when_starts_with_string
217
+ eq true, "{ String(abc) start_with? String(ab) }"
218
+ end
219
+
220
+ def test_start_with_p_returns_false_when_does_not_start_with_string
221
+ eq false, "{ String(abc) start_with? String(bc) }"
222
+ end
223
+
224
+ def test_string_p_returns_self
225
+ eq 'abc', "{ String(abc) string? }"
226
+ end
227
+
228
+ def test_stringify_returns_duplicate_string
229
+ eq 'abc', "{ String(abc) stringify }"
230
+ end
231
+
232
+ def test_strip_returns_stripped_string
233
+ eq 'abc', "{ String(\t\tabc\n) strip }"
234
+ end
235
+
236
+ def test_strip_bang_returns_stripped_string
237
+ eq 'abc', "{ String(\t\tabc\n) strip! }"
238
+ end
239
+
240
+ def test_strip_bang_returns_same_string
241
+ returns :s, "{ :s String(\t\tabc\n) strip! }"
242
+ end
243
+
244
+ def test_swapcase_returns_swapped_case_string
245
+ eq 'AbC', "{ String(aBc) swapcase }"
246
+ end
247
+
248
+ def test_swapcase_bang_returns_swapped_case_string
249
+ eq 'AbC', "{ String(aBc) swapcase! }"
250
+ end
251
+
252
+ def test_swapcase_bang_returns_same_string
253
+ returns :s, "{ :s String(aBc) swapcase! }"
254
+ end
255
+
256
+ def test_tokenize_returns_token
257
+ eq :abc, "{ String(abc) tokenize }"
258
+ end
259
+
260
+ def test_upcase_returns_uppercase_string
261
+ eq 'ABC', "{ String(aBc) upcase }"
262
+ end
263
+
264
+ def test_upcase_bang_returns_uppercase_string
265
+ eq 'ABC', "{ String(aBc) upcase! }"
266
+ end
267
+
268
+ def test_upcase_bang_returns_same_string
269
+ returns :s, "{ :s String(aBc) upcase! }"
270
+ end
271
+ end