-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec_modoki.rb
More file actions
93 lines (75 loc) · 2.1 KB
/
spec_modoki.rb
File metadata and controls
93 lines (75 loc) · 2.1 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
## RSpecのようなRubyベースのDSLライブラリの簡単な構成例
##
## このコードの前半部分では、RSpecもどきの簡単なDSLライブラリを
## プログラムしています。後半部分は、前半で作ったライブラリを
## 使ってDSLを記述しています。
##
## (注) DSL = Domain Specific Language
## (注) このコードは、このままでは実用性はゼロです
##########################################################
###### 以下、RSpecもどきのDSLライブラリのプログラム ######
class TinySpec
@@instance = nil
def TinySpec.instance
@@instance ||= TinySpec.new
end
def initialize
@stack = []
@results = []
end
def describe(desc, &block)
@stack.push("describe #{desc}")
block.call
@stack.pop
end
def context(cont, &block)
@stack.push("context #{cont}")
block.call
@stack.pop
end
def it(str, &block)
@stack.push("it #{str}")
value = block.call
name = @stack.join(', ')
@stack.pop
result = "#{name} => #{value}"
@results.push(result)
result
end
end
def describe(desc, &block)
TinySpec.instance.describe(desc, &block)
end
def context(cont, &block)
TinySpec.instance.context(cont, &block)
end
def it(str, &block)
result = TinySpec.instance.it(str, &block)
puts result
end
############################################
###### 以下、RSpecもどきDSLによる記述 ######
describe "Swift" do
context "of software engineering" do
it "is a programming language" do
true
end
it "is a kind of swallow" do
false
end
end
context "of biology" do
it "is a programming language" do
false
end
it "is a kind of swallow" do
true
end
end
end
######################
###### 実行結果 ######
## describe Swift, context of software engineering, it is a programming language => true
## describe Swift, context of software engineering, it is a kind of swallow => false
## describe Swift, context of biology, it is a programming language => false
## describe Swift, context of biology, it is a kind of swallow => true