- Organized the files
  - Cleaned up some of the white spacing issues
  - A lot can still be done to make the tests more readable to the new developers
This commit is contained in:
DBA
2010-08-23 01:30:01 +08:00
committed by Tobias Lütke
parent 01145f872b
commit c72c84ea9b
29 changed files with 587 additions and 582 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), 'test') unless $:.include? File.join(File.dirname(__FILE__), 'test')
require 'rubygems'
require 'rake'
require 'rake/testtask'
@@ -8,7 +10,7 @@ task :default => 'test'
Rake::TestTask.new(:test) do |t|
t.libs << '.' << 'lib' << 'test'
t.pattern = 'test/*_test.rb'
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = false
end
-11
View File
@@ -1,11 +0,0 @@
require File.dirname(__FILE__) + '/helper'
class AssignTest < Test::Unit::TestCase
include Liquid
def test_assigned_variable
assert_template_result('.foo.','{% assign foo = values %}.{{ foo[0] }}.', 'values' => %w{foo bar baz})
assert_template_result('.bar.','{% assign foo = values %}.{{ foo[1] }}.', 'values' => %w{foo bar baz})
end
end
-21
View File
@@ -1,21 +0,0 @@
#!/usr/bin/env ruby
extras_path = File.join File.dirname(__FILE__), 'extra'
$LOAD_PATH.unshift(extras_path) unless $LOAD_PATH.include? extras_path
require 'test/unit'
require 'test/unit/assertions'
require 'caller'
require 'breakpoint'
require File.dirname(__FILE__) + '/../lib/liquid'
module Test
module Unit
module Assertions
include Liquid
def assert_template_result(expected, template, assigns={}, message=nil)
assert_equal expected, Template.parse(template).render(assigns)
end
end
end
end
-129
View File
@@ -1,129 +0,0 @@
require File.dirname(__FILE__) + '/helper'
class TestFileSystem
def read_template_file(template_path)
case template_path
when "product"
"Product: {{ product.title }} "
when "locale_variables"
"Locale: {{echo1}} {{echo2}}"
when "variant"
"Variant: {{ variant.title }}"
when "nested_template"
"{% include 'header' %} {% include 'body' %} {% include 'footer' %}"
when "body"
"body {% include 'body_detail' %}"
when "nested_product_template"
"Product: {{ nested_product_template.title }} {%include 'details'%} "
when "recursively_nested_template"
"-{% include 'recursively_nested_template' %}"
when "pick_a_source"
"from TestFileSystem"
else
template_path
end
end
end
class OtherFileSystem
def read_template_file(template_path)
'from OtherFileSystem'
end
end
class IncludeTagTest < Test::Unit::TestCase
include Liquid
def setup
Liquid::Template.file_system = TestFileSystem.new
end
def test_include_tag_looks_for_file_system_in_registers_first
assert_equal 'from OtherFileSystem',
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => OtherFileSystem.new})
end
def test_include_tag_with
assert_equal "Product: Draft 151cm ",
Template.parse("{% include 'product' with products[0] %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
end
def test_include_tag_with_default_name
assert_equal "Product: Draft 151cm ",
Template.parse("{% include 'product' %}").render( "product" => {'title' => 'Draft 151cm'} )
end
def test_include_tag_for
assert_equal "Product: Draft 151cm Product: Element 155cm ",
Template.parse("{% include 'product' for products %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
end
def test_include_tag_with_local_variables
assert_equal "Locale: test123 ",
Template.parse("{% include 'locale_variables' echo1: 'test123' %}").render
end
def test_include_tag_with_multiple_local_variables
assert_equal "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}").render
end
def test_include_tag_with_multiple_local_variables_from_context
assert_equal "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}").render('echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'})
end
def test_nested_include_tag
assert_equal "body body_detail",
Template.parse("{% include 'body' %}").render
assert_equal "header body body_detail footer",
Template.parse("{% include 'nested_template' %}").render
end
def test_nested_include_with_variable
assert_equal "Product: Draft 151cm details ",
Template.parse("{% include 'nested_product_template' with product %}").render("product" => {"title" => 'Draft 151cm'})
assert_equal "Product: Draft 151cm details Product: Element 155cm details ",
Template.parse("{% include 'nested_product_template' for products %}").render("products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}])
end
def test_recursively_included_template_does_not_produce_endless_loop
infinite_file_system = Class.new do
def read_template_file(template_path)
"-{% include 'loop' %}"
end
end
Liquid::Template.file_system = infinite_file_system.new
assert_raise(Liquid::StackLevelError) do
Template.parse("{% include 'loop' %}").render!
end
end
def test_dynamically_choosen_template
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
assert_equal "Test321", Template.parse("{% include template %}").render("template" => 'Test321')
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
end
end
+15
View File
@@ -0,0 +1,15 @@
require 'test_helper'
class AssignTest < Test::Unit::TestCase
include Liquid
def test_assigned_variable
assert_template_result('.foo.',
'{% assign foo = values %}.{{ foo[0] }}.',
'values' => %w{foo bar baz})
assert_template_result('.bar.',
'{% assign foo = values %}.{{ foo[1] }}.',
'values' => %w{foo bar baz})
end
end # AssignTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class VariableTest < Test::Unit::TestCase
include Liquid
@@ -33,7 +33,8 @@ class VariableTest < Test::Unit::TestCase
def test_variable_many_embedded_fragments
template = Liquid::Template.parse(" {{funk}} {{so}} {{brother}} ")
assert_equal 7, template.root.nodelist.size
assert_equal [String, Variable, String, Variable, String, Variable, String], block_types(template.root.nodelist)
assert_equal [String, Variable, String, Variable, String, Variable, String],
block_types(template.root.nodelist)
end
def test_with_block
@@ -51,8 +52,7 @@ class VariableTest < Test::Unit::TestCase
end
private
def block_types(nodelist)
nodelist.collect { |node| node.class }
end
end
def block_types(nodelist)
nodelist.collect { |node| node.class }
end
end # VariableTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class CaptureTest < Test::Unit::TestCase
include Liquid
@@ -9,14 +9,14 @@ class CaptureTest < Test::Unit::TestCase
def test_capture_to_variable_from_outer_scope_if_existing
template_source = <<-END_TEMPLATE
{% assign var = '' %}
{% if true %}
{% capture var %}first-block-string{% endcapture %}
{% endif %}
{% if true %}
{% capture var %}test-string{% endcapture %}
{% endif %}
{{var}}
{% assign var = '' %}
{% if true %}
{% capture var %}first-block-string{% endcapture %}
{% endif %}
{% if true %}
{% capture var %}test-string{% endcapture %}
{% endif %}
{{var}}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render
@@ -25,17 +25,16 @@ class CaptureTest < Test::Unit::TestCase
def test_assigning_from_capture
template_source = <<-END_TEMPLATE
{% assign first = '' %}
{% assign second = '' %}
{% for number in (1..3) %}
{% capture first %}{{number}}{% endcapture %}
{% assign second = first %}
{% endfor %}
{{ first }}-{{ second }}
{% assign first = '' %}
{% assign second = '' %}
{% for number in (1..3) %}
{% capture first %}{{number}}{% endcapture %}
{% assign second = first %}
{% endfor %}
{{ first }}-{{ second }}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render
assert_equal "3-3", rendered.gsub(/\s/, '')
end
end
end # CaptureTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class ConditionTest < Test::Unit::TestCase
include Liquid
@@ -96,20 +96,23 @@ class ConditionTest < Test::Unit::TestCase
def test_should_allow_custom_proc_operator
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}}}
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}} }
assert_evalutes_true "'bob'", 'starts_with', "'b'"
assert_evalutes_false "'bob'", 'starts_with', "'o'"
ensure
Condition.operators.delete 'starts_with'
ensure
Condition.operators.delete 'starts_with'
end
private
def assert_evalutes_true(left, op, right)
assert Condition.new(left, op, right).evaluate(@context || Liquid::Context.new), "Evaluated false: #{left} #{op} #{right}"
assert Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
"Evaluated false: #{left} #{op} #{right}"
end
def assert_evalutes_false(left, op, right)
assert !Condition.new(left, op, right).evaluate(@context || Liquid::Context.new), "Evaluated true: #{left} #{op} #{right}"
assert !Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
"Evaluated true: #{left} #{op} #{right}"
end
end
end # ConditionTest
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class HundredCentes
def to_liquid
100
@@ -62,7 +63,6 @@ class ArrayLike
end
end
class ContextTest < Test::Unit::TestCase
include Liquid
@@ -262,10 +262,10 @@ class ContextTest < Test::Unit::TestCase
def test_hash_to_array_transition
@context['colors'] = {
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
'Green' => ['003300','336633', '669966', '99CC99'],
'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
'Red' => ['660000','993333', 'CC6666', 'FF9999']
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
'Green' => ['003300','336633', '669966', '99CC99'],
'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
'Red' => ['660000','993333', 'CC6666', 'FF9999']
}
assert_equal '003366', @context['colors.Blue[0]']
@@ -475,5 +475,4 @@ class ContextTest < Test::Unit::TestCase
assert_kind_of CategoryDrop, @context['category']
assert_equal @context, @context['category'].context
end
end
end # ContextTest
@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class ContextDrop < Liquid::Drop
def scopes
@@ -24,7 +22,6 @@ class ContextDrop < Liquid::Drop
end
end
class ProductDrop < Liquid::Drop
class TextDrop < Liquid::Drop
@@ -74,7 +71,6 @@ class EnumerableDrop < Liquid::Drop
end
end
class DropsTest < Test::Unit::TestCase
include Liquid
@@ -154,9 +150,4 @@ class DropsTest < Test::Unit::TestCase
def test_enumerable_drop_size
assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render('collection' => EnumerableDrop.new)
end
end
end # DropsTest
@@ -1,6 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class ErrorDrop < Liquid::Drop
def standard_error
@@ -17,7 +15,6 @@ class ErrorDrop < Liquid::Drop
end
class ErrorHandlingTest < Test::Unit::TestCase
include Liquid
@@ -42,11 +39,9 @@ class ErrorHandlingTest < Test::Unit::TestCase
assert_equal SyntaxError, template.errors.first.class
end
end
def test_argument
assert_nothing_raised do
template = Liquid::Template.parse( ' {{ errors.argument_error }} ' )
@@ -54,36 +49,21 @@ class ErrorHandlingTest < Test::Unit::TestCase
assert_equal 1, template.errors.size
assert_equal ArgumentError, template.errors.first.class
end
end
def test_missing_endtag_parse_time_error
assert_raise(Liquid::SyntaxError) do
template = Liquid::Template.parse(' {% for a in b %} ... ')
end
end
def test_unrecognized_operator
assert_nothing_raised do
template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ')
assert_equal ' Liquid error: Unknown operator =! ', template.render
assert_equal 1, template.errors.size
assert_equal Liquid::ArgumentError, template.errors.first.class
end
end
end
end # ErrorHandlingTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class FileSystemTest < Test::Unit::TestCase
include Liquid
@@ -27,4 +26,4 @@ class FileSystemTest < Test::Unit::TestCase
file_system.full_path("/etc/passwd")
end
end
end
end # FileSystemTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
module MoneyFilter
def money(input)
@@ -92,4 +91,4 @@ class FiltersInTemplate < Test::Unit::TestCase
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, CanadianMoneyFilter)
assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, [CanadianMoneyFilter])
end
end
end # FiltersTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class HtmlTagTest < Test::Unit::TestCase
include Liquid
@@ -10,8 +10,8 @@ class HtmlTagTest < Test::Unit::TestCase
'numbers' => [1,2,3,4,5,6])
assert_template_result("<tr class=\"row1\">\n</tr>\n",
'{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
'numbers' => [])
'{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
'numbers' => [])
end
def test_html_table_with_different_cols
@@ -25,7 +25,5 @@ class HtmlTagTest < Test::Unit::TestCase
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row2\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row3\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n",
'{% tablerow n in numbers cols:2%}{{tablerowloop.col}}{% endtablerow %}',
'numbers' => [1,2,3,4,5,6])
end
end
end # HtmlTagTest
+127
View File
@@ -0,0 +1,127 @@
require 'test_helper'
class TestFileSystem
def read_template_file(template_path)
case template_path
when "product"
"Product: {{ product.title }} "
when "locale_variables"
"Locale: {{echo1}} {{echo2}}"
when "variant"
"Variant: {{ variant.title }}"
when "nested_template"
"{% include 'header' %} {% include 'body' %} {% include 'footer' %}"
when "body"
"body {% include 'body_detail' %}"
when "nested_product_template"
"Product: {{ nested_product_template.title }} {%include 'details'%} "
when "recursively_nested_template"
"-{% include 'recursively_nested_template' %}"
when "pick_a_source"
"from TestFileSystem"
else
template_path
end
end
end
class OtherFileSystem
def read_template_file(template_path)
'from OtherFileSystem'
end
end
class IncludeTagTest < Test::Unit::TestCase
include Liquid
def setup
Liquid::Template.file_system = TestFileSystem.new
end
def test_include_tag_looks_for_file_system_in_registers_first
assert_equal 'from OtherFileSystem',
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => OtherFileSystem.new})
end
def test_include_tag_with
assert_equal "Product: Draft 151cm ",
Template.parse("{% include 'product' with products[0] %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
end
def test_include_tag_with_default_name
assert_equal "Product: Draft 151cm ",
Template.parse("{% include 'product' %}").render( "product" => {'title' => 'Draft 151cm'} )
end
def test_include_tag_for
assert_equal "Product: Draft 151cm Product: Element 155cm ",
Template.parse("{% include 'product' for products %}").render( "products" => [ {'title' => 'Draft 151cm'}, {'title' => 'Element 155cm'} ] )
end
def test_include_tag_with_local_variables
assert_equal "Locale: test123 ",
Template.parse("{% include 'locale_variables' echo1: 'test123' %}").render
end
def test_include_tag_with_multiple_local_variables
assert_equal "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}").render
end
def test_include_tag_with_multiple_local_variables_from_context
assert_equal "Locale: test123 test321",
Template.parse("{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}").render('echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'})
end
def test_nested_include_tag
assert_equal "body body_detail",
Template.parse("{% include 'body' %}").render
assert_equal "header body body_detail footer",
Template.parse("{% include 'nested_template' %}").render
end
def test_nested_include_with_variable
assert_equal "Product: Draft 151cm details ",
Template.parse("{% include 'nested_product_template' with product %}").render("product" => {"title" => 'Draft 151cm'})
assert_equal "Product: Draft 151cm details Product: Element 155cm details ",
Template.parse("{% include 'nested_product_template' for products %}").render("products" => [{"title" => 'Draft 151cm'}, {"title" => 'Element 155cm'}])
end
def test_recursively_included_template_does_not_produce_endless_loop
infinite_file_system = Class.new do
def read_template_file(template_path)
"-{% include 'loop' %}"
end
end
Liquid::Template.file_system = infinite_file_system.new
assert_raise(Liquid::StackLevelError) do
Template.parse("{% include 'loop' %}").render!
end
end
def test_dynamically_choosen_template
assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')
assert_equal "Test321", Template.parse("{% include template %}").render("template" => 'Test321')
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
end
end # IncludeTagTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class TestClassA
liquid_methods :allowedA, :chainedB
@@ -84,6 +83,5 @@ class ModuleExTest < Test::Unit::TestCase
assert_equal 'another_allowedC', Liquid::Template.parse("{{ a.chainedB.chainedC.another_allowedC }}").render('a'=>@a)
assert_equal '', Liquid::Template.parse("{{ a.restricted }}").render('a'=>@a)
assert_equal '', Liquid::Template.parse("{{ a.unknown }}").render('a'=>@a)
end
end
end
end # ModuleExTest
@@ -1,8 +1,6 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
module FunnyFilter
def make_funny(input)
'LOL'
end
@@ -26,8 +24,8 @@ module FunnyFilter
def link_to(name, url)
%|<a href="#{url}">#{name}</a>|
end
end
end
class OutputTest < Test::Unit::TestCase
include Liquid
@@ -37,7 +35,6 @@ class OutputTest < Test::Unit::TestCase
'best_cars' => 'bmw',
'car' => {'bmw' => 'good', 'gm' => 'bad'}
}
end
def test_variable
@@ -116,6 +113,4 @@ class OutputTest < Test::Unit::TestCase
assert_equal expected, Template.parse(text).render(@assigns, :filters => [FunnyFilter])
end
end
end # OutputTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class ParsingQuirksTest < Test::Unit::TestCase
include Liquid
@@ -50,5 +49,4 @@ class ParsingQuirksTest < Test::Unit::TestCase
markup = "false || true"
assert_template_result('',"{% if #{markup} %} YES {% endif %}")
end
end
end # ParsingQuirksTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class RegexpTest < Test::Unit::TestCase
include Liquid
@@ -34,12 +34,11 @@ class RegexpTest < Test::Unit::TestCase
end
def test_variable_parser
assert_equal ['var'], 'var'.scan(VariableParser)
assert_equal ['var', 'method'], 'var.method'.scan(VariableParser)
assert_equal ['var', '[method]'], 'var[method]'.scan(VariableParser)
assert_equal ['var', '[method]', '[0]'], 'var[method][0]'.scan(VariableParser)
assert_equal ['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VariableParser)
assert_equal ['var'], 'var'.scan(VariableParser)
assert_equal ['var', 'method'], 'var.method'.scan(VariableParser)
assert_equal ['var', '[method]'], 'var[method]'.scan(VariableParser)
assert_equal ['var', '[method]', '[0]'], 'var[method][0]'.scan(VariableParser)
assert_equal ['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VariableParser)
assert_equal ['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VariableParser)
end
end
end # RegexpTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
module SecurityFilter
def add_one(input)
@@ -38,4 +38,4 @@ class SecurityTest < Test::Unit::TestCase
assert_equal expected, Template.parse(text).render(@assigns, :filters => SecurityFilter)
end
end
end # SecurityTest
@@ -1,12 +1,9 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class Filters
include Liquid::StandardFilters
end
class StandardFiltersTest < Test::Unit::TestCase
include Liquid
@@ -180,5 +177,4 @@ class StandardFiltersTest < Test::Unit::TestCase
def test_cannot_access_private_methods
assert_template_result('a',"{{ 'a' | to_number }}")
end
end
end # StandardFiltersTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class StrainerTest < Test::Unit::TestCase
include Liquid
@@ -23,4 +22,4 @@ class StrainerTest < Test::Unit::TestCase
def test_object_respond_to_missing
assert_equal Object.respond_to?(:respond_to_missing?), Strainer.create(nil).respond_to?(:respond_to_missing?)
end
end
end # StrainerTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class IfElseTest < Test::Unit::TestCase
include Liquid
@@ -6,7 +6,7 @@ class IfElseTest < Test::Unit::TestCase
def test_if
assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
assert_template_result(' this text should go into the output ',
' {% if true %} this text should go into the output {% endif %} ')
' {% if true %} this text should go into the output {% endif %} ')
assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
end
@@ -150,4 +150,4 @@ class IfElseTest < Test::Unit::TestCase
ensure
Condition.operators.delete 'contains'
end
end
end # IfElseTest
@@ -1,10 +1,8 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class StandardTagTest < Test::Unit::TestCase
include Liquid
def test_tag
tag = Tag.new('tag', [], [])
assert_equal 'liquid::tag', tag.name
@@ -14,6 +12,7 @@ class StandardTagTest < Test::Unit::TestCase
def test_no_transform
assert_template_result('this text should come out of the template without change...',
'this text should come out of the template without change...')
assert_template_result('blah','blah')
assert_template_result('<blah>','<blah>')
assert_template_result('|,.:','|,.:')
@@ -85,43 +84,48 @@ HERE
def test_for_helpers
assigns = {'array' => [1,2,3] }
assert_template_result(' 1/3 2/3 3/3 ','{%for item in array%} {{forloop.index}}/{{forloop.length}} {%endfor%}',assigns)
assert_template_result(' 1 2 3 ','{%for item in array%} {{forloop.index}} {%endfor%}',assigns)
assert_template_result(' 0 1 2 ','{%for item in array%} {{forloop.index0}} {%endfor%}',assigns)
assert_template_result(' 2 1 0 ','{%for item in array%} {{forloop.rindex0}} {%endfor%}',assigns)
assert_template_result(' 3 2 1 ','{%for item in array%} {{forloop.rindex}} {%endfor%}',assigns)
assert_template_result(' true false false ','{%for item in array%} {{forloop.first}} {%endfor%}',assigns)
assert_template_result(' false false true ','{%for item in array%} {{forloop.last}} {%endfor%}',assigns)
assert_template_result(' 1/3 2/3 3/3 ',
'{%for item in array%} {{forloop.index}}/{{forloop.length}} {%endfor%}',
assigns)
assert_template_result(' 1 2 3 ', '{%for item in array%} {{forloop.index}} {%endfor%}', assigns)
assert_template_result(' 0 1 2 ', '{%for item in array%} {{forloop.index0}} {%endfor%}', assigns)
assert_template_result(' 2 1 0 ', '{%for item in array%} {{forloop.rindex0}} {%endfor%}', assigns)
assert_template_result(' 3 2 1 ', '{%for item in array%} {{forloop.rindex}} {%endfor%}', assigns)
assert_template_result(' true false false ', '{%for item in array%} {{forloop.first}} {%endfor%}', assigns)
assert_template_result(' false false true ', '{%for item in array%} {{forloop.last}} {%endfor%}', assigns)
end
def test_for_and_if
assigns = {'array' => [1,2,3] }
assert_template_result('+--', '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}', assigns)
assert_template_result('+--',
'{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}',
assigns)
end
def test_limiting
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
assert_template_result('12','{%for i in array limit:2 %}{{ i }}{%endfor%}',assigns)
assert_template_result('1234','{%for i in array limit:4 %}{{ i }}{%endfor%}',assigns)
assert_template_result('3456','{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}',assigns)
assert_template_result('3456','{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}',assigns)
assert_template_result('12', '{%for i in array limit:2 %}{{ i }}{%endfor%}', assigns)
assert_template_result('1234', '{%for i in array limit:4 %}{{ i }}{%endfor%}', assigns)
assert_template_result('3456', '{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}', assigns)
assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
end
def test_dynamic_variable_limiting
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
assigns['limit'] = 2
assigns['offset'] = 2
assert_template_result('34','{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}',assigns)
assert_template_result('34', '{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}', assigns)
end
def test_nested_for
assigns = {'array' => [[1,2],[3,4],[5,6]] }
assert_template_result('123456','{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}',assigns)
assert_template_result('123456', '{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}', assigns)
end
def test_offset_only
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
assert_template_result('890','{%for i in array offset:7 %}{{ i }}{%endfor%}',assigns)
assert_template_result('890', '{%for i in array offset:7 %}{{ i }}{%endfor%}', assigns)
end
def test_pause_resume
@@ -199,86 +203,131 @@ HERE
def test_assign
assigns = {'var' => 'content' }
assert_template_result('var2: var2:content','var2:{{var2}} {%assign var2 = var%} var2:{{var2}}',assigns)
assert_template_result('var2: var2:content', 'var2:{{var2}} {%assign var2 = var%} var2:{{var2}}', assigns)
end
def test_hyphenated_assign
assigns = {'a-b' => '1' }
assert_template_result('a-b:1 a-b:2','a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}',assigns)
assert_template_result('a-b:1 a-b:2', 'a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}', assigns)
end
def test_assign_with_colon_and_spaces
assigns = {'var' => {'a:b c' => {'paged' => '1' }}}
assert_template_result('var2: 1','{%assign var2 = var["a:b c"].paged %}var2: {{var2}}',assigns)
assert_template_result('var2: 1', '{%assign var2 = var["a:b c"].paged %}var2: {{var2}}', assigns)
end
def test_capture
assigns = {'var' => 'content' }
assert_template_result('content foo content foo ','{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}', assigns)
assert_template_result('content foo content foo ',
'{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
assigns)
end
def test_capture_detects_bad_syntax
assert_raise(SyntaxError) do
assert_template_result('content foo content foo ','{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}', {'var' => 'content' })
assert_template_result('content foo content foo ',
'{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
{'var' => 'content' })
end
end
def test_case
assigns = {'condition' => 2 }
assert_template_result(' its 2 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
assert_template_result(' its 2 ',
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
assigns)
assigns = {'condition' => 1 }
assert_template_result(' its 1 ','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
assert_template_result(' its 1 ',
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
assigns)
assigns = {'condition' => 3 }
assert_template_result('','{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', assigns)
assert_template_result('',
'{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
assigns)
assigns = {'condition' => "string here" }
assert_template_result(' hit ','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
assert_template_result(' hit ',
'{% case condition %}{% when "string here" %} hit {% endcase %}',
assigns)
assigns = {'condition' => "bad string here" }
assert_template_result('','{% case condition %}{% when "string here" %} hit {% endcase %}', assigns)
assert_template_result('',
'{% case condition %}{% when "string here" %} hit {% endcase %}',\
assigns)
end
def test_case_with_else
assigns = {'condition' => 5 }
assert_template_result(' hit ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
assert_template_result(' hit ',
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
assigns)
assigns = {'condition' => 6 }
assert_template_result(' else ','{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', assigns)
assert_template_result(' else ',
'{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
assigns)
assigns = {'condition' => 6 }
assert_template_result(' else ','{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}', assigns)
assert_template_result(' else ',
'{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}',
assigns)
end
def test_case_on_size
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
assert_template_result('1' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
assert_template_result('2' , '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
end
def test_case_on_size_with_else
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [])
assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1])
assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1])
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1])
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1])
assert_template_result('else', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', 'a' => [1, 1, 1, 1, 1])
assert_template_result('else',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [])
assert_template_result('1',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [1])
assert_template_result('2',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [1, 1])
assert_template_result('else',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [1, 1, 1])
assert_template_result('else',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [1, 1, 1, 1])
assert_template_result('else',
'{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
'a' => [1, 1, 1, 1, 1])
end
def test_case_on_length_with_else
assert_template_result('else', '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
assert_template_result('false', '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
assert_template_result('true', '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
assert_template_result('else', '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', {})
assert_template_result('else',
'{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
{})
assert_template_result('false',
'{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
{})
assert_template_result('true',
'{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
{})
assert_template_result('else',
'{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
{})
end
def test_assign_from_case
@@ -331,7 +380,8 @@ HERE
end
def test_assign_is_global
assert_equal 'variable', Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
assert_equal 'variable',
Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
end
def test_case_detects_bad_syntax
@@ -345,31 +395,31 @@ HERE
end
def test_cycle
assert_template_result('one','{%cycle "one", "two"%}')
assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
assert_template_result(' two','{%cycle "", "two"%} {%cycle "", "two"%}')
assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
assert_template_result('text-align: left text-align: right',
'{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
end
def test_multiple_cycles
assert_template_result('1 2 1 1 2 3 1','{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
assert_template_result('1 2 1 1 2 3 1',
'{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
end
def test_multiple_named_cycles
assert_template_result('one one two two one one','{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
assert_template_result('one one two two one one',
'{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
end
def test_multiple_named_cycles_with_names_from_context
assigns = {"var1" => 1, "var2" => 2 }
assert_template_result('one one two two one one','{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
assert_template_result('one one two two one one',
'{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
end
def test_size_of_array
@@ -383,10 +433,10 @@ HERE
end
def test_illegal_symbols
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
assert_template_result('', '{% if true == null %}?{% endif %}', {})
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
assert_template_result('', '{% if null == true %}?{% endif %}', {})
assert_template_result('', '{% if true == empty %}?{% endif %}', {})
assert_template_result('', '{% if true == null %}?{% endif %}', {})
assert_template_result('', '{% if empty == true %}?{% endif %}', {})
assert_template_result('', '{% if null == true %}?{% endif %}', {})
end
def test_for_reversed
@@ -402,4 +452,4 @@ HERE
assigns = {'array' => [ 1, 1, 1, 1] }
assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
end
end
end # StandardTagTest
@@ -1,10 +1,8 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class StatementsTest < Test::Unit::TestCase
include Liquid
def test_true_eql_true
text = %| {% if true == true %} true {% else %} false {% endif %} |
expected = %| true |
@@ -133,5 +131,4 @@ class StatementsTest < Test::Unit::TestCase
expected = %| true |
assert_equal expected, Template.parse(text).render('var' => 1 )
end
end
end # StatementsTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class UnlessElseTest < Test::Unit::TestCase
include Liquid
@@ -6,7 +6,7 @@ class UnlessElseTest < Test::Unit::TestCase
def test_unless
assert_template_result(' ',' {% unless true %} this text should not go into the output {% endunless %} ')
assert_template_result(' this text should go into the output ',
' {% unless false %} this text should go into the output {% endunless %} ')
' {% unless false %} this text should go into the output {% endunless %} ')
assert_template_result(' you rock ?','{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?')
end
@@ -23,5 +23,4 @@ class UnlessElseTest < Test::Unit::TestCase
def test_unless_else_in_loop
assert_template_result ' TRUE 2 3 ', '{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}', 'choices' => [1, nil, false]
end
end
end # UnlessElseTest
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class TemplateTest < Test::Unit::TestCase
include Liquid
@@ -71,5 +71,4 @@ class TemplateTest < Test::Unit::TestCase
assert_equal '1', t.render(assigns)
@global = nil
end
end
end # TemplateTest
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/helper'
require 'test_helper'
class VariableTest < Test::Unit::TestCase
include Liquid
@@ -168,5 +167,4 @@ class VariableResolutionTest < Test::Unit::TestCase
}
assert_equal "Unknown variable 'test'", e.message
end
end
end # VariableTest
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
extras_path = File.join File.dirname(__FILE__), 'extra'
$LOAD_PATH.unshift(extras_path) unless $LOAD_PATH.include? extras_path
require 'test/unit'
require 'test/unit/assertions'
require 'caller'
require 'breakpoint'
require File.join File.dirname(__FILE__), '..', 'lib', 'liquid'
module Test
module Unit
module Assertions
include Liquid
def assert_template_result(expected, template, assigns={}, message=nil)
assert_equal expected, Template.parse(template).render(assigns)
end
end # Assertions
end # Unit
end # Test