opal 0.7.0.beta3 → 0.7.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +4 -0
  3. data/.travis.yml +7 -3
  4. data/.yardopts +6 -0
  5. data/CHANGELOG.md +28 -0
  6. data/Gemfile +1 -1
  7. data/README.md +3 -12
  8. data/Rakefile +4 -150
  9. data/bin/opal-mspec +1 -1
  10. data/docs/compiler_directives.md +127 -0
  11. data/examples/rack/.gitignore +1 -0
  12. data/examples/rack/app/user.rb +1 -0
  13. data/lib/mspec/opal/special_calls.rb +15 -2
  14. data/lib/opal/builder.rb +15 -8
  15. data/lib/opal/compiler.rb +75 -4
  16. data/lib/opal/erb.rb +22 -2
  17. data/lib/opal/fragment.rb +17 -5
  18. data/lib/opal/nodes/def.rb +174 -53
  19. data/lib/opal/nodes/if.rb +14 -0
  20. data/lib/opal/nodes/module.rb +0 -1
  21. data/lib/opal/nodes/rescue.rb +10 -2
  22. data/lib/opal/nodes/scope.rb +0 -17
  23. data/lib/opal/parser.rb +83 -19
  24. data/lib/opal/parser/grammar.rb +2511 -2414
  25. data/lib/opal/parser/grammar.y +71 -9
  26. data/lib/opal/parser/lexer.rb +44 -12
  27. data/lib/opal/parser/parser_scope.rb +3 -0
  28. data/lib/opal/parser/sexp.rb +7 -1
  29. data/lib/opal/paths.rb +5 -5
  30. data/lib/opal/sprockets/environment.rb +2 -10
  31. data/lib/opal/sprockets/path_reader.rb +1 -1
  32. data/lib/opal/sprockets/processor.rb +1 -0
  33. data/lib/opal/sprockets/server.rb +2 -0
  34. data/lib/opal/util.rb +7 -2
  35. data/lib/opal/version.rb +1 -1
  36. data/opal.gemspec +1 -0
  37. data/opal/README.md +1 -1
  38. data/opal/corelib/dir.rb +1 -1
  39. data/opal/corelib/enumerable.rb +3 -1
  40. data/opal/corelib/error.rb +3 -0
  41. data/opal/corelib/file.rb +2 -0
  42. data/opal/corelib/hash.rb +3 -0
  43. data/opal/corelib/io.rb +15 -1
  44. data/opal/corelib/kernel.rb +8 -0
  45. data/opal/corelib/module.rb +42 -17
  46. data/opal/corelib/runtime.js +223 -49
  47. data/opal/corelib/string.rb +1 -1
  48. data/opal/corelib/struct.rb +1 -7
  49. data/spec/README.md +8 -0
  50. data/spec/filters/bugs/language.rb +1 -0
  51. data/spec/filters/bugs/module.rb +4 -0
  52. data/spec/filters/unsupported/frozen.rb +2 -0
  53. data/spec/lib/compiler/pre_processed_conditionals_spec.rb +87 -0
  54. data/spec/lib/compiler_spec.rb +1 -67
  55. data/spec/lib/fixtures/file_with_directives.js +2 -0
  56. data/spec/lib/fixtures/required_file.js +1 -0
  57. data/spec/lib/parser/def_spec.rb +29 -16
  58. data/spec/lib/parser/variables_spec.rb +5 -5
  59. data/spec/lib/sprockets/path_reader_spec.rb +24 -8
  60. data/spec/lib/sprockets/server_spec.rb +10 -3
  61. data/spec/opal/core/date_spec.rb +14 -0
  62. data/spec/opal/core/language/versions/def_2_0_spec.rb +62 -0
  63. data/spec/opal/core/language_spec.rb +23 -0
  64. data/spec/opal/core/runtime/donate_spec.rb +53 -0
  65. data/spec/opal/stdlib/native/native_alias_spec.rb +19 -0
  66. data/spec/opal/stdlib/native/native_class_spec.rb +18 -0
  67. data/spec/opal/stdlib/native/native_module_spec.rb +13 -0
  68. data/spec/rubyspecs +2 -0
  69. data/stdlib/buffer.rb +1 -0
  70. data/stdlib/date.rb +18 -0
  71. data/stdlib/encoding.rb +3 -2
  72. data/stdlib/minitest.rb +780 -0
  73. data/stdlib/minitest/assertions.rb +662 -0
  74. data/stdlib/minitest/autorun.rb +12 -0
  75. data/stdlib/minitest/benchmark.rb +426 -0
  76. data/stdlib/minitest/expectations.rb +281 -0
  77. data/stdlib/minitest/hell.rb +11 -0
  78. data/stdlib/minitest/mock.rb +220 -0
  79. data/stdlib/minitest/parallel.rb +65 -0
  80. data/stdlib/minitest/pride.rb +4 -0
  81. data/stdlib/minitest/pride_plugin.rb +142 -0
  82. data/stdlib/minitest/spec.rb +310 -0
  83. data/stdlib/minitest/test.rb +293 -0
  84. data/stdlib/minitest/unit.rb +45 -0
  85. data/stdlib/native.rb +12 -3
  86. data/stdlib/nodejs/process.rb +16 -2
  87. data/stdlib/promise.rb +99 -0
  88. data/stdlib/test/unit.rb +10 -0
  89. data/stdlib/thread.rb +4 -0
  90. data/tasks/building.rake +58 -0
  91. data/tasks/documentation.rake +38 -0
  92. data/tasks/documenting.rake +37 -0
  93. data/tasks/testing.rake +102 -0
  94. metadata +57 -2
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "begin & rescue blocks" do
4
+ it "should evaluate to begin blocks last expression when no exception" do
5
+ result = begin
6
+ "a"
7
+ rescue
8
+ "b"
9
+ end
10
+
11
+ result.should == "a"
12
+ end
13
+
14
+ it "should evaluate to rescue blocks last expression when exception" do
15
+ result = begin
16
+ raise "foo"
17
+ rescue
18
+ "bar"
19
+ end
20
+
21
+ result.should == "bar"
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ module RuntimeDonatingMethods
4
+ module A
5
+ def baz
6
+ 'a'
7
+ end
8
+
9
+ def woosh
10
+ :kapow
11
+ end
12
+ end
13
+
14
+ module B
15
+ end
16
+
17
+ class C
18
+ include A
19
+ include B
20
+
21
+ def foo; 'c'; end
22
+ end
23
+
24
+ module B
25
+ def foo; 'b'; end
26
+ def bar; 'b'; end
27
+ def baz; 'b'; end
28
+ end
29
+
30
+ module A
31
+ def bar; 'a'; end
32
+ def woosh; :kaplunk; end
33
+ end
34
+ end
35
+
36
+ describe 'Donating methods in the runtime' do
37
+ before do
38
+ @c = RuntimeDonatingMethods::C.new
39
+ end
40
+
41
+ it 'class methods should not get overwritten by methods from modules' do
42
+ @c.foo.should == 'c'
43
+ end
44
+
45
+ it 'methods defined in modules should respect the include order in a class' do
46
+ @c.bar.should == 'b'
47
+ @c.baz.should == 'b'
48
+ end
49
+
50
+ it 'allows a module to override a method it previously defined' do
51
+ @c.woosh.should == :kaplunk
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ require 'native'
2
+
3
+ describe "Class#native_alias" do
4
+ it "exposes a method to javascript without the '$' prefix" do
5
+ klass = Class.new {
6
+ def a
7
+ 2
8
+ end
9
+
10
+ native_alias :a, :a
11
+ }
12
+ instance = klass.new
13
+ `instance.a()`.should == 2
14
+ end
15
+
16
+ it "raises if the aliased method isn't defined" do
17
+ lambda { Class.new { native_alias :a, :not_a_method } }.should raise_error(NameError)
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'native'
2
+
3
+ describe "Class#native_class" do
4
+ class SomeClass
5
+ end
6
+
7
+ after {`delete Opal.global.SomeClass`}
8
+
9
+ it "adds current constant to the global JS object" do
10
+ SomeClass.native_class
11
+ `Opal.global.SomeClass`.should == SomeClass
12
+ end
13
+
14
+ it 'aliases Class#new to the unprefixed new method in JS world' do
15
+ SomeClass.native_class
16
+ `Opal.global.SomeClass.new()`.is_a?(SomeClass).should == true
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'native'
2
+
3
+ describe "Module#native_module" do
4
+ module SomeModule
5
+ end
6
+
7
+ after {`delete Opal.global.SomeModule`}
8
+
9
+ it "adds current constant to the global JS object" do
10
+ SomeModule.native_module
11
+ `Opal.global.SomeModule`.should == SomeModule
12
+ end
13
+ end
data/spec/rubyspecs CHANGED
@@ -69,6 +69,8 @@ corelib/core/matchdata/to_a_spec
69
69
  corelib/core/matchdata/to_s_spec
70
70
  corelib/core/matchdata/values_at_spec
71
71
 
72
+ corelib/core/module/class_variable_get_spec
73
+ corelib/core/module/class_variable_set_spec
72
74
  corelib/core/module/module_function_spec
73
75
 
74
76
  corelib/core/range/begin_spec
data/stdlib/buffer.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'native'
1
2
  require 'buffer/array'
2
3
  require 'buffer/view'
3
4
 
data/stdlib/date.rb CHANGED
@@ -87,6 +87,24 @@ class Date
87
87
  }
88
88
  end
89
89
 
90
+ def <=>(other)
91
+ %x{
92
+ var a = #@date, b = #{other}.date;
93
+ a.setHours(0, 0, 0, 0);
94
+ b.setHours(0, 0, 0, 0);
95
+
96
+ if (a < b) {
97
+ return -1;
98
+ }
99
+ else if (a > b) {
100
+ return 1;
101
+ }
102
+ else {
103
+ return 0;
104
+ }
105
+ }
106
+ end
107
+
90
108
  def ==(other)
91
109
  %x{
92
110
  var a = #@date, b = other.date;
data/stdlib/encoding.rb CHANGED
@@ -12,12 +12,13 @@ class Encoding
12
12
  end
13
13
 
14
14
  def self.find(name)
15
- return name if self === name
15
+ upcase_name = name.upcase
16
+ return upcase_name if self === upcase_name
16
17
 
17
18
  constants.each {|const|
18
19
  encoding = const_get(const)
19
20
 
20
- if encoding.name == name || encoding.names.include?(name)
21
+ if encoding.name == upcase_name || encoding.names.include?(upcase_name)
21
22
  return encoding
22
23
  end
23
24
  }
@@ -0,0 +1,780 @@
1
+ require "optparse"
2
+ require "thread"
3
+ require "mutex_m"
4
+ require "minitest/parallel"
5
+
6
+ ##
7
+ # :include: README.txt
8
+
9
+ module Minitest
10
+ VERSION = "5.5.1" # :nodoc:
11
+ ENCS = "".respond_to? :encoding # :nodoc:
12
+
13
+ @@installed_at_exit ||= false
14
+ @@after_run = []
15
+ @extensions = []
16
+
17
+ mc = (class << self; self; end)
18
+
19
+ ##
20
+ # Parallel test executor
21
+
22
+ mc.send :attr_accessor, :parallel_executor
23
+ self.parallel_executor = Parallel::Executor.new((ENV['N'] || 2).to_i)
24
+
25
+ ##
26
+ # Filter object for backtraces.
27
+
28
+ mc.send :attr_accessor, :backtrace_filter
29
+
30
+ ##
31
+ # Reporter object to be used for all runs.
32
+ #
33
+ # NOTE: This accessor is only available during setup, not during runs.
34
+
35
+ mc.send :attr_accessor, :reporter
36
+
37
+ ##
38
+ # Names of known extension plugins.
39
+
40
+ mc.send :attr_accessor, :extensions
41
+
42
+ ##
43
+ # Registers Minitest to run at process exit
44
+
45
+ def self.autorun
46
+ at_exit {
47
+ next if $! and not ($!.kind_of? SystemExit and $!.success?)
48
+
49
+ exit_code = nil
50
+
51
+ at_exit {
52
+ @@after_run.reverse_each(&:call)
53
+ exit exit_code || false
54
+ }
55
+
56
+ exit_code = Minitest.run ARGV
57
+ } unless @@installed_at_exit
58
+ @@installed_at_exit = true
59
+ end
60
+
61
+ ##
62
+ # A simple hook allowing you to run a block of code after everything
63
+ # is done running. Eg:
64
+ #
65
+ # Minitest.after_run { p $debugging_info }
66
+
67
+ def self.after_run &block
68
+ @@after_run << block
69
+ end
70
+
71
+ def self.init_plugins options # :nodoc:
72
+ self.extensions.each do |name|
73
+ msg = "plugin_#{name}_init"
74
+ send msg, options if self.respond_to? msg
75
+ end
76
+ end
77
+
78
+ def self.load_plugins # :nodoc:
79
+ # return unless self.extensions.empty?
80
+ #
81
+ # seen = {}
82
+ #
83
+ # require "rubygems" unless defined? Gem
84
+ #
85
+ # Gem.find_files("minitest/*_plugin.rb").each do |plugin_path|
86
+ # name = File.basename plugin_path, "_plugin.rb"
87
+ #
88
+ # next if seen[name]
89
+ # seen[name] = true
90
+ #
91
+ # require plugin_path
92
+ # self.extensions << name
93
+ # end
94
+ end
95
+
96
+ ##
97
+ # This is the top-level run method. Everything starts from here. It
98
+ # tells each Runnable sub-class to run, and each of those are
99
+ # responsible for doing whatever they do.
100
+ #
101
+ # The overall structure of a run looks like this:
102
+ #
103
+ # Minitest.autorun
104
+ # Minitest.run(args)
105
+ # Minitest.__run(reporter, options)
106
+ # Runnable.runnables.each
107
+ # runnable.run(reporter, options)
108
+ # self.runnable_methods.each
109
+ # self.run_one_method(self, runnable_method, reporter)
110
+ # Minitest.run_one_method(klass, runnable_method, reporter)
111
+ # klass.new(runnable_method).run
112
+
113
+ def self.run args = []
114
+ self.load_plugins
115
+
116
+ options = process_args args
117
+
118
+ reporter = CompositeReporter.new
119
+ reporter << SummaryReporter.new(options[:io], options)
120
+ reporter << ProgressReporter.new(options[:io], options)
121
+
122
+ self.reporter = reporter # this makes it available to plugins
123
+ self.init_plugins options
124
+ self.reporter = nil # runnables shouldn't depend on the reporter, ever
125
+
126
+ reporter.start
127
+ __run reporter, options
128
+ self.parallel_executor.shutdown
129
+ reporter.report
130
+
131
+ reporter.passed?
132
+ end
133
+
134
+ ##
135
+ # Internal run method. Responsible for telling all Runnable
136
+ # sub-classes to run.
137
+ #
138
+ # NOTE: this method is redefined in parallel_each.rb, which is
139
+ # loaded if a Runnable calls parallelize_me!.
140
+
141
+ def self.__run reporter, options
142
+ suites = Runnable.runnables.shuffle
143
+ parallel, serial = suites.partition { |s| s.test_order == :parallel }
144
+
145
+ # If we run the parallel tests before the serial tests, the parallel tests
146
+ # could run in parallel with the serial tests. This would be bad because
147
+ # the serial tests won't lock around Reporter#record. Run the serial tests
148
+ # first, so that after they complete, the parallel tests will lock when
149
+ # recording results.
150
+ serial.map { |suite| suite.run reporter, options } +
151
+ parallel.map { |suite| suite.run reporter, options }
152
+ end
153
+
154
+ def self.process_args args = [] # :nodoc:
155
+ options = {
156
+ :io => $stdout,
157
+ }
158
+ orig_args = args.dup
159
+
160
+ OptionParser.new do |opts|
161
+ opts.banner = "minitest options:"
162
+ opts.version = Minitest::VERSION
163
+
164
+ opts.on "-h", "--help", "Display this help." do
165
+ puts opts
166
+ exit
167
+ end
168
+
169
+ desc = "Sets random seed. Also via env. Eg: SEED=n rake"
170
+ opts.on "-s", "--seed SEED", Integer, desc do |m|
171
+ options[:seed] = m.to_i
172
+ end
173
+
174
+ opts.on "-v", "--verbose", "Verbose. Show progress processing files." do
175
+ options[:verbose] = true
176
+ end
177
+
178
+ opts.on "-n", "--name PATTERN","Filter run on /pattern/ or string." do |a|
179
+ options[:filter] = a
180
+ end
181
+
182
+ unless extensions.empty?
183
+ opts.separator ""
184
+ opts.separator "Known extensions: #{extensions.join(', ')}"
185
+
186
+ extensions.each do |meth|
187
+ msg = "plugin_#{meth}_options"
188
+ send msg, opts, options if self.respond_to?(msg)
189
+ end
190
+ end
191
+
192
+ begin
193
+ opts.parse! args
194
+ rescue OptionParser::InvalidOption => e
195
+ puts
196
+ puts e
197
+ puts
198
+ puts opts
199
+ exit 1
200
+ end
201
+
202
+ orig_args -= args
203
+ end unless args.empty?
204
+
205
+ unless options[:seed] then
206
+ srand
207
+ options[:seed] = (ENV["SEED"] || srand).to_i % 0xFFFF
208
+ orig_args << "--seed" << options[:seed].to_s
209
+ end
210
+
211
+ srand options[:seed]
212
+
213
+ options[:args] = orig_args.map { |s|
214
+ s =~ /[\s|&<>$()]/ ? s.inspect : s
215
+ }.join " "
216
+
217
+ options
218
+ end
219
+
220
+ def self.filter_backtrace bt # :nodoc:
221
+ backtrace_filter.filter bt
222
+ end
223
+
224
+ ##
225
+ # Represents anything "runnable", like Test, Spec, Benchmark, or
226
+ # whatever you can dream up.
227
+ #
228
+ # Subclasses of this are automatically registered and available in
229
+ # Runnable.runnables.
230
+
231
+ class Runnable
232
+ ##
233
+ # Number of assertions executed in this run.
234
+
235
+ attr_accessor :assertions
236
+
237
+ ##
238
+ # An assertion raised during the run, if any.
239
+
240
+ attr_accessor :failures
241
+
242
+ ##
243
+ # Name of the run.
244
+
245
+ def name
246
+ @NAME
247
+ end
248
+
249
+ ##
250
+ # Set the name of the run.
251
+
252
+ def name= o
253
+ @NAME = o
254
+ end
255
+
256
+ def self.inherited klass # :nodoc:
257
+ self.runnables << klass
258
+ super
259
+ end
260
+
261
+ ##
262
+ # Returns all instance methods matching the pattern +re+.
263
+
264
+ def self.methods_matching re
265
+ public_instance_methods(true).grep(re).map(&:to_s)
266
+ end
267
+
268
+ def self.reset # :nodoc:
269
+ @@runnables = []
270
+ end
271
+
272
+ reset
273
+
274
+ ##
275
+ # Responsible for running all runnable methods in a given class,
276
+ # each in its own instance. Each instance is passed to the
277
+ # reporter to record.
278
+
279
+ def self.run reporter, options = {}
280
+ filter = options[:filter] || '/./'
281
+ filter = Regexp.new $1 if filter =~ /\/(.*)\//
282
+
283
+ filtered_methods = self.runnable_methods.find_all { |m|
284
+ filter === m || filter === "#{self}##{m}"
285
+ }
286
+
287
+ with_info_handler reporter do
288
+ filtered_methods.each do |method_name|
289
+ run_one_method self, method_name, reporter
290
+ end
291
+ end
292
+ end
293
+
294
+ ##
295
+ # Runs a single method and has the reporter record the result.
296
+ # This was considered internal API but is factored out of run so
297
+ # that subclasses can specialize the running of an individual
298
+ # test. See Minitest::ParallelTest::ClassMethods for an example.
299
+
300
+ def self.run_one_method klass, method_name, reporter
301
+ reporter.record Minitest.run_one_method(klass, method_name)
302
+ end
303
+
304
+ def self.with_info_handler reporter, &block # :nodoc:
305
+ handler = lambda do
306
+ unless reporter.passed? then
307
+ warn "Current results:"
308
+ warn ""
309
+ warn reporter.reporters.first
310
+ warn ""
311
+ end
312
+ end
313
+
314
+ on_signal "INFO", handler, &block
315
+ end
316
+
317
+ SIGNALS = {} # Signal.list # :nodoc:
318
+
319
+ def self.on_signal name, action # :nodoc:
320
+ supported = SIGNALS[name]
321
+
322
+ old_trap = trap name do
323
+ old_trap.call if old_trap.respond_to? :call
324
+ action.call
325
+ end if supported
326
+
327
+ yield
328
+ ensure
329
+ trap name, old_trap if supported
330
+ end
331
+
332
+ ##
333
+ # Each subclass of Runnable is responsible for overriding this
334
+ # method to return all runnable methods. See #methods_matching.
335
+
336
+ def self.runnable_methods
337
+ raise NotImplementedError, "subclass responsibility"
338
+ end
339
+
340
+ ##
341
+ # Returns all subclasses of Runnable.
342
+
343
+ def self.runnables
344
+ @@runnables
345
+ end
346
+
347
+ def marshal_dump # :nodoc:
348
+ [self.name, self.failures, self.assertions]
349
+ end
350
+
351
+ def marshal_load ary # :nodoc:
352
+ self.name, self.failures, self.assertions = ary
353
+ end
354
+
355
+ def failure # :nodoc:
356
+ self.failures.first
357
+ end
358
+
359
+ def initialize name # :nodoc:
360
+ self.name = name
361
+ self.failures = []
362
+ self.assertions = 0
363
+ end
364
+
365
+ ##
366
+ # Runs a single method. Needs to return self.
367
+
368
+ def run
369
+ raise NotImplementedError, "subclass responsibility"
370
+ end
371
+
372
+ ##
373
+ # Did this run pass?
374
+ #
375
+ # Note: skipped runs are not considered passing, but they don't
376
+ # cause the process to exit non-zero.
377
+
378
+ def passed?
379
+ raise NotImplementedError, "subclass responsibility"
380
+ end
381
+
382
+ ##
383
+ # Returns a single character string to print based on the result
384
+ # of the run. Eg ".", "F", or "E".
385
+
386
+ def result_code
387
+ raise NotImplementedError, "subclass responsibility"
388
+ end
389
+
390
+ ##
391
+ # Was this run skipped? See #passed? for more information.
392
+
393
+ def skipped?
394
+ raise NotImplementedError, "subclass responsibility"
395
+ end
396
+ end
397
+
398
+ ##
399
+ # Defines the API for Reporters. Subclass this and override whatever
400
+ # you want. Go nuts.
401
+
402
+ class AbstractReporter
403
+ # include Mutex_m
404
+
405
+ ##
406
+ # Starts reporting on the run.
407
+
408
+ def start
409
+ end
410
+
411
+ ##
412
+ # Record a result and output the Runnable#result_code. Stores the
413
+ # result of the run if the run did not pass.
414
+
415
+ def record result
416
+ end
417
+
418
+ ##
419
+ # Outputs the summary of the run.
420
+
421
+ def report
422
+ end
423
+
424
+ ##
425
+ # Did this run pass?
426
+
427
+ def passed?
428
+ true
429
+ end
430
+ end
431
+
432
+ class Reporter < AbstractReporter # :nodoc:
433
+ ##
434
+ # The IO used to report.
435
+
436
+ attr_accessor :io
437
+
438
+ ##
439
+ # Command-line options for this run.
440
+
441
+ attr_accessor :options
442
+
443
+ def initialize io = $stdout, options = {} # :nodoc:
444
+ super()
445
+ self.io = io
446
+ self.options = options
447
+ end
448
+ end
449
+
450
+ ##
451
+ # A very simple reporter that prints the "dots" during the run.
452
+ #
453
+ # This is added to the top-level CompositeReporter at the start of
454
+ # the run. If you want to change the output of minitest via a
455
+ # plugin, pull this out of the composite and replace it with your
456
+ # own.
457
+
458
+ class ProgressReporter < Reporter
459
+ def record result # :nodoc:
460
+ io.print "%s#%s = %.2f s = " % [result.class, result.name, result.time] if
461
+ options[:verbose]
462
+ io.print result.result_code
463
+ io.puts if options[:verbose]
464
+ end
465
+ end
466
+
467
+ ##
468
+ # A reporter that gathers statistics about a test run. Does not do
469
+ # any IO because meant to be used as a parent class for a reporter
470
+ # that does.
471
+ #
472
+ # If you want to create an entirely different type of output (eg,
473
+ # CI, HTML, etc), this is the place to start.
474
+
475
+ class StatisticsReporter < Reporter
476
+ # :stopdoc:
477
+ attr_accessor :assertions
478
+ attr_accessor :count
479
+ attr_accessor :results
480
+ attr_accessor :start_time
481
+ attr_accessor :total_time
482
+ attr_accessor :failures
483
+ attr_accessor :errors
484
+ attr_accessor :skips
485
+ # :startdoc:
486
+
487
+ def initialize io = $stdout, options = {} # :nodoc:
488
+ super
489
+
490
+ self.assertions = 0
491
+ self.count = 0
492
+ self.results = []
493
+ self.start_time = nil
494
+ self.total_time = nil
495
+ self.failures = nil
496
+ self.errors = nil
497
+ self.skips = nil
498
+ end
499
+
500
+ def passed? # :nodoc:
501
+ results.all?(&:skipped?)
502
+ end
503
+
504
+ def start # :nodoc:
505
+ self.start_time = Time.now
506
+ end
507
+
508
+ def record result # :nodoc:
509
+ self.count += 1
510
+ self.assertions += result.assertions
511
+
512
+ results << result if not result.passed? or result.skipped?
513
+ end
514
+
515
+ def report # :nodoc:
516
+ aggregate = results.group_by { |r| r.failure.class }
517
+ aggregate.default = [] # dumb. group_by should provide this
518
+
519
+ self.total_time = Time.now - start_time
520
+ self.failures = aggregate[Assertion].size
521
+ self.errors = aggregate[UnexpectedError].size
522
+ self.skips = aggregate[Skip].size
523
+ end
524
+ end
525
+
526
+ ##
527
+ # A reporter that prints the header, summary, and failure details at
528
+ # the end of the run.
529
+ #
530
+ # This is added to the top-level CompositeReporter at the start of
531
+ # the run. If you want to change the output of minitest via a
532
+ # plugin, pull this out of the composite and replace it with your
533
+ # own.
534
+
535
+ class SummaryReporter < StatisticsReporter
536
+ # :stopdoc:
537
+ attr_accessor :sync
538
+ attr_accessor :old_sync
539
+ # :startdoc:
540
+
541
+ def start # :nodoc:
542
+ super
543
+
544
+ io.puts "Run options: #{options[:args]}"
545
+ io.puts
546
+ io.puts "# Running:"
547
+ io.puts
548
+
549
+ self.sync = io.respond_to? :"sync=" # stupid emacs
550
+ self.old_sync, io.sync = io.sync, true if self.sync
551
+ end
552
+
553
+ def report # :nodoc:
554
+ super
555
+
556
+ io.sync = self.old_sync
557
+
558
+ io.puts unless options[:verbose] # finish the dots
559
+ io.puts
560
+ io.puts statistics
561
+ io.puts aggregated_results
562
+ io.puts summary
563
+ end
564
+
565
+ def statistics # :nodoc:
566
+ "Finished in %.6fs, %.4f runs/s, %.4f assertions/s." %
567
+ [total_time, count / total_time, assertions / total_time]
568
+ end
569
+
570
+ def aggregated_results # :nodoc:
571
+ filtered_results = results.dup
572
+ filtered_results.reject!(&:skipped?) unless options[:verbose]
573
+
574
+ s = filtered_results.each_with_index.map { |result, i|
575
+ "\n%3d) %s" % [i+1, result]
576
+ }.join("\n") + "\n"
577
+
578
+ s.force_encoding(io.external_encoding) if
579
+ ENCS and io.external_encoding and s.encoding != io.external_encoding
580
+
581
+ s
582
+ end
583
+
584
+ alias to_s aggregated_results
585
+
586
+ def summary # :nodoc:
587
+ extra = ""
588
+
589
+ extra = "\n\nYou have skipped tests. Run with --verbose for details." if
590
+ results.any?(&:skipped?) unless options[:verbose] or ENV["MT_NO_SKIP_MSG"]
591
+
592
+ "%d runs, %d assertions, %d failures, %d errors, %d skips%s" %
593
+ [count, assertions, failures, errors, skips, extra]
594
+ end
595
+ end
596
+
597
+ ##
598
+ # Dispatch to multiple reporters as one.
599
+
600
+ class CompositeReporter < AbstractReporter
601
+ ##
602
+ # The list of reporters to dispatch to.
603
+
604
+ attr_accessor :reporters
605
+
606
+ def initialize *reporters # :nodoc:
607
+ super()
608
+ self.reporters = reporters
609
+ end
610
+
611
+ ##
612
+ # Add another reporter to the mix.
613
+
614
+ def << reporter
615
+ self.reporters << reporter
616
+ end
617
+
618
+ def passed? # :nodoc:
619
+ self.reporters.all?(&:passed?)
620
+ end
621
+
622
+ def start # :nodoc:
623
+ self.reporters.each(&:start)
624
+ end
625
+
626
+ def record result # :nodoc:
627
+ self.reporters.each do |reporter|
628
+ reporter.record result
629
+ end
630
+ end
631
+
632
+ def report # :nodoc:
633
+ self.reporters.each(&:report)
634
+ end
635
+ end
636
+
637
+ ##
638
+ # Represents run failures.
639
+
640
+ class Assertion < Exception
641
+ def error # :nodoc:
642
+ self
643
+ end
644
+
645
+ ##
646
+ # Where was this run before an assertion was raised?
647
+
648
+ def location
649
+ last_before_assertion = ""
650
+ self.backtrace.reverse_each do |s|
651
+ break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
652
+ last_before_assertion = s
653
+ end
654
+ last_before_assertion.sub(/:in .*$/, "")
655
+ end
656
+
657
+ def result_code # :nodoc:
658
+ result_label[0, 1]
659
+ end
660
+
661
+ def result_label # :nodoc:
662
+ "Failure"
663
+ end
664
+ end
665
+
666
+ ##
667
+ # Assertion raised when skipping a run.
668
+
669
+ class Skip < Assertion
670
+ def result_label # :nodoc:
671
+ "Skipped"
672
+ end
673
+ end
674
+
675
+ ##
676
+ # Assertion wrapping an unexpected error that was raised during a run.
677
+
678
+ class UnexpectedError < Assertion
679
+ attr_accessor :exception # :nodoc:
680
+
681
+ def initialize exception # :nodoc:
682
+ super
683
+ self.exception = exception
684
+ end
685
+
686
+ def backtrace # :nodoc:
687
+ self.exception.backtrace
688
+ end
689
+
690
+ def error # :nodoc:
691
+ self.exception
692
+ end
693
+
694
+ def message # :nodoc:
695
+ bt = Minitest::filter_backtrace(self.backtrace).join "\n "
696
+ "#{self.exception.class}: #{self.exception.message}\n #{bt}"
697
+ end
698
+
699
+ def result_label # :nodoc:
700
+ "Error"
701
+ end
702
+ end
703
+
704
+ ##
705
+ # Provides a simple set of guards that you can use in your tests
706
+ # to skip execution if it is not applicable. These methods are
707
+ # mixed into Test as both instance and class methods so you
708
+ # can use them inside or outside of the test methods.
709
+ #
710
+ # def test_something_for_mri
711
+ # skip "bug 1234" if jruby?
712
+ # # ...
713
+ # end
714
+ #
715
+ # if windows? then
716
+ # # ... lots of test methods ...
717
+ # end
718
+
719
+ module Guard
720
+
721
+ ##
722
+ # Is this running on jruby?
723
+
724
+ def jruby? platform = RUBY_PLATFORM
725
+ "java" == platform
726
+ end
727
+
728
+ ##
729
+ # Is this running on maglev?
730
+
731
+ def maglev? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE
732
+ "maglev" == platform
733
+ end
734
+
735
+ ##
736
+ # Is this running on mri?
737
+
738
+ def mri? platform = RUBY_DESCRIPTION
739
+ /^ruby/ =~ platform
740
+ end
741
+
742
+ ##
743
+ # Is this running on rubinius?
744
+
745
+ def rubinius? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE
746
+ "rbx" == platform
747
+ end
748
+
749
+ ##
750
+ # Is this running on windows?
751
+
752
+ def windows? platform = RUBY_PLATFORM
753
+ /mswin|mingw/ =~ platform
754
+ end
755
+ end
756
+
757
+ class BacktraceFilter # :nodoc:
758
+ def filter bt
759
+ return ["No backtrace"] unless bt
760
+
761
+ return bt.dup if $DEBUG
762
+
763
+ new_bt = bt.take_while { |line| line !~ /lib\/minitest/ }
764
+ new_bt = bt.select { |line| line !~ /lib\/minitest/ } if new_bt.empty?
765
+ new_bt = bt.dup if new_bt.empty?
766
+
767
+ new_bt
768
+ end
769
+ end
770
+
771
+ self.backtrace_filter = BacktraceFilter.new
772
+
773
+ def self.run_one_method klass, method_name # :nodoc:
774
+ result = klass.new(method_name).run
775
+ raise "#{klass}#run _must_ return self" unless klass === result
776
+ result
777
+ end
778
+ end
779
+
780
+ require "minitest/test"