template-test 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,2 @@
1
+ require 'autotest/fsevent'
2
+ require 'autotest/growl'
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format d
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in template-test.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ruben Jenster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Template::Test
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'template-test'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install template-test
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
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
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/TODO ADDED
@@ -0,0 +1 @@
1
+ - allow nesting of xpath expressions?
@@ -0,0 +1,5 @@
1
+ module Template
2
+ module Test
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,90 @@
1
+ require "template-test/version"
2
+ require 'nokogiri'
3
+
4
+ module Template
5
+ module Test
6
+ class Context
7
+ attr_accessor :nodes
8
+
9
+ def initialize(template_path, &block)
10
+ @template_path = template_path
11
+ # how to capture local variables
12
+ self.instance_eval(&block)
13
+ end
14
+
15
+ # @return the parsed Nokogiri::HTML document that wraps the rendered template
16
+ # @param [Boolean] reload true if the document should be parsed again
17
+ def document(reload = false)
18
+ if reload
19
+ @document = nil
20
+ end
21
+ @document ||= Nokogiri::HTML(render())
22
+ end
23
+
24
+ # Evaluates the given block of test code in the context
25
+ # of the document which wraps the rendered template.
26
+ # The nodes retrieved by the given xpath expression can
27
+ # be accessed through the 'nodes' method.
28
+ # @param xpath an XPATH search expression
29
+ # @param block the testing code
30
+ # @example see #spec/erb_spec.rb
31
+ def xpath(xpath, &block)
32
+ @xpath = xpath
33
+ @nodes = document().xpath(@xpath)
34
+ self.instance_eval(&block)
35
+ end
36
+
37
+ # Creates an instance variable which is available in the rendered template.
38
+ # @param [Symbol] symbol the name of the instance variable
39
+ # @param [Object] value the value of the instance variable
40
+ # @example see #spec/erb_spec.rb
41
+ def set(symbol, value)
42
+ sym = "@#{symbol.to_s}".to_sym
43
+ instance_variable_set(sym, value)
44
+ self.send(:define_singleton_method, symbol) do
45
+ instance_variable_get(sym)
46
+ end
47
+ end
48
+
49
+ # Renders the template in this context. All variables defined by #set
50
+ # are available in the template. They can be accessed as instance
51
+ # variables or by using the attribute accessor.
52
+ # @return [String] the rendered template
53
+ def render
54
+ case @template_path
55
+ when /\.erb$/
56
+ render_erb(@template_path)
57
+ when /\.haml$/
58
+ render_haml(@template_path)
59
+ else
60
+ raise StandardError, "Unsupported template #{@template_path}"
61
+ end
62
+ end
63
+
64
+ alias :assign :set
65
+
66
+ private
67
+
68
+ def render_erb(template_name)
69
+ require 'erb'
70
+ template = ERB.new File.read("#{@template_path}")
71
+ # render the template in the document context
72
+ template.result(binding())
73
+ end
74
+
75
+ def render_haml(template_name)
76
+ require 'haml'
77
+ engine = Haml::Engine.new(File.read("#{@template_path}"))
78
+ engine.render(binding())
79
+ end
80
+ end
81
+
82
+ # 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 containing the template testing code
85
+ # @example see #spec/erb_spec.rb
86
+ def template(template_path, &block)
87
+ Context.new(template_path, &block)
88
+ end
89
+ end
90
+ end
data/spec/erb_spec.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "testing ERB templates" do
4
+ it "should assign variables and evaluate xpath expressions" do
5
+ template "spec/template/form.html.erb" do
6
+ assign :title, "this is the title"
7
+ assign :content, "this is the content"
8
+
9
+ xpath "//input[@name='title']" do
10
+ nodes.count.should == 1
11
+ nodes[0].attr("value").should == title
12
+ end
13
+
14
+ xpath "//textarea[@name='content']" do
15
+ nodes.count.should == 1
16
+ nodes[0].text.should == content
17
+ end
18
+ end
19
+ end
20
+
21
+ it "should assign variables and evaluate xpath expressions" do
22
+ template "spec/template/form_local.html.erb" do
23
+ assign :title, "this is the title"
24
+ assign :content, "this is the content"
25
+
26
+ xpath "//input[@name='title']" do
27
+ nodes.count.should == 1
28
+ nodes[0].attr("value").should == title
29
+ end
30
+
31
+ xpath "//textarea[@name='content']" do
32
+ nodes.count.should == 1
33
+ nodes[0].text.should == content
34
+ end
35
+ end
36
+ end
37
+ end
data/spec/haml_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "testing HAML templates" do
4
+ it "should assign variables and evaluate xpath expressions" do
5
+ template "spec/template/form.html.haml" do
6
+ # set instance variables required by the template
7
+ assign :title, "this is the title"
8
+ assign :content, "this is the content"
9
+
10
+ xpath "//input[@name='title']" do
11
+ nodes.count.should == 1
12
+ nodes[0].attr("value").should == title
13
+ end
14
+
15
+ xpath "//textarea[@name='content']" do
16
+ nodes.count.should == 1
17
+ nodes[0].text.should == content
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "rspec"
3
+ require File.dirname(__FILE__) + "/../lib/template-test"
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ config.include Template::Test
8
+ end
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head></head>
3
+ <body>
4
+ <form action="/" method="post">
5
+ <input type="text" name="title" value="<%= @title %>">
6
+ <textarea name="content"><%= @content %></textarea>
7
+ </form>
8
+ </body>
9
+ </html>
@@ -0,0 +1,6 @@
1
+ %html
2
+ %head
3
+ %body
4
+ %form{:action => '/', :method => "post"}
5
+ %input{:type => "text", :name => "title", :value => @title}
6
+ %textarea{:name => "content"} #{@content}
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head></head>
3
+ <body>
4
+ <form action="/" method="post">
5
+ <input type="text" name="title" value="<%= title %>">
6
+ <textarea name="content"><%= content %></textarea>
7
+ </form>
8
+ </body>
9
+ </html>
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/template-test/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ruben Jenster"]
6
+ gem.email = ["r@j5r.eu"]
7
+ gem.description = %q{Provides a simple DSL to test the rendering of HTML templates
8
+ defined in ERB or HAML using XPATH expressions}
9
+ gem.summary = %q{Simple DSL for HTML template (ERB,HAML) testing}
10
+ gem.homepage = "https://github.com/r10r/template-test"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "template-test"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Template::Test::VERSION
18
+ gem.add_dependency 'rspec'
19
+ gem.add_dependency 'nokogiri'
20
+ gem.add_dependency 'haml'
21
+ gem.add_development_dependency 'autotest'
22
+ gem.add_development_dependency 'autotest-fsevent'
23
+ gem.add_development_dependency 'autotest-growl'
24
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: template-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ruben Jenster
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: haml
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: autotest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: autotest-fsevent
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: autotest-growl
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! "Provides a simple DSL to test the rendering of HTML templates\n defined
111
+ in ERB or HAML using XPATH expressions"
112
+ email:
113
+ - r@j5r.eu
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .autotest
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - TODO
126
+ - lib/template-test.rb
127
+ - lib/template-test/version.rb
128
+ - spec/erb_spec.rb
129
+ - spec/haml_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/template/form.html.erb
132
+ - spec/template/form.html.haml
133
+ - spec/template/form_local.html.erb
134
+ - template-test.gemspec
135
+ homepage: https://github.com/r10r/template-test
136
+ licenses: []
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 1.8.22
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Simple DSL for HTML template (ERB,HAML) testing
159
+ test_files:
160
+ - spec/erb_spec.rb
161
+ - spec/haml_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/template/form.html.erb
164
+ - spec/template/form.html.haml
165
+ - spec/template/form_local.html.erb