Class: Goodcheck::Commands::Test

Inherits:
Object
  • Object
show all
Includes:
ConfigLoading
Defined in:
lib/goodcheck/commands/test.rb

Instance Attribute Summary collapse

Attributes included from ConfigLoading

#config

Instance Method Summary collapse

Methods included from ConfigLoading

#load_config!

Constructor Details

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

Returns a new instance of Test.



10
11
12
13
14
# File 'lib/goodcheck/commands/test.rb', line 10

def initialize(stdout:, stderr:, config_path:)
  @stdout = stdout
  @stderr = stderr
  @config_path = config_path
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



8
9
10
# File 'lib/goodcheck/commands/test.rb', line 8

def config_path
  @config_path
end

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/goodcheck/commands/test.rb', line 7

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/goodcheck/commands/test.rb', line 6

def stdout
  @stdout
end

Instance Method Details

#rule_matches_example?(rule, example) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/goodcheck/commands/test.rb', line 88

def rule_matches_example?(rule, example)
  buffer = Buffer.new(path: Pathname("-"), content: example)
  analyzer = Analyzer.new(rule: rule, buffer: buffer)
  analyzer.scan.count > 0
end

#runObject



16
17
18
19
20
21
22
23
# File 'lib/goodcheck/commands/test.rb', line 16

def run
  load_config!

  validate_rule_uniqueness or return 1
  validate_rules or return 1

  0
end

#validate_rule_uniquenessObject



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

def validate_rule_uniqueness
  stdout.puts "Validating rule id uniqueness..."

  duplicated_ids = []

  config.rules.group_by(&:id).each do |id, rules|
    if rules.size > 1
      duplicated_ids << id
    end
  end

  if duplicated_ids.empty?
    stdout.puts "  OK!👍"
    true
  else
    stdout.puts(Rainbow("  Found #{duplicated_ids.size} duplications.😞").red)
    duplicated_ids.each do |id|
      stdout.puts "    #{id}"
    end
    false
  end
end

#validate_rulesObject



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
# File 'lib/goodcheck/commands/test.rb', line 48

def validate_rules
  test_pass = true

  config.rules.each do |rule|
    if !rule.passes.empty? || !rule.fails.empty?
      stdout.puts "Testing rule #{rule.id}..."

      pass_errors = rule.passes.each.with_index.select do |pass, index|
        rule_matches_example?(rule, pass)
      end

      fail_errors = rule.fails.each.with_index.reject do |fail, index|
        rule_matches_example?(rule, fail)
      end

      unless pass_errors.empty?
        test_pass = false

        pass_errors.each do |_, index|
          stdout.puts "  #{(index+1).ordinalize} pass example matched.😱"
        end
      end

      unless fail_errors.empty?
        test_pass = false

        fail_errors.each do |_, index|
          stdout.puts "  #{(index+1).ordinalize} fail example didn't match.😱"
        end
      end

      if pass_errors.empty? && fail_errors.empty?
        stdout.puts "  OK!🎉"
      end
    end
  end

  test_pass
end