diff --git a/doc/generic/pgf/lib/examplewithoptionfinder.lua b/doc/generic/pgf/lib/examplewithoptionfinder.lua index 5560b4a2f..3286eec50 100644 --- a/doc/generic/pgf/lib/examplewithoptionfinder.lua +++ b/doc/generic/pgf/lib/examplewithoptionfinder.lua @@ -10,25 +10,19 @@ if UNIT_TESTING then package.path = pwd() .. "?.lua;" .. package.path local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua" package.path = package.path .. ";" .. luarocks_path - print("package.path: " .. package.path) +-- print("package.path: " .. package.path) -- local tostring = require "ml".tstring end local lpeg = require("lpeg") local loc = lpeg.locale() --- local u = require("utils") +local u = require("utils") local str = require("stringmatcher") local C, Ct, P, V = lpeg.C, lpeg.Ct, lpeg.P, lpeg.V local t = {} local SP = loc.space ^ 0 -local result = {} -local function keyval(key, val) - result[key] = val - return result -end - -- local function sanitize(matches) -- if type(matches) == "table" then -- return matches @@ -55,10 +49,12 @@ t.grammar = example_begin = P "\n" ^ -1 * "example({" * SP, example_end = "\n})" * SP, -- content = C((1 - V "example_end") ^ 0), - content = Ct(V "options" ^ -1 * V "code"), + content = Ct(lpeg.Cf(Ct "" * V "options" ^ -1 * V "code", u.set)), anything = (1 - V "example") ^ 0, - options = SP * C("options") * SP * "=" * str ^ -1 * (P ",") ^ -1 / keyval, - code = SP * C("code") * SP * "=" * str / keyval + options = SP * (V "optionskv") ^ -1 * (P ",") ^ -1, + optionskv = lpeg.Cg(C("options") * SP * "=" * str), + code = SP * V "codekv", + codekv = lpeg.Cg(C("code") * SP * "=" * str) } local function preamble(options) @@ -88,8 +84,7 @@ if UNIT_TESTING then local test_options = [[ preamble=\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{layered} ]] local p = SP * P "preamble" * SP * "=" * SP * C(P(1) ^ 1) local matches = p:match(test_options) - print("type(matches)", type(matches)) - print("to_string(p:match(test_options)):", tostring(p:match(test_options))) + assert(matches == [[\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{layered} ]]) -- os.exit() -- local tostring = require "ml".tstring @@ -119,8 +114,24 @@ example({ -- local result = t.grammar:match(mini_test_case) -- print("captured:", captured) -- print("tostring(result): ", tostring(matches[1])) - print("t.get_options(matches):", tostring(t.get_options(matches))) - print("t.get_content(matches):", t.get_content(matches)) + assert(#matches == 1) + local e = matches[1] + assert(tostring(t.get_options(e)) == [[{preamble="\\usetikzlibrary{graphs,graphdrawing} \\usegdlibrary{layered"}]]) + -- print("t.get_content(matches):", t.get_content(e)) + assert( + u.strip(t.get_content(e)) == + u.strip( + [[ + \begin{tikzpicture} + \draw [help lines] (0,0) grid (2,2); + + \graph [layered layout, edges=rounded corners] + { a -- {b, c [anchor here] } -- d -- a}; + \end{tikzpicture +]] + ) + ) + -- print("captured.options:", captured.options) -- print("captured.code:", captured.code) -- print("result.options:", result.options)