-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_build.jl
144 lines (128 loc) · 4.62 KB
/
test_build.jl
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function is_file_starting_with(filename, bytes::DenseVector{UInt8})
isfile(filename) && read(filename, length(bytes)) == bytes
end
function is_file_starting_with(filename, regex::Regex, nlines = 1)
isfile(filename) || return false
content = ""
open(filename, "r") do io
for _ in 1:nlines
content *= readline(io; keep = false)
end
end
occursin(regex, content)
end
is_png_file(filename) = is_file_starting_with(filename, b"\x89PNG")
is_pdf_file(filename) = is_file_starting_with(filename, b"%PDF")
is_tex_document(filename) = # may have a \Require in the first line
is_file_starting_with(filename, r"\\documentclass\[tikz\]{standalone}", 2)
is_tikz_standalone(filename) =
is_file_starting_with(filename, r"\\begin{tikzpicture}")
is_svg_file(filename) = is_file_starting_with(filename, r"<svg .*>", 2)
@testset "preamble" begin
mktemp() do path, f
withenv("PGFPLOTSX_PREAMBLE_PATH" => path) do
try
test_preamble_env = "test preamble env"
test_preamble_var = "test preamble var"
push!(PGFPlotsX.CUSTOM_PREAMBLE, test_preamble_var)
print(f, test_preamble_env)
close(f)
td = TikzDocument()
io = IOBuffer()
PGFPlotsX.savetex(io, td)
texstring = String(take!(io))
@test occursin(test_preamble_env, texstring)
@test occursin(test_preamble_var, texstring)
finally
empty!(PGFPlotsX.CUSTOM_PREAMBLE)
end
end
end
end
@testset "simple" begin
tmp = tempname()
mktempdir() do dir
cd(dir) do
a = Axis(Plot(Expression("x^2")))
pgfsave("$tmp.tex", a)
@test is_tex_document("$tmp.tex")
println(read("$tmp.tex", String))
pgfsave("$tmp.png", a)
@test is_png_file("$tmp.png")
pgfsave("$tmp.pdf", a)
@test is_pdf_file("$tmp.pdf")
pgfsave("$tmp.svg", a)
@test is_svg_file("$tmp.svg")
pgfsave("$tmp.tikz", a)
@test is_tikz_standalone("$tmp.tikz")
let tikz_lines = readlines("$tmp.tikz")
@test occursin(r"^\\begin{tikzpicture}.*", tikz_lines[1])
last_line = findlast(!isempty, tikz_lines)
@test strip(tikz_lines[last_line]) == "\\end{tikzpicture}"
end
end
end
end
@testset "gnuplot / shell-escape" begin
tmp_pdf = tempname() * ".pdf"
expr = "-2.051^3*1000./(2*3.1415*(2.99*10^2)^2)/(x^2*cos(y)^2)"
mktempdir() do dir
cd(dir) do
@pgf p =
Axis(
{
colorbar,
xlabel = "x",
ylabel = "y",
domain = "1:2",
y_domain = "74:87.9",
view = (0, 90),
},
Plot3(
{
contour_gnuplot = {
number = 30,
labels = false},
thick,
samples = 40,
},
Expression(expr)))
pgfsave(tmp_pdf, p)
@test is_pdf_file(tmp_pdf)
rm(tmp_pdf)
end
end
end
@testset "legend to name; ref" begin
tmp_pdf = tempname() * ".pdf"
mktempdir() do dir
cd(dir) do
@pgf a = [Axis({legend_to_name = "named",
title = "k = $k", legend_columns = 2,
legend_entries = {"\$x^k\$", "\$(x + 1)^k\$"}},
PlotInc(Expression("x^$k")),
PlotInc(Expression("(x + 1)^$k")))
for k in 1:3]
p = TikzPicture("\\matrix{", a[1], "&", a[2], "&", a[3], raw"\\\\};",
raw"\node at (.5, -4.5) {\ref{named}};")
pgfsave(tmp_pdf, p)
@test is_pdf_file(tmp_pdf)
rm(tmp_pdf)
end
end
end
@testset "class options" begin
tmp_pdf = tempname() * ".pdf"
mktempdir() do dir
cd(dir) do
PGFPlotsX.CLASS_OPTIONS[1] = "varwidth"
push!(PGFPlotsX.CLASS_OPTIONS, "crop = false")
td = TikzDocument("\\begin{tabular}{cc}",
TikzPicture(Axis(Plot(Expression("x^2")))),
"& A \\end{tabular}")
pgfsave(tmp_pdf, td)
@test is_pdf_file(tmp_pdf)
rm(tmp_pdf)
end
end
end