-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectation.rb
71 lines (55 loc) · 1.46 KB
/
expectation.rb
1
2
3
4
5
6
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class ExpectationTo
def initialize(value)
@value = value
end
def be(expected)
unless @value == expected
puts "Expected #{@value} to be #{expected}"
return
end
puts 'Test passed'
end
# Avaliando se existe método correspondente no @value
def method_missing(method_name, *args)
if method_name.to_s.start_with?('be_') #
value_method = (method_name.to_s[3..-1] + '?').to_sym
unless @value.respond_to?(value_method) #
puts "#{@value.inspect} does not respond to ##{value_method}"
return
end
unless @value.send(value_method, *args) == true #
puts "Expected #{@value.inspect} to be #{value_method.to_s[0..-2]}"
return
end
puts 'Test passed' #
else
super
end
end
end
class Expectation
def initialize(value)
@value = value
end
def to
ExpectationTo.new(@value)
end
end
def expect(value)
Expectation.new(value)
end
# ------------------------------
expect(1).to.be(1) # Test passed
# Baseado no método #nil? do Ruby
expect('').to.be_nil # Gera um erro
expect(nil).to.be_nil # Test passed
class Object
def present? # Simulando o método #present? do Rails
true
end
end
expect('').to.be_present # Test passed
expect('hello world').to.be_greeting # Gera um erro
expect('hello world').to.foo # Gera um erro
# Acredito que para separar o .to do .be(como no RSpec) é necessário
# um nível maior ainda de abstração e metaprogramação