aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Ruby/test/unit/test-template.rb
blob: cf9c7c0678063818cd28220b3447caf6a32176c9 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/ruby
# encoding: utf-8

require 'spec'
require 'antlr3/template'
require 'antlr3/util'

include ANTLR3
MethodDescription = Struct.new( :name, :body, :arguments )
TEMPLATE_NAMES = %w( method class_definition attribute )
SAMPLE_GROUP_FILE = File.join(
  File.dirname( __FILE__ ), 'sample-input', 'template-group'
)

describe Template::Context do
  example "creating an empty context" do
    context = Template::Context.new
    context.instance_variables.should be_empty
  end
  
  example "creating a context with a variable map" do
    context = Template::Context.new(
      :a => 1, :b => 2
    )
    
    vars = context.instance_variables.map { | i | i.to_s }
    vars.should include( '@a' )
    vars.should include( '@b' )
    
    context.instance_variable_get( '@a' ).should == 1
    context.instance_variable_get( '@b' ).should == 2
  end
  
  example "fetching variable values from []" do
    context = Template::Context.new(
      :a => 1, :b => 2
    )
    
    context[ :a ].should == 1
    context[ 'a' ].should == 1
    context[ :b ].should == 2
    context[ 'b' ].should == 2
  end
  
  example "defining variables with []=" do
    context = Template::Context.new( :a => 3 )
    context[ :a ] = 1
    context[ 'b' ] = 2
    
    context.instance_variable_get( '@a' ).should == 1
    context.instance_variable_get( '@b' ).should == 2
  end
  
  example "using method missing to assign values" do
    context = Template::Context.new( :a => 3 )
    context.a = 1
    context.b = 2
    
    context.instance_variable_get( '@a' ).should == 1
    context.instance_variable_get( '@b' ).should == 2
  end
  
  example "using method missing to get variable values" do
    context = Template::Context.new( :a => 1, :b => 2)
    
    context.a.should == 1
    context.b.should == 2
  end
  
end


shared_examples_for "template groups" do
  include ANTLR3::Util
  
  example "template definitions" do
    templates = @group.templates
    templates.should_not be_empty
    templates.should equal @group::TEMPLATES
    
    names = templates.keys
    
    names.should have(3).things
    for template_name in TEMPLATE_NAMES
      names.should include template_name
      template_class = templates[ template_name ]
      template_class.should be_a_kind_of Class
      template_class.superclass.should equal Template::Context
      template_class.should be < @group # it should include the group module
    end
  end
  
  example "template_defined?( name ) should verify whether a template is defined in a group" do
    for name in TEMPLATE_NAMES
      @group.template_defined?( name ).should be_true
      @group.template_defined?( name.to_s ).should be_true
    end
    
    @group.template_defined?( :something_else ).should be_false
  end
  
  example "template method definitions" do
    for name in TEMPLATE_NAMES
      @group.should respond_to( name )
      @group.should respond_to( "#{ name }!" )
      if RUBY_VERSION =~ /^1\.9/
        @group.private_instance_methods.should include name.to_sym
        @group.private_instance_methods.should include :"#{ name }!"
      else
        @group.private_instance_methods.should include name.to_s
        @group.private_instance_methods.should include "#{ name }!"
      end
    end
  end
  
  example "template method operation" do
    value = @group.class_definition
    value.should be_a_kind_of Template::Context
    
    value = @group.class_definition!
    value.should be_a_kind_of String
    
    value = @group.attribute( :name => 'a' )
    value.should be_a_kind_of Template::Context
  end
end

describe Template::Group, "dynamic template definition" do
  include ANTLR3::Util
  
  before :each do
    @group = Template::Group.new do
      extend ANTLR3::Util
      define_template( :class_definition, tidy( <<-'END'.chomp ) )
      | class <%= @name %><% if @superclass %> < <%= @superclass %><% end %>
      | % if @attributes
      | 
      | % for attr, access in @attributes 
      | <%= attribute( :name => attr, :access => ( access || 'rw' ) ).to_s.chomp %>
      | % end
      | % end
      | % if @methods
      | % for method in ( @methods || [] )
      | <%= method( method ) %>
      | % end
      | % end
      | end
      END
      
      define_template( :attribute, tidy( <<-'END'.chomp ) )
      | % case @access.to_s.downcase
      | % when 'r'
      |   attr_reader :<%= @name %>
      | % when 'w'
      |   attr_writer :<%= @name %>
      | % else
      |   attr_accessor :<%= @name %>
      | % end
      END
      
      define_template( :method, tidy( <<-'END'.chomp ) )
      |   
      |   def <%= @name %><% if @arguments and not @arguments.empty? %>( <%= @arguments.join( ', ' ) %> )<% end %>
      | <%= @body.gsub( /^/, '    ' ) %>
      |   end
      END
    end
  end
  
  it_should_behave_like "template groups"
  
  example "template object string rendering" do
    attributes = [
      %w( family ),
      %w( name r )
    ]
          
    methods = [
      MethodDescription.new( 'eat', %q[puts( "ate %s %s" % [ number, @name ] )], %w( number ) ),
      MethodDescription.new( :to_s, '@name.to_s.dup' )
    ]
    
    vegetable = @group.class_definition(
      :name => 'Vegetable',
      :superclass => 'Food',
      :attributes => attributes,
      :methods => methods
    )
    
    vegetable.to_s.should == tidy( <<-END.chomp )
    | class Vegetable < Food
    |
    |   attr_accessor :family
    |   attr_reader :name
    |   
    |   def eat( number )
    |     puts( "ate %s %s" % [ number, @name ] )
    |   end
    |   
    |   def to_s
    |     @name.to_s.dup
    |   end
    | end
    END
  end
end

describe Template::Group, "loading a template definition file" do
  
  before :each do
    @group = Template::Group.load( SAMPLE_GROUP_FILE )
  end
  
  it_should_behave_like "template groups"
  
  example "template object string rendering" do
    attributes = [
      %w( family ),
      %w( name r )
    ]
          
    methods = [
      MethodDescription.new( 'eat', %q[puts( "ate %s %s" % [ number, @name ] )], %w( number ) ),
      MethodDescription.new( :to_s, '@name.to_s.dup' )
    ]
    
    vegetable = @group.class_definition(
      :name => 'Vegetable',
      :superclass => 'Food',
      :attributes => attributes,
      :methods => methods
    )
    
    vegetable.to_s.should == tidy( <<-END.chomp )
    | class Vegetable < Food
    |
    |   attr_accessor :family
    |   attr_reader :name
    |   
    |   def eat( number )
    |     puts( "ate %s %s" % [ number, @name ] )
    |   end
    |   
    |   def to_s
    |     @name.to_s.dup
    |   end
    | end
    END
  end
end