Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 4334 Bytes
1 | require File.join(File.dirname(__FILE__), 'test_helper') |
2 | |
3 | class QuickstartTest < Test::Unit::TestCase |
4 | |
5 | def test_hello_world |
6 | context = Radius::Context.new |
7 | context.define_tag "hello" do |tag| |
8 | "Hello #{tag.attr['name'] || 'World'}!" |
9 | end |
10 | parser = Radius::Parser.new(context) |
11 | assert_equal "<p>Hello World!</p>", parser.parse('<p><radius:hello /></p>') |
12 | assert_equal "<p>Hello John!</p>", parser.parse('<p><radius:hello name="John" /></p>') |
13 | end |
14 | |
15 | def test_example_2 |
16 | require 'redcloth' |
17 | context = Radius::Context.new |
18 | context.define_tag "textile" do |tag| |
19 | contents = tag.expand |
20 | RedCloth.new(contents).to_html |
21 | end |
22 | parser = Radius::Parser.new(context) |
23 | assert_equal "<h1>Hello <b>World</b>!</h1>", parser.parse('<radius:textile>h1. Hello **World**!</radius:textile>') |
24 | end |
25 | |
26 | def test_nested_example |
27 | context = Radius::Context.new |
28 | |
29 | context.define_tag "stooge" do |tag| |
30 | content = '' |
31 | ["Larry", "Moe", "Curly"].each do |name| |
32 | tag.locals.name = name |
33 | content << tag.expand |
34 | end |
35 | content |
36 | end |
37 | |
38 | context.define_tag "stooge:name" do |tag| |
39 | tag.locals.name |
40 | end |
41 | |
42 | parser = Radius::Parser.new(context) |
43 | |
44 | template = <<-TEMPLATE |
45 | <ul> |
46 | <radius:stooge> |
47 | <li><radius:name /></li> |
48 | </radius:stooge> |
49 | </ul> |
50 | TEMPLATE |
51 | |
52 | output = <<-OUTPUT |
53 | <ul> |
54 | |
55 | <li>Larry</li> |
56 | |
57 | <li>Moe</li> |
58 | |
59 | <li>Curly</li> |
60 | |
61 | </ul> |
62 | OUTPUT |
63 | |
64 | assert_equal output, parser.parse(template) |
65 | end |
66 | |
67 | class User |
68 | attr_accessor :name, :age, :email |
69 | end |
70 | def test_exposing_objects_example |
71 | context = Radius::Context.new |
72 | parser = Radius::Parser.new(context) |
73 | |
74 | context.define_tag "count", :for => 1 |
75 | assert_equal "1", parser.parse("<radius:count />") |
76 | |
77 | user = User.new |
78 | user.name, user.age, user.email = "John", 29, "john@example.com" |
79 | context.define_tag "user", :for => user, :expose => [ :name, :age, :email ] |
80 | assert_equal "John", parser.parse("<radius:user><radius:name /></radius:user>") |
81 | |
82 | assert_equal "John", parser.parse("<radius:user:name />") |
83 | end |
84 | |
85 | class LazyContext < Radius::Context |
86 | def tag_missing(tag, attr, &block) |
87 | "<strong>ERROR: Undefined tag `#{tag}' with attributes #{attr.inspect}</strong>" |
88 | end |
89 | end |
90 | def test_tag_missing_example |
91 | parser = Radius::Parser.new(LazyContext.new, :tag_prefix => 'lazy') |
92 | output = %{<strong>ERROR: Undefined tag `weird' with attributes {"value"=>"true"}</strong>} |
93 | assert_equal output, parser.parse('<lazy:weird value="true" />') |
94 | end |
95 | |
96 | def test_tag_globals_example |
97 | context = Radius::Context.new |
98 | parser = Radius::Parser.new(context) |
99 | |
100 | context.define_tag "inc" do |tag| |
101 | tag.globals.count ||= 0 |
102 | tag.globals.count += 1 |
103 | "" |
104 | end |
105 | |
106 | context.define_tag "count" do |tag| |
107 | tag.globals.count || 0 |
108 | end |
109 | |
110 | assert_equal "0 1", parser.parse("<radius:count /> <radius:inc /><radius:count />") |
111 | end |
112 | |
113 | class Person |
114 | attr_accessor :name, :friend |
115 | def initialize(name) |
116 | @name = name |
117 | end |
118 | end |
119 | def test_tag_locals_and_globals_example |
120 | jack = Person.new('Jack') |
121 | jill = Person.new('Jill') |
122 | jack.friend = jill |
123 | jill.friend = jack |
124 | |
125 | context = Radius::Context.new do |c| |
126 | c.define_tag "jack" do |tag| |
127 | tag.locals.person = jack |
128 | tag.expand |
129 | end |
130 | c.define_tag "jill" do |tag| |
131 | tag.locals.person = jill |
132 | tag.expand |
133 | end |
134 | c.define_tag "name" do |tag| |
135 | tag.locals.person.name rescue tag.missing! |
136 | end |
137 | c.define_tag "friend" do |tag| |
138 | tag.locals.person = tag.locals.person.friend rescue tag.missing! |
139 | tag.expand |
140 | end |
141 | end |
142 | |
143 | parser = Radius::Parser.new(context, :tag_prefix => 'r') |
144 | |
145 | assert_equal "Jack", parser.parse('<r:jack:name />') #=> "Jack" |
146 | assert_equal "Jill", parser.parse('<r:jill:name />') #=> "Jill" |
147 | assert_equal "Jack", parser.parse('<r:jill:friend:name />') #=> "Jack" |
148 | assert_equal "Jack", parser.parse('<r:jack:friend:friend:name />') #=> "Jack" |
149 | assert_equal "Jack and Jill", parser.parse('<r:jill><r:friend:name /> and <r:name /></r:jill>') #=> "Jack and Jill" |
150 | assert_raises(Radius::UndefinedTagError) { parser.parse('<r:name />') } # raises a Radius::UndefinedTagError exception |
151 | end |
152 | |
153 | end |