Skip to content

Commit

Permalink
initial tests for DRYML using Cucumber and Aruba
Browse files Browse the repository at this point in the history
  • Loading branch information
al2o3cr committed Nov 22, 2011
1 parent 4aec06f commit 207429d
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ hobo/taglib-docs
*/pkg

dryml/lib/doc
dryml/tmp
doc
dryml/doc
.yardoc
6 changes: 6 additions & 0 deletions dryml/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

gem 'hobo_support', :path => '../hobo_support'

gemspec

2 changes: 2 additions & 0 deletions dryml/dryml.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('actionpack', ["~> 3.1.0"])
s.add_runtime_dependency('hobo_support', ["= #{version}"])
s.add_development_dependency('rubydoctest', [">= 0"])
s.add_development_dependency('cucumber', '~> 1.1.0')
s.add_development_dependency('aruba', '~> 0.4.6')

s.files = `git ls-files -x #{name}/* -z`.split("\0")

Expand Down
135 changes: 135 additions & 0 deletions dryml/features/doctest_examples.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Feature: Doctest examples

Scenario: plain text
Given a file named "doctest.dryml" with:
"""
hi
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
hi
"""

Scenario: single ERB output tag
Given a file named "doctest.dryml" with:
"""
<%= this %>
"""
And the local variable "this" has the value "hello"
When I render "doctest.dryml"
Then the output DOM should be:
"""
hello
"""

Scenario: if-else
Given a file named "doctest.dryml" with:
"""
<if test="&true">
Hi
</if>
<else>
Bye
</else>
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
Hi
"""

Scenario: repeating tags
Given a file named "doctest.dryml" with:
"""
<repeat with="&[1,2,3]">
<span><%= this %></span>
</repeat>
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
<span>1</span>
<span>2</span>
<span>3</span>
"""

Scenario: defining a tag with a default parameter
Given a file named "doctest.dryml" with:
"""
<def tag="myp">
<p param="default"/>
</def>
<myp>Hi</myp>
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
<p>
Hi
</p>
"""

Scenario: calling a tag using call-tag
Given a file named "doctest.dryml" with:
"""
<def tag="myp">
<p param="default" />
</def>
<call-tag tag="myp">
Hi
</call-tag>
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
<p>
Hi
</p>
"""

Scenario: wrapping content with a custom tag
Given a file named "doctest.dryml" with:
"""
<def tag="myp">
<p param="default" />
</def>
<wrap tag="myp" when="&true">
img
</wrap>
"""
When I render "doctest.dryml"
Then the output DOM should be:
"""
<p>
img
</p>
"""

Scenario: extending a tag
Given a file named "doctest_taglib.dryml" with:
"""
<def tag="myp">
<p param="default" />
</def>
"""
And a file named "doctest_extend.dryml" with:
"""
<extend tag="myp">
<old-myp merge>
<default: replace>Hello <default restore /></default:>
</old-myp>
</extend>
"""
And a file named "doctest.dryml" with:
"""
<myp>New World</myp>
"""
When I include the taglib "doctest_taglib"
And I include the taglib "doctest_extend"
When I render "doctest.dryml"
Then the output DOM should be:
"""
<p>Hello New World</p>
"""

35 changes: 35 additions & 0 deletions dryml/features/static_tags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Feature: Static tags render correctly

Scenario: Static tag with static attributes

Given a file named "static_tag.dryml" with:
"""
<div class="foo">
FOO
</div>
"""
When I render "static_tag.dryml"
Then the output DOM should be:
"""
<div class="foo">
FOO
</div>
"""

Scenario: Static tag with an interpolated attribute

Given a file named "static_tag.dryml" with:
"""
<% some_var = 'foo' %>
<div class="#{some_var}">
FOO
</div>
"""
When I render "static_tag.dryml"
Then the output DOM should be:
"""
<div class="foo">
FOO
</div>
"""

10 changes: 10 additions & 0 deletions dryml/features/step_definitions/dom_comparison.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Then /^the output DOM should be:$/ do |expected_html|
expected_rendered = ''
actual_rendered = ''
formatter = StripFormatter.new
# wrap in html tag, as REXML gets grumpy if there's more than one root node
formatter.write(REXML::Document.new("<html>#{expected_html}</html>"), expected_rendered)
formatter.write(REXML::Document.new("<html>#{@rendered_dom}</html>"), actual_rendered)
actual_rendered.should eq(expected_rendered)
end

21 changes: 21 additions & 0 deletions dryml/features/step_definitions/rendering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
When /^I render "([^"]*)"$/ do |file|
file_path = aruba_path(file)
file_data = File.read(file_path)
@locals ||= {}
@taglibs ||= []
@rendered_dom = Dryml.render(file_data, @locals, file_path, @taglibs)
Dryml::Template.clear_build_cache
end

When /^I include the taglib "([^"]*)"$/ do |file|
file_path = aruba_path(file+".dryml")
template_dir = File.dirname(file_path)
@taglibs ||= []
@taglibs << { :src => file, :absolute_template_path => template_dir }
end

Given /^the local variable "([^"]*)" has the value "([^"]*)"$/ do |var, value|
@locals ||= {}
@locals[var.to_sym] = value
end

25 changes: 25 additions & 0 deletions dryml/features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
lib = File.expand_path('../../../lib', __FILE__)
$:.unshift lib unless $:.include?(lib)

require 'bundler'
Bundler.setup
require 'aruba/cucumber'

require 'active_support'
require 'action_view'
require 'action_controller'

require 'dryml'
require 'dryml/railtie/template_handler'

def aruba_path(file_or_dir)
File.expand_path("../../../tmp/aruba/#{file_or_dir}", __FILE__)
end

# stub this
module Hobo
def self.root
'no_such_path'
end
end

14 changes: 14 additions & 0 deletions dryml/features/support/strip_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rexml/document'
require 'rexml/formatters/default'

# strip whitespace when formatting XML

class StripFormatter < REXML::Formatters::Default

protected

def write_text(node, output)
output << node.to_s.strip
end

end
2 changes: 1 addition & 1 deletion dryml/lib/dryml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def render(template_src, locals={}, template_path=nil, included_taglibs=[], view
this = locals.delete(:this) || nil

renderer_class = Dryml::Template.build_cache[template_path]._?.environment ||
make_renderer_class(template_src, template_path, locals.keys)
make_renderer_class(template_src, template_path, locals.keys, included_taglibs)
renderer_class.new(view).render_page(this, locals)
end

Expand Down

2 comments on commit 207429d

@bryanlarsen
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still dislike Cucumber, but I must admit that Aruba is pretty cool. Thanks, Matt.

@al2o3cr
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aruba came to mind mostly for avoiding the "can't extend tags in view files" problem; taglibs seem to be really firmly rooted in the filesystem.

One note: the last test in doctest_examples doesn't pass. The default param replace doesn't do anything. I've tried with other param names and it doesn't work the same way as it does in live DRYML, so there's also a test env issue there. More updates as I keep digging.

Please sign in to comment.