public
Fork of KirinDave/fuzed
Description: A new revision of Fuzed, the Erlang-based frontend for web apps.
Clone URL: git://github.com/mojombo/fuzed.git
Search Repo:
spring cleaning for ruby code
mojombo (author)
Fri May 02 22:30:01 -0700 2008
commit  19568e88c5d923cb10d4df541599f43e111df265
tree    300a52604d76602048cc7f2bedf6dce8b91c4561
parent  7f391bbe3602141547236d9aa16602194e1efe5d
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
0
@@ -1 +1,674 @@
0
+require 'rubygems'
0
+require 'optparse'
0
+require 'rpm_interface'
0
+require 'erlectricity'
0
+require 'json'
0
+
0
+class Chassis
0
+ class << self
0
+ attr_accessor :signatures, :node_kind, :pkgs, :tags, :roles, :extra_config, :exception_handler, :exit_after_current_dispatch
0
+ end
0
+
0
+ self.signatures = {}
0
+ self.node_kind = nil
0
+ self.pkgs = []
0
+ self.tags = []
0
+ self.roles = []
0
+ self.extra_config = nil
0
+ self.exception_handler = nil
0
+ self.exit_after_current_dispatch = false
0
+
0
+ # Define a handler method
0
+ # +method+ is the Symbol method name
0
+ # +names+ is a list of required parameters
0
+ #
0
+ # Returns nothing
0
+ def self.handle(method, *names, &block)
0
+ Chassis.signatures[method] = names.sort { |a, b| a.to_s <=> b.to_s }
0
+
0
+ define_method("handle_proxy_#{method}", &block)
0
+
0
+ define_method("handle_#{method}".to_sym) do |iargs|
0
+ args = self.convert_args(iargs)
0
+ self.verify_args(method, args)
0
+
0
+ self.send("handle_proxy_#{method}", args)
0
+ end
0
+ end
0
+
0
+ # Specify this handler's kind
0
+ # +name+ is a String identifier
0
+ #
0
+ # Returns nothing
0
+ def self.kind(name)
0
+ Chassis.node_kind = name
0
+ end
0
+
0
+ # Specify package dependencies for the details list. If this is called
0
+ # more than once, the first list that contains a full match for installed
0
+ # packages will be used.
0
+ # +names+ is a list of package names (their dependencies will be
0
+ # traced automatically)
0
+ #
0
+ # Returns nothing
0
+ def self.packages(*names)
0
+ return if !Chassis.pkgs.empty?
0
+
0
+ fully_installed = names.all? do |package|
0
+ Powerset::PowerPack.installed?(package)
0
+ end
0
+
0
+ if fully_installed
0
+ deps = names.inject([]) do |acc, package|
0
+ acc << package
0
+ acc.concat Powerset::PowerPack.check(package).package_dependencies
0
+ acc
0
+ end
0
+
0
+ packs = Powerset::PowerPack.check_packages(deps.uniq)
0
+
0
+ Chassis.pkgs = packs.compact.map { |p| [p.name, self.limit_version(p.version)] }
0
+ end
0
+ end
0
+
0
+ def self.config(&block)
0
+ Chassis.extra_config = block.call
0
+ end
0
+
0
+ # Specify a block that takes an Exception as its single argument
0
+ # to be called when an exception is not handled inside a handler
0
+ # +block+ is the block to execute
0
+ #
0
+ # Block should
0
+ # Raise -or-
0
+ # Return one of:
0
+ # [:result, <jsonified result>]
0
+ # [:error, <error string>]
0
+ #
0
+ # Returns nothing
0
+ def self.handle_exception(&block)
0
+ Chassis.exception_handler = block
0
+ end
0
+
0
+ # Limit version number to 9999 max
0
+ # +version+ is the verstion number to limit
0
+ #
0
+ # Returns Integer
0
+ def self.limit_version(version)
0
+ version.map { |x| x > 9999 ? 9999 : x }
0
+ end
0
+
0
+ # Convert Erlang-style args to Ruby-style args
0
+ # +iargs+ is the Erlang-style arg structure
0
+ #
0
+ # Returns Hash (converted args)
0
+ def convert_args(iargs)
0
+ args = HashWithIndifferentAccess.new
0
+ iargs.each do |a|
0
+ args[a[0]] = self.convert_args_node(a[1])
0
+ end
0
+ args
0
+ end
0
+
0
+ # Helper for +convert_args+. Recursively converts a node in
0
+ # Erlang-style to a node in Ruby-style
0
+ # +node+ is the Erlang-style node
0
+ #
0
+ # Returns String|Symbol|Hash|Array
0
+ # Raises if an invalid node is encountered
0
+ def convert_args_node(node)
0
+ if node.kind_of?(String) || node.kind_of?(Symbol) || node.kind_of?(Numeric) ||
0
+ node.kind_of?(TrueClass) || node.kind_of?(FalseClass) || node.kind_of?(NilClass)
0
+ node
0
+ elsif node.instance_of?(Array)
0
+ if node[0] == :struct
0
+ node[1].inject(HashWithIndifferentAccess.new) do |acc, x|
0
+ acc[x[0]] = convert_args_node(x[1]); acc
0
+ end
0
+ elsif node[0] == :array
0
+ node[1].inject([]) do |acc, x|
0
+ acc << convert_args_node(x)
0
+ end
0
+ else
0
+ raise "Invalid tagged node: #{node.inspect}"
0
+ end
0
+ else
0
+ raise "Invalid node, must be an instance of String, Symbol, Numeric, True, False, Nil, or Array: #{node.inspect} (#{node.class.inspect})"
0
+ end
0
+ end
0
+
0
+ # Verify that the required args are met for the given method call
0
+ # +method+ is the Symbol representing the method name
0
+ # +args+ is the Ruby-style args to check
0
+ #
0
+ # Returns nothing
0
+ # Raises on verification failure
0
+ def verify_args(method, args)
0
+ matches = Chassis.signatures[method].select do |key|
0
+ args[key]
0
+ end
0
+
0
+ misses = Chassis.signatures[method] - matches
0
+
0
+ unless misses.empty?
0
+ raise "Required arguments missing for '#{method}': #{misses.join(", ")}"
0
+ end
0
+ end
0
+
0
+ # Dispatch a method by its name
0
+ # +method+ is the Symbol representing the method name
0
+ # +retype+ is the response type Symbol (:json | :pure)
0
+ # +args+ is the Erlang-stle args for the call
0
+ #
0
+ # Returns one of:
0
+ # [:result, <jsonified result>]
0
+ # [:error, <error string>]
0
+ def dispatch(method, retype, args)
0
+ result = self.send("handle_#{method}".to_sym, args)
0
+ result_key = Chassis.exit_after_current_dispatch ? :last_result : :result
0
+ case retype
0
+ when :json
0
+ [result_key, [:raw, result.to_json]]
0
+ when :pure
0
+ [result_key, result]
0
+ else
0
+ raise "Unknown response type: #{retype}"
0
+ end
0
+ rescue Exception => e
0
+ if e.instance_of?(SystemExit)
0
+ exit
0
+ elsif Chassis.exception_handler
0
+ begin
0
+ Chassis.exception_handler.call(e)
0
+ rescue Exception => e2
0
+ [:error, e2.message + "\n\n" + e2.backtrace.join("\n")]
0
+ end
0
+ else
0
+ [:error, e.message + "\n\n" + e.backtrace.join("\n")]
0
+ end
0
+ end
0
+
0
+ # The API structure of this Chassis, sorted alphabetically by method name
0
+ #
0
+ # Returns Array (Erlang-style nested structure)
0
+ # e.g.
0
+ # [[:meth1, []],
0
+ # [:meth2, [:arg1, :arg2]]]
0
+ def api
0
+ api = Chassis.signatures.to_a.sort { |a, b| a.first.to_s <=> b.first.to_s }
0
+ api
0
+ end
0
+
0
+ # Construct the config (aka details)
0
+ #
0
+ # Returns Array (config)
0
+ def config
0
+ details = Chassis.pkgs.dup
0
+ details << ["tags"] + Chassis.tags unless Chassis.tags.empty?
0
+ details << ["roles"] + Chassis.roles unless Chassis.roles.empty?
0
+ details << ["kind"] + [Chassis.node_kind] if Chassis.node_kind
0
+ details << Chassis.extra_config if Chassis.extra_config
0
+ details
0
+ end
0
+
0
+ # Start the Erlectricity recieve/respond loop
0
+ #
0
+ # Never returns
0
+ def start
0
+ receive(IO.new(3), IO.new(4)) do |f|
0
+ f.when(:call, Array) do |args|
0
+ method = args[0]
0
+ retype = args[1]
0
+ args = args[2..-1]
0
+ f.send! self.dispatch(method, retype, args)
0
+ exit if Chassis.exit_after_current_dispatch
0
+ f.receive_loop
0
+ end
0
+
0
+ f.when(:config) do
0
+ f.send! :result, self.config
0
+ f.receive_loop
0
+ end
0
+
0
+ f.when(:api) do
0
+ f.send! :result, self.api
0
+ f.receive_loop
0
+ end
0
+
0
+ f.when(:ping) do
0
+ f.send! :pong
0
+ f.receive_loop
0
+ end
0
+
0
+ f.when(:quit) do
0
+ exit(0)
0
+ end
0
+ end
0
+ end
0
+
0
+ def self.pull_cli_args
0
+ Chassis.tags = []
0
+ Chassis.roles = []
0
+
0
+ # parse the options
0
+ opts = OptionParser.new
0
+ opts.on("--tags x,y,z", Array) do |val|
0
+ Chassis.tags = val
0
+ end
0
+ opts.on("--roles x,y,z", Array) do |val|
0
+ Chassis.roles = val
0
+ end
0
+ opts.parse(ARGV)
0
+
0
+ # grab the extras
0
+ extras = []
0
+ dashdash = ARGV.index('--')
0
+ if dashdash
0
+ extras = ARGV[(dashdash + 1)..-1]
0
+ end
0
+
0
+ # ramrod the extras into ARGV
0
+ ARGV.clear
0
+ ARGV.concat(extras)
0
+ end
0
+
0
+ def self.start
0
+ # get any handlers
0
+ handlers = []
0
+ ObjectSpace.each_object(Class) do |o|
0
+ handlers << o if o < Chassis
0
+ end
0
+
0
+ if $!
0
+ raise $!
0
+ end
0
+
0
+ # check that one and only one exist
0
+ if handlers.size != 1
0
+ raise "There must be exactly one class that extends Chassis, but there are #{handlers.size}"
0
+ end
0
+
0
+ # check that a kind has been set
0
+ unless Chassis.node_kind
0
+ raise "A kind must be specified"
0
+ end
0
+
0
+ h = handlers.first.new
0
+ h.start
0
+ end
0
+
0
+ def return_and_exit(data)
0
+ Chassis.exit_after_current_dispatch = true
0
+ data
0
+ end
0
+end
0
+
0
+class HashWithIndifferentAccess < Hash
0
+ def initialize(constructor = {})
0
+ if constructor.is_a?(Hash)
0
+ super()
0
+ update(constructor)
0
+ else
0
+ super(constructor)
0
+ end
0
+ end
0
+
0
+ def default(key = nil)
0
+ if key.is_a?(Symbol) && include?(key = key.to_s)
0
+ self[key]
0
+ else
0
+ super
0
+ end
0
+ end
0
+
0
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
0
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
0
+
0
+ def []=(key, value)
0
+ regular_writer(convert_key(key), convert_value(value))
0
+ end
0
+
0
+ def update(other_hash)
0
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
0
+ self
0
+ end
0
+
0
+ alias_method :merge!, :update
0
+
0
+ def key?(key)
0
+ super(convert_key(key))
0
+ end
0
+
0
+ alias_method :include?, :key?
0
+ alias_method :has_key?, :key?
0
+ alias_method :member?, :key?
0
+
0
+ def fetch(key, *extras)
0
+ super(convert_key(key), *extras)
0
+ end
0
+
0
+ def values_at(*indices)
0
+ indices.collect {|key| self[convert_key(key)]}
0
+ end
0
+
0
+ def dup
0
+ HashWithIndifferentAccess.new(self)
0
+ end
0
+
0
+ def merge(hash)
0
+ self.dup.update(hash)
0
+ end
0
+
0
+ def delete(key)
0
+ super(convert_key(key))
0
+ end
0
+
0
+ def stringify_keys!; self end
0
+ def symbolize_keys!; self end
0
+ def to_options!; self end
0
+
0
+ # Convert to a Hash with String keys.
0
+ def to_hash
0
+ Hash.new(default).merge(self)
0
+ end
0
+
0
+ protected
0
+ def convert_key(key)
0
+ key.kind_of?(Symbol) ? key.to_s : key
0
+ end
0
+
0
+ def convert_value(value)
0
+ case value
0
+ when Hash
0
+ value.with_indifferent_access
0
+ when Array
0
+ value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
0
+ else
0
+ value
0
+ end
0
+ end
0
+end
0
+
0
+class Hash
0
+ def with_indifferent_access
0
+ hash = HashWithIndifferentAccess.new(self)
0
+ hash.default = self.default
0
+ hash
0
+ end
0
+end
0
+
0
+at_exit do
0
+ Chassis.start
0
+end
0
+
0
+Chassis.pull_cli_args
0
+
0
+#############################################################################
0
+# Tests
0
+
0
+if $0 == __FILE__
0
+ require 'test/unit'
0
+ require 'mocha'
0
+
0
+ class Chassis
0
+ def self.start
0
+ nil
0
+ end
0
+ end
0
+
0
+ class Awesome < Chassis
0
+ kind "awesome"
0
+
0
+ packages "failboat"
0
+ packages "powerset_ruby", "tk"
0
+
0
+ config do
0
+ ["grammar", "/tmp/awesome.lfg"]
0
+ end
0
+
0
+ handle(:alpha) do
0
+ 'alpha'
0
+ end
0
+
0
+ handle(:beta, :foo, :bar) do |args|
0
+ helper(args[:foo], args[:bar])
0
+ end
0
+
0
+ handle(:gamma) do
0
+ raise "once more unto the breach"
0
+ end
0
+
0
+ handle(:delta) do
0
+ exit
0
+ end
0
+
0
+ handle(:epsilon) do
0
+ return_and_exit('foo')
0
+ end
0
+
0
+ handle_exception do |e|
0
+ raise "EBORKD"
0
+ end
0
+
0
+ def helper(x, y)
0
+ "#{x}=#{y}"
0
+ end
0
+ end
0
+
0
+ class TestChassis < Test::Unit::TestCase
0
+ def setup
0
+ @a = Awesome.new
0
+ end
0
+
0
+ # signatures
0
+
0
+ def test_signatures_should_match_declaration
0
+ sigs = {:gamma=>[], :alpha=>[], :delta=>[], :beta=>[:bar, :foo], :epsilon=>[]}
0
+ assert_equal sigs, Chassis.signatures
0
+ end
0
+
0
+ # handler meta
0
+
0
+ def test_handler_methods_should_exist
0
+ assert @a.respond_to?(:handle_alpha)
0
+ assert @a.respond_to?(:handle_beta)
0
+ end
0
+
0
+ def test_handler_methods_arity_should_always_be_one
0
+ assert 1, @a.method(:handle_alpha).arity
0
+ assert 1, @a.method(:handle_beta).arity
0
+ end
0
+
0
+ # convert_args
0
+
0
+ def test_convert_args_should_convert_top_level_array_to_hash
0
+ i = [[:foo, 'bar'], [:qux, 'quux']]
0
+ o = {'foo' => 'bar', 'qux' => 'quux'}
0
+ assert_equal o, @a.convert_args(i)
0
+
0
+ i = [['foo', 'bar'], ['qux', 'quux']]
0
+ o = {'foo' => 'bar', 'qux' => 'quux'}
0
+ assert_equal o, @a.convert_args(i)
0
+ end
0
+
0
+ def test_convert_args_should_convert_structs_to_indifferent_hashes
0
+ i = [[:foo, 'bar'], [:baz, [:struct, [[:qux, 'quux']]]]]
0
+ o = {'foo' => 'bar', 'baz' => {'qux' => 'quux'}}
0
+ assert_equal o, @a.convert_args(i)
0
+
0
+ i = [[:foo, 'bar'], [:baz, [:struct, [[:qux, [:struct, [[:a, 'b']]]]]]]]
0
+ o = {'foo' => 'bar', 'baz' => {'qux' => {'a' => 'b'}}}
0
+ assert_equal o, @a.convert_args(i)
0
+ end
0
+
0
+ def test_convert_args_should_make_all_hashes_indifferent
0
+ i = [[:foo, 'bar'], [:baz, [:struct, [[:qux, 'quux']]]]]
0
+ # o = {'foo' => 'bar', 'baz' => {'qux' => 'quux'}}
0
+ o = @a.convert_args(i)
0
+ assert_equal 'bar', o[:foo]
0
+ assert_equal 'bar', o['foo']
0
+
0
+ assert_equal 'quux', o[:baz][:qux]
0
+ assert_equal 'quux', o['baz']['qux']
0
+ end
0
+
0
+ def test_convert_args_should_convert_arrays_to_arrays
0
+ i = [[:foo, 'bar'], [:baz, [:array, [:qux, 'quux']]]]
0
+ o = {'foo' => 'bar', 'baz' => [:qux, 'quux']}
0
+ assert_equal o, @a.convert_args(i)
0
+
0
+ i = [[:foo, 'bar'], [:baz, [:array, [:qux, [:array, [:a, 'b']]]]]]
0
+ o = {'foo' => 'bar', 'baz' => [:qux, [:a, 'b']]}
0
+ assert_equal o, @a.convert_args(i)
0
+ end
0
+
0
+ # convert_args_node
0
+
0
+ def test_convert_node_should_be_identity_for_strings
0
+ assert_equal 'foo', @a.convert_args_node('foo')
0
+ end
0
+
0
+ def test_convert_node_should_be_identity_for_symbol
0
+ assert_equal :foo, @a.convert_args_node(:foo)
0
+ end
0
+
0
+ def test_convert_node_should_be_identity_for_true
0
+ assert_equal true, @a.convert_args_node(true)
0
+ end
0
+
0
+ def test_convert_node_should_be_identity_for_false
0
+ assert_equal false, @a.convert_args_node(false)
0
+ end
0
+
0
+ def test_convert_node_should_be_identity_for_nil
0
+ assert_equal nil, @a.convert_args_node(nil)
0
+ end
0
+
0
+ def test_convert_node_should_be_identity_for_numeric
0
+ assert_equal 1, @a.convert_args_node(1)
0
+ assert_equal 1.0, @a.convert_args_node(1.0)
0
+ assert_equal 9999999999999999999, @a.convert_args_node(9999999999999999999)
0
+ end
0
+
0
+ def test_convert_node_should_convert_array
0
+ assert_equal [1], @a.convert_args_node([:array, [1]])
0
+ assert_equal [1, 2], @a.convert_args_node([:array, [1, 2]])
0
+ assert_equal [1, 2, 3], @a.convert_args_node([:array, [1, 2, 3]])
0
+ end
0
+
0
+ def test_convert_node_should_convert_nested_array
0
+ assert_equal [[1]], @a.convert_args_node([:array, [[:array, [1]]]])
0
+ end
0
+
0
+ def test_convert_node_should_convert_struct_to_hash
0
+ i = [:struct, [[:foo, 'foo']]]
0
+ o = {"foo" => 'foo'}
0
+ assert_equal o, @a.convert_args_node(i)
0
+ end
0
+
0
+ # handlers
0
+
0
+ def test_alpha_nominal
0
+ assert_equal 'alpha', @a.handle_alpha([])
0
+ end
0
+
0
+ def test_beta_nominal
0
+ assert_equal 'foo=bar', @a.handle_beta([[:foo, 'foo'], [:bar, 'bar']])
0
+ end
0
+
0
+ def test_beta_with_missing_required_arg_should_raise
0
+ assert_raise RuntimeError do
0
+ @a.handle_beta([[:foo, 'foo']])
0
+ end
0
+ end
0
+
0
+ def test_epsilon_should_set_exit_after_current_dispatch
0
+ assert_equal false, Chassis.exit_after_current_dispatch
0
+ @a.dispatch(:epsilon, :json, [])
0
+ assert_equal true, Chassis.exit_after_current_dispatch
0
+ rescue SystemExit
0
+ # prevent from exiting
0
+ ensure
0
+ Chassis.exit_after_current_dispatch = false
0
+ end
0
+
0
+ def test_epsilon_should_respond_with_last_result
0
+ res = @a.dispatch(:epsilon, :json, [])
0
+ assert_equal [:last_result, [:raw, "\"foo\""]], res
0
+ rescue SystemExit
0
+ # prevent from exiting
0
+ ensure
0
+ Chassis.exit_after_current_dispatch = false
0
+ end
0
+
0
+ # api
0
+
0
+ def test_api
0
+ assert_equal [[:alpha, []], [:beta, [:bar, :foo]], [:delta, []], [:epsilon, []], [:gamma, []]], @a.api
0
+ end
0
+
0
+ # kind
0
+
0
+ def test_kind
0
+ assert_equal "awesome", Chassis.node_kind
0
+ end
0
+
0
+ # packages
0
+
0
+ def test_packages
0
+ packages = [["powerset_ruby", [1, 8, 5, 9999]],
0
+ ["tk", [8, 4, 14, 0]],
0
+ ["readline", [5, 1, 4, 0]],
0
+ ["tcl", [8, 4, 14, 0]]]
0
+ assert_equal packages, Chassis.pkgs
0
+ end
0
+
0
+ # handle_exception
0
+
0
+ def test_handle_exception
0
+ @a.dispatch(:gamma, :json, [])
0
+ rescue => e
0
+ assert_equal "EBORKD", e.message
0
+ end
0
+
0
+ # pull_cli_args
0
+
0
+ def test_pull_cli_args
0
+ ARGV.clear
0
+ ARGV.concat(["--tags=foo,bar", "--", "--tags=baz,qux"])
0
+
0
+ Chassis.pull_cli_args
0
+
0
+ assert_equal ["foo", "bar"], Chassis.tags
0
+ assert_equal ["--tags=baz,qux"], ARGV
0
+ end
0
+
0
+ # config
0
+
0
+ def test_config
0
+ ARGV.clear
0
+ ARGV.concat(["--tags=foo,bar", "--roles=production", "--", "--tags=baz,qux"])
0
+ Chassis.pull_cli_args
0
+
0
+ config = [["powerset_ruby", [1, 8, 5, 9999]],
0
+ ["tk", [8, 4, 14, 0]],
0
+ ["readline", [5, 1, 4, 0]],
0
+ ["tcl", [8, 4, 14, 0]],
0
+ ["tags", "foo", "bar"],
0
+ ["roles", "production"],
0
+ ["kind", "awesome"],
0
+ ["grammar", "/tmp/awesome.lfg"]]
0
+
0
+ assert_equal config, @a.config
0
+ end
0
+
0
+ # return_and_exit
0
+
0
+ def test_return_and_exit
0
+ assert_equal false, Chassis.exit_after_current_dispatch
0
+ assert_equal 'foo', @a.return_and_exit('foo')
0
+ Chassis.exit_after_current_dispatch = false
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,29 @@
0
+require 'chassis'
0
+require 'rubygems'
0
+require 'erlectricity'
0
+require 'optparse'
0
+
0
+
0
+# speller handler
0
+class FakeHandler < Chassis
0
+ kind "fake"
0
+
0
+ packages "powerset_ruby"
0
+
0
+ handle(:echo, :textsy, :options) do |args|
0
+ "You said: " + args[:textsy] + ' ' + args[:options].inspect
0
+ end
0
+
0
+ handle(:killer) do |args|
0
+ exit
0
+ end
0
+
0
+ handle(:mangler) do |args|
0
+ return_and_exit("Let slip the dogs of war")
0
+ end
0
+
0
+ handle(:failboat) do |args|
0
+ raise "This boat failed under an endless sky."
0
+ end
0
+end
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,56 +1 @@
0
-require 'open-uri'
0
-
0
-require 'rubygems'
0
-
0
-def root_binding
0
- binding
0
-end
0
-
0
-module Pontoon
0
- EXTENSIONS = ['', '.rb']
0
-
0
- class << self
0
- attr_accessor :paths
0
- end
0
-
0
- self.paths = []
0
-
0
- def self.add_path(path)
0
- self.paths << path
0
- end
0
-
0
- def self.remote_code_load(file)
0
- self.paths.each do |path|
0
- EXTENSIONS.each do |ext|
0
- begin
0
- location = File.join(path, file + ext)
0
- eval(open(location).read, root_binding, file)
0
- return
0
- rescue OpenURI::HTTPError
0
- # do nothing
0
- end
0
- end
0
- end
0
- raise LoadError.new("No local or remote code found for #{file}")
0
- end
0
-end
0
-
0
-alias :require_without_remote_loading :require
0
-
0
-def require(file)
0
- begin
0
- require_without_remote_loading(file)
0
- STDERR.puts "\rLoaded [LOCAL] #{file}\n"
0
- rescue LoadError => e
0
- Pontoon.remote_code_load(file)
0
- STDERR.puts "\rLoaded [REMOTE] #{file}\n"
0
- end
0
-end
0
-
0
-if $0 == __FILE__
0
- file = ARGV.shift
0
- path = ARGV.shift
0
- Pontoon.add_path(path)
0
- require file
0
-end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
&nb