Class: Fix::Test

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout, color: defined?(COLOR)) ⇒ Test

Returns a new instance of Test.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fix/test.rb', line 7

def initialize io = $stdout, color: defined?(COLOR)
  @io     = io
  @color  = color

  start_time = Time.now

  @results = DB.instance.flat_map do |object, expectations|
    expectations.map {|expectation| log expectation.evaluate(object) }
  end

  @total_time = Time.now - start_time

  @results = @results.sort {|a, b| a.fetch(:level) <=> b.fetch(:level) }

  @io.puts

  %i(errors failures infos).each do |results_with_state|
    if reports(results_with_state).any?
      @io.puts
      @io.puts "# #{results_with_state.capitalize}:"
      @io.puts

      reports(results_with_state).each_with_index do |report, index|
        @io.print about "#{index.next}. "
        @io.puts report
      end
    end
  end

  @io.puts
  @io.puts about "Finished in #{@total_time} seconds."
  @io.puts statistics

  freeze
end

Instance Attribute Details

#total_timeObject (readonly)

Returns the value of attribute total_time.



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

def total_time
  @total_time
end

Instance Method Details

#errorsObject



43
44
45
# File 'lib/fix/test.rb', line 43

def errors
  @results.select {|result| state(result) == :error }
end

#fail?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/fix/test.rb', line 127

def fail?
  !pass?
end

#failuresObject



47
48
49
# File 'lib/fix/test.rb', line 47

def failures
  @results.select {|result| state(result) == :failure }
end

#infosObject



51
52
53
# File 'lib/fix/test.rb', line 51

def infos
  @results.select {|result| state(result) == :info }
end

#pass?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/fix/test.rb', line 123

def pass?
  !@results.any? {|result| result.fetch(:pass) == false }
end

#reports(results_with_state) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
# File 'lib/fix/test.rb', line 55

def reports results_with_state
  __send__(results_with_state).map do |result|
    result.fetch(:object).inspect +
      if result.fetch(:params).any?
        '.' +
        result.fetch(:params).map.with_index do |args, i|
          color = if i == result.fetch(:last_arg)
            state result
          elsif i > result.fetch(:last_arg)
            :pending
          else
            :default
          end

          __send__(color, "#{args.first}" +
            if args.length > 1
              '(' + args[1..-1].map {|arg| arg.to_s }.join(',') + ')'
            else
              ''
            end
          )
        end.join('.')
      else
        ''
      end +
      '.' +
      (
        color = if result.fetch(:params).length == result.fetch(:last_arg)
          state result
        else
          :pending
        end

        __send__(color, "#{result.fetch(:challenge).first}" +
          if result.fetch(:challenge).length > 1
            '(' + result.fetch(:challenge)[1..-1].map {|arg| arg.to_s }.join(',') + ')'
          else
            ''
          end
        )
      ) +
      ' ' +
      expectation_level(result) +
      ' ' +
      matcher_with_expected_if_given(result) +
      returned_value(result)
  end
end

#statisticsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fix/test.rb', line 104

def statistics
  color = if errors.any?
    :error
  elsif failures.any?
    :failure
  else
    :success
  end

  percents = (@results.count {|result| result.fetch(:pass) != false }) / @results.length.to_f * 100

  __send__ color, "#{percents.round}% compliant (" + [
    "#{@results.length} specs",
    "#{infos.length} infos",
    "#{failures.length} failures",
    "#{errors.length} errors"
  ].join(', ') + ')'
end