Class: Querly::CLI::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/cli/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path:, stdout: STDOUT, stderr: STDERR) ⇒ Test

Returns a new instance of Test.



8
9
10
11
12
13
# File 'lib/querly/cli/test.rb', line 8

def initialize(config_path:, stdout: STDOUT, stderr: STDERR)
  @config_path = config_path
  @stdout = stdout
  @stderr = stderr
  @success = true
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



4
5
6
# File 'lib/querly/cli/test.rb', line 4

def config_path
  @config_path
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/querly/cli/test.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/querly/cli/test.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#fail!Object



15
16
17
# File 'lib/querly/cli/test.rb', line 15

def fail!
  @success = false
end

#failed?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/querly/cli/test.rb', line 19

def failed?
  !@success
end

#load_configObject



153
154
155
156
157
158
# File 'lib/querly/cli/test.rb', line 153

def load_config
  if config_path.file?
    yaml = YAML.load(config_path.read)
    Config.load(yaml, config_path: config_path, root_dir: config_path.parent.realpath, stderr: STDERR)
  end
end

#ordinalize(number) ⇒ Object



160
161
162
# File 'lib/querly/cli/test.rb', line 160

def ordinalize(number)
  ActiveSupport::Inflector.ordinalize(number)
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/querly/cli/test.rb', line 23

def run
  config = load_config

  unless config
    stdout.puts "There is nothing to test at #{config_path} ..."
    stdout.puts "Make a configuration and run test again!"
    return 1
  end

  validate_rule_uniqueness(config.rules)
  validate_rule_patterns(config.rules)

  failed? ? 1 : 0
rescue => exn
  stderr.puts Rainbow("Fatal error:").red
  stderr.puts exn.inspect
  stderr.puts exn.backtrace.map {|x| "  " + x }.join("\n")

  1
end

#test_pattern(pattern, example, expected:) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/querly/cli/test.rb', line 138

def test_pattern(pattern, example, expected:)
  analyzer = Analyzer.new(config: nil, rule: nil)

  found = false

  node = Parser::Ruby25.parse(example)
  NodePair.new(node: node).each_subpair do |pair|
    if analyzer.test_pair(pair, pattern)
      found = true
    end
  end

  found == expected
end

#validate_rule_patterns(rules) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/querly/cli/test.rb', line 61

def validate_rule_patterns(rules)
  stdout.puts "Checking rule patterns..."

  tests = 0
  false_positives = 0
  false_negatives = 0
  errors = 0

  rules.each do |rule|
    rule.before_examples.each.with_index(1) do |example, example_index|
      tests += 1

      begin
        unless rule.patterns.any? {|pat| test_pattern(pat, example, expected: true) }
          stdout.puts(Rainbow("  #{rule.id}").red + ":\t#{ordinalize example_index} *before* example didn't match with any pattern")
          false_negatives += 1
        end
      rescue Parser::SyntaxError
        errors += 1
        stdout.puts(Rainbow("  #{rule.id}").red + ":\tParsing failed for #{ordinalize example_index} *before* example")
      end
    end

    rule.after_examples.each.with_index(1) do |example, example_index|
      tests += 1

      begin
        unless rule.patterns.all? {|pat| test_pattern(pat, example, expected: false) }
          stdout.puts(Rainbow("  #{rule.id}").red + ":\t#{ordinalize example_index} *after* example matched with some of patterns")
          false_positives += 1
        end
      rescue Parser::SyntaxError
        errors += 1
        stdout.puts(Rainbow("  #{rule.id}") + ":\tParsing failed for #{ordinalize example_index} *after* example")
      end
    end

    rule.examples.each.with_index(1) do |example, index|
      if example.before
        tests += 1
        begin
          unless rule.patterns.any? {|pat| test_pattern(pat, example.before, expected: true) }
            stdout.puts(Rainbow("  #{rule.id}").red + ":\tbefore of #{ordinalize index} example didn't match with any pattern")
            false_negatives += 1
          end
        rescue Parser::SyntaxError
          errors += 1
          stdout.puts(Rainbow("  #{rule.id}").red + ":\tParsing failed on before of #{ordinalize index} example")
        end
      end

      if example.after
        tests += 1
        begin
          unless rule.patterns.all? {|pat| test_pattern(pat, example.after, expected: false) }
            stdout.puts(Rainbow("  #{rule.id}").red + ":\tafter of #{ordinalize index} example matched with some of patterns")
            false_positives += 1
          end
        rescue Parser::SyntaxError
          errors += 1
          stdout.puts(Rainbow("  #{rule.id}") + ":\tParsing failed on after of #{ordinalize index} example")
        end
      end
    end
  end

  stdout.puts "Tested #{rules.size} rules with #{tests} tests."
  if false_positives > 0 || false_negatives > 0 || errors > 0
    stdout.puts "  #{false_positives} examples found which should not match, but matched"
    stdout.puts "  #{false_negatives} examples found which should match, but didn't"
    stdout.puts "  #{errors} examples raised error"
    fail!
  else
    stdout.puts Rainbow("  All tests green!").green
  end
end

#validate_rule_uniqueness(rules) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/querly/cli/test.rb', line 44

def validate_rule_uniqueness(rules)
  ids = Set.new

  stdout.puts "Checking rule id uniqueness..."

  duplications = 0

  rules.each do |rule|
    unless ids.add?(rule.id)
      stdout.puts Rainbow("  Rule id #{rule.id} duplicated!").red
      duplications += 1
    end
  end

  fail! unless duplications == 0
end