Class: MountainBerryFields::Test::MagicComments

Inherits:
Object
  • Object
show all
Includes:
Strategy
Defined in:
lib/mountain_berry_fields/test/magic_comments.rb

Overview

test strategy for magic comments:

1 + 2 # => 3

Like that, but if the output isn’t 3, then this will fail your test

Instance Method Summary collapse

Instance Method Details

#chomp_or_nil(value) ⇒ Object



94
95
96
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 94

def chomp_or_nil value
  value && value.chomp
end

#each_line_pairObject



81
82
83
84
85
86
87
88
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 81

def each_line_pair
  result_lines = lines result
  code_to_test_lines = lines code_to_test

  while result_lines.any? || code_to_test_lines.any?
    yield chomp_or_nil(code_to_test_lines.shift), chomp_or_nil(result_lines.shift)
  end
end

#failure_messageObject



60
61
62
63
64
65
66
67
68
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 60

def failure_message
  syntax_error_message ||
    each_line_pair do |expected_line, actual_line|
      next if lines_match? expected_line, actual_line
      return "Output had extra line: #{actual_line}\n" unless expected_line
      return "Input had extra line: #{expected_line}\n" unless actual_line
      return "Expected: #{expected_line.gsub /^\s+/, ''}\nActual:   #{actual_line.lstrip.gsub /^\s+/, ''}\n" if expected_line != actual_line
    end
end

#lines(string) ⇒ Object



90
91
92
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 90

def lines string
  string.lines.to_a
end

#lines_match?(line1, line2) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 70

def lines_match?(line1, line2)
  normalize(line1) == normalize(line2)
end

#normalize(line) ⇒ Object



74
75
76
77
78
79
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 74

def normalize(line)
  return unless line
  line.gsub!(/(#<\w+?):(0x[0-9a-f]+)/, '\1')         # replace anonymous instances
  line.gsub!(/#<(#<Class>):(0x[0-9a-f]+)>/, '#<\1>') # replace anonymous instances of anonymous classes end
  line
end

#pass?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 53

def pass?
  each_line_pair do |expected_line, actual_line|
    return false unless normalize(expected_line) == normalize(actual_line)
  end
  true
end

#resultObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 24

def result
  @result ||= Rcodetools::XMPFilter.run(
    code_to_test,
    :interpreter => "ruby",
    :options => [],
    :min_codeline_size => 50,
    :libs => [],
    :evals => [],
    :include_paths => [],
    :dump => nil,
    :wd => nil,
    :warnings => false,
    :use_parentheses => true,
    :column => nil,
    :output_stdout => true,
    :test_script => nil,
    :test_method => nil,
    :detect_rct_fork => false,
    :use_rbtest => false,
    :detect_rbtest => false,
    :execute_ruby_tmpfile => false
  ).join
end

#syntax_checkerObject



20
21
22
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 20

def syntax_checker
  @syntax_checker ||= syntax_checker_class.new code_to_test
end

#syntax_error_messageObject



48
49
50
51
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 48

def syntax_error_message
  return if syntax_checker.valid?
  syntax_checker.invalid_message
end