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), hide_progress: defined?(HIDE_PROGRESS)) ⇒ 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
42
43
44
45
46
47
# File 'lib/fix/test.rb', line 7

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

  start_time = Time.now

  @results = DB.instance.flat_map do |object, expectations|
    expectations.map do |expectation|
      if hide_progress
        expectation.evaluate object
      else
        log expectation.evaluate(object)
      end
    end
  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 "Ran #{@results.length} tests in #{@total_time} seconds"
  @io.puts statistics

  exit 1 if fail?
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



49
50
51
# File 'lib/fix/test.rb', line 49

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

#fail?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/fix/test.rb', line 136

def fail?
  !pass?
end

#failuresObject



53
54
55
# File 'lib/fix/test.rb', line 53

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

#infosObject



57
58
59
# File 'lib/fix/test.rb', line 57

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

#pass?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/fix/test.rb', line 132

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

#reports(results_with_state) ⇒ Object



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
103
104
105
106
107
108
# File 'lib/fix/test.rb', line 61

def reports results_with_state
  __send__(results_with_state).map do |result|
    truncate(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.inspect }.join(',') + ')'
        else
          ''
        end
      )
    ) +
    ' ' +
    expectation_level(result) +
    ' ' +
    matcher_with_expected_if_given(result) +
    returned_value(result)
  end
end

#statisticsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fix/test.rb', line 110

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

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

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