raf-parser 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafblockparser.tab'
5
+
6
+ class TestRafBlockParser < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::BlockParser.new
9
+ @single_line_str = "aaa"
10
+ @multi_line_str = <<EOL
11
+ aaa bbb ccc
12
+ ddd eee fff
13
+ EOL
14
+ @multi_block_str = <<EOL
15
+ aaa bbb ccc
16
+ ddd eee fff
17
+
18
+ ggg hhh iii
19
+ jjj kkk lll
20
+ EOL
21
+
22
+ @single_line_with_inline_elements_str = <<EOL
23
+ aaa ((*bbb*)) ((*ccc*))
24
+ EOL
25
+
26
+ @one_space = <<EOL
27
+ aaa
28
+ EOL
29
+ end
30
+
31
+ def test_paragraph_is_paragraph
32
+ nodes = @raf.parse(@single_line_str)
33
+ assert_kind_of Raf::Paragraph, nodes[0]
34
+ end
35
+
36
+ def test_paragraph_single_line_node_is_plain_text
37
+ nodes = @raf.parse(@single_line_str)
38
+ nodes.each do |node|
39
+ assert_kind_of Raf::Plain, node.contents.first
40
+ assert_kind_of String, node.apply
41
+ assert_equal node.apply, @single_line_str
42
+ end
43
+ end
44
+
45
+ def test_paragraph_multi_line
46
+ nodes = @raf.parse(@multi_line_str)
47
+ assert_kind_of String, nodes.first.apply
48
+ assert_equal nodes.first.apply, @multi_line_str
49
+ end
50
+
51
+ def test_paragraph_multi_block
52
+ nodes = @raf.parse(@multi_block_str)
53
+ assert_equal 3, nodes.size # textblock, whiteline, textblock
54
+ assert_kind_of Raf::Plain, nodes[0].contents[0]
55
+ assert_kind_of String, nodes[0].apply
56
+ assert_kind_of Raf::WhiteLine, nodes[1]
57
+ assert_kind_of String, nodes[1].apply
58
+ assert_kind_of Raf::Plain, nodes[2].contents[0]
59
+ assert_kind_of String, nodes[2].apply
60
+ end
61
+
62
+ def test_paragraph_with_inline_elements
63
+ # nodes = plain emphasis plain(space) emphasis
64
+ nodes = @raf.parse(@single_line_with_inline_elements_str)
65
+ assert_kind_of Raf::Emphasis, nodes[0].contents[1]
66
+ assert_kind_of Raf::Emphasis, nodes[0].contents[3]
67
+
68
+ assert_equal "aaa <Emphasis>bbb</Emphasis> <Emphasis>ccc</Emphasis>\n", nodes[0].apply
69
+ end
70
+
71
+ def test_paragraph_one_space
72
+ res = @raf.parse(@one_space)
73
+ assert_kind_of Raf::Paragraph, res[0]
74
+ assert_equal " aaa\n", res[0].apply
75
+ end
76
+ end
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafblockparser.tab'
5
+
6
+ class TestRafQuoteBlock < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @raf = Raf::BlockParser.new
10
+ @quote = QuoteString.new
11
+ end
12
+
13
+ def test_quote_is_quote
14
+ nodes = @raf.parse(@quote.single)
15
+ assert_kind_of Raf::Quote, nodes[0]
16
+ assert_equal delete_indent(@quote.single), nodes[0].apply
17
+ end
18
+
19
+ def test_quote_multi
20
+ nodes = @raf.parse(@quote.multi)
21
+ assert_equal nodes.size, 1, "ホワイトリストがあっても1ブロック" # [quote]
22
+ assert_kind_of Raf::Quote, nodes[0]
23
+ assert_equal delete_indent(@quote.multi), nodes[0].apply
24
+ end
25
+
26
+ def test_quote_inline_element
27
+ nodes = @raf.parse(@quote.inline_element)
28
+ assert_kind_of Raf::Quote, nodes[0]
29
+ # インラインエレメントは変換しないでそのまま
30
+ assert_equal delete_indent(@quote.inline_element), nodes[0].apply
31
+ end
32
+
33
+ def test_quote_multi_indent
34
+ nodes = @raf.parse(@quote.multi_indent)
35
+ assert_kind_of Raf::Quote, nodes[0]
36
+ # インデントを無視する
37
+ assert_equal "aaa\n bbb\n ccc\n ddd\nfff", nodes[0].apply
38
+ end
39
+
40
+ def test_quote_space
41
+ res1 = @raf.parse(@quote.space)
42
+ assert_kind_of Raf::Quote, res1[0]
43
+ assert_equal "aaa\nbbb",res1[0].apply
44
+
45
+ res2 = @raf.parse(@quote.space2)
46
+ assert_kind_of Raf::Quote, res2[0]
47
+ assert_equal "aaa\nbbb",res2[0].apply
48
+ end
49
+
50
+ def delete_indent(str)
51
+ str.map {|s| s.sub(/^ /, "") }.to_s.strip
52
+ end
53
+ end
54
+
55
+
56
+ class QuoteString
57
+ def single
58
+ <<EOL
59
+ aaa
60
+ bbb
61
+ EOL
62
+ end
63
+
64
+ def multi
65
+ <<EOL
66
+ aaa
67
+ bbb
68
+
69
+ ccc
70
+ ddd
71
+ EOL
72
+ end
73
+
74
+ def inline_element
75
+ <<EOL
76
+ ((*aaa*))
77
+ ((_bbb_))
78
+ EOL
79
+ end
80
+
81
+ def multi_indent
82
+ <<EOL
83
+ aaa
84
+ bbb
85
+ ccc
86
+ ddd
87
+ fff
88
+ EOL
89
+ end
90
+
91
+ def space
92
+ <<EOL
93
+ aaa
94
+ bbb
95
+
96
+ ccc
97
+ EOL
98
+ end
99
+
100
+ def space2
101
+ <<EOL
102
+ aaa
103
+ bbb
104
+
105
+
106
+ ccc
107
+ EOL
108
+ end
109
+ end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafinlineparser.tab'
5
+
6
+ class TestRafReference < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::InlineParser.new
9
+ end
10
+
11
+ def test_reference_is_reference
12
+ res = @raf.parse('((<aaa>))')
13
+ assert_kind_of Raf::Reference, res[0]
14
+ end
15
+
16
+ def test_reference_label_only
17
+ str = '((<aaa>))'
18
+ res = @raf.parse(str)
19
+ assert_kind_of Raf::Reference, res[0]
20
+ assert_equal "aaa", res[0].contents[0] # title
21
+ assert_equal "#" + "aaa".to_code, res[0].contents[1] # label
22
+ end
23
+
24
+ def test_reference_title_and_label
25
+ str = '((<aaa|hoge>))'
26
+ res = @raf.parse(str)
27
+ assert_kind_of Raf::Reference, res[0]
28
+ assert_equal "aaa", res[0].contents[0] # title
29
+ assert_equal "#" + "hoge".to_code, res[0].contents[1] # label
30
+ end
31
+
32
+ def test_reference_uri_only
33
+ str = '((<http://example.com>))'
34
+ res = @raf.parse(str)
35
+ assert_kind_of Raf::Reference, res[0]
36
+ assert_equal "http://example.com", res[0].contents[0] # title
37
+ assert_equal "http://example.com", res[0].contents[1] # label
38
+ end
39
+
40
+ def test_reference_title_and_uri
41
+ str = '((<aaa|http://example.com>))'
42
+ res = @raf.parse(str)
43
+ assert_kind_of Raf::Reference, res[0]
44
+ assert_equal "aaa", res[0].contents[0] # title
45
+ assert_equal "http://example.com", res[0].contents[1] # label
46
+ end
47
+ end
data/test/test_ruby.rb ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafinlineparser.tab'
5
+
6
+ class TestRafRuby < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::InlineParser.new
9
+ end
10
+
11
+ def test_ruby_is_ruby
12
+ str = '((^AAA|aaa^))'
13
+ res = @raf.parse(str)
14
+ assert_kind_of Raf::Ruby, res[0]
15
+ assert_equal ["AAA","aaa"], res[0].contents
16
+ assert_equal '<Ruby>Base:AAA,Text:aaa</Ruby>', res[0].apply
17
+ end
18
+
19
+ def test_ruby_base_only
20
+ str = '((^AAA|^))'
21
+ res = @raf.parse(str)
22
+ assert_kind_of Raf::Ruby, res[0]
23
+ assert_equal '<Ruby>Base:AAA,Text:</Ruby>', res[0].apply
24
+
25
+ str2 = '((^AAA^))'
26
+ res = @raf.parse(str2)
27
+ assert_kind_of Raf::Ruby, res[0]
28
+ assert_equal '<Ruby>Base:AAA,Text:AAA</Ruby>', res[0].apply
29
+ end
30
+
31
+ def test_ruby_text_only
32
+ str = '((^|aaa^))'
33
+ res = @raf.parse(str)
34
+ assert_kind_of Raf::Ruby, res[0]
35
+ assert_equal '<Ruby>Base:,Text:aaa</Ruby>', res[0].apply
36
+ end
37
+
38
+ def test_ruby_in_element
39
+ str1 = '((^((*AAA*))|aaa^))'
40
+ res = @raf.parse(str1)
41
+ assert_equal '<Ruby>Base:((*AAA*)),Text:aaa</Ruby>', res[0].apply
42
+
43
+ str2 = '((*aaa|((^AAA|bbb^))*))'
44
+ res = @raf.parse(str2)
45
+ assert_equal '<Emphasis>aaa|<Ruby>Base:AAA,Text:bbb</Ruby></Emphasis>', res[0].apply
46
+ end
47
+
48
+ def test_emphasis_raise_non_terminate
49
+ str = '((^aaa))'
50
+ assert_raise Racc::ParseError do
51
+ @raf.parse(str)
52
+ end
53
+
54
+ str = '((^aaa'
55
+ assert_raise Racc::ParseError do
56
+ @raf.parse(str)
57
+ end
58
+ end
59
+
60
+ def test_emphasis_raise_different_terminate
61
+ str = '((^aaa-))'
62
+ assert_raise Racc::ParseError do
63
+ @raf.parse(str)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafinlineparser.tab'
5
+
6
+ class TestRafStrike < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::InlineParser.new
9
+ end
10
+
11
+ def test_strike_is_strike
12
+ str = '((-aaa-))'
13
+ res = @raf.parse(str)
14
+ assert_kind_of Raf::Strike, res[0]
15
+ end
16
+
17
+ def test_strike_apply
18
+ str = '((-aaa-))'
19
+ res = @raf.parse(str)
20
+ assert_respond_to res[0], 'apply'
21
+ assert_equal '<Strike>aaa</Strike>', res[0].apply
22
+ end
23
+
24
+ def test_strike_in_element
25
+ str1 = '((-((-aaa-))-))'
26
+ res = @raf.parse(str1)
27
+ assert_equal [Raf::Strike], res[0].contents.map {|c| c.class }
28
+ assert_equal '<Strike><Strike>aaa</Strike></Strike>', res[0].apply
29
+
30
+ str2 = '((-a((-a-))a-))'
31
+ res = @raf.parse(str2)
32
+ assert_equal [Raf::Plain, Raf::Strike, Raf::Plain], res[0].contents.map {|c| c.class }
33
+ assert_equal '<Strike>a<Strike>a</Strike>a</Strike>', res[0].apply
34
+
35
+ str3 = '((-a((*a*))a-))'
36
+ res = @raf.parse(str3)
37
+ assert_equal [Raf::Plain, Raf::Emphasis, Raf::Plain], res[0].contents.map {|c| c.class }
38
+ assert_equal '<Strike>a<Emphasis>a</Emphasis>a</Strike>', res[0].apply
39
+ end
40
+
41
+ def test_strike_raise_non_terminate
42
+ str = '((-aaa))'
43
+ assert_raise Racc::ParseError do
44
+ @raf.parse(str)
45
+ end
46
+
47
+ str = '((-aaa'
48
+ assert_raise Racc::ParseError do
49
+ @raf.parse(str)
50
+ end
51
+ end
52
+
53
+ def test_strike_raise_different_terminate
54
+ str = '((-aaa*))'
55
+ assert_raise Racc::ParseError do
56
+ @raf.parse(str)
57
+ end
58
+ end
59
+
60
+ def test_strike_linefeed
61
+ str1 = <<EOL
62
+ ((-a
63
+ aa-))
64
+ EOL
65
+ res = @raf.parse(str1)
66
+ assert_kind_of Raf::Strike, res[0]
67
+ assert_equal "<Strike>a\naa</Strike>", res[0].apply
68
+
69
+ str2 = '((-a
70
+
71
+ aa-))'
72
+ res = @raf.parse(str2)
73
+ assert_kind_of Raf::Strike, res[0]
74
+ assert_equal "<Strike>a\n\naa</Strike>", res[0].apply
75
+ end
76
+ end
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafblockparser.tab'
5
+
6
+ class TestRafTableBlock < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::BlockParser.new
9
+ @table = TableString.new
10
+ end
11
+
12
+ def test_table_is_table
13
+ res = @raf.parse(@table.single)
14
+ assert_kind_of Raf::Table, res[0]
15
+ assert_equal "|aaa|bbb\n|ccc|ddd\n", res[0].apply
16
+ end
17
+
18
+ def test_table_with_terminate
19
+ res = @raf.parse(@table.single_with_terminate)
20
+ assert_kind_of Raf::Table, res[0]
21
+ assert_equal "|aaa|bbb\n|ccc|ddd\n", res[0].apply
22
+ end
23
+
24
+ def test_table_with_head
25
+ res = @raf.parse(@table.head)
26
+ assert_kind_of Raf::Table, res[0]
27
+ assert_equal "|*aaa|*bbb\n|ccc|ddd\n", res[0].apply
28
+ end
29
+
30
+ def test_table_with_head_and_terminate
31
+ res = @raf.parse(@table.head_with_terminate)
32
+ assert_kind_of Raf::Table, res[0]
33
+ assert_equal "|*aaa|*bbb\n|ccc|ddd\n", res[0].apply
34
+ end
35
+ end
36
+
37
+ class TableString
38
+ def single
39
+ <<EOL
40
+ |aaa|bbb
41
+ |ccc|ddd
42
+ EOL
43
+ end
44
+ def single_with_terminate
45
+ <<EOL
46
+ |aaa|bbb|
47
+ |ccc|ddd|
48
+ EOL
49
+ end
50
+
51
+ def head
52
+ <<EOL
53
+ |*aaa|*bbb|
54
+ |ccc|ddd|
55
+ EOL
56
+ end
57
+ def head_with_terminate
58
+ <<EOL
59
+ |*aaa*|*bbb*|
60
+ |ccc|ddd|
61
+ EOL
62
+ end
63
+ end
data/test/test_verb.rb ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
3
+ require 'test_helper'
4
+ require 'rafinlineparser.tab'
5
+
6
+ class TestRafVerb < Test::Unit::TestCase
7
+ def setup
8
+ @raf = Raf::InlineParser.new
9
+ end
10
+
11
+ def test_verb_is_verb
12
+ str = "(('aaa'))"
13
+ res = @raf.parse(str)
14
+ assert_kind_of Raf::Verb, res[0]
15
+ assert_equal ["aaa"], res[0].contents
16
+ assert_equal '<Verb>aaa</Verb>', res[0].apply
17
+ end
18
+
19
+ def test_verb_in_emphasis
20
+ str = "(('((*aaa*))'))"
21
+ res = @raf.parse(str)
22
+ assert_kind_of Raf::Verb, res[0]
23
+ assert_equal '<Verb>((*aaa*))</Verb>', res[0].apply
24
+
25
+ str = "(('((*aaa))'))"
26
+ res = @raf.parse(str)
27
+ assert_kind_of Raf::Verb, res[0]
28
+ assert_equal '<Verb>((*aaa))</Verb>', res[0].apply
29
+ end
30
+
31
+ def test_verb_in_italic
32
+ str = "(('((_aaa_))'))"
33
+ res = @raf.parse(str)
34
+ assert_kind_of Raf::Verb, res[0]
35
+ assert_equal '<Verb>((_aaa_))</Verb>', res[0].apply
36
+ end
37
+
38
+ def test_verb_in_strike
39
+ str = "(('((-aaa-))'))"
40
+ res = @raf.parse(str)
41
+ assert_kind_of Raf::Verb, res[0]
42
+ assert_equal '<Verb>((-aaa-))</Verb>', res[0].apply
43
+ end
44
+
45
+ def test_verb_in_ruby
46
+ str = "(('((^aaa^))'))"
47
+ res = @raf.parse(str)
48
+ assert_kind_of Raf::Verb, res[0]
49
+ assert_equal '<Verb>((^aaa^))</Verb>', res[0].apply
50
+ end
51
+
52
+ def test_verb_in_footnote
53
+ str = "(('(([aaa]))'))"
54
+ res = @raf.parse(str)
55
+ assert_kind_of Raf::Verb, res[0]
56
+ assert_equal '<Verb>(([aaa]))</Verb>', res[0].apply
57
+ end
58
+
59
+ def test_verb_in_image
60
+ str = "(('(($aaa$))'))"
61
+ res = @raf.parse(str)
62
+ assert_kind_of Raf::Verb, res[0]
63
+ assert_equal '<Verb>(($aaa$))</Verb>', res[0].apply
64
+ end
65
+
66
+ def test_verb_in_reference
67
+ str = "(('((<aaa>))'))"
68
+ res = @raf.parse(str)
69
+ assert_kind_of Raf::Verb, res[0]
70
+ assert_equal '<Verb>((<aaa>))</Verb>', res[0].apply
71
+ end
72
+
73
+ def test_verb_in_manuedo
74
+ str = "(('((/aaa/))'))"
75
+ res = @raf.parse(str)
76
+ assert_kind_of Raf::Verb, res[0]
77
+ assert_equal '<Verb>((/aaa/))</Verb>', res[0].apply
78
+ end
79
+
80
+ def test_verb_in_erb
81
+ str = "(('<%= aaa %>'))"
82
+ res = @raf.parse(str)
83
+ assert_kind_of Raf::Verb, res[0]
84
+ assert_equal '<Verb><%= aaa %></Verb>', res[0].apply
85
+ end
86
+ end