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

You may also test functions, in that case remember adding a # at the start of the line:

require 'arrow_testing'

# digits(9001) #=> [9, 0, 0, 1]
def digits(n)
  n.to_s.chars.map(&:to_i)
end

arrow_test

Arguments:

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


66
67
68
# File 'lib/arrow_test.rb', line 66

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.


10
11
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 10

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)}
    .map(&:strip)
    .map {|line| line[0] == '#' ? line[1...line.length] : 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