-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcomponent_compiler_spec.rb
115 lines (92 loc) · 3.33 KB
/
component_compiler_spec.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
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
109
110
111
112
113
114
115
describe Curly::ComponentCompiler do
describe ".compile" do
let(:presenter_class) do
Class.new do
def title
"Welcome!"
end
def i18n(key, fallback: nil)
case key
when "home.welcome" then "Welcome to our lovely place!"
else fallback
end
end
def collected(**options)
options.to_a.map { |(k, v)| "#{k}: #{v}" }.join("\n")
end
def summary(length = "long")
case length
when "long" then "This is a long summary"
when "short" then "This is a short summary"
end
end
def invalid(x, y)
end
def widget(size:, color: nil)
s = "Widget (#{size})"
s << " - #{color}" if color
s
end
def form(&block)
"some form"
end
def self.component_available?(name)
true
end
end
end
it "compiles components with identifiers" do
expect(evaluate("i18n.home.welcome")).to eq "Welcome to our lovely place!"
end
it "compiles components with optional identifiers" do
expect(evaluate("summary")).to eq "This is a long summary"
expect(evaluate("summary.short")).to eq "This is a short summary"
end
it "compiles components with attributes" do
expect(evaluate("widget size=100px")).to eq "Widget (100px)"
end
it "compiles components with collected attributes" do
expect(evaluate("collected class=test for=you")).to eq "class: test\nfor: you"
end
it "compiles components with optional attributes" do
expect(evaluate("widget color=blue size=50px")).to eq "Widget (50px) - blue"
end
it "compiles context block components" do
expect(evaluate("form", type: :context)).to eq "some form"
end
it "allows both identifier and attributes" do
expect(evaluate("i18n.hello fallback=yolo")).to eq "yolo"
end
it "fails when an invalid attribute is used" do
expect { evaluate("i18n.foo extreme=true") }.to raise_exception(Curly::Error)
end
it "fails when a component is missing a required identifier" do
expect { evaluate("i18n") }.to raise_exception(Curly::Error)
end
it "fails when a component is missing a required attribute" do
expect { evaluate("widget") }.to raise_exception(Curly::Error)
end
it "fails when an identifier is specified for a component that doesn't support one" do
expect { evaluate("title.rugby") }.to raise_exception(Curly::Error)
end
it "fails when the method takes more than one argument" do
expect { evaluate("invalid") }.to raise_exception(Curly::Error)
end
it "fails when a context block component is used with a method that doesn't take a block" do
expect { evaluate("title", type: :context) }.to raise_exception(Curly::Error)
end
end
def evaluate(text, type: nil, &block)
name, identifier, attributes = Curly::ComponentScanner.scan(text)
component = Curly::Parser::Component.new(name, identifier, attributes)
code = Curly::ComponentCompiler.compile(presenter_class, component, type: type)
presenter = presenter_class.new
context = double("context", presenter: presenter)
context.instance_eval(<<-RUBY)
def self.render
#{code}
end
RUBY
context.render(&block)
end
end