latex-tools 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "lib"))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(File.expand_path(__FILE__)),"lib"))
5
+
6
+ require "test/unit"
7
+ require 'latex_table'
8
+ require 'custom_assertions'
9
+ include LatexTools
10
+
11
+
12
+ class LatexTableTest < Test::Unit::TestCase
13
+
14
+ # Called before every test method runs. Can be used
15
+ # to set up fixture information.
16
+ def setup
17
+ # Do nothing
18
+ end
19
+
20
+ # Called after every test method runs. Can be used to tear
21
+ # down fixture information.
22
+ def teardown
23
+ # Do nothing
24
+ end
25
+
26
+ def test_table_example1
27
+
28
+ assert_example_works("table/example1.rb")
29
+ end
30
+
31
+ def assert_example_works(example_file)
32
+ example_dir = File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "examples")
33
+ result = eval(IO.read("#{example_dir}/#{example_file}"))
34
+ assert_equal_ignore_spaces(
35
+ IO.read("#{example_dir}/#{example_file}.out"),
36
+ result
37
+ )
38
+ end
39
+
40
+ end
@@ -0,0 +1,399 @@
1
+
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "lib"))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"lib"))
5
+
6
+ require "test/unit"
7
+ require 'latex_table'
8
+ require 'custom_assertions'
9
+ include LatexTools
10
+
11
+ class LatexTableTest < Test::Unit::TestCase
12
+
13
+ # Called before every test method runs. Can be used
14
+ # to set up fixture information.
15
+ def setup
16
+ # Do nothing
17
+ end
18
+
19
+ # Called after every test method runs. Can be used to tear
20
+ # down fixture information.
21
+
22
+ def teardown
23
+ # Do nothing
24
+ end
25
+
26
+ # Build a simple set of tables for testing
27
+ def make_table(name)
28
+ case(name)
29
+ when "empty"
30
+ @table = LatexTable.new
31
+ when "simple 3x3"
32
+ @table = LatexTable.new
33
+ @table.add_column
34
+ @table.add_column
35
+ @table.add_column
36
+ @table.add_row
37
+ @table.add_element("1")
38
+ @table.add_element("2")
39
+ @table.add_element("3")
40
+ @table.add_row
41
+ @table.add_element("4")
42
+ @table.add_element("5")
43
+ @table.add_element("6")
44
+ @table.add_row
45
+ @table.add_element("7")
46
+ @table.add_element("8")
47
+ @table.add_element("9")
48
+ when "hlines"
49
+ @table = LatexTable.new(5)
50
+ @table << :hline
51
+ @table << :hline
52
+ @table << 1 << 2 << 3 << 4 << 5 << :endl
53
+ @table << :hline
54
+ @table << 1 << 2 << 3 << 4 << 5 << :endl
55
+ @table << :hline
56
+ @table << :hline
57
+ when "autoadd"
58
+ @table = LatexTable.new
59
+ @table.auto_add_columns = true
60
+ @table << 1 << 2 << 3 << :endl
61
+ @table << hline
62
+ @table << 1 << 2 << 3 << 4 << :endl
63
+ @table << :hline
64
+ @table << 1 << 2 << 3 << 4 << 5 << :endl
65
+ when "multicol"
66
+ @table = LatexTable.new(4)
67
+ @table << 1 << 2 << 3 << 4 << :endl
68
+ @table << :hline
69
+ @table << multicol(3, "hello") << 4 << :endl
70
+ @table << :hline
71
+ @table << 1 << fill_to_end(:r, "world") << :endl
72
+ @table << 1 << fill_to_end("world") << :endl
73
+ @table << 1 << multicol(2, :l, 3.14) << 4 << :endl
74
+ @table << :hline
75
+ @table << :hline
76
+ @table.add_element_spanning(3, "hello")
77
+ @table.add_element(4)
78
+ @table.add_spanning_row("full row")
79
+ @table << :hline
80
+ @table.add_spanning_row("full row")
81
+ @table.add_element(1)
82
+ @table.add_element_spanning_to_end("world", :r)
83
+ @table.add_row
84
+ @table << 1
85
+ @table.add_element_spanning(2, 3.14, :l)
86
+ @table.add_element(4)
87
+ when "cline"
88
+ @table = LatexTable.new(4)
89
+ @table << 1 << 2 << 3 << 4 << :endl
90
+ @table << cline(1,3) << :endl
91
+ @table << 1 << 2 << 3 << 4 << :endl
92
+ @table << cline(1,2) << cline(3,4) << :endl
93
+ @table << 1 << 2 << 3 << 4 << :endl
94
+ @table << :hline
95
+ else
96
+ raise "Unknown table requested for testing in LatexTableTest#make_table"
97
+ end
98
+
99
+ end
100
+
101
+ def test_stupidity
102
+ # Misalign stuff...
103
+ assert_raise RuntimeError do
104
+ t = LatexTable.new(3)
105
+ t << 1 << 2 << :endl
106
+ end
107
+ assert_raise IndexError do
108
+ t = LatexTable.new(3)
109
+ t << 1 << 2 << 3 << 4 << :endl
110
+ end
111
+ assert_raise IndexError do
112
+ t = LatexTable.new(3)
113
+ t.add_element_spanning(4, "text")
114
+ end
115
+
116
+ # Invalid argument counts and types
117
+ assert_raise(ArgumentError) { LatexTable.new(1,2,3) }
118
+ assert_raise(TypeError) { LatexTable.new("hello") }
119
+
120
+
121
+
122
+ end
123
+
124
+ def test_not_implemented
125
+ end
126
+
127
+ def test_to_str_etc
128
+ make_table("simple 3x3")
129
+ assert_equal(@table.to_str, @table.to_s)
130
+ end
131
+
132
+ def test_divider_raises
133
+ make_table("empty")
134
+ assert_raise(ArgumentError) { @table.col_divider = "|||" }
135
+ make_table("empty")
136
+ assert_raise(ArgumentError) { @table.col_divider = "something_weird" }
137
+ make_table("simple 3x3")
138
+ assert_raise(ArgumentError) { @table.col_dividers = ["|","something_weird"] }
139
+ make_table("simple 3x3")
140
+ assert_raise(ArgumentError) { @table.col_dividers = ["|","|","|","|","|"] }
141
+ make_table("simple 3x3")
142
+ assert_raise(ArgumentError) { @table.col_dividers = ["|","|","|"] }
143
+ end
144
+
145
+ def test_simple_3x3_num_rows
146
+ make_table("simple 3x3")
147
+ assert_equal(3, @table.rows)
148
+ end
149
+
150
+ def test_hlines_num_rows
151
+ make_table("hlines")
152
+ assert_equal(7, @table.rows)
153
+ end
154
+
155
+ def test_simple_3x3_tabular_string
156
+ make_table("simple 3x3")
157
+ assert_equal("ccc", @table.tabular_string)
158
+ @table.col_divider = "|"
159
+ assert_equal("c|c|c", @table.tabular_string)
160
+ @table.col_divider = "||"
161
+ assert_equal("c||c||c", @table.tabular_string)
162
+ @table.col_dividers = ["||","|"]
163
+ assert_equal("c||c|c", @table.tabular_string)
164
+ @table.col_dividers = ["|","||","|","||"]
165
+ assert_equal("|c||c|c||", @table.tabular_string)
166
+ @table.col_dividers = ["||","","","||"]
167
+ assert_equal("||ccc||", @table.tabular_string)
168
+ @table.col_dividers = ["",""]
169
+ assert_equal("||ccc||", @table.tabular_string)
170
+ @table.col_dividers = ["","","",""]
171
+ assert_equal("ccc", @table.tabular_string)
172
+ @table.add_v_border
173
+ assert_equal("|ccc|", @table.tabular_string)
174
+ @table.col_dividers = ["|","|"]
175
+ assert_equal("|c|c|c|", @table.tabular_string)
176
+ @table.add_v_double_border
177
+ assert_equal("||c|c|c||", @table.tabular_string)
178
+ @table.col_dividers = ["","","",""]
179
+ assert_equal("ccc", @table.tabular_string)
180
+ @table.add_v_border
181
+ @table.add_v_border
182
+ assert_equal("||ccc||", @table.tabular_string)
183
+ @table.col_dividers = ["|","","","|"]
184
+ assert_equal("|ccc|", @table.tabular_string)
185
+ end
186
+
187
+ def test_simple_3x3_fragment
188
+ make_table("simple 3x3")
189
+ assert_equal_ignore_spaces(
190
+ '\begin{table}
191
+ \begin{tabular}{ccc}
192
+ 1 & 2 & 3 \\\\
193
+ 4 & 5 & 6 \\\\
194
+ 7 & 8 & 9
195
+ \end{tabular}
196
+ \end{table}',
197
+ @table.to_latex
198
+ )
199
+ @table.add_v_double_border
200
+ assert_equal_ignore_spaces(
201
+ '\begin{table}
202
+ \begin{tabular}{||ccc||}
203
+ 1 & 2 & 3 \\\\
204
+ 4 & 5 & 6 \\\\
205
+ 7 & 8 & 9
206
+ \end{tabular}
207
+ \end{table}',
208
+ @table.to_latex
209
+ )
210
+ @table.float_number_format = "%.2f"
211
+ assert_equal_ignore_spaces(
212
+ '\begin{table}
213
+ \begin{tabular}{||ccc||}
214
+ 1.00 & 2.00 & 3.00 \\\\
215
+ 4.00 & 5.00 & 6.00 \\\\
216
+ 7.00 & 8.00 & 9.00
217
+ \end{tabular}
218
+ \end{table}',
219
+ @table.to_latex
220
+ )
221
+ end
222
+
223
+ def test_borders
224
+ make_table("simple 3x3")
225
+ @table.add_v_border
226
+ assert_equal_ignore_spaces(
227
+ '\begin{table}
228
+ \begin{tabular}{|ccc|}
229
+ 1 & 2 & 3 \\\\
230
+ 4 & 5 & 6 \\\\
231
+ 7 & 8 & 9
232
+ \end{tabular}
233
+ \end{table}',
234
+ @table.to_latex
235
+ )
236
+ @table.add_h_border
237
+ assert_equal_ignore_spaces(
238
+ '\begin{table}
239
+ \begin{tabular}{|ccc|}
240
+ \hline
241
+ 1 & 2 & 3 \\\\
242
+ 4 & 5 & 6 \\\\
243
+ 7 & 8 & 9 \\\\
244
+ \hline
245
+ \end{tabular}
246
+ \end{table}',
247
+ @table.to_latex
248
+ )
249
+ @table.add_border
250
+ assert_equal_ignore_spaces(
251
+ '\begin{table}
252
+ \begin{tabular}{||ccc||}
253
+ \hline
254
+ \hline
255
+ 1 & 2 & 3 \\\\
256
+ 4 & 5 & 6 \\\\
257
+ 7 & 8 & 9 \\\\
258
+ \hline
259
+ \hline
260
+ \end{tabular}
261
+ \end{table}',
262
+ @table.to_latex
263
+ )
264
+
265
+ end
266
+
267
+ def test_quick_add
268
+ tmp = LatexTable.new(3)
269
+ tmp << 1 << 2 << 3 << :endl
270
+ tmp << 4 << 5 << 6 << :endl
271
+ tmp << 7 << 8 << 9 << :endl
272
+ make_table("simple 3x3")
273
+ assert_equal(@table.to_s, tmp.to_s)
274
+ end
275
+
276
+ def test_hlines
277
+ make_table("hlines")
278
+ assert_equal_ignore_spaces(
279
+ '\hline
280
+ \hline
281
+ 1 & 2 & 3 & 4 & 5 \\\\
282
+ \hline
283
+ 1 & 2 & 3 & 4 & 5 \\\\
284
+ \hline
285
+ \hline
286
+ ',
287
+ @table.to_latex_fragment
288
+ )
289
+ end
290
+
291
+ def test_auto_add
292
+ make_table("autoadd")
293
+ assert_equal_ignore_spaces(
294
+ '1 & 2 & 3 & & \\\\
295
+ \hline
296
+ 1 & 2 & 3 & 4 & \\\\
297
+ \hline
298
+ 1 & 2 & 3 & 4 & 5
299
+ ',
300
+ @table.to_latex_fragment
301
+ )
302
+ @table.add_v_border
303
+ @table.col_divider = "||"
304
+ @table << 1 << 2 << 3 << 4 << 5 << 6 << :endl
305
+ assert_equal("|c||c||c||c||c||c|", @table.tabular_string)
306
+ @table.add_element_spanning(7, "test", :l)
307
+ assert_equal("|c||c||c||c||c||c||c|", @table.tabular_string)
308
+
309
+ end
310
+
311
+ def test_multicol
312
+ make_table("multicol")
313
+ assert_equal_ignore_spaces(
314
+ '1 & 2 & 3 & 4 \\\\
315
+ \hline
316
+ \multicolumn{3}{c}{hello} & 4 \\\\
317
+ \hline
318
+ 1 & \multicolumn{3}{r}{world} \\\\
319
+ 1 & \multicolumn{3}{c}{world} \\\\
320
+ 1 & \multicolumn{2}{l}{3.14} & 4 \\\\
321
+ \hline
322
+ \hline
323
+ \multicolumn{3}{c}{hello} & 4 \\\\
324
+ \multicolumn{4}{c}{full row} \\\\
325
+ \hline
326
+ \multicolumn{4}{c}{full row} \\\\
327
+ 1 & \multicolumn{3}{r}{world} \\\\
328
+ 1 & \multicolumn{2}{l}{3.14} & 4
329
+ ', @table.to_latex_fragment
330
+ )
331
+ end
332
+
333
+ def test_row_num_columns
334
+ r = LatexTable::Row.new
335
+ r << 1 << 2 << 3
336
+ assert_equal(3, r.num_columns)
337
+
338
+ r = LatexTable::Row.new
339
+ r << multicol(3, "hello")
340
+ r << 1
341
+ assert_equal(4, r.num_columns)
342
+
343
+ r << multicol(:fill, "test")
344
+ assert_equal(-1, r.num_columns)
345
+ end
346
+
347
+ def test_cline
348
+ make_table("cline")
349
+ assert_equal_ignore_spaces(
350
+ '1 & 2 & 3 & 4 \\\\
351
+ \cline{1-3}
352
+ 1 & 2 & 3 & 4 \\\\
353
+ \cline{1-2} \cline{3-4}
354
+ 1 & 2 & 3 & 4 \\\\
355
+ \hline
356
+ ', @table.to_latex_fragment
357
+ )
358
+ @table.auto_add_columns = true
359
+ @table << 1 << 3 << 5 << 6 << endl
360
+ @table << cline(1,6)
361
+ @table << 1 << 3 << 5 << 6 << 8 << 9 << endl
362
+ assert_equal_ignore_spaces(
363
+ '1 & 2 & 3 & 4 & & \\\\
364
+ \cline{1-3}
365
+ 1 & 2 & 3 & 4 & & \\\\
366
+ \cline{1-2} \cline{3-4}
367
+ 1 & 2 & 3 & 4 & & \\\\
368
+ \hline
369
+ 1 & 3 & 5 & 6 & & \\\\
370
+ \cline{1-6}
371
+ 1 & 3 & 5 & 6 & 8 & 9
372
+ ', @table.to_latex_fragment
373
+ )
374
+
375
+ end
376
+
377
+ def test_cline_fail
378
+ @table = LatexTable.new(3)
379
+ assert_raise ArgumentError do
380
+ @table << cline(1,2) << cline(2,3)
381
+ end
382
+
383
+ @table = LatexTable.new(3)
384
+ assert_raise RuntimeError do
385
+ @table << 1 << cline(2,3) << endl
386
+ end
387
+
388
+ @table = LatexTable.new(3)
389
+ assert_raise RuntimeError do
390
+ @table << 1 << cline(2,4) << endl
391
+ end
392
+
393
+ @table = LatexTable.new(3)
394
+ assert_raise RuntimeError do
395
+ @table << cline(0,1)
396
+ end
397
+ end
398
+
399
+ end
@@ -0,0 +1,24 @@
1
+
2
+ require 'test/unit'
3
+
4
+ module Test::Unit::Assertions
5
+
6
+ def assert_equal_ignore_spaces(expected, actual, message = nil)
7
+ # Also ignore trailing newlines
8
+ full_message = build_message(message, "<?> expected (ignoring spaces), but was actually <?>. With whitespace removed:\n expected: <?>\n actual: <?>", expected, actual, expected.gsub(" ", '').chomp, actual.gsub(" ", '').chomp)
9
+ assert_block(full_message) do
10
+ expected.gsub(" ", '').chomp == actual.gsub(" ", '').chomp
11
+ end
12
+ end
13
+
14
+
15
+ def assert_equal_ignore_whitespace(expected, actual, message = nil)
16
+ full_message = build_message(message, "<?> expected (ignoring whitespace), but was actually <?>", expected, actual)
17
+ assert_block(full_message) do
18
+ expected.gsub(/\s+/, '') == actual.gsub(/\s+/, '')
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+