class Object

Public Instance Methods

arrow_test(filename=$0, verbose=false) click to toggle source

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

Example:

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

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

require 'arrow_test'

# 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.
# 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) click to toggle source

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.
# 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