Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#arrow_test(filename = $0, verbose = false) ⇒ Object

Performs Input/Output wrapping ‘helper_arrow_test` Call this in your code.

Example:

require 'arrow_testing'
1 + 1 # -> 3
arrow_test

Arguments:

filename: (String) The filename to be tested. Default is $0: your current file.
verbose: (Boolean) If true outputs also correct tests.


52
53
54
# File 'lib/arrow_test.rb', line 52

def arrow_test(filename=$0, verbose=false)
  puts helper_arrow_test(IO.foreach(filename).to_a, verbose)
end

#helper_arrow_test(lines, verbose = false) ⇒ Object

Performs the logic of the arrow test.

Example:

>> helper_arrow_test(["", "1+1 #-> 3"])
=> ["ERROR:\n        EXECUTING: 1  + 1\n        EXPECTED: 3\n        INSTEAD GOT: 2\n\n\n"]

Arguments:

lines: (String) The lines of code to be arrow tested.
verbose: (Boolean) If true returns also correct tests.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/arrow_test.rb', line 12

def helper_arrow_test(lines, verbose=false)
  messages = []
  arrow_regex = Regexp.new('\ *\#\ *\-\ *\>\ *')
  thick_arrow_regex = Regexp.new('\ *\#\ *\=\ *\>\ *')
  lines
    .select{|line| arrow_regex.match(line) || thick_arrow_regex.match(line)}
    .each do |line|
      expression = line.split('#').first.strip
      result = line.split('#').last.sub("\n", '').sub("-",'').sub(">",'').sub('=','').strip
      got = eval(expression)
      actual = eval(result)
      if got != actual
        messages.push <<END
ERROR:
        EXECUTING: #{expression}
        EXPECTED: #{result}
        INSTEAD GOT: #{got}\n\n
END
      elsif verbose
        messages.push <<END
CORRECT:
        EXECUTING: #{expression}
        REALLY_IS: #{got}\n\n
END
       end
      end   
  messages
end