template-test 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,12 +9,7 @@ module Template
9
9
  # @see #set
10
10
  class Context
11
11
  attr_accessor :nodes
12
-
13
- def initialize(template_path, &block)
14
- @template_path = template_path
15
- # how to capture local variables
16
- self.instance_eval(&block)
17
- end
12
+ attr_writer :html, :template_path
18
13
 
19
14
  # @return [Nokogiri::HTML] the document that wraps the rendered template
20
15
  # @param [Boolean] reload true if the document should be parsed again
@@ -22,26 +17,30 @@ module Template
22
17
  if reload
23
18
  @document = nil
24
19
  end
25
- @document ||= Nokogiri::HTML(render())
20
+ @document ||= Nokogiri::HTML(html())
26
21
  end
27
22
 
28
- # Evaluates the given block of test code in the context
29
- # of the document which wraps the rendered template.
30
- # The nodes retrieved by the given xpath expression can
31
- # be accessed through the 'nodes' method.
23
+ # @return [String] the rendered template
24
+ def html()
25
+ @html ||= render();
26
+ end
27
+
28
+ # Searches the rendered HTML document for the given
29
+ # XPATH query. In the given block the result of the
30
+ # xpath query is available through the 'nodes' instance variable.
32
31
  # @param [String] xpath an XPATH search expression
33
32
  # @param [Proc] block the testing code
34
- # @see {file:spec/erb_spec.rb}
33
+ # @example see {file:spec/erb_spec.rb}.
35
34
  def xpath(xpath, &block)
36
35
  @xpath = xpath
37
36
  @nodes = document().xpath(@xpath)
38
37
  self.instance_eval(&block)
39
38
  end
40
39
 
41
- # Creates an instance variable which is available in the rendered template.
40
+ # Assigns an instance variable which is available when the template is rendered.
42
41
  # @param [Symbol] symbol the name of the instance variable
43
42
  # @param [Object] value the value of the instance variable
44
- # @see {file:spec/erb_spec.rb}
43
+ # @example see {file:spec/erb_spec.rb}.
45
44
  def set(symbol, value)
46
45
  sym = "@#{symbol.to_s}".to_sym
47
46
  instance_variable_set(sym, value)
@@ -50,7 +49,7 @@ module Template
50
49
  end
51
50
  end
52
51
 
53
- # Renders the template in this context. All variables defined by #set
52
+ # Renders the template. All variables defined by @see #set
54
53
  # are available in the template. They can be accessed as instance
55
54
  # variables or by using the attribute accessor.
56
55
  # @return [String] the rendered template
@@ -86,10 +85,25 @@ module Template
86
85
  # Runs the test code in the provided block for the specified template.
87
86
  # @param [String] template_path the path to the template
88
87
  # @param [Proc] block a block with the template testing code
89
- # @see {file:spec/erb_spec.rb}
90
88
  # @return [Context] the template test context
89
+ # @example see {file:spec/erb_spec.rb}.
91
90
  def template(template_path, &block)
92
- Context.new(template_path, &block)
91
+ context = Context.new
92
+ context.template_path = template_path
93
+ context.instance_eval &block
94
+ context
95
+ end
96
+
97
+ # Runs the test code in the provided block for the specified rendered HTML.
98
+ # @param [String] html the rendered HTML content
99
+ # @param [Proc] block a block with template testing code
100
+ # @return [Context] the template test context
101
+ # @example see {file:spec/html_spec.rb}
102
+ def html(html, &block)
103
+ context = Context.new
104
+ context.html = html
105
+ context.instance_eval &block
106
+ context
93
107
  end
94
108
  end
95
109
  end
@@ -1,5 +1,5 @@
1
1
  module Template
2
2
  module Test
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "test custom rendered HTML" do
4
+ it "should test custom rendered HTML" do
5
+
6
+ # render the template manually
7
+ @title = "the title"
8
+ @content = "the content"
9
+ template = ERB.new File.read("spec/template/form.html.erb")
10
+ body = template.result(binding())
11
+
12
+ # inject the rendered template into the context
13
+ html body do
14
+ # check if the rendered template was properly injected
15
+ @html.should == body
16
+ html().should == body
17
+
18
+ # and query on the injected rendered template
19
+ xpath "//input[@name='title']" do
20
+ nodes.length.should == 1
21
+ nodes[0].attr("text").should == @title
22
+ end
23
+ xpath "//textarea[@name='content']" do
24
+ nodes.length.should == 1
25
+ # FIXME why is the @content not available here ?
26
+ nodes[0].text().should == "the content"
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: template-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-29 00:00:00.000000000 Z
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -127,6 +127,7 @@ files:
127
127
  - lib/template-test/version.rb
128
128
  - spec/erb_spec.rb
129
129
  - spec/haml_spec.rb
130
+ - spec/html_spec.rb
130
131
  - spec/spec_helper.rb
131
132
  - spec/template/form.html.erb
132
133
  - spec/template/form.html.haml
@@ -159,7 +160,9 @@ summary: Simple DSL for HTML template (ERB,HAML) testing
159
160
  test_files:
160
161
  - spec/erb_spec.rb
161
162
  - spec/haml_spec.rb
163
+ - spec/html_spec.rb
162
164
  - spec/spec_helper.rb
163
165
  - spec/template/form.html.erb
164
166
  - spec/template/form.html.haml
165
167
  - spec/template/form_local.html.erb
168
+ has_rdoc: