Class: Probatio::Context

Inherits:
Object
  • Object
show all
Includes:
Helpers, Waiters
Defined in:
lib/probatio/assertions.rb,
lib/probatio.rb,
lib/probatio/helpers.rb,
lib/probatio/waiters.rb

Overview

probatio/assertions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Waiters

#monow, #wait_until

Methods included from Helpers

#beep

Constructor Details

#initialize(group, test) ⇒ Context

Returns a new instance of Context.



677
678
679
680
681
682
683
# File 'lib/probatio.rb', line 677

def initialize(group, test)

  @__group = group
  @__test = test

  group.context.each { |k, v| instance_variable_set(k, v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Checks whether its “_assert_something”, if that’s the case, just flags the assertion as :pending an moves on



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/probatio/assertions.rb', line 183

def method_missing(name, *args, &block)

  n = name.to_s

  if n.start_with?('_assert') && self.respond_to?(n[1..-1])

    Probatio.despatch(:assertion_pending, self, @__child)

    :pending

  else

    super
  end
end

Instance Attribute Details

#__groupObject (readonly)

Returns the value of attribute __group.



674
675
676
# File 'lib/probatio.rb', line 674

def __group
  @__group
end

#__testObject (readonly)

Returns the value of attribute __test.



675
676
677
# File 'lib/probatio.rb', line 675

def __test
  @__test
end

Instance Method Details

#__group_nameObject



693
# File 'lib/probatio.rb', line 693

def __group_name; @__group.name; end

#__test_nameObject



692
# File 'lib/probatio.rb', line 692

def __test_name; @__test.name; end

#assert(*as) ⇒ Object

Jack of all trade assert



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/probatio/assertions.rb', line 201

def assert(*as)

  count = {
    rexes: 0, hashes: 0, arrays: 0, strings: 0, scalars: 0, others: 0 }

  as.each { |a|
    k =
      case a
      when Regexp then :rexes
      when Hash then :hashes
      when Array then :arrays
      when String then :strings
      else :others; end
    count[k] = count[k] + 1
    count[:scalars] += 1 if %i[ rexes strings ].include?(k) }

  if as.length == 1 && count[:hashes] == 1

    assert_hashy(*as)

  elsif as.length == 1

    assert_truthy(*as)

  elsif count[:rexes] == 1 && count[:strings] == as.length - 1

    assert_match(*as)

  #elsif count[:arrays] > 0
  #  assert_include(*as)

  else

    assert_equal(*as)
  end
end

#assert_any(*as) ⇒ Object



42
43
44
45
# File 'lib/probatio/assertions.rb', line 42

def assert_any(*as)

  do_assert(as, 'any') { |a| a.respond_to?(:size) && a.size > 0 }
end

#assert_empty(*as) ⇒ Object



47
48
49
50
# File 'lib/probatio/assertions.rb', line 47

def assert_empty(*as)

  do_assert(as, 'empty') { |a| a.respond_to?(:size) && a.size == 0 }
end

#assert_end_with(*as) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/probatio/assertions.rb', line 77

def assert_end_with(*as)

  fail ArgumentError.new(
    "assert_end_with expects strings, not #{x.inspect}") \
      if as.find { |a| ! a.is_a?(String) }

  sta = as.inject { |s, a| s.length < a.length ? s : a }

  do_assert(as.filter { |a| a != sta }, 'ending with') { |a|
    a.end_with?(sta) }
end

#assert_equal(*as) ⇒ Object



52
53
54
55
# File 'lib/probatio/assertions.rb', line 52

def assert_equal(*as)

  do_assert(as, 'equal') { |a| a == as[0] }
end

#assert_error(*as, &block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/probatio/assertions.rb', line 128

def assert_error(*as, &block)

  block = block || as.find { |a| a.is_a?(Proc) }
  as.delete(block)

  fail ArgumentError.new("assert_error expects a block or a proc") \
    unless block

  err = nil;
    begin; block.call; rescue => err; end

  return "no error raised" unless err.is_a?(StandardError)

  s = nil

  as.each do |a|

    s ||=
      case a
      when String
        if err.message != a
          msg = err.message.sub(/\s*Did you mean\?.+/, '')
          "error message #{msg.inspect} is not #{a.inspect}"
        else
          nil
        end
      when Regexp
        ! err.message.match(a) ?
          "error message #{err.message} did not match #{a.inspect}" : nil
      when Module
        ! err.is_a?(a) ?
          "error is of class #{err.class} not #{a.name}" : nil
      else
        fail ArgumentError.new("assert_error cannot fathom #{a.inspect}")
      end
  end

  do_assert_(as) { s }
end

#assert_false(*as) ⇒ Object



37
38
39
40
# File 'lib/probatio/assertions.rb', line 37

def assert_false(*as)

  do_assert(as, 'false') { |a| a == false }
end

#assert_falsey(*as) ⇒ Object Also known as: assert_falsy, assert_falseish



25
26
27
28
# File 'lib/probatio/assertions.rb', line 25

def assert_falsey(*as)

  do_assert(as, 'falsey') { |a| ! a }
end

#assert_hashy(*as) ⇒ Object



114
115
116
117
# File 'lib/probatio/assertions.rb', line 114

def assert_hashy(*as)

  do_assert(as[0].to_a, 'hashy equal') { |k, v| k == v }
end

#assert_include(*as) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/probatio/assertions.rb', line 89

def assert_include(*as)

  ai =
    as.index { |a| a.is_a?(Array) } ||
    fail(ArgumentError.new("assert_include found no array"))

  arr = as.delete_at(ai)

  do_assert(as, 'included') { |e| arr.include?(e) }
end

#assert_instance_of(*as) ⇒ Object Also known as: assert_is_a



119
120
121
122
123
124
125
# File 'lib/probatio/assertions.rb', line 119

def assert_instance_of(*as)

  moc = as.find { |a| a.is_a?(Module) }
  as.delete(moc)

  do_assert(as, 'instance of') { |e| e.is_a?(moc) }
end

#assert_match(*as) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/probatio/assertions.rb', line 57

def assert_match(*as)

  strings, others = as.partition { |a| a.is_a?(String) }
  rex = others.find { |o| o.is_a?(Regexp) } || strings.pop

  do_assert(strings, 'matched') { |s| s.match?(rex) }
end

#assert_nil(*as) ⇒ Object

Beware: ws.any? returns false when ws == [ false ]



9
10
11
12
# File 'lib/probatio/assertions.rb', line 9

def assert_nil(*as)

  do_assert(as, 'nil') { |a| a == nil }
end

#assert_not_error(*as, &block) ⇒ Object Also known as: assert_no_error



168
169
170
171
172
173
174
175
176
177
# File 'lib/probatio/assertions.rb', line 168

def assert_not_error(*as, &block)

  block = block || as.find { |a| a.is_a?(Proc) }

  err = nil;
    begin; block.call; rescue => err; end

  do_assert_([]) {
    err && "no error expected but returned #{err.class} #{err.name}" }
end

#assert_not_nil(*as) ⇒ Object



14
15
16
17
# File 'lib/probatio/assertions.rb', line 14

def assert_not_nil(*as)

  do_assert(as, 'not nil') { |a| a != nil }
end

#assert_size(*as) ⇒ Object Also known as: assert_count



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/probatio/assertions.rb', line 100

def assert_size(*as)

  ai =
    as.index { |a| a.is_a?(Integer) && a >= 0 } ||
    fail(ArgumentError.new("assert_size found no integer >= 0"))

  sz = as.delete_at(ai)

  as = as.collect { |a| a.respond_to?(:size) ? a.size : a.class }

  do_assert(as, "size #{sz}") { |a| a == sz }
end

#assert_start_with(*as) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/probatio/assertions.rb', line 65

def assert_start_with(*as)

  fail ArgumentError.new(
    "assert_start_with expects strings, not #{x.inspect}") \
      if as.find { |a| ! a.is_a?(String) }

  sta = as.inject { |s, a| s.length < a.length ? s : a }

  do_assert(as.filter { |a| a != sta }, 'starting with') { |a|
    a.start_with?(sta) }
end

#assert_true(*as) ⇒ Object



32
33
34
35
# File 'lib/probatio/assertions.rb', line 32

def assert_true(*as)

  do_assert(as, 'true') { |a| a == true }
end

#assert_truthy(*as) ⇒ Object Also known as: assert_trueish



19
20
21
22
# File 'lib/probatio/assertions.rb', line 19

def assert_truthy(*as)

  do_assert(as, 'truthy') { |a| !! a }
end

#blockObject



685
# File 'lib/probatio.rb', line 685

def block; @__block; end

#run(run_opts) ⇒ Object



687
688
689
690
# File 'lib/probatio.rb', line 687

def run(run_opts)

  _run(@__group.arounds + [ :do_run ], run_opts)
end