template-test 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +10 -15
- data/lib/template-test/version.rb +1 -1
- data/lib/template-test.rb +13 -8
- metadata +1 -1
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Template::Test
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Template::Test provides a simple DSL to test the rendering of HTML templates
|
|
4
|
+
defined in ERB or HAML using XPATH expressions.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -8,22 +9,16 @@ Add this line to your application's Gemfile:
|
|
|
8
9
|
|
|
9
10
|
gem 'template-test'
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
## Using with RSpec
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
In your `test_helper.rb` include the `Template::Test module in the rspec config:
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
require 'template-test'
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
RSpec.configure do |config|
|
|
19
|
+
config.include Template::Test
|
|
20
|
+
end
|
|
18
21
|
|
|
19
|
-
##
|
|
22
|
+
## Examples
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Contributing
|
|
24
|
-
|
|
25
|
-
1. Fork it
|
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
-
5. Create new Pull Request
|
|
24
|
+
Have a look at the `*_spec.rb` files in the `spec` folder.
|
data/lib/template-test.rb
CHANGED
|
@@ -3,6 +3,10 @@ require 'nokogiri'
|
|
|
3
3
|
|
|
4
4
|
module Template
|
|
5
5
|
module Test
|
|
6
|
+
# Class that holds the context for a template
|
|
7
|
+
# Variables used by the template can be added to the
|
|
8
|
+
# context using the set method.
|
|
9
|
+
# @see #set
|
|
6
10
|
class Context
|
|
7
11
|
attr_accessor :nodes
|
|
8
12
|
|
|
@@ -12,7 +16,7 @@ module Template
|
|
|
12
16
|
self.instance_eval(&block)
|
|
13
17
|
end
|
|
14
18
|
|
|
15
|
-
# @return
|
|
19
|
+
# @return [Nokogiri::HTML] the document that wraps the rendered template
|
|
16
20
|
# @param [Boolean] reload true if the document should be parsed again
|
|
17
21
|
def document(reload = false)
|
|
18
22
|
if reload
|
|
@@ -25,9 +29,9 @@ module Template
|
|
|
25
29
|
# of the document which wraps the rendered template.
|
|
26
30
|
# The nodes retrieved by the given xpath expression can
|
|
27
31
|
# be accessed through the 'nodes' method.
|
|
28
|
-
# @param xpath an XPATH search expression
|
|
29
|
-
# @param block the testing code
|
|
30
|
-
# @
|
|
32
|
+
# @param [String] xpath an XPATH search expression
|
|
33
|
+
# @param [Proc] block the testing code
|
|
34
|
+
# @see {file:spec/erb_spec.rb}
|
|
31
35
|
def xpath(xpath, &block)
|
|
32
36
|
@xpath = xpath
|
|
33
37
|
@nodes = document().xpath(@xpath)
|
|
@@ -37,7 +41,7 @@ module Template
|
|
|
37
41
|
# Creates an instance variable which is available in the rendered template.
|
|
38
42
|
# @param [Symbol] symbol the name of the instance variable
|
|
39
43
|
# @param [Object] value the value of the instance variable
|
|
40
|
-
# @
|
|
44
|
+
# @see {file:spec/erb_spec.rb}
|
|
41
45
|
def set(symbol, value)
|
|
42
46
|
sym = "@#{symbol.to_s}".to_sym
|
|
43
47
|
instance_variable_set(sym, value)
|
|
@@ -80,9 +84,10 @@ module Template
|
|
|
80
84
|
end
|
|
81
85
|
|
|
82
86
|
# Runs the test code in the provided block for the specified template.
|
|
83
|
-
# @param template_path the path to the template
|
|
84
|
-
# @param block a block
|
|
85
|
-
# @
|
|
87
|
+
# @param [String] template_path the path to the template
|
|
88
|
+
# @param [Proc] block a block with the template testing code
|
|
89
|
+
# @see {file:spec/erb_spec.rb}
|
|
90
|
+
# @return [Context] the template test context
|
|
86
91
|
def template(template_path, &block)
|
|
87
92
|
Context.new(template_path, &block)
|
|
88
93
|
end
|