-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.rb
52 lines (42 loc) · 1.11 KB
/
interpreter.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
# Design pattern: interpreter
# Description: Given a language, define a representation for its grammar along
# with an interpreter that uses the representation to interpret sentences in the language.
# Ruby allows easy creation of DSL without overhead of building interpreter
# Example of implementing test specification syntax (Rspec) see below
class Object
def describe(description, &block)
block.call
end
alias it describe
def expect(obj)
Expectation.new(obj)
end
def be(bool)
[:===, bool]
end
def equal(value)
[:==, value]
end
# greater_or_equal_thand
end
class Expectation
def initialize(obj)
@obj = obj
end
def to(array)
method, value = array
method_description = {:== => "be equal", :=== => "be"}[method]
result = @obj.send(method, value) ? "OK" : "Failed"
puts "Expected #{@obj} to #{method_description} #{value} - #{result}!"
end
end
describe "test specification" do
it "passes!" do
expect(true).to be true
# => Expected true to be true - OK!
end
it "fails!" do
expect(1).to equal 2
# => Expected 1 to be equal 2 - Failed!
end
end