Skip to content

Commit

Permalink
Add unit tests to documentfinder.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Jun 23, 2024
1 parent 557ab7e commit 8bd289e
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions doc/generic/pgf/lib/documentfinder.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
local UNIT_TESTING = false

local function pwd()
local info = debug.getinfo(1, "S")
local path = info.source:match("@(.*)")
local dir = path:match("(.*[/\\])") or "./"
return dir
end
package.path = pwd() .. "?.lua;" .. package.path

if UNIT_TESTING then
local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua"
package.path = package.path .. ";" .. luarocks_path
end

local lpeg = require("lpeg")
local C, Cf, Cg, Ct, P, S, V = lpeg.C, lpeg.Cf, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.S, lpeg.V
local u = require("utils")
Expand Down Expand Up @@ -34,4 +49,72 @@ function finder.get_name()
return "document"
end

if not UNIT_TESTING then
return finder
end

local tostring = require "ml".tstring

local testcase_1 =
[=[
\begin{codeexample}[preamble={\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}}]
\tikz \graph [tree layout, nodes={draw}, component sep=0pt,
component packing=rectangular]
{ a -- long text, longer text -- b};
\end{codeexample}
]=]

do
local matches = finder.grammar:match(testcase_1)
assert(#matches == 1)
assert(
finder.get_options(matches[1]).preamble == [[
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}]]
)
assert(
u.strip(finder.get_content(matches[1])) ==
[[
\tikz \graph [tree layout, nodes={draw}, component sep=0pt,
component packing=rectangular]
{ a -- long text, longer text -- b};]]
)
end

local testcase_2 =
[=[
\begin{codeexample}[code only]
\graph {
% The nodes:
a, b, c, d;
% The edges:
{[hyper] a,b,c};
{[hyper] b,c,d};
{[hyper] a,c};
{[hyper] d}
};
\end{codeexample}
]=]
do
local matches = finder.grammar:match(testcase_2)
assert(#matches == 1)
assert(finder.get_options(matches[1])["code only"] == u.invalid)
assert(
u.strip(finder.get_content(matches[1])) ==
[[
\graph {
% The nodes:
a, b, c, d;
% The edges:
{[hyper] a,b,c};
{[hyper] b,c,d};
{[hyper] a,c};
{[hyper] d}
};]]
)
end

return finder

0 comments on commit 8bd289e

Please sign in to comment.