forked from bruce/rtex
/
document_test.rb
86 lines (73 loc) · 2.54 KB
/
document_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require File.dirname(__FILE__) << '/test_helper'
class DocumentTest < Test::Unit::TestCase
context "Document Generation" do
setup do
change_tmpdir_for_testing
end
should "have a to_pdf method" do
assert document(:first).respond_to?(:to_pdf)
end
context "when escaping" do
setup do
@obj = Object.new
def @obj.to_s
'\~'
end
@escaped = '\textbackslash{}\textasciitilde{}'
end
should "escape character" do
assert_equal @escaped, ArTeX::Document.escape(@obj.to_s)
end
should "convert argument to string before attempting escape" do
assert_equal @escaped, ArTeX::Document.escape(@obj)
end
end
should "use a to_pdf block to move a file to a relative path" do
begin
path = File.expand_path(File.dirname(__FILE__) << '/tmp/this_is_relative_to_pwd.pdf')
document(:first).to_pdf do |filename|
assert_nothing_raised do
FileUtils.move filename, path
end
assert File.exists?(path)
end
ensure
FileUtils.rm path rescue nil
end
end
should "generate PDF and return as a string" do
@author = 'Foo'
assert_equal '%PDF', document(:first).to_pdf(binding)[0, 4]
end
should "generate TeX source and return as a string with debug option" do
@author = 'Foo'
assert_not_equal '%PDF', document(:first, :tex => true).to_pdf(binding)[0, 4]
end
should "generate PDF and give access to file directly" do
@author = 'Foo'
data_read = nil
invocation_result = document(:first).to_pdf(binding) do |filename|
data_read = File.open(filename, 'rb') { |f| f.read }
:not_the_file_contents
end
assert_equal '%PDF', data_read[0, 4]
assert_equal :not_the_file_contents, invocation_result
end
should "generate TeX source and give access to file directly" do
@author = 'Foo'
data_read = nil
invocation_result = document(:first, :tex => true).to_pdf(binding) do |filename|
data_read = File.open(filename, 'rb') { |f| f.read }
:not_the_file_contents
end
assert_not_equal '%PDF', data_read[0, 4]
assert_equal :not_the_file_contents, invocation_result
end
should "wrap in a layout using `yield'" do
doc = document(:fragment, :layout => 'testing_layout[<%= yield %>]')
@name = 'ERB'
source = doc.source(binding)
assert source =~ /^testing_layout.*?ERB, Fragmented/
end
end
end