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

Constant Summary collapse

ELLIPSIS_REGEX =
/\s+\.{3,}\s*/

Instance Method Summary collapse

Instance Method Details

#chomp_or_nil(value) ⇒ Object



113
114
115
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 113

def chomp_or_nil value
  value && value.chomp
end

#each_line_pairObject



100
101
102
103
104
105
106
107
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 100

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

#ellipses_to_wildcards_regex(line) ⇒ Object

magic for “ …” in magic comments

This translates a string with groups of three or more dots into a regular expression with the dot groups replaced by wildcards. For example, “Blah, blah, blah, … and so on” is translated into /ABlah,\ blah,\ blah,.*and so onz/.



87
88
89
90
91
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 87

def ellipses_to_wildcards_regex(line)
  content_pieces = line.split(ELLIPSIS_REGEX, -1)
                       .map(&Regexp.method(:escape))
  /\A#{content_pieces.join('.*?')}\Z/
end

#failure_messageObject



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

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



109
110
111
# File 'lib/mountain_berry_fields/test/magic_comments.rb', line 109

def lines string
  string.lines.to_a
end

#lines_match?(expected, actual) ⇒ Boolean

Returns:

  • (Boolean)


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

def lines_match?(expected, actual)
  if ELLIPSIS_REGEX.match(expected)
    expected_regex = ellipses_to_wildcards_regex(normalize(expected))
    expected_regex.match(actual)
  else
    normalize(expected) == normalize(actual)
  end
end

#normalize(line) ⇒ Object



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

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)


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

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

#resultObject



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

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



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

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

#syntax_error_messageObject



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

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