Class: Querly::CLI::Test
- Inherits:
-
Object
- Object
- Querly::CLI::Test
- Defined in:
- lib/querly/cli/test.rb
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#initialize(config_path:, stdout: STDOUT, stderr: STDERR) ⇒ Test
constructor
A new instance of Test.
- #load_config ⇒ Object
- #run ⇒ Object
- #test_pattern(pattern, example, expected:) ⇒ Object
- #validate_rule_patterns(rules) ⇒ Object
- #validate_rule_uniqueness(rules) ⇒ Object
Constructor Details
#initialize(config_path:, stdout: STDOUT, stderr: STDERR) ⇒ Test
Returns a new instance of Test.
8 9 10 11 12 |
# File 'lib/querly/cli/test.rb', line 8 def initialize(config_path:, stdout: STDOUT, stderr: STDERR) @config_path = config_path @stdout = stdout @stderr = stderr end |
Instance Attribute Details
#config_path ⇒ Object (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 |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
6 7 8 |
# File 'lib/querly/cli/test.rb', line 6 def stderr @stderr end |
#stdout ⇒ Object (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
#load_config ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/querly/cli/test.rb', line 109 def load_config if config_path.file? config = Config.new config.add_file config_path config end end |
#run ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/querly/cli/test.rb', line 14 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 end validate_rule_uniqueness(config.rules) validate_rule_patterns(config.rules) rescue => exn stderr.puts Rainbow("Fatal error:").red stderr.puts exn.inspect stderr.puts exn.backtrace.map {|x| " " + x }.join("\n") end |
#test_pattern(pattern, example, expected:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/querly/cli/test.rb', line 94 def test_pattern(pattern, example, expected:) analyzer = Analyzer.new(taggings: []) found = false node = Parser::CurrentRuby.parse(example) analyzer.each_subnode NodePair.new(node: node) do |pair| if analyzer.test_pair(pair, pattern) found = true end end found == expected end |
#validate_rule_patterns(rules) ⇒ Object
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 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/querly/cli/test.rb', line 46 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 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#{example_index}th *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 #{example_index}th *before* example") end end rule.after_examples.each.with_index 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#{example_index}th *after* example matched with some of patterns") false_positives += 1 end rescue Parser::SyntaxError errors += 1 stdout.puts(Rainbow(" #{rule.id}") + ":\tParsing failed for #{example_index}th *after* example") 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" else stdout.puts Rainbow(" All tests green!").green end end |
#validate_rule_uniqueness(rules) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/querly/cli/test.rb', line 31 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 end |