| 1 |
require 'test/unit' |
|---|
| 2 |
require 'yaml' |
|---|
| 3 |
require File.join(File.dirname(__FILE__) , '..', 'lib', 'parser') |
|---|
| 4 |
require File.join(File.dirname(__FILE__), '..','..','yaml_test') |
|---|
| 5 |
require 'ruby-debug' |
|---|
| 6 |
Debugger.start |
|---|
| 7 |
unless Module.const_defined?(:ActiveRecord) |
|---|
| 8 |
|
|---|
| 9 |
class String |
|---|
| 10 |
def blank? |
|---|
| 11 |
empty? || strip.empty? |
|---|
| 12 |
end |
|---|
| 13 |
end |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
class ParserModule::DummyHelper |
|---|
| 17 |
def find_node_by_pseudo(*args) |
|---|
| 18 |
args[0] |
|---|
| 19 |
end |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
module Zafu |
|---|
| 23 |
module Tags |
|---|
| 24 |
def r_hello |
|---|
| 25 |
'hello world!' |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def r_only_hello |
|---|
| 29 |
expand_with(:only=>['hello']) |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def r_only_string |
|---|
| 33 |
out expand_with(:only=>[:string]) |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
def r_text |
|---|
| 37 |
@params[:text] |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
def r_repeat |
|---|
| 41 |
count = @params[:count] || 2 |
|---|
| 42 |
count.to_i.times do |
|---|
| 43 |
out expand_with |
|---|
| 44 |
end |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def r_set_context |
|---|
| 48 |
params = @params.dup |
|---|
| 49 |
if ignore = @params[:ignore] |
|---|
| 50 |
params[:ignore] = @params[:ignore].split(',').map {|e| e.strip} |
|---|
| 51 |
end |
|---|
| 52 |
expand_with(params) |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def r_missing |
|---|
| 56 |
return '' unless check_params(:good, :night) |
|---|
| 57 |
"nothing missing" |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def r_textarea |
|---|
| 61 |
res = "<#{@html_tag}#{params_to_html(@params)}" |
|---|
| 62 |
@html_tag_done = true |
|---|
| 63 |
inner = expand_with |
|---|
| 64 |
if inner == '' |
|---|
| 65 |
res + "/>" |
|---|
| 66 |
else |
|---|
| 67 |
res + ">#{inner}" |
|---|
| 68 |
end |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
def r_test |
|---|
| 72 |
self.inspect |
|---|
| 73 |
end |
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
class String |
|---|
| 78 |
def pseudo_id(*args) |
|---|
| 79 |
self |
|---|
| 80 |
end |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
class ParserTest < Test::Unit::TestCase |
|---|
| 84 |
yaml_test :files => [:zafu, :zafu_asset, :zafu_insight, :zazen] |
|---|
| 85 |
@@test_parsers = {} |
|---|
| 86 |
@@test_options = {} |
|---|
| 87 |
|
|---|
| 88 |
@@file_list.each do |file, file_path, opts| |
|---|
| 89 |
mod_name = opts.delete(:module) || file |
|---|
| 90 |
mod_name = mod_name.to_s.split("_").first.capitalize |
|---|
| 91 |
@@test_parsers[file] = Parser.parser_with_rules(eval("#{mod_name}::Rules"), eval("#{mod_name}::Tags")) |
|---|
| 92 |
@@test_options[file] = opts |
|---|
| 93 |
end |
|---|
| 94 |
|
|---|
| 95 |
def do_test(file, test) |
|---|
| 96 |
res = @@test_parsers[file].new_with_url("/#{test.gsub('_', '/')}", :helper=>ParserModule::DummyHelper.new(@@test_strings[file])).render(@@test_options[file]) |
|---|
| 97 |
if @@test_strings[file][test]['res'] |
|---|
| 98 |
assert_yaml_test @@test_strings[file][test]['res'], res |
|---|
| 99 |
end |
|---|
| 100 |
end |
|---|
| 101 |
|
|---|
| 102 |
def test_single |
|---|
| 103 |
do_test('zafu', 'only_hello') |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
def test_zazen_image_no_image |
|---|
| 107 |
file = 'zazen' |
|---|
| 108 |
test = 'image_no_image' |
|---|
| 109 |
res = @@test_parsers[file].new_with_url("/#{test.gsub('_', '/')}", :helper=>ParserModule::DummyHelper.new(@@test_strings[file])).render(:images=>false) |
|---|
| 110 |
assert_equal @@test_strings[file][test]['res'], res |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
def test_all_descendants |
|---|
| 114 |
block = @@test_parsers['zafu'].new( |
|---|
| 115 |
"<r:pages><r:each><b do='test'/></r:each><r:add><p><i do='add_link'/><b do='title'/></p></r:add><b do='title'/></r:pages>", |
|---|
| 116 |
:helper=>ParserModule::DummyHelper.new(@@test_strings['basic'])) |
|---|
| 117 |
assert_equal ['add', 'add_link', 'each', 'pages', 'test', 'title'], block.all_descendants.keys.sort |
|---|
| 118 |
assert_equal 2, block.all_descendants['title'].size |
|---|
| 119 |
assert_equal ['add_link', 'title'], block.descendant('add').all_descendants.keys.sort |
|---|
| 120 |
end |
|---|
| 121 |
|
|---|
| 122 |
def test_descendants |
|---|
| 123 |
block = @@test_parsers['zafu'].new( |
|---|
| 124 |
"<r:pages><r:each><b do='test'/></r:each><r:add><p><i do='add_link'/><b do='title'/></p></r:add><b do='title'/></r:pages>", |
|---|
| 125 |
:helper=>ParserModule::DummyHelper.new(@@test_strings['basic'])) |
|---|
| 126 |
assert_equal 2, block.descendants('title').size |
|---|
| 127 |
assert_equal ['test'], block.descendants('each')[0].descendants('test').map {|n| n.method} |
|---|
| 128 |
assert_equal [], block.descendants('each')[0].descendants('foo') |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
def test_ancestor |
|---|
| 132 |
block = @@test_parsers['zafu'].new( |
|---|
| 133 |
"<r:pages><r:each><b do='test'/></r:each><r:add><p><i do='add_link'/><b do='title'/></p></r:add><b do='title'/></r:pages>", |
|---|
| 134 |
:helper=>ParserModule::DummyHelper.new(@@test_strings['basic'])) |
|---|
| 135 |
sub_block = block.descendant('add_link') |
|---|
| 136 |
assert_equal ['void', 'pages', 'add'], sub_block.ancestors.map{|a| a.method} |
|---|
| 137 |
assert_equal sub_block.ancestor('pages'), block.descendant('pages') |
|---|
| 138 |
end |
|---|
| 139 |
|
|---|
| 140 |
def test_public_descendants |
|---|
| 141 |
block = @@test_parsers['zafu'].new( |
|---|
| 142 |
"<r:pages><r:each><b do='test'/></r:each><r:add><p><i do='add_link'/><b do='title'/></p></r:add><b do='title'/></r:pages>", |
|---|
| 143 |
:helper=>ParserModule::DummyHelper.new(@@test_strings['basic'])) |
|---|
| 144 |
block.all_descendants.merge('self'=>[block]).each do |k,blocks| |
|---|
| 145 |
blocks.each do |b| |
|---|
| 146 |
b.send(:remove_instance_variable, :@all_descendants) |
|---|
| 147 |
class << b |
|---|
| 148 |
def public_descendants |
|---|
| 149 |
if ['each'].include?(@method) |
|---|
| 150 |
{} |
|---|
| 151 |
else |
|---|
| 152 |
super |
|---|
| 153 |
end |
|---|
| 154 |
end |
|---|
| 155 |
end |
|---|
| 156 |
end |
|---|
| 157 |
end |
|---|
| 158 |
assert_equal ['add', 'add_link', 'each', 'pages', 'title'], block.all_descendants.keys.sort |
|---|
| 159 |
assert_equal ['test'], block.descendant('each').all_descendants.keys.sort |
|---|
| 160 |
end |
|---|
| 161 |
|
|---|
| 162 |
def test_root |
|---|
| 163 |
block = @@test_parsers['zafu'].new( |
|---|
| 164 |
"<r:pages><r:each><b do='test'/></r:each><r:add><p><i do='add_link'/><b do='title'/></p></r:add><b do='title'/></r:pages>", |
|---|
| 165 |
:helper=>ParserModule::DummyHelper.new(@@test_strings['basic'])) |
|---|
| 166 |
sub_block = block.descendant('add_link') |
|---|
| 167 |
assert_equal 'add_link', sub_block.method |
|---|
| 168 |
assert_equal 'add', sub_block.parent.method |
|---|
| 169 |
assert_equal block, sub_block.root |
|---|
| 170 |
end |
|---|
| 171 |
|
|---|
| 172 |
make_tests |
|---|
| 173 |
end |
|---|