Module: LC3Spec::Dsl

Defined in:
lib/lc3spec/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lc3spec/dsl.rb', line 3

def self.extended(base)
  base.instance_eval do
    @options ||= {
      :output => $stdout,
      :keep_score => false
    }

    @possible_points ||= 0
    @earned_points ||= 0

    @num_tests ||= 0
    @num_passed ||= 0
  end
end

Instance Method Details

#configure(&block) ⇒ Object



44
45
46
# File 'lib/lc3spec/dsl.rb', line 44

def configure(&block)
  yield if block_given?
end

#count_points(result, possible) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/lc3spec/dsl.rb', line 88

def count_points(result, possible)
  @earned_points += result == :pass ? possible : 0
  @possible_points += possible

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

#fail(description, points) ⇒ Object



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

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



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

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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lc3spec/dsl.rb', line 96

def print_score
  return if @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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lc3spec/dsl.rb', line 18

def set(option, value = true)
  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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lc3spec/dsl.rb', line 48

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