sexp_processor 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,297 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ $TESTING = true
4
+
5
+ require 'sexp_processor'
6
+ require 'stringio'
7
+ require 'test/unit'
8
+ require 'pp'
9
+
10
+ # Fake test classes:
11
+
12
+ class TestProcessor < SexpProcessor # ZenTest SKIP
13
+ attr_accessor :auto_shift_type
14
+
15
+ def process_acc1(exp)
16
+ out = self.expected.new(:acc2, exp.thing_three, exp.thing_two, exp.thing_one)
17
+ exp.clear
18
+ return out
19
+ end
20
+
21
+ def process_acc2(exp)
22
+ out = []
23
+ out << exp.thing_one
24
+ end
25
+
26
+ def process_specific(exp)
27
+ name = exp.shift
28
+ result = s(:blah)
29
+ until exp.empty?
30
+ result.push process(exp.shift)
31
+ end
32
+ result
33
+ end
34
+
35
+ def process_strip(exp)
36
+ result = exp.deep_clone
37
+ exp.clear
38
+ result
39
+ end
40
+
41
+ def process_nonempty(exp)
42
+ s(*exp)
43
+ end
44
+
45
+ def process_broken(exp)
46
+ result = [*exp]
47
+ exp.clear
48
+ result
49
+ end
50
+
51
+ def process_expected(exp)
52
+ exp.clear
53
+ return {}
54
+ end
55
+
56
+ def process_string(exp)
57
+ return exp.shift
58
+ end
59
+
60
+ def rewrite_rewritable(exp) # (a b c) => (a c b)
61
+ return s(exp.shift, exp.pop, exp.shift)
62
+ end
63
+
64
+ def process_rewritable(exp)
65
+ @n ||= 0
66
+ exp.shift # name
67
+ result = s(:rewritten)
68
+ until exp.empty?
69
+ result.push process(exp.shift)
70
+ end
71
+ result.push @n
72
+ @n += 1
73
+ result
74
+ end
75
+
76
+ def rewrite_major_rewrite(exp)
77
+ exp[0] = :rewritable
78
+ exp
79
+ end
80
+ end
81
+
82
+ class TestProcessorDefault < SexpProcessor # ZenTest SKIP
83
+ def initialize
84
+ super
85
+ self.default_method = :def_method
86
+ end
87
+
88
+ def def_method(exp)
89
+ exp.clear
90
+ self.expected.new(42)
91
+ end
92
+ end
93
+
94
+ # Real test classes:
95
+
96
+ class TestSexpProcessor < Test::Unit::TestCase
97
+
98
+ def setup
99
+ @processor = TestProcessor.new
100
+ end
101
+
102
+ def test_process_specific
103
+ a = [:specific, [:x, 1], [:y, 2], [:z, 3]]
104
+ expected = [:blah, [:x, 1], [:y, 2], [:z, 3]]
105
+ assert_equal(expected, @processor.process(a))
106
+ end
107
+
108
+ def test_process_generic
109
+ a = [:blah, 1, 2, 3]
110
+ expected = a.deep_clone
111
+ assert_equal(expected, @processor.process(a))
112
+ end
113
+
114
+ def test_process_default
115
+ @processor = TestProcessorDefault.new
116
+ @processor.warn_on_default = false
117
+
118
+ a = s(:blah, 1, 2, 3)
119
+ assert_equal(@processor.expected.new(42), @processor.process(a))
120
+ end
121
+
122
+ def test_process_not_sexp
123
+ @processor = TestProcessor.new
124
+ @processor.warn_on_default = false
125
+
126
+ assert_raises SexpTypeError do
127
+ @processor.process([:broken, 1, 2, 3])
128
+ end
129
+ end
130
+
131
+ def test_process_unsupported_wrong
132
+ @processor = TestProcessor.new
133
+ @processor.unsupported << :strip
134
+
135
+ assert_raises UnsupportedNodeError do
136
+ @processor.process([:whatever])
137
+ end
138
+ end
139
+
140
+ def test_unsupported_equal
141
+ @processor.strict = true
142
+ @processor.unsupported = [ :unsupported ]
143
+ assert_raises UnsupportedNodeError do
144
+ @processor.process([:unsupported, 42])
145
+ end
146
+ end
147
+
148
+ def test_strict
149
+ @processor.strict = true
150
+ assert_raises UnknownNodeError do
151
+ @processor.process([:blah, 1, 2, 3])
152
+ end
153
+ end
154
+ def test_strict=; end #Handled
155
+
156
+ def test_require_empty_false
157
+ @processor.require_empty = false
158
+
159
+ @processor.process([:nonempty, 1, 2, 3])
160
+ end
161
+
162
+ def test_require_empty_true
163
+ assert_raises NotEmptyError do
164
+ @processor.process([:nonempty, 1, 2, 3])
165
+ end
166
+ end
167
+ def test_require_empty=; end # handled
168
+
169
+ def test_process_strip
170
+ @processor.auto_shift_type = true
171
+ assert_equal([1, 2, 3], @processor.process(s(:strip, 1, 2, 3)))
172
+ end
173
+
174
+ def test_rewrite
175
+ assert_equal(s(:rewritable, :b, :a),
176
+ @processor.rewrite(s(:rewritable, :a, :b)))
177
+ end
178
+
179
+ def test_rewrite_different_type
180
+ assert_equal(s(:rewritable, :b, :a),
181
+ @processor.rewrite(s(:major_rewrite, :a, :b)))
182
+ end
183
+
184
+ def test_rewrite_deep
185
+ assert_equal(s(:specific, s(:rewritable, :b, :a)),
186
+ @processor.rewrite(s(:specific, s(:rewritable, :a, :b))))
187
+ end
188
+
189
+ def test_rewrite_not_empty
190
+ insert = s(:rewritable, 1, 2, 2)
191
+ expect = s(:rewritable, 2, 1)
192
+ result = @processor.rewrite(insert)
193
+ assert_equal(expect, result)
194
+ assert_equal(s(2), insert) # post-processing
195
+ end
196
+
197
+ def test_process_rewrite
198
+ assert_equal(s(:rewritten, s(:y, 2), s(:x, 1), 0),
199
+ @processor.process(s(:rewritable, s(:x, 1), s(:y, 2))))
200
+ end
201
+
202
+ def test_process_rewrite_deep
203
+ assert_equal(s(:blah, s(:rewritten, s(:b), s(:a), 0)),
204
+ @processor.process(s(:specific, s(:rewritable, s(:a), s(:b)))))
205
+ end
206
+
207
+ def test_rewrite_depth_first
208
+ inn = s(:specific,
209
+ s(:rewritable,
210
+ s(:a),
211
+ s(:rewritable,
212
+ s(:rewritable, s(:b), s(:c)),
213
+ s(:d))))
214
+ out = s(:specific,
215
+ s(:rewritable,
216
+ s(:rewritable,
217
+ s(:d),
218
+ s(:rewritable, s(:c), s(:b))),
219
+ s(:a)))
220
+
221
+ assert_equal(out, @processor.rewrite(inn))
222
+ end
223
+
224
+ def test_process_rewrite_depth_first
225
+ inn = s(:specific,
226
+ s(:rewritable,
227
+ s(:a),
228
+ s(:rewritable,
229
+ s(:rewritable, s(:b), s(:c)),
230
+ s(:d))))
231
+ out = s(:blah,
232
+ s(:rewritten,
233
+ s(:rewritten,
234
+ s(:d),
235
+ s(:rewritten, s(:c), s(:b), 0), 1),
236
+ s(:a), 2))
237
+
238
+ assert_equal(out, @processor.process(inn))
239
+ end
240
+
241
+ def test_assert_type_hit
242
+ @processor.assert_type([:blah, 1, 2, 3], :blah)
243
+ end
244
+
245
+ def test_assert_type_miss
246
+ assert_raises SexpTypeError do
247
+ @processor.assert_type([:thingy, 1, 2, 3], :blah)
248
+ end
249
+ end
250
+
251
+ def test_generate
252
+ # nothing to test at this time... soon.
253
+ end
254
+
255
+ def test_auto_shift_type
256
+ @processor.auto_shift_type = false
257
+ assert_equal(false, @processor.auto_shift_type)
258
+ @processor.auto_shift_type = true
259
+ assert_equal(true, @processor.auto_shift_type)
260
+ end
261
+ def test_auto_shift_type_equal; end # handled
262
+
263
+ def test_default_method
264
+ # default functionality tested in process_default
265
+ assert_nil @processor.default_method
266
+ @processor.default_method = :something
267
+ assert_equal :something, @processor.default_method
268
+ end
269
+ def test_default_method=; end # handled
270
+
271
+ def test_expected
272
+ assert_equal Sexp, @processor.expected
273
+ assert_raises SexpTypeError do
274
+ @processor.process([:expected]) # should raise
275
+ end
276
+
277
+ @processor.process(s(:str, "string")) # shouldn't raise
278
+
279
+ @processor.expected = Hash
280
+ assert_equal Hash, @processor.expected
281
+ assert !(Hash === s()), "Hash === s() should not be true"
282
+
283
+ assert_raises SexpTypeError do
284
+ @processor.process(s(:string, "string")) # should raise
285
+ end
286
+
287
+ @processor.process([:expected]) # shouldn't raise
288
+ end
289
+ def test_expected=; end # handled
290
+
291
+ # Not Testing:
292
+ def test_debug; end
293
+ def test_debug=; end
294
+ def test_warn_on_default; end
295
+ def test_warn_on_default=; end
296
+
297
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sexp_processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Davis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-22 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.0
24
+ version:
25
+ description: FIX (describe your package)
26
+ email:
27
+ - ryand-ruby@zenspider.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - lib/composite_sexp_processor.rb
42
+ - lib/sexp.rb
43
+ - lib/sexp_processor.rb
44
+ - test/test_composite_sexp_processor.rb
45
+ - test/test_environment.rb
46
+ - test/test_sexp.rb
47
+ - test/test_sexp_processor.rb
48
+ has_rdoc: true
49
+ homepage: FIX (url)
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --main
53
+ - README.txt
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project: parsetree
71
+ rubygems_version: 1.3.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: FIX (describe your package)
75
+ test_files:
76
+ - test/test_composite_sexp_processor.rb
77
+ - test/test_environment.rb
78
+ - test/test_sexp.rb
79
+ - test/test_sexp_processor.rb