Module: LC3Spec::Dsl

Defined in:
lib/lc3spec/dsl.rb

Instance Method Summary collapse

Instance Method Details

#configure(&block) ⇒ Object



34
35
36
# File 'lib/lc3spec/dsl.rb', line 34

def configure(&block)
  yield if block_given?
end

#count_points(result, possible) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lc3spec/dsl.rb', line 78

def count_points(result, possible)
  @possible_points ||= 0
  @earned_points ||= 0

  @earned_points += result == :pass ? possible : 0
  @possible_points += possible

  @num_tests ||= 0
  @num_tests += 1

  @num_passed ||= 0
  @num_passed += result == :pass ? 1 : 0
end

#fail(description, points) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/lc3spec/dsl.rb', line 68

def fail(description, points)
  count_points(:fail, points)

  if !@options[:keep_score] || (points == 0)
    @options[:output].puts "#{description} [FAIL]"
  else
    @options[:output].puts "#{description} 0/#{points}"
  end
end

#pass(description, points) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/lc3spec/dsl.rb', line 58

def pass(description, points)
  count_points(:pass, points)

  if !@options[:keep_score] || (points == 0)
    @options[:output].puts "#{description} [OK]"
  else
    @options[:output].puts "#{description} #{points}/#{points}"
  end
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lc3spec/dsl.rb', line 92

def print_score
  return if @num_tests.nil? or (@num_tests == 0)

  output = @options[:output]

  if @options[:keep_score]
    output.puts "Score: #@earned_points/#@possible_points"
  else
    if @num_passed == @num_tests
      output.puts "[ALL OK]"
    elsif @num_passed == 0
      output.puts "[ALL FAIL]"
    end
  end
end

#set(option, value = true) ⇒ Object



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
# File 'lib/lc3spec/dsl.rb', line 3

def set(option, value = true)
  @options ||= {
    :output => $stdout,
    :keep_score => false
  }

  if option == :output
    case value
    when File
      if value.path =~ /\.asm$/
        raise "Not writing output to .asm file"
      end

      @options[:output] = value
    when String
      if value =~ /\.asm$/
        raise "Not writing output to .asm file"
      end

      @options[:output] = open(value, 'w')
    else
      @options[:output] = value
    end

    return @options[:output]
  end

  @options[option] = value

end

#test(description, points = 0, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lc3spec/dsl.rb', line 38

def test(description, points = 0, &block)
  t = LC3Spec::Test.new(:options => @options,
                        :points => points,
                        &block)

  output = @options[:output]

  if t.pass
    pass(description, points)
  else
    fail(description, points)

    # FIXME: Ugly
    errors = t.reporter.errors.join("\n")

    output.puts errors.each_line.map { |line| '  ' + line }.join('')
    output.puts
  end
end