require File.dirname(__FILE__) + '/../test_helper'
class PageContextTest < Test::Unit::TestCase
fixtures :pages, :page_parts, :layouts, :snippets, :users
def setup
setup_for_page(:radius)
end
def test_initialize
assert_equal(@page, @context.page)
end
def test_tag_page
assert_parse_output 'Radius Test Page', ""
assert_parse_output 'Ruby Home Page | Radius Test Page', %{ | }
end
def test_tags_page_attributes
@page.attributes.keys.each do |attr|
value = @page.send(attr)
unless [:created_by, :updated_by].include? attr.to_s.intern
assert_parse_output value.to_s, ""
end
end
end
def test_tag_children
expected = 'Radius Test Child 1 Radius Test Child 2 Radius Test Child 3 '
input = ' '
assert_parse_output expected, input
end
def test_tag_children_each
assert_parse_output 'radius/child-1 radius/child-2 radius/child-3 ' , '/ '
end
def test_tag_children_each_attributes
setup_for_page(:assorted)
assert_parse_output 'a b c d e f g h i j ', page_children_each_tags
assert_parse_output 'a b c d e ', page_children_each_tags(%{limit="5"})
assert_parse_output 'd e f g h ', page_children_each_tags(%{offset="3" limit="5"})
assert_parse_output 'j i h g f e d c b a ', page_children_each_tags(%{order="desc"})
assert_parse_output 'f e d c b a j i h g ', page_children_each_tags(%{by="breadcrumb"})
assert_parse_output 'g h i j a b c d e f ', page_children_each_tags(%{by="breadcrumb" order="desc"})
end
def test_tag_children_each_with_status_attribute
setup_for_page(:assorted)
assert_parse_output_match /^(draft |)a b c d e f g h i j( draft|) $/, page_children_each_tags(%{status="all"})
assert_parse_output 'draft ', page_children_each_tags(%{status="draft"})
assert_parse_output 'a b c d e f g h i j ', page_children_each_tags(%{status="published"})
assert_parse_output_match "`status' attribute of `each' tag must be set to a valid status", page_children_each_tags(%{status="askdf"})
end
def test_tag_children_each_attributes_with_invalid_limit
message = "`limit' attribute of `each' tag must be a positive number between 1 and 4 digits"
assert_parse_output_match message, page_children_each_tags(%{limit="a"})
assert_parse_output_match message, page_children_each_tags(%{limit="-10"})
assert_parse_output_match message, page_children_each_tags(%{limit="50000"})
end
def test_tag_children_each_attributes_with_invalid_offset
message = "`offset' attribute of `each' tag must be a positive number between 1 and 4 digits"
assert_parse_output_match message, page_children_each_tags(%{offset="a"})
assert_parse_output_match message, page_children_each_tags(%{offset="-10"})
assert_parse_output_match message, page_children_each_tags(%{offset="50000"})
end
def test_tag_children_each_attributes_with_invalid_by_field_name
message = "`by' attribute of `each' tag must be set to a valid field name"
assert_parse_output_match message, page_children_each_tags(%{by="non-existant-field"})
end
def test_tag_children_each_attributes_with_invalid_limit
message = %{`order' attribute of `each' tag must be set to either "asc" or "desc"}
assert_parse_output_match message, page_children_each_tags(%{order="asdf"})
end
def test_tag_children_each_does_not_list_virtual_pages
setup_for_page(:archive)
assert_parse_output 'article article-2 article-3 article-4 article-5 ', ' '
assert_parse_output_match /^(draft |)article article-2 article-3 article-4 article-5( draft|) $/, ' '
end
def test_tag_children_each_header
setup_for_page(:archive)
assert_parse_output '[May/00] article [Jun/00] article-2 article-3 [Aug/00] article-4 [Aug/01] article-5 ', '[] '
end
def test_tag_children_each_header_with_name_attribute
setup_for_page(:archive)
assert_parse_output '[2000] (May) article (Jun) article-2 article-3 (Aug) article-4 [2001] article-5 ', %{[] () }
end
def test_tag_children_each_header_with_restart_attribute
setup_for_page(:archive)
assert_parse_output(
'[2000] (May) article (Jun) article-2 article-3 (Aug) article-4 [2001] (Aug) article-5 ',
%{[] () }
)
assert_parse_output(
'[2000] (May) <01> article (Jun) <09> article-2 <10> article-3 (Aug) <06> article-4 [2001] (Aug) <06> article-5 ',
%{[] () <> }
)
end
def test_tag_children_count
assert_parse_output '3', ''
end
def test_tag_children_first
assert_parse_output 'Radius Test Child 1', ''
end
def test_tag_children_first_is_nil
setup_for_page(:textile)
assert_parse_output '', ''
end
def test_tag_children_last
assert_parse_output 'Radius Test Child 3', ''
end
def test_tag_children_last_nil
setup_for_page(:textile)
assert_parse_output '', ''
end
def test_tag_content
expected = "
Radius Test Page
\n\n\n\t\n\t- Radius Test Child 1
\n\t\t- Radius Test Child 2
\n\t\t- Radius Test Child 3
\n\t
"
assert_parse_output expected, ''
end
def test_tag_content_with_part_attribute
assert_parse_output "Just a test.\n", ''
end
def test_tag_content_with_inherit_attribute
assert_parse_output '', ''
assert_parse_output '', ''
assert_parse_output 'Radius Test Page sidebar.', ''
assert_parse_output_match %{`inherit' attribute of `content' tag must be set to either "true" or "false"}, ''
assert_parse_output '', ''
end
def test_tag_content_with_inherit_and_contextual_attributes
assert_parse_output 'Radius Test Page sidebar.', ''
assert_parse_output 'Ruby Home Page sidebar.', ''
setup_for_page(:inheritance_test_page_grandchild)
assert_parse_output 'Inheritance Test Page Grandchild inherited body.', ''
end
def test_tag_child_content
expected = "Radius test child 1 body.\nRadius test child 2 body.\nRadius test child 3 body.\n"
assert_parse_output expected, ''
end
def test_tag_if_content_without_body_attribute
assert_parse_output 'true', 'true'
end
def test_tag_if_content_with_body_attribute
assert_parse_output 'true', 'true'
end
def test_tag_if_content_with_nonexistant_body_attribute
assert_parse_output '', 'true'
end
def test_tag_unless_content_without_body_attribute
assert_parse_output '', 'false'
end
def test_tag_unless_content_with_body_attribute
assert_parse_output '', 'false'
end
def test_tag_unless_content_with_nonexistant_body_attribute
assert_parse_output 'false', 'false'
end
def test_tag_author
assert_parse_output 'Admin User', ''
end
def test_tag_author_nil
setup_for_page(:textile)
assert_parse_output '', ''
end
def test_tag_date
assert_parse_output 'Monday, January 30, 2006', ''
end
def test_tag_date_with_format_attribute
assert_parse_output '30 Jan 2006', ''
end
def test_tag_link
assert_parse_output 'Radius Test Page', ''
end
def test_tag_link__attributes
expected = 'Radius Test Page'
assert_parse_output expected, ''
end
def test_tag_link__block_form
assert_parse_output 'Test', 'Test'
end
def test_tag_link__anchor
assert_parse_output 'Test', 'Test'
end
def test_tag_child_link
expected = "Radius Test Child 1 Radius Test Child 2 Radius Test Child 3 "
assert_parse_output expected, ' '
end
def test_tag_snippet
assert_parse_output 'test', ''
end
def test_tag_snippet_not_found
assert_parse_output_match 'snippet not found', ''
end
def test_tag_snippet_without_name
assert_parse_output_match "`snippet' tag must contain `name' attribute", ''
end
def test_tag_snippet_with_markdown
assert_parse_output 'markdown
', ''
end
def test_tag_random
assert_parse_output_match /^(1|2|3)$/, " 1 2 3 "
end
def test_tag_comment
assert_parse_output 'just a test', 'just a small test'
end
def test_tag_navigation_1
tags = %{
|
}
expected = %{Home: Boy | Archives | Radius | Docs}
assert_parse_output expected, tags
end
def test_tag_navigation_2
tags = %{
}
expected = %{Home Archives Radius Docs}
assert_parse_output expected, tags
end
def test_tag_navigation_3
tags = %{
}
expected = %{Home Archives Radius Docs}
assert_parse_output expected, tags
end
def test_tag_navigation_without_urls
assert_parse_output '', %{}
end
def test_tag_navigation_without_urls
assert_parse_output_match "`navigation' tag must include a `normal' tag", %{}
end
def test_tag_navigation_with_urls_without_slashes
tags = %{
}
expected = %{Home Archives Radius Docs}
assert_parse_output expected, tags
end
def test_tag_find
assert_parse_output 'Ruby Home Page', %{}
end
def test_tag_find_without_url
assert_parse_output_match "`find' tag must contain `url' attribute", %{}
end
def test_tag_find_with_nonexistant_url
assert_parse_output '', %{}
end
def test_tag_find_and_children
assert_parse_output 'a-great-day-for-ruby another-great-day-for-ruby ', %{ }
end
def test_tag_escape_html
assert_parse_output '<strong>a bold move</strong>', 'a bold move'
end
def test_tag_rfc1123_date
@page.published_at = Time.utc(2004, 5, 2)
assert_parse_output 'Sun, 02 May 2004 00:00:00 GMT', ''
end
def test_tag_breadcrumbs
setup_for_page(:deep_nested_child_for_breadcrumbs)
assert_parse_output 'Home > Radius Test Page > Radius Test Child 1 > Deeply nested child',
''
end
def test_tag_breadcrumbs_with_separator_attribute
setup_for_page(:deep_nested_child_for_breadcrumbs)
assert_parse_output 'Home :: Radius Test Page :: Radius Test Child 1 :: Deeply nested child',
''
end
def test_tag_if_url_does_not_match
assert_parse_output '', 'true'
end
def test_tag_if_url_matches
assert_parse_output 'true', 'true'
end
def test_tag_if_url_without_ignore_case
assert_parse_output 'true', 'true'
end
def test_tag_if_url_with_ignore_case_true
assert_parse_output 'true', 'true'
end
def test_tag_if_url_with_ignore_case_false
assert_parse_output '', 'true'
end
def test_tag_if_url_with_malformatted_regexp
assert_parse_output_match "Malformed regular expression in `matches' argument of `if_url' tag:", 'true'
end
def test_tag_if_url_empty
assert_parse_output_match "`if_url' tag must contain a `matches' attribute", 'test'
end
def test_tag_unless_url_does_not_match
assert_parse_output 'true', 'true'
end
def test_tag_unless_url_matches
assert_parse_output '', 'true'
end
def test_tag_unless_url_without_ignore_case
assert_parse_output '', 'true'
end
def test_tag_unless_url_with_ignore_case_true
assert_parse_output '', 'true'
end
def test_tag_unless_url_with_ignore_case_false
assert_parse_output 'true', 'true'
end
def test_tag_unless_url_with_malformatted_regexp
assert_parse_output_match "Malformed regular expression in `matches' argument of `unless_url' tag:", 'true'
end
def test_tag_unless_url_empty
assert_parse_output_match "`unless_url' tag must contain a `matches' attribute", 'test'
end
def test_tag_missing
assert_parse_output_match "undefined tag `missing'", ''
end
protected
def setup_for_page(page)
@page = pages(page)
@context = PageContext.new(@page)
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
end
def page_children_each_tags(attr = nil)
attr = ' ' + attr unless attr.nil?
" "
end
def assert_parse_output(expected, input)
output = @parser.parse(input)
assert_equal expected, output
end
def assert_parse_output_match(regexp, input)
output = @parser.parse(input)
assert_match regexp, output
end
end