Module: Scampi

Defined in:
lib/scampi.rb,
lib/scampi/error.rb,
lib/scampi/should.rb,
lib/scampi/context.rb,
lib/scampi/version.rb

Defined Under Namespace

Classes: Context, Error, Should

Constant Summary collapse

Counter =
Hash.new(0)
ErrorLog =
"".dup
Shared =
Hash.new { |_, name|
  raise NameError, "no such context: #{name.inspect}"
}
RestrictName =
//
RestrictContext =
//
Backtraces =
true
VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.handle_requirement(description, indent = 0, local_n = 1) ⇒ Object

TAP output



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/scampi.rb', line 84

def self.handle_requirement(description, indent = 0, local_n = 1)
  ErrorLog.replace ""
  error = yield
  prefix = "    " * indent
  if error.empty?
    puts "#{prefix}#{"ok".green} #{local_n} - #{description}"
    true
  else
    puts "#{prefix}#{"not ok".red} #{local_n} - #{description}: #{error}"
    puts ErrorLog.strip.gsub(/^/, "#{prefix}# ")  if Backtraces
    false
  end
end

.queueObject



29
30
31
# File 'lib/scampi.rb', line 29

def self.queue
  @queue
end

.runObject



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
# File 'lib/scampi.rb', line 33

def self.run
  return if @ran
  @ran = true

  # Register: evaluate all describe blocks to discover specs
  @queue.each { |item| item.register if item.is_a?(Context) }

  # TAP version + plan
  puts "TAP version 14"
  puts "1..#{@queue.size}"

  # Execute: contexts become subtests, raw specs become flat lines
  @queue.each_with_index do |item, i|
    n = i + 1
    if item.is_a?(Context)
      passed = item.execute(0)
      if passed
        puts "#{"ok".green} #{n} - #{item.name}"
      else
        puts "#{"not ok".red} #{n} - #{item.name}"
      end
    else
      _, description, block = item
      Counter[:specifications] += 1
      passed = run_bare_spec(description, block, n)
    end
  end

  # Summary comment
  tests, assertions, failures, errors =
    Counter.values_at(:specifications, :requirements, :failed, :errors)
  puts "# #{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors"
end

.run_bare_spec(description, block, n) ⇒ Object



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
# File 'lib/scampi.rb', line 98

def self.run_bare_spec(description, block, n)
  handle_requirement(description, 0, n) do
    begin
      Counter[:depth] += 1
      rescued = false
      begin
        prev_req = Counter[:requirements]
        block.call
      rescue Object => e
        rescued = true
        raise e
      ensure
        if Counter[:requirements] == prev_req and not rescued
          raise Error.new(:missing, "empty specification: #{description}")
        end
      end
    rescue SystemExit, Interrupt
      raise
    rescue Object => e
      ErrorLog << "#{e.class}: #{e.message}\n"
      e.backtrace.find_all { |line| line !~ /bin\/scampi|\/scampi\.rb:\d+/ }.
        each_with_index { |line, i|
        ErrorLog << "\t#{line}#{i==0 ? ": #{description}" : ""}\n"
      }
      ErrorLog << "\n"

      if e.kind_of? Error
        Counter[e.count_as] += 1
        e.count_as.to_s.upcase
      else
        Counter[:errors] += 1
        "ERROR: #{e.class}"
      end
    else
      ""
    ensure
      Counter[:depth] -= 1
    end
  end
end

.summary_on_exitObject Also known as: summary_at_exit



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scampi.rb', line 67

def self.summary_on_exit
  return  if Counter[:installed_summary] > 0
  @timer = Time.now
  at_exit {
    run
    if $!
      raise $!
    elsif Counter[:errors] + Counter[:failed] > 0
      exit 1
    end
  }
  Counter[:installed_summary] += 1
end