# Using qstest to implement unit tests

I was trying to figure out how to test my xpeek package (see https://github.com/jcsalomon/xpeek) and I came across What's the folk-lore on automatic testing of TeX programming? and Testing Framework API for LaTeX. Both of these mention the qstest package, but I’m having trouble understanding how to use it.

To be concrete, consider this test of xspace (xpeek can be used to implement \xspace, so this is not far removed from what I want to do):

\documentclass{article}
\usepackage{xspace, qstest}
\begin{document}

\begin{qstest}{test xspace}{}
\Expect{foo\xspace.}{foo.}
\end{qstest}

\end{document}


This fails, yielding this error message:

! Package qstest Error: Failed: test xspace
\Expect: foo\xspace .
<foo.
>foo\xspace ..

See the qstest package documentation for explanation.


Obviously I’m missing something fundamental here, but reading the qstest package documentation is not making things any clearer to me. Can anyone explain to me how to write the test so it succeeds?

-

"foo\xspace." and "foo." are indeed different, thus the test must fail. \Expect can expand the arguments if they are preceded by a star:

\Expect*{foo\xspace.}{foo.}


However, this will not help either, because \xspace is not expandable (\futurelet and other assignments). But we can put the argument in a box and measure the width. If \xspace would insert a space, the width of the box would be larger.

\begin{qstest}{test xspace}{}
\sbox0{foo\xspace.}
\sbox2{foo.}
\Expect*{\the\wd0}*{\the\wd2}
\end{qstest}

-
Is that what the test macros inside your packages boil down to? Seems like quite a limitation. BTW, you & Will Robertson are the only ones whose packages on CTAN use qstest that I can find. –  J. C. Salomon Aug 15 '12 at 16:32
@Salomon, you can take LuaTeX and look into the contents of boxes using Lua and check, whether is a space. Testing is sometimes something like finding relevant properties of a feature that can be checked against the expectations. –  Heiko Oberdiek Aug 15 '12 at 16:41
LuaTeX is a rabbit-hole I’m not looking to climb down at the moment, but it’s good to know about. I’ve added a simple test-suite based on your answer to xpeek at this commit. –  J. C. Salomon Aug 15 '12 at 19:59