Class: Moto::Modes::Validate::TestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/modes/validate/test_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(tests_metadata, validation_options, test_reporter) ⇒ TestValidator

Returns a new instance of TestValidator.

Parameters:

  • tests_metadata (Array)

    Collection of [Moto::Test::Metadata] objects describing Tests

  • validation_options (Hash)

    User input in form of a Hash - specifies options of validation

  • test_reporter (Moto::Reporting::TestReporter)

    Reporter of test/run statuses that communicates with external status listeners



15
16
17
18
19
20
# File 'lib/modes/validate/test_validator.rb', line 15

def initialize(, validation_options, test_reporter)

   = 
  @validation_options = validation_options
  @test_reporter = test_reporter
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/modes/validate/test_validator.rb', line 22

def run
  @test_reporter.report_start_run

  test_generator = Moto::Test::Generator.new

  .each do ||
    tests = test_generator.get_test_with_variants(, 1)
    tests.each do |test|
      @test_reporter.report_start_test(test.status, test.)
      test.status.initialize_run

      # Validate if tags are not empty
      if @validation_options[:has_tags] && .tags.empty?
        test.status.log_exception(Exceptions::TestForcedFailure.new('No tags.'))
      end

      # Validate if test description was provided
      if @validation_options[:has_description] && .description.empty?
        test.status.log_exception(Exceptions::TestForcedFailure.new('No description.'))
      end

      # Validate if tags contain entries only from the whitelist
      if @validation_options.key?(:tag_whitelist)
        .tags.each do |tag|
          if !@validation_options[:tag_whitelist].include?(tag)
            test.status.log_exception(Exceptions::TestForcedFailure.new("Tags contain non-whitelisted entry: #{tag}"))
            break
          end
        end
      end

      # Validate if provided regex is found within tags
      if @validation_options.key?(:tags_regex_positive)
        tags_string = .tags.join(',')
        regexp = Regexp.new(@validation_options[:tags_regex_positive])
        result = regexp.match(tags_string)
        if result.nil?
          test.status.log_exception(Exceptions::TestForcedFailure.new("Positive match should have been found in: #{metadata.tags.join(',')}"))
        end
      end

      # Validate if provided regex is NOT found within tags
      if @validation_options.key?(:tags_regex_negative)
        tags_string = .tags.join(',')
        regexp = Regexp.new(@validation_options[:tags_regex_negative])
        result = regexp.match(tags_string)
        if !result.nil?
          test.status.log_exception(Exceptions::TestForcedFailure.new("Negative match shouldn't have been found in: #{metadata.tags.join(',')}"))
        end
      end

      test.status.finalize_run
      @test_reporter.report_end_test(test.status)
    end
  end

  @test_reporter.report_end_run

  # Exit application with code that represents status of test run
  Kernel.exit(@test_reporter.run_status.bitmap)
end