Class: ButterflyNet::TestUnitMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/butterfly_net/test_unit_method.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTestUnitMethod

Returns a new instance of TestUnitMethod.



4
5
6
# File 'lib/butterfly_net/test_unit_method.rb', line 4

def initialize
  @lines = []
end

Class Method Details

.assertion(expected, line) ⇒ Object

todo: extract to assertion class



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/butterfly_net/test_unit_method.rb', line 16

def self.assertion(expected, line)     # todo: extract to assertion class
  if expected && simple_assignment_only?(line)
    line
  elsif expected && expected == line # type not supported, assume object inequality
    "assert_not_equal((#{expected}), #{line})"
  elsif expected == "true" # use simple assert() for true boolean expressions
    "assert(#{line})"
  elsif expected
    "assert_equal(#{expected}, #{line})"
  else
    nil
  end
end

.simple_assignment_only?(line) ⇒ Boolean

todo: extract to line class

Returns:

  • (Boolean)


12
13
14
# File 'lib/butterfly_net/test_unit_method.rb', line 12

def self.simple_assignment_only?(line)  # todo: extract to line class
  line =~ /[^=<>!*%\/+-\\|&]=[^=~]/
end

Instance Method Details

#<<(line) ⇒ Object



8
9
10
# File 'lib/butterfly_net/test_unit_method.rb', line 8

def <<(line)
  @lines << line
end

#expected_assertion(current_i) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/butterfly_net/test_unit_method.rb', line 43

def expected_assertion(current_i)

  current_line = @lines[current_i]
  commands = current_line
  start_i = current_i

  begin
    retval = eval commands
  rescue
    start_i -= 1
    commands = @lines[start_i..current_i].join("\n")

    if start_i < 0
      puts "failure evaluating: eval[#{start_i}..#{current_i}] : #{commands}\n"
      return nil
    else
      retry
    end
  end
  if retval && TestUnitMethod.simple_assignment_only?(current_line)
    current_line

  elsif instances_equal_by_value?(retval) # expression result supports value equality

    if retval == true # use simple assert() for true boolean expressions
      "assert(#{current_line})"
    elsif retval.nil?
      "assert_nil(#{current_line})"
    else
      "assert_equal(#{retval.inspect}, #{current_line})"
    end

  else

    # any other sort of object is handled as a not equal assertion
    "assert_not_equal((#{current_line}), #{current_line})"
  end

end

#instances_equal_by_value?(instance) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/butterfly_net/test_unit_method.rb', line 83

def instances_equal_by_value?(instance)
  instance == instance.dup rescue true  # can't dup Fixnum, et al...
end

#text(method_name) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/butterfly_net/test_unit_method.rb', line 34

def text(method_name)
  method_string = "def test_#{method_name}\n"
  @lines.each_with_index do |line, i|
    text = text_from_expression(line, i)
    method_string += "    #{text}\n" if text
  end
  method_string += "  end"
end

#text_from_expression(line, i) ⇒ Object



30
31
32
# File 'lib/butterfly_net/test_unit_method.rb', line 30

def text_from_expression(line, i)
  expected_assertion(i)
end