Class: Emendate::Examples::TestableExample

Inherits:
Object
  • Object
show all
Defined in:
lib/emendate/examples/testable_example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows) ⇒ TestableExample

Returns a new instance of TestableExample.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emendate/examples/testable_example.rb', line 12

def initialize(rows)
  @rows = rows

  fail(EmptyTestSetError.new) if rows.empty?

  @fingerprint = rows.first.test_fingerprint
  @test_string = rows.first.test_string
  @test_options = rows.first.test_options
  @test_pattern = rows.first.test_pattern
  @processed = nil
  @errors = {}
  @test_results = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def errors
  @errors
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def fingerprint
  @fingerprint
end

#processedObject (readonly)

Returns the value of attribute processed.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def processed
  @processed
end

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def rows
  @rows
end

#test_optionsObject (readonly)

Returns the value of attribute test_options.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def test_options
  @test_options
end

#test_patternObject (readonly)

Returns the value of attribute test_pattern.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def test_pattern
  @test_pattern
end

#test_stringObject (readonly)

Returns the value of attribute test_string.



10
11
12
# File 'lib/emendate/examples/testable_example.rb', line 10

def test_string
  @test_string
end

Instance Method Details

#add_error(testname, err) ⇒ Object



26
27
28
# File 'lib/emendate/examples/testable_example.rb', line 26

def add_error(testname, err)
  @errors[testname] = formatted_error(testname, err)
end

#add_test_result(testname, result) ⇒ Object



36
37
38
# File 'lib/emendate/examples/testable_example.rb', line 36

def add_test_result(testname, result)
  @test_results[testname] = result
end

#all_tagsObject



40
41
42
# File 'lib/emendate/examples/testable_example.rb', line 40

def all_tags
  [tags("data_set"), tags("date_type")].flatten.sort.uniq
end

#formatted_error(testname, err) ⇒ Object



30
31
32
33
34
# File 'lib/emendate/examples/testable_example.rb', line 30

def formatted_error(testname, err)
  return err unless errors.key?(testname)

  "#{errors[testname]}|#{err}"
end

#report_error(err) ⇒ Object



44
45
46
# File 'lib/emendate/examples/testable_example.rb', line 44

def report_error(err)
  puts err.map { |line| "    #{line}" }
end

#report_failureObject



48
49
50
51
52
53
54
# File 'lib/emendate/examples/testable_example.rb', line 48

def report_failure
  puts "string: #{test_string} -- opts: #{test_options}"
  errors.each do |test, err|
    puts "  test: #{test}"
    report_error(err)
  end
end

#run_tests(tests: nil, fail_fast: false, mode: :normal) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/emendate/examples/testable_example.rb', line 56

def run_tests(tests: nil, fail_fast: false, mode: :normal)
  reset_test_data unless test_results.empty?
  puts "Testing: #{fingerprint}" if mode == :verbose
  return unless testable?

  to_run = tests ? tests.intersection(runnable_tests) : runnable_tests
  testers = to_run.map do |test|
    Examples::Tester.build(test: test, example: self)
  end
  testers.each do |test|
    test.call
    break if fail_fast && !errors[test.name.to_sym].nil?
  end
end

#runnable_testsObject



71
72
73
# File 'lib/emendate/examples/testable_example.rb', line 71

def runnable_tests
  @runnable_tests ||= determine_runnable_tests
end

#tags(type) ⇒ Object

Parameters:

  • type ('data_set', 'date_type')


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/emendate/examples/testable_example.rb', line 76

def tags(type)
  meth = :"tags_#{type}"
  rows.map { |row| row.send(meth) }
    .compact
    .map { |rowtags| rowtags.split(";") }
    .flatten
    .uniq
    .sort
    .reject { |val| val.blank? }
    .map { |val| "#{val} (#{meth})" }
end

#test_statusObject



88
89
90
91
92
# File 'lib/emendate/examples/testable_example.rb', line 88

def test_status
  return :no_tests_run if test_results.empty?

  test_results.values.any?(:failure) ? :failure : :success
end

#testable?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/emendate/examples/testable_example.rb', line 94

def testable?
  @processed ? true : check_testable
end

#to_sObject Also known as: inspect



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/emendate/examples/testable_example.rb', line 98

def to_s
  <<~OBJ
    #<#{self.class.name}:#{object_id}
      @fingerprint: "#{fingerprint}",
      @rows: #{rows.length},
      @runnable_tests: #{runnable_tests.inspect},
      @processed: #{processed.class.name},
      @test_results: #{test_results.inspect},
      @errors: #{errors.inspect}>
  OBJ
end

#type_pattern(date_only: false, stage: :tokens) ⇒ Object



111
112
113
114
115
116
# File 'lib/emendate/examples/testable_example.rb', line 111

def type_pattern(date_only: false, stage: :tokens)
  return [:date_string_not_processed] unless testable?
  return processed.send(stage).date_part_types if date_only

  processed.send(stage).types
end