Class: Cutest

Inherits:
Object
  • Object
show all
Defined in:
lib/cutest.rb

Defined Under Namespace

Classes: AssertionFailed, Scope

Constant Summary collapse

VERSION =
"1.2.3"
FILTER =
%r[/(ruby|jruby|rbx)[-/]([0-9\.])+]
CACHE =
Hash.new { |h, k| h[k] = File.readlines(k) }

Class Method Summary collapse

Class Method Details

.code(fn, ln) ⇒ Object


49
50
51
52
53
54
55
# File 'lib/cutest.rb', line 49

def self.code(fn, ln)
  begin
    CACHE[fn][ln.to_i - 1].strip
  rescue
    "(Can't display line)"
  end
end

.display_errorObject


57
58
59
60
# File 'lib/cutest.rb', line 57

def self.display_error
  print "\n#{$!.class}: "
  print "#{$!.message}\n"
end

.display_trace(line) ⇒ Object


62
63
64
65
66
67
# File 'lib/cutest.rb', line 62

def self.display_trace(line)
  fn, ln = line.split(":")

  puts "  line: #{code(fn, ln)}"
  puts "  file: #{fn} +#{ln}"
end

.run(files) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cutest.rb', line 8

def self.run(files)
  status = files.all? do |file|
    run_file(file)

    Process.wait2.last.success?
  end

  puts

  status
end

.run_file(file) ⇒ Object


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

def self.run_file(file)
  fork do
    begin
      load(file)

    rescue LoadError, SyntaxError
      display_error
      exit 1

    rescue StandardError
      trace = $!.backtrace
      pivot = trace.index { |line| line.match(file) }

      puts "\n  test: %s" % cutest[:test]

      if pivot
        other = trace[0..pivot].select { |line| line !~ FILTER }
        other.reverse.each { |line| display_trace(line) }
      else
        display_trace(trace.first)
      end

      display_error

      exit 1
    end
  end
end