mathml 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ # Utility for tests
2
+ #
3
+ # Copyright (C) 2006, KURODA Hiraku <hiraku@hinet.mydns.jp>
4
+ # You can redistribute it and/or modify it under GPL2.
5
+
6
+ class Symbol
7
+ def <=>(s)
8
+ self.to_s<=>s.to_s
9
+ end
10
+ end
11
+ class Hash
12
+ def each
13
+ keys.sort.each do |k|
14
+ yield(k, self[k])
15
+ end
16
+ end
17
+ end
18
+
19
+ module Util4TC_MathML
20
+ def setup
21
+ @p = MathML::LaTeX::Parser.new unless @p
22
+ super
23
+ end
24
+
25
+ def strip(str)
26
+ str.gsub(/>\s*/, ">").gsub(/\s*</, "<")
27
+ end
28
+
29
+ def strip_mathml(mathml)
30
+ strip(mathml)[/\A<math [^>]*>(.*)<\/math>\Z/m, 1]
31
+ end
32
+
33
+ def smml(str, display_style=false, p=nil)
34
+ p = @p unless p
35
+ mml = p.parse(str, display_style).to_s
36
+ strip_mathml(mml)
37
+ end
38
+
39
+ def parse_error(e)
40
+ [e.message, e.done, e.rest].flatten
41
+ end
42
+
43
+ def assert_parse_error(message=nil, done=nil, rest=nil, write=false)
44
+ e = assert_raises(MathML::LaTeX::ParseError){yield}
45
+ puts e.inspect if write
46
+ assert_equal([message, done, rest], parse_error(e))
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ # Test for test_util
2
+ #
3
+ # Copyright (C) 2006, KURODA Hiraku <hiraku@hinet.mydns.jp>
4
+ # You can redistribute it and/or modify it under GPL2.
5
+
6
+ require "test/unit"
7
+ require "test/math_ml_test_util"
8
+ require "math_ml"
9
+
10
+ class TC_Util4TC_MathML < Test::Unit::TestCase
11
+ include Util4TC_MathML
12
+
13
+ def test_strip
14
+ assert_equal("><", strip(">\n \t<"))
15
+ assert_equal("<mn>1</mn>", smml("1"))
16
+ end
17
+
18
+ def test_strip_mathml
19
+ src = "<math test='dummy'> <a> b </a> <c> d </c></math>"
20
+ assert_equal("<a>b</a><c>d</c>", strip_mathml(src))
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ # Test for math_ml/util.rb
2
+ #
3
+ # Copyright (C) 2007, KURODA Hiraku <hiraku@hinet.mydns.jp>
4
+ # You can redistribute it and/or modify it under GPL2.
5
+
6
+ require "test/unit"
7
+ require "math_ml/string"
8
+
9
+ class TC_MathML_String < Test::Unit::TestCase
10
+ def setup
11
+ @mlp = MathML::LaTeX::Parser.new
12
+ end
13
+
14
+ def test_mathml_latex_parser
15
+ assert_kind_of(MathML::LaTeX::Parser, MathML::String.mathml_latex_parser)
16
+ mlp = MathML::LaTeX::Parser.new
17
+ MathML::String.mathml_latex_parser = mlp
18
+ assert_equal(mlp.object_id, MathML::String.mathml_latex_parser.object_id)
19
+ assert_raises(TypeError){MathML::String.mathml_latex_parser=String}
20
+ assert_equal(mlp.object_id, MathML::String.mathml_latex_parser.object_id)
21
+
22
+ MathML::String.mathml_latex_parser = nil
23
+ assert_kind_of(MathML::LaTeX::Parser, MathML::String.mathml_latex_parser)
24
+ assert_not_equal(mlp.object_id, MathML::String.mathml_latex_parser.object_id)
25
+ end
26
+
27
+ def test_to_mathml
28
+ assert_equal(@mlp.parse("").to_s, "".to_mathml.to_s)
29
+ assert_equal(@mlp.parse("", true).to_s, "".to_mathml(true).to_s)
30
+
31
+ MathML::String.mathml_latex_parser.macro.parse(<<'EOT')
32
+ \newcommand{\test}{x}
33
+ EOT
34
+ assert_equal(@mlp.parse("x").to_s, '\test'.to_mathml.to_s)
35
+ end
36
+ end
data/test/util_test.rb ADDED
@@ -0,0 +1,694 @@
1
+ # Test for math_ml/util.rb
2
+ #
3
+ # Copyright (C) 2006, KURODA Hiraku <hiraku@hinet.mydns.jp>
4
+ # You can redistribute it and/or modify it under GPL2.
5
+
6
+ require "test/unit"
7
+ require "math_ml_test_util"
8
+ require "math_ml"
9
+ require "math_ml/util"
10
+
11
+ class TC_MathML_Util < Test::Unit::TestCase
12
+ include MathML::Util
13
+
14
+ def test_escapeXML
15
+ assert_equal("&lt;&gt;&amp;&quot;&apos;", escapeXML("<>&\"'"))
16
+ assert_equal("\n", escapeXML("\n"))
17
+ assert_equal("<br />\n", escapeXML("\n", true))
18
+
19
+ assert_equal("&lt;&gt;&amp;&quot;&apos;", MathML::Util.escapeXML("<>&\"'"))
20
+ assert_equal("\n", MathML::Util.escapeXML("\n"))
21
+ assert_equal("<br />\n", MathML::Util.escapeXML("\n", true))
22
+ end
23
+
24
+ def test_collect_regexp
25
+ assert_equal(/#{/a/}|#{/b/}|#{/c/}/, collect_regexp([/a/, /b/, /c/]))
26
+ assert_equal(/#{/a/}|#{/b/}|#{/c/}/, collect_regexp([[/a/, /b/, /c/]]))
27
+ assert_equal(/(?!)/, collect_regexp([]))
28
+ assert_equal(/#{/a/}/, collect_regexp(/a/))
29
+
30
+ assert_equal(/#{/a/}|#{/b/}|#{/c/}/, MathML::Util.collect_regexp([/a/, /b/, /c/]))
31
+ assert_equal(/#{/a/}|#{/b/}|#{/c/}/, MathML::Util.collect_regexp([[/a/, /b/, /c/]]))
32
+ assert_equal(/(?!)/, MathML::Util.collect_regexp([]))
33
+ assert_equal(/#{/a/}/, MathML::Util.collect_regexp(/a/))
34
+
35
+ assert_equal(/#{/a/}|#{/b/}/, MathML::Util.collect_regexp([nil, /a/, "text", /b/]))
36
+
37
+ assert_equal(/#{/a/}|#{/b/}|#{/c/}/, MathML::Util.collect_regexp([nil, [/a/, [/b/, /c/]]]))
38
+ end
39
+
40
+ def test_invalid_re
41
+ assert_equal(/(?!)/, MathML::Util::INVALID_RE)
42
+ end
43
+ end
44
+
45
+ class TC_MathData < Test::Unit::TestCase
46
+ include MathML::Util
47
+
48
+ def test_update
49
+ m = MathData.new
50
+ m.math_list << "ml1"
51
+ m.msrc_list << "sl1"
52
+ m.dmath_list << "dml1"
53
+ m.dsrc_list << "dsl1"
54
+ m.escape_list << "el1"
55
+ m.esrc_list << "es1"
56
+ m.user_list << "ul1"
57
+ m.usrc_list << "usl1"
58
+ assert_equal(["ml1"], m.math_list)
59
+ assert_equal(["sl1"], m.msrc_list)
60
+ assert_equal(["dml1"], m.dmath_list)
61
+ assert_equal(["dsl1"], m.dsrc_list)
62
+ assert_equal(["el1"], m.escape_list)
63
+ assert_equal(["es1"], m.esrc_list)
64
+ assert_equal(["ul1"], m.user_list)
65
+ assert_equal(["usl1"], m.usrc_list)
66
+
67
+ m2 = MathData.new
68
+ m2.math_list << "ml2"
69
+ m2.msrc_list << "sl2"
70
+ m2.dmath_list << "dml2"
71
+ m2.dsrc_list << "dsl2"
72
+ m2.escape_list << "el2"
73
+ m2.esrc_list << "es2"
74
+ m2.user_list << "ul2"
75
+ m2.usrc_list << "usl2"
76
+
77
+ m.update(m2)
78
+
79
+ assert_equal(["ml1", "ml2"], m.math_list)
80
+ assert_equal(["sl1", "sl2"], m.msrc_list)
81
+ assert_equal(["dml1", "dml2"], m.dmath_list)
82
+ assert_equal(["dsl1", "dsl2"], m.dsrc_list)
83
+ assert_equal(["el1", "el2"], m.escape_list)
84
+ assert_equal(["es1", "es2"], m.esrc_list)
85
+ assert_equal(["ul1", "ul2"], m.user_list)
86
+ assert_equal(["usl1", "usl2"], m.usrc_list)
87
+ end
88
+ end
89
+
90
+ class TC_SimpleLaTeX < Test::Unit::TestCase
91
+ include MathML::Util
92
+ include Util4TC_MathML
93
+
94
+ def strip_math(s)
95
+ strip(s)[/<math.*?>(.*)<\/math>/m, 1]
96
+ end
97
+
98
+ def sma(a) # Stripped Mathml Array
99
+ r = []
100
+ a.each do |i|
101
+ r << strip_math(i.to_s)
102
+ end
103
+ r
104
+ end
105
+
106
+ def assert_data(src,
107
+ expected_math, expected_src,
108
+ expected_dmath, expected_dsrc,
109
+ expected_escaped, expected_esrc,
110
+ expected_encoded, expected_decoded,
111
+ simple_latex = SimpleLaTeX)
112
+ encoded, data = simple_latex.encode(src)
113
+
114
+ data.math_list.each do |i|
115
+ assert_equal("inline", i.attributes[:display])
116
+ end
117
+ data.dmath_list.each do |i|
118
+ assert_equal("block", i.attributes[:display])
119
+ end
120
+
121
+ assert_equal(expected_math, sma(data.math_list))
122
+ assert_equal(expected_src, data.msrc_list)
123
+ assert_equal(expected_dmath, sma(data.dmath_list))
124
+ assert_equal(expected_dsrc, data.dsrc_list)
125
+ assert_equal(expected_escaped, data.escape_list)
126
+ assert_equal(expected_esrc, data.esrc_list)
127
+ assert_equal(expected_encoded, encoded)
128
+ assert_equal(expected_decoded, simple_latex.decode(encoded, data))
129
+ end
130
+
131
+ def test_math
132
+ assert_data("a\n$\nb\n$\nc\\(\nd\n\\)e",
133
+ ["<mi>b</mi>", "<mi>d</mi>"],
134
+ ["$\nb\n$", "\\(\nd\n\\)"],
135
+ [], [], [], [],
136
+ "a\n\001m0\001\nc\001m1\001e",
137
+ "a\n<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>\nc<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>d</mi></math>e")
138
+
139
+ assert_data('$\\$$',
140
+ ["<mo>$</mo>"],
141
+ ['$\$$'], [], [], [], [], "\001m0\001",
142
+ "<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mo>$</mo></math>")
143
+ end
144
+
145
+ def test_dmath
146
+ assert_data("a\n$$\nb\n$$\nc\\[\nd\n\\]e",
147
+ [], [],
148
+ ["<mi>b</mi>", "<mi>d</mi>"],
149
+ ["$$\nb\n$$", "\\[\nd\n\\]"],
150
+ [], [],
151
+ "a\n\001d0\001\nc\001d1\001e",
152
+ "a\n<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>\nc<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>d</mi></math>e")
153
+ end
154
+
155
+ def test_math_and_dmath
156
+ assert_data('a$b$c$$d$$e\(f\)g\[h\]i',
157
+ ["<mi>b</mi>", "<mi>f</mi>"],
158
+ ["$b$", '\(f\)'],
159
+ ["<mi>d</mi>", "<mi>h</mi>"],
160
+ ["$$d$$", '\[h\]'],
161
+ [], [],
162
+ "a\001m0\001c\001d0\001e\001m1\001g\001d1\001i",
163
+ "a<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>c<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>d</mi></math>e<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>f</mi></math>g<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>h</mi></math>i")
164
+ end
165
+
166
+ def test_escapeing
167
+ assert_data('a\bc\d\e', [], [], [], [], ['b', 'd', 'e'], ['\b', '\d', '\e'], "a\001e0\001c\001e1\001\001e2\001", 'abcde')
168
+ assert_data('\$a$$b$$', [], [], ["<mi>b</mi>"], ["$$b$$"], [%[$]], ['\$'], "\001e0\001a\001d0\001",
169
+ "$a<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>")
170
+
171
+ assert_data("\\<\\\n", [], [], [], [], ["&lt;", "<br />\n"], ["\\<", "\\\n"], "\001e0\001\001e1\001", "&lt;<br />\n")
172
+ end
173
+
174
+ def test_through
175
+ s = SimpleLaTeX.new(:through_list=>[/\{\{.*\}\}/, /\(.*\)/])
176
+ assert_data("{{$a$}}($b$)", [], [], [], [], [], [], "{{$a$}}($b$)", "{{$a$}}($b$)", s)
177
+
178
+ s = SimpleLaTeX.new(:through_list=>/\{.*\}/)
179
+ assert_data("{$a$}", [], [], [], [], [], [], "{$a$}", "{$a$}", s)
180
+ end
181
+
182
+ def test_options_parser
183
+ ps = MathML::LaTeX::Parser.new
184
+ ps.macro.parse('\newcommand{\test}{t}')
185
+ s = SimpleLaTeX.new(:parser=>ps)
186
+ assert_data('$\test$', ["<mi>t</mi>"], ['$\test$'], [], [], [], [], "\001m0\001",
187
+ "<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>t</mi></math>", s)
188
+ end
189
+
190
+ def test_options_escape
191
+ s = SimpleLaTeX.new(:escape_list=>[/\/(.)/, /(\^.)/])
192
+ assert_data('\$a$', ["<mi>a</mi>"], ['$a$'], [], [], [], [], "\\\001m0\001",
193
+ "\\<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math>", s)
194
+ assert_data(%[/$a/$], [], [], [], [], [%[$], %[$]], [%[/$], %[/$]], "\001e0\001a\001e1\001", "$a$", s)
195
+ assert_data('^\(a^\)', [], [], [], [], ['^\\', '^\\'], ['^\\', '^\\'], "\001e0\001(a\001e1\001)", '^\(a^\)', s)
196
+
197
+ s = SimpleLaTeX.new(:escape_list=>/_(.)/)
198
+ assert_data("_$a$", [], [], [], [], ['$'], ["_$"], %[\001e0\001a$], '$a$', s)
199
+ end
200
+
201
+ def test_options_delimiter
202
+ s = SimpleLaTeX.new(:delimiter=>"\002\003")
203
+ assert_data("a$b$c", ["<mi>b</mi>"], ["$b$"], [], [], [], [], "a\002\003m0\002\003c",
204
+ "a<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>c", s)
205
+
206
+ s = SimpleLaTeX.new(:delimiter=>"$")
207
+ assert_data("a$b$c", ["<mi>b</mi>"], ["$b$"], [], [], [], [], "a$m0$c",
208
+ "a<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>c", s)
209
+ end
210
+
211
+ def test_options_math_env_list
212
+ s = SimpleLaTeX.new(:math_env_list=>/%(.*?)%/, :dmath_env_list=>/\[(.*?)\]/)
213
+ assert_data("a$b$c%d%e[f]", ["<mi>d</mi>"], ["%d%"], ["<mi>f</mi>"], ["[f]"], [], [],
214
+ "a$b$c\001m0\001e\001d0\001",
215
+ "a$b$c<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>d</mi></math>e<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>f</mi></math>", s)
216
+
217
+ s = SimpleLaTeX.new(:math_env_list=>[/!(.*?)!/, /"(.*)"/], :dmath_env_list=>[/\#(.*)\#/, /&(.*)&/])
218
+ assert_data('a!b!c"d"e#f#g&h&i',
219
+ ["<mi>b</mi>", "<mi>d</mi>"], ['!b!', '"d"'],
220
+ ["<mi>f</mi>", "<mi>h</mi>"], ['#f#', '&h&'],
221
+ [], [],
222
+ "a\001m0\001c\001m1\001e\001d0\001g\001d1\001i",
223
+ "a<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>c<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>d</mi></math>e<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>f</mi></math>g<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>h</mi></math>i", s)
224
+ end
225
+
226
+ def test_options_through_list
227
+ s = SimpleLaTeX.new(:through_list=>[/<%=.*?%>/m, /\(\(.*?\)\)/m])
228
+ assert_data("<%=$a$%>(($b$))", [], [], [], [], [], [], "<%=$a$%>(($b$))", "<%=$a$%>(($b$))", s)
229
+
230
+ s = SimpleLaTeX.new(:through_list=>/<%=.*?%>/)
231
+ assert_data("<%=$a$%>", [], [], [], [], [], [], "<%=$a$%>", "<%=$a$%>", s)
232
+ end
233
+
234
+ def test_empty_list
235
+ s = SimpleLaTeX.new(:through_list=>[])
236
+ assert_data("$a$", ["<mi>a</mi>"], [%[$a$]], [], [], [], [], "\001m0\001",
237
+ "<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math>", s)
238
+ end
239
+
240
+ def test_options_without_parse
241
+ s = SimpleLaTeX.new(:without_parse=>true)
242
+ encoded, data = s.encode("$a$ $$b$$")
243
+ assert_equal([], data.math_list)
244
+ assert_equal(["$a$"], data.msrc_list)
245
+ assert_equal([], data.dmath_list)
246
+ assert_equal(["$$b$$"], data.dsrc_list)
247
+ assert_equal("\001m0\001 \001d0\001", encoded)
248
+
249
+ s.parse(data)
250
+ assert_equal("inline", data.math_list[0].attributes[:display])
251
+ assert_equal("block", data.dmath_list[0].attributes[:display])
252
+ assert_equal(["<mi>a</mi>"], sma(data.math_list))
253
+ assert_equal(["<mi>b</mi>"], sma(data.dmath_list))
254
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math> <math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>", s.decode(encoded, data))
255
+ end
256
+
257
+ def test_set_encode_proc
258
+ s = SimpleLaTeX.new
259
+ s.set_encode_proc(/\{\{/) do |scanner|
260
+ if scanner.scan(/\{\{(.*?)\}\}/m)
261
+ "<%=#{scanner[1]}%>"
262
+ end
263
+ end
264
+ src = "{{$a$}}{{$$b$$}}{{"
265
+ assert_data(src, [], [], [], [], [], [], "\001u0\001\001u1\001{{", "<%=$a$%><%=$$b$$%>{{", s)
266
+ encoded, data = s.encode(src)
267
+ assert_equal(["<%=$a$%>", "<%=$$b$$%>"], data.user_list)
268
+ assert_equal(["{{$a$}}", "{{$$b$$}}"], data.usrc_list)
269
+
270
+ s.set_encode_proc(/\{\{/) do |scanner|
271
+ end
272
+ src = "{{a"
273
+ assert_data(src, [], [], [], [], [], [], "{{a", "{{a", s)
274
+ encoded, data = s.encode(src)
275
+ assert_equal([], data.user_list)
276
+ assert_equal([], data.usrc_list)
277
+ end
278
+
279
+ def test_encode_proc_with_arrayed_regexp
280
+ s = SimpleLaTeX.new
281
+ src = "{{a}}((b)){{(("
282
+ encoded, data = s.encode(src, /\{\{/, /\(\(/) do |scanner|
283
+ case
284
+ when scanner.scan(/\{\{.*?\}\}/)
285
+ "brace"
286
+ when scanner.scan(/\(\(.*?\)\)/)
287
+ "parenthesis"
288
+ end
289
+ end
290
+ assert_equal("\001u0\001\001u1\001{{((", encoded)
291
+ assert_equal("braceparenthesis{{((", s.decode(encoded, data))
292
+
293
+ s.set_encode_proc(/\{\{/, /\(\(/) do |scanner|
294
+ case
295
+ when scanner.scan(/\{\{.*?\}\}/)
296
+ "brace"
297
+ when scanner.scan(/\(\(.*?\)\)/)
298
+ "parenthesis"
299
+ end
300
+ end
301
+ encoded, data = s.encode(src)
302
+ assert_equal("\001u0\001\001u1\001{{((", encoded)
303
+ assert_equal("braceparenthesis{{((", s.decode(encoded, data))
304
+ end
305
+
306
+ def test_encode_with_proc
307
+ s = SimpleLaTeX.new
308
+ src = "{{$a$}}{{$$b$$}}{{"
309
+ encoded, data = s.encode(src, /\{\{/) do |scanner|
310
+ if scanner.scan(/\{\{(.*?)\}\}/m)
311
+ "<%=#{scanner[1]}%>"
312
+ end
313
+ end
314
+ assert_equal([], data.math_list)
315
+ assert_equal([], data.dmath_list)
316
+ assert_equal([], data.escape_list)
317
+ assert_equal("\001u0\001\001u1\001{{", encoded)
318
+ assert_equal("<%=$a$%><%=$$b$$%>{{", s.decode(encoded, data))
319
+ end
320
+
321
+ def test_encode_with_proc_with_encode_proc_set
322
+ s = SimpleLaTeX.new
323
+ src = "{{$a$}}{{$$b$$}}{{"
324
+ s.set_encode_proc(/\{\{/) do |scanner|
325
+ if scanner.scan(/\{\{(.*?)\}\}/m)
326
+ "<%=#{scanner[1]}%>"
327
+ end
328
+ end
329
+ encoded, data = s.encode(src, /\{\{/) do |scanner|
330
+ if scanner.scan(/\{\{(.*?)\}\}/m)
331
+ "<$=#{scanner[1]}$>"
332
+ end
333
+ end
334
+ assert_equal([], data.math_list)
335
+ assert_equal([], data.dmath_list)
336
+ assert_equal([], data.escape_list)
337
+ assert_equal("\001u0\001\001u1\001{{", encoded)
338
+ assert_equal("<$=$a$$><$=$$b$$$>{{", s.decode(encoded, data))
339
+ end
340
+
341
+ def test_unencode
342
+ src = "$\na\n$\n$$\nb\n$$"
343
+ s = SimpleLaTeX.new
344
+ encoded, data = s.encode(src)
345
+ assert_equal("$<br />\na<br />\n$\n$$<br />\nb<br />\n$$", s.unencode(encoded, data))
346
+
347
+ s = SimpleLaTeX.new(:delimiter=>"$")
348
+ e, d = s.encode("$a$")
349
+ assert_equal("$a$", s.unencode(e, d))
350
+ end
351
+
352
+ def test_set_rescue_proc
353
+ src = '$a\test$ $$b\dummy$$'
354
+ s = SimpleLaTeX.new
355
+ encoded, data = s.encode(src)
356
+ assert_equal("<br />\nUndefined command.<br />\n<code>a<strong>\\test</strong></code><br />", data.math_list[0])
357
+ assert_equal("<br />\nUndefined command.<br />\n<code>b<strong>\\dummy</strong></code><br />", data.dmath_list[0])
358
+
359
+ s.set_rescue_proc do |e|
360
+ e
361
+ end
362
+ encoded, data = s.encode(src)
363
+ assert_kind_of(MathML::LaTeX::ParseError, data.math_list[0])
364
+ assert_equal("a", data.math_list[0].done)
365
+ assert_kind_of(MathML::LaTeX::ParseError, data.dmath_list[0])
366
+ assert_equal("b", data.dmath_list[0].done)
367
+ end
368
+
369
+ def test_decode_with_proc
370
+ s = SimpleLaTeX.new
371
+ encoded, data = s.encode('$a$$b$$$c$$$$d$$\e\\\\')
372
+ r = s.decode(encoded, data) do |item, opt|
373
+ case opt[:type]
374
+ when :dmath
375
+ assert_equal("block", item.attributes[:display])
376
+ i = strip_math(item.to_s)
377
+ when :math
378
+ assert_equal("inline", item.attributes[:display])
379
+ i = strip_math(item.to_s)
380
+ else
381
+ i = item
382
+ end
383
+ r = "t#{opt[:type]}i#{opt[:index]}s#{opt[:src]}#{i}"
384
+ end
385
+ assert_equal("tmathi0s$a$<mi>a</mi>tmathi1s$b$<mi>b</mi>tdmathi0s$$c$$<mi>c</mi>tdmathi1s$$d$$<mi>d</mi>tescapei0s\\eetescapei1s\\\\\\", r)
386
+
387
+ r = s.decode(encoded, data) do |item, opt|
388
+ nil
389
+ end
390
+ assert_equal(s.decode(encoded, data), r)
391
+
392
+ s.set_encode_proc(/\{\{/) do |scanner|
393
+ "<%=#{scanner[1]}%>" if scanner.scan(/\{\{(.*?)\}\}/m)
394
+ end
395
+ encoded, data = s.encode("{{a}}{{")
396
+ r = s.decode(encoded, data) do |item, opt|
397
+ assert_equal("<%=a%>", item)
398
+ assert_equal(:user, opt[:type])
399
+ assert_equal(0, opt[:index])
400
+ assert_equal("{{a}}", opt[:src])
401
+ nil
402
+ end
403
+ assert_equal("<%=a%>{{", r)
404
+
405
+ s.set_decode_proc do |item, opt|
406
+ "dummy"
407
+ end
408
+ assert_equal("dummy{{", s.decode(encoded, data))
409
+ r = s.decode(encoded, data) do |item, opt|
410
+ nil
411
+ end
412
+ assert_equal("<%=a%>{{", r)
413
+ end
414
+
415
+ def test_set_decode_proc
416
+ s = SimpleLaTeX.new
417
+ src = '$a$$b$$$c$$$$d$$\e\\\\'
418
+ encoded, data = s.encode(src)
419
+ original_decoded = s.decode(encoded, data)
420
+ s.set_decode_proc do |item, opt|
421
+ case opt[:type]
422
+ when :dmath
423
+ assert_equal("block", item.attributes[:display])
424
+ i = strip_math(item.to_s)
425
+ when :math
426
+ assert_equal("inline", item.attributes[:display])
427
+ i = strip_math(item.to_s)
428
+ else
429
+ i = item
430
+ end
431
+ r = "t#{opt[:type]}i#{opt[:index]}s#{opt[:src]}#{i}"
432
+ end
433
+ encoded, data = s.encode(src)
434
+ r = s.decode(encoded, data)
435
+ assert_equal("tmathi0s$a$<mi>a</mi>tmathi1s$b$<mi>b</mi>tdmathi0s$$c$$<mi>c</mi>tdmathi1s$$d$$<mi>d</mi>tescapei0s\\eetescapei1s\\\\\\", r)
436
+
437
+ s.reset_decode_proc
438
+ assert_equal(original_decoded, s.decode(encoded, data))
439
+ end
440
+
441
+ def test_unencode_with_proc
442
+ s = SimpleLaTeX.new
443
+ src = '$a$$b$$$c$$$$d$$\e\\\\'
444
+ encoded, data = s.encode(src)
445
+ r = s.unencode(encoded, data) do |item, opt|
446
+ r = "t#{opt[:type]}i#{opt[:index]}#{item.to_s}"
447
+ end
448
+ assert_equal("tmathi0$a$tmathi1$b$tdmathi0$$c$$tdmathi1$$d$$tescapei0\\etescapei1\\\\", r)
449
+
450
+ r = s.unencode(encoded, data) do |item, opt|
451
+ nil
452
+ end
453
+ assert_equal(s.unencode(encoded, data), r)
454
+
455
+ s.set_encode_proc(/\{\{/) do |scanner|
456
+ "<%=#{scanner[1]}%>" if scanner.scan(/\{\{(.*?)\}\}/m)
457
+ end
458
+ encoded, data = s.encode("{{a}}{{")
459
+ r = s.unencode(encoded, data) do |item, opt|
460
+ assert_equal("{{a}}", item)
461
+ assert_equal(:user, opt[:type])
462
+ assert_equal(0, opt[:index])
463
+ nil
464
+ end
465
+ assert_equal("{{a}}{{", r)
466
+ end
467
+
468
+ def test_unencode_proc
469
+ s = SimpleLaTeX.new
470
+ src = '$a$$b$$$c$$$$d$$\e\\\\'
471
+ encoded, data = s.encode(src)
472
+ original_unencoded = s.unencode(encoded, data)
473
+
474
+ s.set_unencode_proc do |item, opt|
475
+ r = "t#{opt[:type]}i#{opt[:index]}#{item.to_s}"
476
+ end
477
+ r = s.unencode(encoded, data)
478
+ assert_equal("tmathi0$a$tmathi1$b$tdmathi0$$c$$tdmathi1$$d$$tescapei0\\etescapei1\\\\", r)
479
+
480
+ s.set_unencode_proc do |item, opt|
481
+ nil
482
+ end
483
+ assert_equal(original_unencoded, s.unencode(encoded, data))
484
+
485
+ s.set_encode_proc(/\{\{/) do |scanner|
486
+ "<%=#{scanner[1]}%>" if scanner.scan(/\{\{(.*?)\}\}/m)
487
+ end
488
+ encoded, data = s.encode("{{a}}{{")
489
+ s.set_unencode_proc do |item, opt|
490
+ assert_equal("{{a}}", item)
491
+ assert_equal(:user, opt[:type])
492
+ assert_equal(0, opt[:index])
493
+ nil
494
+ end
495
+ r = s.unencode(encoded, data)
496
+ assert_equal("{{a}}{{", r)
497
+ end
498
+
499
+ def test_reset_unencode_proc
500
+ s = SimpleLaTeX.new
501
+ s.set_unencode_proc do |item, opt|
502
+ "dummy"
503
+ end
504
+ encoded, data = s.encode("$a$ $$b$$")
505
+ assert_equal("dummy dummy", s.unencode(encoded, data))
506
+
507
+ s.reset_unencode_proc
508
+ assert_equal("$a$ $$b$$", s.unencode(encoded, data))
509
+ end
510
+
511
+ def test_unencode_escaping
512
+ s = SimpleLaTeX.new
513
+ src = %[$<>&'"\n$ $$<>&"'\n$$]
514
+ encoded, data = s.encode(src)
515
+ assert_equal("$&lt;&gt;&amp;&apos;&quot;<br />\n$ $$&lt;&gt;&amp;&quot;&apos;<br />\n$$", s.unencode(encoded, data))
516
+ assert_equal(src, s.unencode(encoded, data, true), "without escaping")
517
+ end
518
+
519
+ def test_decode_without_parsed
520
+ s = SimpleLaTeX.new
521
+ src = '$a$$$b$$\a'
522
+ encoded, data = s.encode(src)
523
+ assert_equal("$a$$$b$$a", s.decode(encoded, data, true))
524
+ s.decode(encoded, data, true) do |item, opt|
525
+ case opt[:type]
526
+ when :math
527
+ assert_equal("$a$", item)
528
+ when :dmath
529
+ assert_equal("$$b$$", item)
530
+ when :escape
531
+ assert_equal("a", item)
532
+ end
533
+ end
534
+
535
+ encoded, data = s.encode("$<\n$ $$<\n$$")
536
+ assert_equal("$&lt;<br />\n$ $$&lt;<br />\n$$", s.decode(encoded, data, true))
537
+ end
538
+
539
+ def test_decode_partial
540
+ s = SimpleLaTeX.new
541
+ encoded, data = s.encode("$a$$b$")
542
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>", s.decode_partial(:math, encoded, data))
543
+
544
+ s.set_encode_proc(/\\</) do |scanner|
545
+ if scanner.scan(/\\<(.)(.*?)\1>/)
546
+ scanner[2]
547
+ end
548
+ end
549
+ src='$a$$$b$$\c\<.$d$.>'
550
+ encoded, data = s.encode(src)
551
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math>\001d0\001\001e0\001\001u0\001", s.decode_partial(:math, encoded, data))
552
+ assert_equal("\001m0\001<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>\001e0\001\001u0\001", s.decode_partial(:dmath, encoded, data))
553
+ assert_equal("\001m0\001\001d0\001c\001u0\001", s.decode_partial(:escape, encoded, data))
554
+ assert_equal("\001m0\001\001d0\001\001e0\001$d$", s.decode_partial(:user, encoded, data))
555
+
556
+ r = s.decode_partial(:math, encoded, data) do |item, opt|
557
+ assert_equal(:math, opt[:type])
558
+ assert_equal("$a$", opt[:src])
559
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math>", item.to_s)
560
+ item
561
+ end
562
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>a</mi></math>\001d0\001\001e0\001\001u0\001", r)
563
+
564
+ r = s.decode_partial(:dmath, encoded, data) do |item, opt|
565
+ assert_equal(:dmath, opt[:type])
566
+ assert_equal("$$b$$", opt[:src])
567
+ assert_equal("<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>", item.to_s)
568
+ item
569
+ end
570
+ assert_equal("\001m0\001<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mi>b</mi></math>\001e0\001\001u0\001", r)
571
+
572
+ r = s.decode_partial(:escape, encoded, data) do |item, opt|
573
+ assert_equal(:escape, opt[:type])
574
+ assert_equal("\\c", opt[:src])
575
+ assert_equal("c", item)
576
+ item
577
+ end
578
+ assert_equal("\001m0\001\001d0\001c\001u0\001", r)
579
+
580
+ r = s.decode_partial(:user, encoded, data) do |item, opt|
581
+ assert_equal(:user, opt[:type])
582
+ assert_equal("\\<.$d$.>", opt[:src])
583
+ assert_equal("$d$", item)
584
+ item
585
+ end
586
+ assert_equal("\001m0\001\001d0\001\001e0\001$d$", r)
587
+
588
+ s = SimpleLaTeX.new
589
+ encoded, data = s.encode("\\a")
590
+ assert_equal("a", s.decode_partial(:escape, encoded, data))
591
+ r = s.decode_partial(:escape, encoded, data) do |item, opt|
592
+ end
593
+ assert_equal("\001e0\001", r)
594
+
595
+ s = SimpleLaTeX.new(:delimiter=>"$")
596
+ encoded, data = s.encode("$a$")
597
+ assert_match(/^<math.*<\/math>/m, s.decode_partial(:math, encoded, data))
598
+ end
599
+
600
+ def test_regexp_order
601
+ s = SimpleLaTeX.new
602
+ s.set_encode_proc(/\$/) do |sc|
603
+ if sc.scan(/\$(.*)\z/)
604
+ sc[1]+"is rest"
605
+ end
606
+ end
607
+
608
+ encoded, data = s.encode("$a$$b")
609
+ assert_equal("\001m0\001\001u0\001", encoded)
610
+ end
611
+
612
+ def test_eqnarray
613
+ s = SimpleLaTeX.new
614
+ src = <<'EOT'
615
+ test
616
+ \begin
617
+ {eqnarray}
618
+ a&=&b\\
619
+ c&=&d
620
+ \end
621
+ {eqnarray}
622
+ end
623
+ EOT
624
+ encoded, data = s.encode(src, MathML::Util::EQNARRAY_RE) do |scanner|
625
+ if scanner.scan(MathML::Util::EQNARRAY_RE)
626
+ s.parse_eqnarray(scanner[1])
627
+ end
628
+ end
629
+ assert_equal("test\n\001u0\001\nend\n", encoded)
630
+ assert_equal("test\n<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mtable><mtr><mtd><mi>a</mi></mtd><mtd><mo>=</mo></mtd><mtd><mi>b</mi></mtd></mtr><mtr><mtd><mi>c</mi></mtd><mtd><mo>=</mo></mtd><mtd><mi>d</mi></mtd></mtr></mtable></math>\nend\n", s.decode(encoded, data).gsub(/>\s*</, "><"))
631
+
632
+ encoded, data = s.encode('\begin{eqnarray}a\end{eqnarray}', MathML::Util::EQNARRAY_RE) do |scanner|
633
+ s.parse_eqnarray(scanner[1]) if scanner.scan(MathML::Util::EQNARRAY_RE)
634
+ end
635
+ assert_equal("<br />\nNeed more column.<br />\n<code>\\begin{eqnarray}a<strong>\\end{eqnarray}</strong></code><br />", s.decode(encoded, data))
636
+ end
637
+
638
+ def test_single_command
639
+ s = SimpleLaTeX.new
640
+ encoded, data = s.encode(%q[\alpha\|\<\>\&\"\'\test], MathML::Util::SINGLE_COMMAND_RE) do |scanner|
641
+ if scanner.scan(MathML::Util::SINGLE_COMMAND_RE)
642
+ s.parse_single_command(scanner.matched)
643
+ end
644
+ end
645
+ assert_equal("\001u0\001\001e0\001\001e1\001\001e2\001\001e3\001\001e4\001\001e5\001\001u1\001", encoded)
646
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>&alpha;</mi></math>|&lt;&gt;&amp;&quot;&apos;test", s.decode(encoded, data))
647
+ encoded, data = s.encode('\alpha test', MathML::Util::SINGLE_COMMAND_RE) do |scanner|
648
+ if scanner.scan(MathML::Util::SINGLE_COMMAND_RE)
649
+ s.parse_single_command(scanner.matched)
650
+ end
651
+ end
652
+ assert_equal("\001u0\001test", encoded)
653
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>&alpha;</mi></math>test", s.decode(encoded, data))
654
+
655
+ encoded, data = s.encode('\alpha test', MathML::Util::SINGLE_COMMAND_RE) do |scanner|
656
+ if scanner.scan(MathML::Util::SINGLE_COMMAND_RE)
657
+ s.parse_single_command(scanner.matched)
658
+ end
659
+ end
660
+ assert_equal("\001u0\001 test", encoded)
661
+ assert_equal("<math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><mi>&alpha;</mi></math> test", s.decode(encoded, data))
662
+
663
+ encoded, data = s.encode("\\alpha\ntest", MathML::Util::SINGLE_COMMAND_RE) do |scanner|
664
+ if scanner.scan(MathML::Util::SINGLE_COMMAND_RE)
665
+ s.parse_single_command(scanner.matched)
666
+ end
667
+ end
668
+ assert_equal("\001u0\001\ntest", encoded)
669
+ end
670
+
671
+ def test_encode_append
672
+ s = SimpleLaTeX.new
673
+ encoded, data = s.encode('$a$')
674
+ encoded, data = s.encode('$b$', data)
675
+ assert_equal("\001m1\001", encoded)
676
+ assert_equal(["$a$", '$b$'], data.msrc_list)
677
+ assert_equal(2, data.math_list.size)
678
+ assert_equal("<mi>a</mi>", strip_mathml(data.math_list[0].to_s))
679
+ assert_equal("<mi>b</mi>", strip_mathml(data.math_list[1].to_s))
680
+
681
+ encoded, data = s.encode('a', data, /a/) do |sc|
682
+ sc.scan(/a/)
683
+ end
684
+ assert_equal("\001u0\001", encoded)
685
+ assert_equal(["$a$", '$b$'], data.msrc_list)
686
+ assert_equal(["a"], data.usrc_list)
687
+
688
+ encoded, data = s.encode('a', nil, /a/) do |s|
689
+ s.scan(/a/)
690
+ end
691
+ assert_equal("\001u0\001", encoded)
692
+ assert_equal(["a"], data.usrc_list)
693
+ end
694
+ end