texinfo.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ;;;; texinfo.test -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010 Free Software Foundation, Inc.
  4. ;;;; Copyright (C) 2001,2002 Oleg Kiselyov <oleg at pobox dot com>
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. ;;; Commentary:
  20. ;;
  21. ;; Unit tests for (sxml texinfo). Adapted from xml.ssax.scm.
  22. ;;
  23. ;;; Code:
  24. (define-module (test-suite texinfo)
  25. #:use-module (test-suite lib)
  26. #:use-module (texinfo))
  27. (define exception:eof-while-reading-token
  28. '(parser-error . "^EOF while reading a token"))
  29. (define exception:wrong-character
  30. '(parser-error . "^Wrong character"))
  31. (define exception:eof-while-reading-char-data
  32. '(parser-error . "^EOF while reading char data"))
  33. (define exception:no-settitle
  34. '(parser-error . "^No \\\\n@settitle found"))
  35. (define exception:unexpected-arg
  36. '(parser-error . "^@-command didn't expect more arguments"))
  37. (define exception:bad-enumerate
  38. '(parser-error . "^Invalid"))
  39. (define nl (string #\newline))
  40. (define texinfo:read-verbatim-body
  41. (@@ (texinfo) read-verbatim-body))
  42. (with-test-prefix "test-read-verbatim-body"
  43. (define (read-verbatim-body-from-string str)
  44. (define (consumer fragment foll-fragment seed)
  45. (cons* (if (equal? foll-fragment (string #\newline))
  46. (string-append " NL" nl)
  47. foll-fragment)
  48. fragment seed))
  49. (reverse
  50. (call-with-input-string
  51. str
  52. (lambda (port) (texinfo:read-verbatim-body port consumer '())))))
  53. (pass-if (equal? '()
  54. (read-verbatim-body-from-string "@end verbatim\n")))
  55. ;; after @verbatim, the current position will always directly after
  56. ;; the newline.
  57. (pass-if-exception "@end verbatim needs a newline"
  58. exception:eof-while-reading-token
  59. (read-verbatim-body-from-string "@end verbatim"))
  60. (pass-if (equal? '("@@end verbatim" " NL\n")
  61. (read-verbatim-body-from-string "@@end verbatim\n@end verbatim\n")))
  62. (pass-if (equal? '("@@@@faosfasf adsfas " " NL\n" " asf @foo{asdf}" " NL\n")
  63. (read-verbatim-body-from-string
  64. "@@@@faosfasf adsfas \n asf @foo{asdf}\n@end verbatim\n")))
  65. (pass-if (equal? '("@end verbatim " " NL\n")
  66. (read-verbatim-body-from-string "@end verbatim \n@end verbatim\n"))))
  67. (define texinfo:read-arguments
  68. (@@ (texinfo) read-arguments))
  69. (with-test-prefix "test-read-arguments"
  70. (define (read-arguments-from-string str)
  71. (call-with-input-string
  72. str
  73. (lambda (port) (texinfo:read-arguments port #\}))))
  74. (define (test str expected-res)
  75. (pass-if (equal? expected-res
  76. (read-arguments-from-string str))))
  77. (test "}" '())
  78. (test "foo}" '("foo"))
  79. (test "foo,bar}" '("foo" "bar"))
  80. (test " foo , bar }" '("foo" "bar"))
  81. (test " foo , , bar }" '("foo" #f "bar"))
  82. (test "foo,,bar}" '("foo" #f "bar"))
  83. (pass-if-exception "need a } when reading arguments"
  84. exception:eof-while-reading-token
  85. (call-with-input-string
  86. "foo,,bar"
  87. (lambda (port) (texinfo:read-arguments port #\})))))
  88. (define texinfo:complete-start-command
  89. (@@ (texinfo) complete-start-command))
  90. (with-test-prefix "test-complete-start-command"
  91. (define (test command str)
  92. (call-with-input-string
  93. str
  94. (lambda (port)
  95. (call-with-values
  96. (lambda ()
  97. (texinfo:complete-start-command command port))
  98. list))))
  99. (pass-if (equal? '(section () EOL-TEXT)
  100. (test 'section "foo bar baz bonzerts")))
  101. (pass-if (equal? '(deffnx ((category "Function") (name "foo") (arguments)) EOL-TEXT-ARGS)
  102. (test 'deffnx "Function foo")))
  103. (pass-if-exception "@emph missing a start brace"
  104. exception:wrong-character
  105. (test 'emph "no brace here"))
  106. (pass-if (equal? '(emph () INLINE-TEXT)
  107. (test 'emph "{foo bar baz bonzerts")))
  108. (pass-if (equal? '(ref ((node "foo bar") (section "baz") (info-file "bonzerts"))
  109. INLINE-ARGS)
  110. (test 'ref "{ foo bar ,, baz, bonzerts}")))
  111. (pass-if (equal? '(node ((name "referenced node")) EOL-ARGS)
  112. (test 'node " referenced node\n"))))
  113. (define texinfo:read-char-data
  114. (@@ (texinfo) read-char-data))
  115. (define make-texinfo-token cons)
  116. (with-test-prefix "test-read-char-data"
  117. (let* ((code (make-texinfo-token 'START 'code))
  118. (ref (make-texinfo-token 'EMPTY 'ref))
  119. (title (make-texinfo-token 'LINE 'title))
  120. (node (make-texinfo-token 'EMPTY 'node))
  121. (eof-object (with-input-from-string "" read))
  122. (str-handler (lambda (fragment foll-fragment seed)
  123. (if (string-null? foll-fragment)
  124. (cons fragment seed)
  125. (cons* foll-fragment fragment seed)))))
  126. (define (test str expect-eof? preserve-ws? expected-data expected-token)
  127. (call-with-values
  128. (lambda ()
  129. (call-with-input-string
  130. str
  131. (lambda (port)
  132. (texinfo:read-char-data
  133. port expect-eof? preserve-ws? str-handler '()))))
  134. (lambda (seed token)
  135. (let ((result (reverse seed)))
  136. (pass-if (equal? expected-data result))
  137. (pass-if (equal? expected-token token))))))
  138. ;; add some newline-related tests here
  139. (test "" #t #f '() eof-object)
  140. (test "foo bar baz" #t #f '("foo bar baz") eof-object)
  141. (pass-if-exception "eof reading char data"
  142. exception:eof-while-reading-token
  143. (test "" #f #f '() eof-object))
  144. (test " " #t #f '(" ") eof-object)
  145. (test " @code{foo} " #f #f '(" ") code)
  146. (test " @code" #f #f '(" ") code)
  147. (test " {text here} asda" #f #f '(" ") (make-texinfo-token 'START '*braces*))
  148. (test " blah blah} asda" #f #f '(" blah blah") (make-texinfo-token 'END #f))))
  149. (with-test-prefix "test-texinfo->stexinfo"
  150. (define (test str expected-res)
  151. (pass-if (equal? expected-res
  152. (call-with-input-string str texi->stexi))))
  153. (define (try-with-title title str)
  154. (call-with-input-string
  155. (string-append "foo bar baz\n@settitle " title "\n" str)
  156. texi->stexi))
  157. (define (test-with-title title str expected-res)
  158. (test (string-append "foo bar baz\n@settitle " title "\n" str)
  159. expected-res))
  160. (define (test-body str expected-res)
  161. (pass-if (equal? expected-res
  162. (cddr (try-with-title "zog" str)))))
  163. (define (list-intersperse src-l elem)
  164. (if (null? src-l) src-l
  165. (let loop ((l (cdr src-l)) (dest (cons (car src-l) '())))
  166. (if (null? l) (reverse dest)
  167. (loop (cdr l) (cons (car l) (cons elem dest)))))))
  168. (define (join-lines . lines)
  169. (apply string-append (list-intersperse lines "\n")))
  170. (pass-if-exception "missing @settitle"
  171. exception:no-settitle
  172. (call-with-input-string "@dots{}\n" texi->stexi))
  173. (test "\\input texinfo\n@settitle my title\n@dots{}\n"
  174. '(texinfo (% (title "my title")) (para (dots))))
  175. (test-with-title "my title" "@dots{}\n"
  176. '(texinfo (% (title "my title")) (para (dots))))
  177. (test-with-title "my title" "@dots{}"
  178. '(texinfo (% (title "my title")) (para (dots))))
  179. (pass-if-exception "arg to @dots{}"
  180. exception:unexpected-arg
  181. (call-with-input-string
  182. "foo bar baz\n@settitle my title\n@dots{arg}"
  183. texi->stexi))
  184. (test-body "@code{arg}"
  185. '((para (code "arg"))))
  186. (test-body "@code{ }"
  187. '((para (code))))
  188. (test-body "@code{ @code{} }"
  189. '((para (code (code)))))
  190. (test-body "@code{ abc @code{} }"
  191. '((para (code "abc " (code)))))
  192. (test-body "@code{ arg }"
  193. '((para (code "arg"))))
  194. (test-body "@example\n foo asdf asd sadf asd \n@end example\n"
  195. '((example " foo asdf asd sadf asd ")))
  196. (test-body (join-lines
  197. "@quotation"
  198. "@example"
  199. " foo asdf asd sadf asd "
  200. "@end example"
  201. "@end quotation"
  202. "")
  203. '((quotation (example " foo asdf asd sadf asd "))))
  204. (test-body (join-lines
  205. "@quotation"
  206. "@example"
  207. " foo asdf @var{asd} sadf asd "
  208. "@end example"
  209. "@end quotation"
  210. "")
  211. '((quotation (example " foo asdf " (var "asd") " sadf asd "))))
  212. (test-body (join-lines
  213. "@quotation"
  214. "@example"
  215. " foo asdf @var{asd} sadf asd "
  216. ""
  217. "not in new para, this is an example"
  218. "@end example"
  219. "@end quotation"
  220. "")
  221. '((quotation
  222. (example
  223. " foo asdf " (var "asd")
  224. " sadf asd \n\nnot in new para, this is an example"))))
  225. (test-body (join-lines
  226. "@titlepage"
  227. "@quotation"
  228. " foo asdf @var{asd} sadf asd "
  229. ""
  230. "should be in new para"
  231. "@end quotation"
  232. "@end titlepage"
  233. "")
  234. '((titlepage
  235. (quotation (para "foo asdf " (var "asd") " sadf asd")
  236. (para "should be in new para")))))
  237. (test-body (join-lines
  238. ""
  239. "@titlepage"
  240. ""
  241. "@quotation"
  242. " foo asdf @var{asd} sadf asd "
  243. ""
  244. "should be in new para"
  245. ""
  246. ""
  247. "@end quotation"
  248. "@end titlepage"
  249. ""
  250. "@bye"
  251. ""
  252. "@foo random crap at the end"
  253. "")
  254. '((titlepage
  255. (quotation (para "foo asdf " (var "asd") " sadf asd")
  256. (para "should be in new para")))))
  257. (test-body (join-lines
  258. ""
  259. "random notes"
  260. "@quotation"
  261. " foo asdf @var{asd} sadf asd "
  262. ""
  263. "should be in new para"
  264. ""
  265. ""
  266. "@end quotation"
  267. ""
  268. " hi mom"
  269. "")
  270. '((para "random notes")
  271. (quotation (para "foo asdf " (var "asd") " sadf asd")
  272. (para "should be in new para"))
  273. (para "hi mom")))
  274. (test-body (join-lines
  275. "@enumerate"
  276. "@item one"
  277. "@item two"
  278. "@item three"
  279. "@end enumerate"
  280. )
  281. '((enumerate (item (para "one"))
  282. (item (para "two"))
  283. (item (para "three")))))
  284. (test-body (join-lines
  285. "@enumerate 44"
  286. "@item one"
  287. "@item two"
  288. "@item three"
  289. "@end enumerate"
  290. )
  291. '((enumerate (% (start "44"))
  292. (item (para "one"))
  293. (item (para "two"))
  294. (item (para "three")))))
  295. (pass-if-exception "bad enumerate formatter"
  296. exception:bad-enumerate
  297. (try-with-title "foo" (join-lines
  298. "@enumerate string"
  299. "@item one"
  300. "@item two"
  301. "@item three"
  302. "@end enumerate"
  303. )))
  304. (pass-if-exception "bad itemize formatter"
  305. exception:bad-enumerate
  306. (try-with-title "foo" (join-lines
  307. "@itemize string"
  308. "@item one"
  309. "@item two"
  310. "@item three"
  311. "@end itemize"
  312. )))
  313. (test-body (join-lines
  314. "@itemize" ;; no formatter, should default to bullet
  315. "@item one"
  316. "@item two"
  317. "@item three"
  318. "@end itemize"
  319. )
  320. '((itemize (% (bullet (bullet)))
  321. (item (para "one"))
  322. (item (para "two"))
  323. (item (para "three")))))
  324. (test-body (join-lines
  325. "@itemize @bullet"
  326. "@item one"
  327. "@item two"
  328. "@item three"
  329. "@end itemize"
  330. )
  331. '((itemize (% (bullet (bullet)))
  332. (item (para "one"))
  333. (item (para "two"))
  334. (item (para "three")))))
  335. (test-body (join-lines
  336. "@itemize -"
  337. "@item one"
  338. "@item two"
  339. "@item three"
  340. "@end itemize"
  341. )
  342. '((itemize (% (bullet "-"))
  343. (item (para "one"))
  344. (item (para "two"))
  345. (item (para "three")))))
  346. (test-body (join-lines
  347. "@table @code"
  348. "preliminary text -- should go in a pre-item para"
  349. "@item one"
  350. "item one text"
  351. "@item two"
  352. "item two text"
  353. ""
  354. "includes a paragraph"
  355. "@item three"
  356. "@end itemize"
  357. )
  358. '((table (% (formatter (code)))
  359. (para "preliminary text -- should go in a pre-item para")
  360. (entry (% (heading "one"))
  361. (para "item one text"))
  362. (entry (% (heading "two"))
  363. (para "item two text")
  364. (para "includes a paragraph"))
  365. (entry (% (heading "three"))))))
  366. (test-body (join-lines
  367. "@chapter @code{foo} bar"
  368. "text that should be in a para"
  369. )
  370. '((chapter (code "foo") " bar")
  371. (para "text that should be in a para")))
  372. (test-body (join-lines
  373. "@deffnx Method foo bar @code{baz}"
  374. "text that should be in a para"
  375. )
  376. '((deffnx (% (category "Method")
  377. (name "foo")
  378. (arguments "bar " (code "baz"))))
  379. (para "text that should be in a para")))
  380. )