Class: LC3Spec::Test

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/lc3spec/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#is_absolute_path?, #normalize_to_i, #normalize_to_s

Constructor Details

#initialize(options, &block) ⇒ Test

Returns a new instance of Test.



17
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/base.rb', line 17

def initialize(options, &block)
  # Save src_dir
  @src_dir = File.expand_path(Dir.pwd)
  @reporter = Reporter.new
  @pass = true

  # Do everything inside tmp_dir
  Dir.mktmpdir('spec') do |tmp_dir|
    @tmp_dir = tmp_dir

    Dir.chdir(tmp_dir) do
      @lc3 = LC3.new

      begin
        instance_eval(&block) if block_given?
      rescue Timeout::Error => err
        @reporter.report "Execution timed-out, likely due to an infinite loop"
      rescue LC3Spec::DoesNotAssembleError => err
        @reporter.report err.message
        $stderr.puts err.message
      end

      @pass = false if @reporter.fail?
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



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

def method_missing(method_name, *arguments, &block)
  if @lc3.respond_to? method_name
    res = @lc3.send(method_name, *arguments, &block)

    if method_name.to_s =~ /^get_/
      res
    else
      self
    end
  elsif method_name.to_s =~ /^expect_/
    LC3Spec::Expectations.send(method_name, @lc3, @reporter, *arguments, &block)

    self
  else
    super
  end
end

Instance Attribute Details

#passObject

Returns the value of attribute pass.



15
16
17
# File 'lib/lc3spec/base.rb', line 15

def pass
  @pass
end

#reporterObject

Returns the value of attribute reporter.



15
16
17
# File 'lib/lc3spec/base.rb', line 15

def reporter
  @reporter
end

Instance Method Details

#continue(duration = 1.5) ⇒ Object



75
76
77
78
79
# File 'lib/lc3spec/base.rb', line 75

def continue(duration = 1.5)
  Timeout.timeout(duration) do
    @lc3.continue
  end
end

#file(filename) ⇒ Object Also known as: load_file



67
68
69
70
71
72
73
# File 'lib/lc3spec/base.rb', line 67

def file(filename)
  basename = ensure_present(filename)
  ensure_assembled(basename)
  @lc3.file(basename)

  self
end

#file_from_asm(asm) ⇒ Object



61
62
63
64
65
# File 'lib/lc3spec/base.rb', line 61

def file_from_asm(asm)
  assemble_and_load { |f| f.puts asm }

  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
# File 'lib/lc3spec/base.rb', line 115

def respond_to_missing?(method_name, include_private = false)
  if @lc3.respond_to? method_name
    true
  elsif method_name.to_s =~ /^expect_/
    true
  else
    super
  end
end

#set_label(label, addr) ⇒ Object



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

def set_label(label, addr)
  label = label.to_s.upcase

  if @labels.include? label
    raise "Unable to replace label #{label}"
  end

  assemble_and_load do |f|
    f.puts ".ORIG #{normalize_to_s(addr)}\n#{label}\n.END"
  end

  self
end

#set_register(reg, val = 0) ⇒ Object Also known as: set_registers



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lc3spec/base.rb', line 44

def set_register(reg, val = 0)
  case reg
  when Hash
    reg.each do |r, v|
      @lc3.set_register(r, v)
    end
  when Array
    reg.each_with_index do |v, i|
      @lc3.set_register("R#{i}", v)
    end
  else
    @lc3.set_register(reg, val)
  end
end