add radius
This commit is contained in:
1 parent
8d3e11cabe
commit
6e100d3499
42 files changed
+5795
No files matched your search
+35
@@ -0,0 +1,35 @@
|
||||
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
||||
require 'radius'
|
||||
|
||||
if RUBY_PLATFORM == 'java'
|
||||
require 'java'
|
||||
require 'radius/parser/jscanner'
|
||||
end
|
||||
|
||||
require 'benchmark'
|
||||
|
||||
document = <<EOF
|
||||
Before it all
|
||||
<r:foo>
|
||||
Middle Top
|
||||
<r:bar />
|
||||
Middle Bottom
|
||||
</r:foo>
|
||||
After it all
|
||||
EOF
|
||||
|
||||
amount = 1000
|
||||
|
||||
Benchmark.bmbm do |bm|
|
||||
bm.report('vanilla') do
|
||||
scanner = Radius::Scanner.new(:scanner => Radius::Scanner)
|
||||
amount.times { scanner.operate('r', document) }
|
||||
end
|
||||
|
||||
if RUBY_PLATFORM == 'java'
|
||||
bm.report('JavaScanner') do
|
||||
scanner = Radius::JavaScanner.new(JRuby.runtime)
|
||||
amount.times { scanner.operate('r', document) }
|
||||
end
|
||||
end
|
||||
end
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
||||
|
||||
class RadiusContextTest < Test::Unit::TestCase
|
||||
include RadiusTestHelper
|
||||
|
||||
def setup
|
||||
@context = new_context
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
@context = Radius::Context.new
|
||||
end
|
||||
|
||||
def test_initialize_with_block
|
||||
@context = Radius::Context.new do |c|
|
||||
assert_kind_of Radius::Context, c
|
||||
c.define_tag('test') { 'just a test' }
|
||||
end
|
||||
assert_not_equal Hash.new, @context.definitions
|
||||
end
|
||||
|
||||
def test_with
|
||||
got = @context.with do |c|
|
||||
assert_equal @context, c
|
||||
end
|
||||
assert_equal @context, got
|
||||
end
|
||||
|
||||
def test_render_tag
|
||||
define_global_tag "hello" do |tag|
|
||||
"Hello #{tag.attr['name'] || 'World'}!"
|
||||
end
|
||||
assert_render_tag_output 'Hello World!', 'hello'
|
||||
assert_render_tag_output 'Hello John!', 'hello', 'name' => 'John'
|
||||
end
|
||||
|
||||
def test_render_tag__undefined_tag
|
||||
e = assert_raises(Radius::UndefinedTagError) { @context.render_tag('undefined_tag') }
|
||||
assert_equal "undefined tag `undefined_tag'", e.message
|
||||
end
|
||||
|
||||
def test_tag_missing
|
||||
class << @context
|
||||
def tag_missing(tag, attr, &block)
|
||||
"undefined tag `#{tag}' with attributes #{attr.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
text = ''
|
||||
expected = %{undefined tag `undefined_tag' with attributes {"cool"=>"beans"}}
|
||||
assert_nothing_raised { text = @context.render_tag('undefined_tag', 'cool' => 'beans') }
|
||||
assert_equal expected, text
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_render_tag_output(output, *render_tag_params)
|
||||
assert_equal output, @context.render_tag(*render_tag_params)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
require 'test/unit'
|
||||
require 'radius'
|
||||
|
||||
class MultithreadTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
Thread.abort_on_exception
|
||||
@context = Radius::Context.new do |c|
|
||||
c.define_tag('thread') do |tag|
|
||||
"#{tag.locals.thread_id} / #{tag.globals.object_id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if RUBY_PLATFORM == 'java'
|
||||
require 'java'
|
||||
# call once before the thread to keep from using hidden require in a thread
|
||||
Radius::Parser.new
|
||||
def test_runs_multithreaded
|
||||
lock = java.lang.String.new("lock")
|
||||
threads = []
|
||||
1000.times do |t|
|
||||
thread = Thread.new do
|
||||
parser = Radius::Parser.new(@context, :tag_prefix => 'r')
|
||||
parser.context.globals.thread_id = Thread.current.object_id
|
||||
expected = "#{Thread.current.object_id} / "+
|
||||
"#{parser.context.globals.object_id}"
|
||||
actual = parser.parse('<r:thread />')
|
||||
assert_equal expected, actual
|
||||
end
|
||||
lock.synchronized do
|
||||
threads << thread
|
||||
end
|
||||
end
|
||||
lock.synchronized do
|
||||
threads.each{|t| t.join }
|
||||
end
|
||||
end
|
||||
else
|
||||
def test_runs_multithreaded
|
||||
threads = []
|
||||
mute = Mutex.new
|
||||
1000.times do |t|
|
||||
thread = Thread.new do
|
||||
parser = Radius::Parser.new(@context, :tag_prefix => 'r')
|
||||
parser.context.globals.thread_id = Thread.current.object_id
|
||||
expected = "#{Thread.current.object_id} / "+
|
||||
"#{parser.context.globals.object_id}"
|
||||
actual = parser.parse('<r:thread />')
|
||||
assert_equal expected, actual
|
||||
end
|
||||
mute.synchronize do
|
||||
threads << thread
|
||||
end
|
||||
end
|
||||
mute.synchronize do
|
||||
threads.each{|t| t.join }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
require 'test/unit'
|
||||
require 'radius'
|
||||
|
||||
class RadiusOrdStringTest < Test::Unit::TestCase
|
||||
|
||||
def test_string_slice_integer
|
||||
str = Radius::OrdString.new "abc"
|
||||
assert_equal str[0], 97
|
||||
assert_equal str[1], 98
|
||||
assert_equal str[2], 99
|
||||
end
|
||||
|
||||
def test_string_slice_range
|
||||
str = Radius::OrdString.new "abc"
|
||||
assert_equal str[0..-1], "abc"
|
||||
end
|
||||
|
||||
end
|
||||
+307
@@ -0,0 +1,307 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
||||
|
||||
class RadiusParserTest < Test::Unit::TestCase
|
||||
include RadiusTestHelper
|
||||
|
||||
def setup
|
||||
@context = new_context
|
||||
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
@parser = Radius::Parser.new
|
||||
assert_kind_of Radius::Context, @parser.context
|
||||
end
|
||||
|
||||
def test_initialize_with_params
|
||||
@parser = Radius::Parser.new(TestContext.new)
|
||||
assert_kind_of TestContext, @parser.context
|
||||
|
||||
@parser = Radius::Parser.new(:context => TestContext.new)
|
||||
assert_kind_of TestContext, @parser.context
|
||||
|
||||
@parser = Radius::Parser.new('context' => TestContext.new)
|
||||
assert_kind_of TestContext, @parser.context
|
||||
|
||||
@parser = Radius::Parser.new(:tag_prefix => 'r')
|
||||
assert_kind_of Radius::Context, @parser.context
|
||||
assert_equal 'r', @parser.tag_prefix
|
||||
|
||||
@parser = Radius::Parser.new(TestContext.new, :tag_prefix => 'r')
|
||||
assert_kind_of TestContext, @parser.context
|
||||
assert_equal 'r', @parser.tag_prefix
|
||||
end
|
||||
|
||||
def test_parse_individual_tags_and_parameters
|
||||
define_tag "add" do |tag|
|
||||
tag.attr["param1"].to_i + tag.attr["param2"].to_i
|
||||
end
|
||||
assert_parse_output "<3>", %{<<r:add param1="1" param2='2'/>>}
|
||||
end
|
||||
|
||||
def test_parse_attributes
|
||||
attributes = %{{"a"=>"1", "b"=>"2", "c"=>"3", "d"=>"'"}}
|
||||
assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'" />}
|
||||
assert_parse_output attributes, %{<r:attr a="1" b='2'c="3"d="'"></r:attr>}
|
||||
end
|
||||
|
||||
def test_parse_attributes_with_slashes_or_angle_brackets
|
||||
slash = %{{"slash"=>"/"}}
|
||||
angle = %{{"angle"=>">"}}
|
||||
assert_parse_output slash, %{<r:attr slash="/"></r:attr>}
|
||||
assert_parse_output slash, %{<r:attr slash="/"><r:attr /></r:attr>}
|
||||
assert_parse_output angle, %{<r:attr angle=">"></r:attr>}
|
||||
end
|
||||
|
||||
def test_parse_quotes
|
||||
assert_parse_output "test []", %{<r:echo value="test" /> <r:wrap attr="test"></r:wrap>}
|
||||
end
|
||||
|
||||
def test_things_that_should_be_left_alone
|
||||
[
|
||||
%{ test="2"="4" },
|
||||
%{="2" }
|
||||
].each do |middle|
|
||||
assert_parsed_is_unchanged "<r:attr#{middle}/>"
|
||||
assert_parsed_is_unchanged "<r:attr#{middle}>"
|
||||
end
|
||||
end
|
||||
|
||||
def test_tags_inside_html_tags
|
||||
assert_parse_output %{<div class="xzibit">tags in yo tags</div>},
|
||||
%{<div class="<r:reverse>tibizx</r:reverse>">tags in yo tags</div>}
|
||||
end
|
||||
|
||||
def test_parse_result_is_always_a_string
|
||||
define_tag("twelve") { 12 }
|
||||
assert_parse_output "12", "<r:twelve />"
|
||||
end
|
||||
|
||||
def test_parse_double_tags
|
||||
assert_parse_output "test".reverse, "<r:reverse>test</r:reverse>"
|
||||
assert_parse_output "tset TEST", "<r:reverse>test</r:reverse> <r:capitalize>test</r:capitalize>"
|
||||
end
|
||||
|
||||
def test_parse_tag_nesting
|
||||
define_tag("parent", :for => '')
|
||||
define_tag("parent:child", :for => '')
|
||||
define_tag("extra", :for => '')
|
||||
define_tag("nesting") { |tag| tag.nesting }
|
||||
define_tag("extra:nesting") { |tag| tag.nesting.gsub(':', ' > ') }
|
||||
define_tag("parent:child:nesting") { |tag| tag.nesting.gsub(':', ' * ') }
|
||||
assert_parse_output "nesting", "<r:nesting />"
|
||||
assert_parse_output "parent:nesting", "<r:parent:nesting />"
|
||||
assert_parse_output "extra > nesting", "<r:extra:nesting />"
|
||||
assert_parse_output "parent * child * nesting", "<r:parent:child:nesting />"
|
||||
assert_parse_output "parent > extra > nesting", "<r:parent:extra:nesting />"
|
||||
assert_parse_output "parent > child > extra > nesting", "<r:parent:child:extra:nesting />"
|
||||
assert_parse_output "parent * extra * child * nesting", "<r:parent:extra:child:nesting />"
|
||||
assert_parse_output "parent > extra > child > extra > nesting", "<r:parent:extra:child:extra:nesting />"
|
||||
assert_parse_output "parent > extra > child > extra > nesting", "<r:parent><r:extra><r:child><r:extra><r:nesting /></r:extra></r:child></r:extra></r:parent>"
|
||||
assert_parse_output "extra * parent * child * nesting", "<r:extra:parent:child:nesting />"
|
||||
assert_parse_output "extra > parent > nesting", "<r:extra><r:parent:nesting /></r:extra>"
|
||||
assert_parse_output "extra * parent * child * nesting", "<r:extra:parent><r:child:nesting /></r:extra:parent>"
|
||||
assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:child />") }
|
||||
end
|
||||
def test_parse_tag_nesting_2
|
||||
define_tag("parent", :for => '')
|
||||
define_tag("parent:child", :for => '')
|
||||
define_tag("content") { |tag| tag.nesting }
|
||||
assert_parse_output 'parent:child:content', '<r:parent><r:child:content /></r:parent>'
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_do_missing
|
||||
define_tag 'test' do |tag|
|
||||
tag.missing!
|
||||
end
|
||||
e = assert_raises(Radius::UndefinedTagError) { @parser.parse("<r:test />") }
|
||||
assert_equal "undefined tag `test'", e.message
|
||||
end
|
||||
|
||||
def test_parse_chirpy_bird
|
||||
# :> chirp chirp
|
||||
assert_parse_output "<:", "<:"
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_render_tag
|
||||
define_tag('test') { |tag| "Hello #{tag.attr['name']}!" }
|
||||
define_tag('hello') { |tag| tag.render('test', tag.attr) }
|
||||
assert_parse_output 'Hello John!', '<r:hello name="John" />'
|
||||
end
|
||||
|
||||
def test_accessing_tag_attributes_through_tag_indexer
|
||||
define_tag('test') { |tag| "Hello #{tag['name']}!" }
|
||||
assert_parse_output 'Hello John!', '<r:test name="John" />'
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_render_tag_with_block
|
||||
define_tag('test') { |tag| "Hello #{tag.expand}!" }
|
||||
define_tag('hello') { |tag| tag.render('test') { tag.expand } }
|
||||
assert_parse_output 'Hello John!', '<r:hello>John</r:hello>'
|
||||
end
|
||||
|
||||
def test_tag_locals
|
||||
define_tag "outer" do |tag|
|
||||
tag.locals.var = 'outer'
|
||||
tag.expand
|
||||
end
|
||||
define_tag "outer:inner" do |tag|
|
||||
tag.locals.var = 'inner'
|
||||
tag.expand
|
||||
end
|
||||
define_tag "outer:var" do |tag|
|
||||
tag.locals.var
|
||||
end
|
||||
assert_parse_output 'outer', "<r:outer><r:var /></r:outer>"
|
||||
assert_parse_output 'outer:inner:outer', "<r:outer><r:var />:<r:inner><r:var /></r:inner>:<r:var /></r:outer>"
|
||||
assert_parse_output 'outer:inner:outer:inner:outer', "<r:outer><r:var />:<r:inner><r:var />:<r:outer><r:var /></r:outer>:<r:var /></r:inner>:<r:var /></r:outer>"
|
||||
assert_parse_output 'outer', "<r:outer:var />"
|
||||
end
|
||||
|
||||
def test_tag_globals
|
||||
define_tag "set" do |tag|
|
||||
tag.globals.var = tag.attr['value']
|
||||
''
|
||||
end
|
||||
define_tag "var" do |tag|
|
||||
tag.globals.var
|
||||
end
|
||||
assert_parse_output " true false", %{<r:var /> <r:set value="true" /> <r:var /> <r:set value="false" /> <r:var />}
|
||||
end
|
||||
|
||||
def test_parse_loops
|
||||
@item = nil
|
||||
define_tag "each" do |tag|
|
||||
result = []
|
||||
["Larry", "Moe", "Curly"].each do |item|
|
||||
tag.locals.item = item
|
||||
result << tag.expand
|
||||
end
|
||||
result.join(tag.attr["between"] || "")
|
||||
end
|
||||
define_tag "each:item" do |tag|
|
||||
tag.locals.item
|
||||
end
|
||||
assert_parse_output %{Three Stooges: "Larry", "Moe", "Curly"}, %{Three Stooges: <r:each between=", ">"<r:item />"</r:each>}
|
||||
end
|
||||
|
||||
def test_parse_speed
|
||||
define_tag "set" do |tag|
|
||||
tag.globals.var = tag.attr['value']
|
||||
''
|
||||
end
|
||||
define_tag "var" do |tag|
|
||||
tag.globals.var
|
||||
end
|
||||
parts = %w{decima nobis augue at facer processus commodo legentis odio lectorum dolore nulla esse lius qui nonummy ullamcorper erat ii notare}
|
||||
multiplier = parts.map{|p| "#{p}=\"#{rand}\""}.join(' ')
|
||||
assert_nothing_raised do
|
||||
Timeout.timeout(10) do
|
||||
assert_parse_output " false", %{<r:set value="false" #{multiplier} /> <r:var />}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_tag_option_for
|
||||
define_tag 'fun', :for => 'just for kicks'
|
||||
assert_parse_output 'just for kicks', '<r:fun />'
|
||||
end
|
||||
|
||||
def test_tag_expose_option
|
||||
define_tag 'user', :for => users.first, :expose => ['name', :age]
|
||||
assert_parse_output 'John', '<r:user:name />'
|
||||
assert_parse_output '25', '<r:user><r:age /></r:user>'
|
||||
e = assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user:email />" }
|
||||
assert_equal "undefined tag `email'", e.message
|
||||
end
|
||||
|
||||
def test_tag_expose_attributes_option_on_by_default
|
||||
define_tag 'user', :for => user_with_attributes
|
||||
assert_parse_output 'John', '<r:user:name />'
|
||||
end
|
||||
def test_tag_expose_attributes_set_to_false
|
||||
define_tag 'user_without_attributes', :for => user_with_attributes, :attributes => false
|
||||
assert_raises(Radius::UndefinedTagError) { @parser.parse "<r:user_without_attributes:name />" }
|
||||
end
|
||||
|
||||
def test_tag_options_must_contain_a_for_option_if_methods_are_exposed
|
||||
e = assert_raises(ArgumentError) { define_tag('fun', :expose => :today) { 'test' } }
|
||||
assert_equal "tag definition must contain a :for option when used with the :expose option", e.message
|
||||
end
|
||||
|
||||
def test_parse_fail_on_missing_end_tag
|
||||
assert_raises(Radius::MissingEndTagError) { @parser.parse("<r:reverse>") }
|
||||
end
|
||||
|
||||
def test_parse_fail_on_wrong_end_tag
|
||||
assert_raises(Radius::WrongEndTagError) { @parser.parse("<r:reverse><r:capitalize></r:reverse>") }
|
||||
end
|
||||
|
||||
def test_parse_with_default_tag_prefix
|
||||
@parser = Radius::Parser.new(@context)
|
||||
define_tag("hello") { |tag| "Hello world!" }
|
||||
assert_equal "<p>Hello world!</p>", @parser.parse('<p><radius:hello /></p>')
|
||||
end
|
||||
|
||||
def test_parse_with_other_radius_like_tags
|
||||
@parser = Radius::Parser.new(@context, :tag_prefix => "ralph")
|
||||
define_tag('hello') { "hello" }
|
||||
assert_equal "<r:ralph:hello />", @parser.parse("<r:ralph:hello />")
|
||||
end
|
||||
|
||||
def test_copyin_global_values
|
||||
@context.globals.foo = 'bar'
|
||||
assert_equal 'bar', Radius::Parser.new(@context).context.globals.foo
|
||||
end
|
||||
|
||||
def test_does_not_pollute_copied_globals
|
||||
@context.globals.foo = 'bar'
|
||||
parser = Radius::Parser.new(@context)
|
||||
parser.context.globals.foo = '[baz]'
|
||||
assert_equal 'bar', @context.globals.foo
|
||||
end
|
||||
|
||||
def test_parse_with_other_namespaces
|
||||
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
|
||||
assert_equal "<fb:test>hello world</fb:test>", @parser.parse("<fb:test>hello world</fb:test>")
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def assert_parse_output(output, input, message = nil)
|
||||
r = @parser.parse(input)
|
||||
assert_equal(output, r, message)
|
||||
end
|
||||
|
||||
def assert_parsed_is_unchanged(something)
|
||||
assert_parse_output something, something
|
||||
end
|
||||
|
||||
class User
|
||||
attr_accessor :name, :age, :email, :friend
|
||||
def initialize(name, age, email)
|
||||
@name, @age, @email = name, age, email
|
||||
end
|
||||
def <=>(other)
|
||||
name <=> other.name
|
||||
end
|
||||
end
|
||||
|
||||
class UserWithAttributes < User
|
||||
def attributes
|
||||
{ :name => name, :age => age, :email => email }
|
||||
end
|
||||
end
|
||||
|
||||
def users
|
||||
[
|
||||
User.new('John', 25, 'test@johnwlong.com'),
|
||||
User.new('James', 27, 'test@jameslong.com')
|
||||
]
|
||||
end
|
||||
|
||||
def user_with_attributes
|
||||
UserWithAttributes.new('John', 25, 'test@johnwlong.com')
|
||||
end
|
||||
|
||||
end
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
||||
|
||||
class QuickstartTest < Test::Unit::TestCase
|
||||
|
||||
def test_hello_world
|
||||
context = Radius::Context.new
|
||||
context.define_tag "hello" do |tag|
|
||||
"Hello #{tag.attr['name'] || 'World'}!"
|
||||
end
|
||||
parser = Radius::Parser.new(context)
|
||||
assert_equal "<p>Hello World!</p>", parser.parse('<p><radius:hello /></p>')
|
||||
assert_equal "<p>Hello John!</p>", parser.parse('<p><radius:hello name="John" /></p>')
|
||||
end
|
||||
|
||||
def test_example_2
|
||||
require 'redcloth'
|
||||
context = Radius::Context.new
|
||||
context.define_tag "textile" do |tag|
|
||||
contents = tag.expand
|
||||
RedCloth.new(contents).to_html
|
||||
end
|
||||
parser = Radius::Parser.new(context)
|
||||
assert_equal "<p>Hello <b>World</b>!</p>", parser.parse('<radius:textile>Hello **World**!</radius:textile>')
|
||||
end
|
||||
|
||||
def test_nested_example
|
||||
context = Radius::Context.new
|
||||
|
||||
context.define_tag "stooge" do |tag|
|
||||
content = ''
|
||||
["Larry", "Moe", "Curly"].each do |name|
|
||||
tag.locals.name = name
|
||||
content << tag.expand
|
||||
end
|
||||
content
|
||||
end
|
||||
|
||||
context.define_tag "stooge:name" do |tag|
|
||||
tag.locals.name
|
||||
end
|
||||
|
||||
parser = Radius::Parser.new(context)
|
||||
|
||||
template = <<-TEMPLATE
|
||||
<ul>
|
||||
<radius:stooge>
|
||||
<li><radius:name /></li>
|
||||
</radius:stooge>
|
||||
</ul>
|
||||
TEMPLATE
|
||||
|
||||
output = <<-OUTPUT
|
||||
<ul>
|
||||
|
||||
<li>Larry</li>
|
||||
|
||||
<li>Moe</li>
|
||||
|
||||
<li>Curly</li>
|
||||
|
||||
</ul>
|
||||
OUTPUT
|
||||
|
||||
assert_equal output, parser.parse(template)
|
||||
end
|
||||
|
||||
class User
|
||||
attr_accessor :name, :age, :email
|
||||
end
|
||||
def test_exposing_objects_example
|
||||
parser = Radius::Parser.new
|
||||
|
||||
parser.context.define_tag "count", :for => 1
|
||||
assert_equal "1", parser.parse("<radius:count />")
|
||||
|
||||
user = User.new
|
||||
user.name, user.age, user.email = "John", 29, "john@example.com"
|
||||
parser.context.define_tag "user", :for => user, :expose => [ :name, :age, :email ]
|
||||
assert_equal "John", parser.parse("<radius:user><radius:name /></radius:user>")
|
||||
|
||||
assert_equal "John", parser.parse("<radius:user:name />")
|
||||
end
|
||||
|
||||
class LazyContext < Radius::Context
|
||||
def tag_missing(tag, attr, &block)
|
||||
"<strong>ERROR: Undefined tag `#{tag}' with attributes #{attr.inspect}</strong>"
|
||||
end
|
||||
end
|
||||
def test_tag_missing_example
|
||||
parser = Radius::Parser.new(LazyContext.new, :tag_prefix => 'lazy')
|
||||
output = %{<strong>ERROR: Undefined tag `weird' with attributes {"value"=>"true"}</strong>}
|
||||
assert_equal output, parser.parse('<lazy:weird value="true" />')
|
||||
end
|
||||
|
||||
def test_tag_globals_example
|
||||
parser = Radius::Parser.new
|
||||
|
||||
parser.context.define_tag "inc" do |tag|
|
||||
tag.globals.count ||= 0
|
||||
tag.globals.count += 1
|
||||
""
|
||||
end
|
||||
|
||||
parser.context.define_tag "count" do |tag|
|
||||
tag.globals.count || 0
|
||||
end
|
||||
|
||||
assert_equal "0 1", parser.parse("<radius:count /> <radius:inc /><radius:count />")
|
||||
end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :friend
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
end
|
||||
def test_tag_locals_and_globals_example
|
||||
jack = Person.new('Jack')
|
||||
jill = Person.new('Jill')
|
||||
jack.friend = jill
|
||||
jill.friend = jack
|
||||
|
||||
context = Radius::Context.new do |c|
|
||||
c.define_tag "jack" do |tag|
|
||||
tag.locals.person = jack
|
||||
tag.expand
|
||||
end
|
||||
c.define_tag "jill" do |tag|
|
||||
tag.locals.person = jill
|
||||
tag.expand
|
||||
end
|
||||
c.define_tag "name" do |tag|
|
||||
tag.locals.person.name rescue tag.missing!
|
||||
end
|
||||
c.define_tag "friend" do |tag|
|
||||
tag.locals.person = tag.locals.person.friend rescue tag.missing!
|
||||
tag.expand
|
||||
end
|
||||
end
|
||||
|
||||
parser = Radius::Parser.new(context, :tag_prefix => 'r')
|
||||
|
||||
assert_equal "Jack", parser.parse('<r:jack:name />') #=> "Jack"
|
||||
assert_equal "Jill", parser.parse('<r:jill:name />') #=> "Jill"
|
||||
assert_equal "Jack", parser.parse('<r:jill:friend:name />') #=> "Jack"
|
||||
assert_equal "Jack", parser.parse('<r:jack:friend:friend:name />') #=> "Jack"
|
||||
assert_equal "Jack and Jill", parser.parse('<r:jill><r:friend:name /> and <r:name /></r:jill>') #=> "Jack and Jill"
|
||||
assert_raises(Radius::UndefinedTagError) { parser.parse('<r:name />') } # raises a Radius::UndefinedTagError exception
|
||||
end
|
||||
|
||||
end
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
||||
require 'radius/parser/squiggle_scanner'
|
||||
|
||||
class RadiusSquiggleTest < Test::Unit::TestCase
|
||||
include RadiusTestHelper
|
||||
|
||||
def setup
|
||||
@context = new_context
|
||||
@parser = Radius::Parser.new(@context, :scanner => Radius::SquiggleScanner.new)
|
||||
end
|
||||
|
||||
def test_sane_scanner_default
|
||||
assert !Radius::Parser.new.scanner.is_a?(Radius::SquiggleScanner)
|
||||
end
|
||||
|
||||
def test_initialize_with_params
|
||||
@parser = Radius::Parser.new(:scanner => Radius::SquiggleScanner.new)
|
||||
assert_kind_of Radius::SquiggleScanner, @parser.scanner
|
||||
end
|
||||
|
||||
def test_parse_individual_tags_and_parameters
|
||||
define_tag "add" do |tag|
|
||||
tag.attr["param1"].to_i + tag.attr["param2"].to_i
|
||||
end
|
||||
assert_parse_output "{3}", %[{{ add param1="1" param2='2'/}}]
|
||||
end
|
||||
|
||||
def test_parse_attributes
|
||||
attributes = %[{"a"=>"1", "b"=>"2", "c"=>"3", "d"=>"'"}]
|
||||
assert_parse_output attributes, %[{attr a="1" b='2'c="3"d="'" /}]
|
||||
assert_parse_output attributes, %[{attr a="1" b='2'c="3"d="'"}{/attr}]
|
||||
end
|
||||
|
||||
def test_parse_attributes_with_slashes_or_angle_brackets
|
||||
slash = %[{"slash"=>"/"}]
|
||||
angle = %[{"angle"=>">"}]
|
||||
assert_parse_output slash, %[{attr slash="/"}{/attr}]
|
||||
assert_parse_output slash, %[{attr slash="/"}{attr /}{/attr}]
|
||||
assert_parse_output angle, %[{attr angle=">"}{/attr}]
|
||||
end
|
||||
|
||||
def test_parse_quotes
|
||||
assert_parse_output "test []", %[{echo value="test" /} {wrap attr="test"}{/wrap}]
|
||||
end
|
||||
|
||||
def test_things_that_should_be_left_alone
|
||||
[
|
||||
%[ test="2"="4" ],
|
||||
%[="2" ]
|
||||
].each do |middle|
|
||||
assert_parsed_is_unchanged "{attr#{middle}/}"
|
||||
assert_parsed_is_unchanged "{attr#{middle}}"
|
||||
end
|
||||
end
|
||||
|
||||
def test_tags_inside_html_tags
|
||||
assert_parse_output %[<div class="xzibit">tags in yo tags</div>],
|
||||
%[<div class="{reverse}tibizx{/reverse}">tags in yo tags</div>]
|
||||
end
|
||||
|
||||
def test_parse_result_is_always_a_string
|
||||
define_tag("twelve") { 12 }
|
||||
assert_parse_output "12", "{ twelve /}"
|
||||
end
|
||||
|
||||
def test_parse_double_tags
|
||||
assert_parse_output "test".reverse, "{reverse}test{/reverse}"
|
||||
assert_parse_output "tset TEST", "{reverse}test{/reverse} {capitalize}test{/capitalize}"
|
||||
end
|
||||
|
||||
def test_parse_tag_nesting
|
||||
define_tag("parent", :for => '')
|
||||
define_tag("parent:child", :for => '')
|
||||
define_tag("extra", :for => '')
|
||||
define_tag("nesting") { |tag| tag.nesting }
|
||||
define_tag("extra:nesting") { |tag| tag.nesting.gsub(':', ' > ') }
|
||||
define_tag("parent:child:nesting") { |tag| tag.nesting.gsub(':', ' * ') }
|
||||
assert_parse_output "nesting", "{nesting /}"
|
||||
assert_parse_output "parent:nesting", "{parent:nesting /}"
|
||||
assert_parse_output "extra > nesting", "{extra:nesting /}"
|
||||
assert_parse_output "parent * child * nesting", "{parent:child:nesting /}"
|
||||
assert_parse_output "parent > extra > nesting", "{parent:extra:nesting /}"
|
||||
assert_parse_output "parent > child > extra > nesting", "{parent:child:extra:nesting /}"
|
||||
assert_parse_output "parent * extra * child * nesting", "{parent:extra:child:nesting /}"
|
||||
assert_parse_output "parent > extra > child > extra > nesting", "{parent:extra:child:extra:nesting /}"
|
||||
assert_parse_output "parent > extra > child > extra > nesting", "{parent}{extra}{child}{extra}{nesting /}{/extra}{/child}{/extra}{/parent}"
|
||||
assert_parse_output "extra * parent * child * nesting", "{extra:parent:child:nesting /}"
|
||||
assert_parse_output "extra > parent > nesting", "{extra}{parent:nesting /}{/extra}"
|
||||
assert_parse_output "extra * parent * child * nesting", "{extra:parent}{child:nesting /}{/extra:parent}"
|
||||
assert_raises(Radius::UndefinedTagError) { @parser.parse("{child /}") }
|
||||
end
|
||||
def test_parse_tag_nesting_2
|
||||
define_tag("parent", :for => '')
|
||||
define_tag("parent:child", :for => '')
|
||||
define_tag("content") { |tag| tag.nesting }
|
||||
assert_parse_output 'parent:child:content', '{parent}{child:content /}{/parent}'
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_do_missing
|
||||
define_tag 'test' do |tag|
|
||||
tag.missing!
|
||||
end
|
||||
e = assert_raises(Radius::UndefinedTagError) { @parser.parse("{test /}") }
|
||||
assert_equal "undefined tag `test'", e.message
|
||||
end
|
||||
|
||||
def test_parse_chirpy_bird
|
||||
# :> chirp chirp
|
||||
assert_parse_output "<:", "<:"
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_render_tag
|
||||
define_tag('test') { |tag| "Hello #{tag.attr['name']}!" }
|
||||
define_tag('hello') { |tag| tag.render('test', tag.attr) }
|
||||
assert_parse_output 'Hello John!', '{hello name="John" /}'
|
||||
end
|
||||
|
||||
def test_accessing_tag_attributes_through_tag_indexer
|
||||
define_tag('test') { |tag| "Hello #{tag['name']}!" }
|
||||
assert_parse_output 'Hello John!', '{test name="John" /}'
|
||||
end
|
||||
|
||||
def test_parse_tag__binding_render_tag_with_block
|
||||
define_tag('test') { |tag| "Hello #{tag.expand}!" }
|
||||
define_tag('hello') { |tag| tag.render('test') { tag.expand } }
|
||||
assert_parse_output 'Hello John!', '{hello}John{/hello}'
|
||||
end
|
||||
|
||||
def test_tag_locals
|
||||
define_tag "outer" do |tag|
|
||||
tag.locals.var = 'outer'
|
||||
tag.expand
|
||||
end
|
||||
define_tag "outer:inner" do |tag|
|
||||
tag.locals.var = 'inner'
|
||||
tag.expand
|
||||
end
|
||||
define_tag "outer:var" do |tag|
|
||||
tag.locals.var
|
||||
end
|
||||
assert_parse_output 'outer', "{outer}{var /}{/outer}"
|
||||
assert_parse_output 'outer:inner:outer', "{outer}{var /}:{inner}{var /}{/inner}:{var /}{/outer}"
|
||||
assert_parse_output 'outer:inner:outer:inner:outer', "{outer}{var /}:{inner}{var /}:{outer}{var /}{/outer}:{var /}{/inner}:{var /}{/outer}"
|
||||
assert_parse_output 'outer', "{outer:var /}"
|
||||
end
|
||||
|
||||
def test_tag_globals
|
||||
define_tag "set" do |tag|
|
||||
tag.globals.var = tag.attr['value']
|
||||
''
|
||||
end
|
||||
define_tag "var" do |tag|
|
||||
tag.globals.var
|
||||
end
|
||||
assert_parse_output " true false", %[{var /} {set value="true" /} {var /} {set value="false" /} {var /}]
|
||||
end
|
||||
|
||||
def test_parse_loops
|
||||
@item = nil
|
||||
define_tag "each" do |tag|
|
||||
result = []
|
||||
["Larry", "Moe", "Curly"].each do |item|
|
||||
tag.locals.item = item
|
||||
result << tag.expand
|
||||
end
|
||||
result.join(tag.attr["between"] || "")
|
||||
end
|
||||
define_tag "each:item" do |tag|
|
||||
tag.locals.item
|
||||
end
|
||||
assert_parse_output %[Three Stooges: "Larry", "Moe", "Curly"], %[Three Stooges: {each between=", "}"{item /}"{/each}]
|
||||
end
|
||||
|
||||
def test_parse_speed
|
||||
define_tag "set" do |tag|
|
||||
tag.globals.var = tag.attr['value']
|
||||
''
|
||||
end
|
||||
define_tag "var" do |tag|
|
||||
tag.globals.var
|
||||
end
|
||||
parts = %w{decima nobis augue at facer processus commodo legentis odio lectorum dolore nulla esse lius qui nonummy ullamcorper erat ii notare}
|
||||
multiplier = parts.map{|p| "#{p}=\"#{rand}\""}.join(' ')
|
||||
assert_nothing_raised do
|
||||
Timeout.timeout(10) do
|
||||
assert_parse_output " false", %[{set value="false" #{multiplier} /} {var /}]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_tag_option_for
|
||||
define_tag 'fun', :for => 'just for kicks'
|
||||
assert_parse_output 'just for kicks', '{fun /}'
|
||||
end
|
||||
|
||||
def test_tag_expose_option
|
||||
define_tag 'user', :for => users.first, :expose => ['name', :age]
|
||||
assert_parse_output 'John', '{user:name /}'
|
||||
assert_parse_output '25', '{user}{age /}{/user}'
|
||||
e = assert_raises(Radius::UndefinedTagError) { @parser.parse "{user:email /}" }
|
||||
assert_equal "undefined tag `email'", e.message
|
||||
end
|
||||
|
||||
def test_tag_expose_attributes_option_on_by_default
|
||||
define_tag 'user', :for => user_with_attributes
|
||||
assert_parse_output 'John', '{user:name /}'
|
||||
end
|
||||
def test_tag_expose_attributes_set_to_false
|
||||
define_tag 'user_without_attributes', :for => user_with_attributes, :attributes => false
|
||||
assert_raises(Radius::UndefinedTagError) { @parser.parse "{user_without_attributes:name /}" }
|
||||
end
|
||||
|
||||
def test_tag_options_must_contain_a_for_option_if_methods_are_exposed
|
||||
e = assert_raises(ArgumentError) { define_tag('fun', :expose => :today) { 'test' } }
|
||||
assert_equal "tag definition must contain a :for option when used with the :expose option", e.message
|
||||
end
|
||||
|
||||
def test_parse_fail_on_missing_end_tag
|
||||
assert_raises(Radius::MissingEndTagError) { @parser.parse("{reverse}") }
|
||||
end
|
||||
|
||||
def test_parse_fail_on_wrong_end_tag
|
||||
assert_raises(Radius::WrongEndTagError) { @parser.parse("{reverse}{capitalize}{/reverse}") }
|
||||
end
|
||||
|
||||
def test_copyin_global_values
|
||||
@context.globals.foo = 'bar'
|
||||
assert_equal 'bar', Radius::Parser.new(@context).context.globals.foo
|
||||
end
|
||||
|
||||
def test_does_not_pollute_copied_globals
|
||||
@context.globals.foo = 'bar'
|
||||
parser = Radius::Parser.new(@context)
|
||||
parser.context.globals.foo = '[baz]'
|
||||
assert_equal 'bar', @context.globals.foo
|
||||
end
|
||||
|
||||
def test_parse_with_other_namespaces
|
||||
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
|
||||
assert_equal "{fb:test}hello world{/fb:test}", @parser.parse("{fb:test}hello world{/fb:test}")
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def assert_parse_output(output, input, message = nil)
|
||||
r = @parser.parse(input)
|
||||
assert_equal(output, r, message)
|
||||
end
|
||||
|
||||
def assert_parsed_is_unchanged(something)
|
||||
assert_parse_output something, something
|
||||
end
|
||||
|
||||
class User
|
||||
attr_accessor :name, :age, :email, :friend
|
||||
def initialize(name, age, email)
|
||||
@name, @age, @email = name, age, email
|
||||
end
|
||||
def <=>(other)
|
||||
name <=> other.name
|
||||
end
|
||||
end
|
||||
|
||||
class UserWithAttributes < User
|
||||
def attributes
|
||||
{ :name => name, :age => age, :email => email }
|
||||
end
|
||||
end
|
||||
|
||||
def users
|
||||
[
|
||||
User.new('John', 25, 'test@johnwlong.com'),
|
||||
User.new('James', 27, 'test@jameslong.com')
|
||||
]
|
||||
end
|
||||
|
||||
def user_with_attributes
|
||||
UserWithAttributes.new('John', 25, 'test@johnwlong.com')
|
||||
end
|
||||
|
||||
end
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
require 'rubygems'
|
||||
require 'timeout'
|
||||
|
||||
unless defined? RADIUS_LIB
|
||||
|
||||
RADIUS_LIB = File.join(File.dirname(__FILE__), '..', 'lib')
|
||||
$LOAD_PATH << RADIUS_LIB
|
||||
|
||||
require 'radius'
|
||||
require 'test/unit'
|
||||
|
||||
module RadiusTestHelper
|
||||
class TestContext < Radius::Context; end
|
||||
|
||||
def new_context
|
||||
Radius::Context.new do |c|
|
||||
c.define_tag("reverse" ) { |tag| tag.expand.reverse }
|
||||
c.define_tag("capitalize") { |tag| tag.expand.upcase }
|
||||
c.define_tag("echo" ) { |tag| tag.attr['value'] }
|
||||
c.define_tag("wrap" ) { |tag| "[#{tag.expand}]" }
|
||||
c.define_tag("attr") do |tag|
|
||||
kv = tag.attr.keys.sort.collect{|k| "#{k.inspect}=>#{tag[k].inspect}"}
|
||||
"{#{kv.join(', ')}}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def define_tag(name, options = {}, &block)
|
||||
@parser.context.define_tag name, options, &block
|
||||
end
|
||||
|
||||
def define_global_tag(name, options = {}, &block)
|
||||
@context.define_tag name, options, &block
|
||||
end
|
||||
end
|
||||
end
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
require 'test/unit'
|
||||
require 'radius'
|
||||
|
||||
class RadiusUtilityTest < Test::Unit::TestCase
|
||||
|
||||
def test_symbolize_keys
|
||||
h = Radius::Utility.symbolize_keys({ 'a' => 1, :b => 2 })
|
||||
assert_equal h[:a], 1
|
||||
assert_equal h[:b], 2
|
||||
end
|
||||
|
||||
def test_impartial_hash_delete
|
||||
h = { 'a' => 1, :b => 2 }
|
||||
assert_equal Radius::Utility.impartial_hash_delete(h, :a), 1
|
||||
assert_equal Radius::Utility.impartial_hash_delete(h, 'b'), 2
|
||||
assert_equal h.empty?, true
|
||||
end
|
||||
|
||||
def test_constantize
|
||||
assert_equal Radius::Utility.constantize('String'), String
|
||||
end
|
||||
|
||||
def test_camelize
|
||||
assert_equal Radius::Utility.camelize('ab_cd_ef'), 'AbCdEf'
|
||||
end
|
||||
|
||||
def test_array_to_s
|
||||
assert_equal Radius::Utility.array_to_s(['a', 1, [:c]]), 'a1c'
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user