Class: Moon::Test::SpecSuite

Inherits:
Object
  • Object
show all
Includes:
Assert, Colorize
Defined in:
lib/moon/packages/test/spec.rb,
lib/moon/packages/test/spec/stats.rb

Defined Under Namespace

Classes: Stats

Constant Summary

Constants included from Colorize

Colorize::CONSOLE_COLORS, Colorize::CONSOLE_MODES

Instance Attribute Summary collapse

Attributes included from Assert

#assertion_errors, #assertion_quiet, #assertions, #fail_on_assert_error

Attributes included from Colorize

#colorize_enabled

Instance Method Summary collapse

Methods included from Assert

#assert, #assert_ary_include, #assert_ary_size, #assert_const_defined, #assert_equal, #assert_false, #assert_float, #assert_kind_of, #assert_kind_of_any, #assert_method_defined, #assert_nil, #assert_not_equal, #assert_not_kind_of, #assert_not_raise, #assert_not_same, #assert_raise, #assert_respond_to, #assert_same, #assert_true, #catch_assertion_errors, #check_float

Methods included from Colorize

#colorize, #colorize_enabled?

Constructor Details

#initialize(name = nil) ⇒ SpecSuite

Returns a new instance of SpecSuite.


16
17
18
19
20
21
22
23
24
25
# File 'lib/moon/packages/test/spec.rb', line 16

def initialize(name = nil)
  init_assertions  # spec log

  @logger = Logger.new  # debug debug_logger

  @debug_logger = NullIO::OUT
  @name = name || "#{self.class}"
  @tests = []
  @test_stack = [@tests]
end

Instance Attribute Details

#debug_loggerObject

Returns the value of attribute debug_logger


13
14
15
# File 'lib/moon/packages/test/spec.rb', line 13

def debug_logger
  @debug_logger
end

#loggerObject

Returns the value of attribute logger


12
13
14
# File 'lib/moon/packages/test/spec.rb', line 12

def logger
  @logger
end

#nameObject

Returns the value of attribute name


11
12
13
# File 'lib/moon/packages/test/spec.rb', line 11

def name
  @name
end

#testsObject

Returns the value of attribute tests


14
15
16
# File 'lib/moon/packages/test/spec.rb', line 14

def tests
  @tests
end

Instance Method Details

#describe(obj) {|_self| ... } ⇒ Object Also known as: context

Yields:

  • (_self)

Yield Parameters:


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/moon/packages/test/spec.rb', line 27

def describe(obj)
  @debug_logger.puts "#{name}.DESCRIBE: #{obj}"
  @test_stack << []
  yield self
  stack = @test_stack.pop
  stack.map! do |a|
    str, b = *a
    [obj.to_s + " " + str, b]
  end
  @test_stack[-1].concat(stack)
end

#describe_top(&block) ⇒ Object


41
42
43
# File 'lib/moon/packages/test/spec.rb', line 41

def describe_top(&block)
  describe(name, &block)
end

#given(str, &block) ⇒ Object


50
51
52
# File 'lib/moon/packages/test/spec.rb', line 50

def given(str, &block)
  it("given #{str}", &block)
end

#it(str, &block) ⇒ Object


45
46
47
48
# File 'lib/moon/packages/test/spec.rb', line 45

def it(str, &block)
  @debug_logger.puts "#{name}.IT: #{str}"
  @test_stack[-1] << [str, block]
end

#run_specs(options = {}) ⇒ Object


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
# File 'lib/moon/packages/test/spec.rb', line 64

def run_specs(options = {})
  with_assertions = options.fetch(:assertions, true)
  realtime = options.fetch(:realtime_result, true)
  quiet = options.fetch(:quiet, false)

  time_then = Time.now
  passed = 0
  stats = Stats.new
  stats.logger = @logger
  @tests.each do |a|
    test_name, block = *a
    @debug_logger.print test_name
    if (n = 120 - test_name.size - 7) > 0
      @debug_logger.print '.' * n
    end

    failure = false
    catch_assertion_errors do |err|
      failure = true
      stats.add_failure [test_name, err]
    end

    begin
      instance_exec(&block)
      if failure
        @debug_logger.print colorize('FAILED', :light_red)
      else
        @debug_logger.print colorize('PASSED', :light_green)
        stats.add_pass
      end
    rescue AssertError      # just ignore it

    rescue Exception => ex
      stats.add_failure [test_name, ex]
      @debug_logger.puts colorize('KO!', :light_red)
      @debug_logger.puts ex.inspect
      @debug_logger.puts ex.backtrace.join("\n")
    end
    @debug_logger.puts
  end

  time_now = Time.now
  stats.time_diff = (time_now - time_then)
  stats.test_count = @tests.size
  stats.assertions = @assertions
  stats.assertion_errors = @assertion_errors
  stats.display unless quiet
  stats
end

#spec(str = '', &block) ⇒ Object


54
55
56
# File 'lib/moon/packages/test/spec.rb', line 54

def spec(str = '', &block)
  it(str, &block)
end

#spec_bm(*args, &block) ⇒ Object


58
59
60
61
62
# File 'lib/moon/packages/test/spec.rb', line 58

def spec_bm(*args, &block)
  spec do
    bench(*args, &block)
  end
end