Debian Codegraph @

t/test-api.lisp

raw
1 ;;;; test-api.lisp --- CL-MUSTACHE API test
2
3 ;;; Copyright (C) 2012 Kan-Ru Chen (陳侃如)
4
5 ;;; Author(s): Kan-Ru Chen (陳侃如) <[email protected]>
6
7 ;;; Permission is hereby granted, free of charge, to any person obtaining a
8 ;;; copy of this software and associated documentation files (the "Software"),
9 ;;; to deal in the Software without restriction, including without limitation
10 ;;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 ;;; and/or sell copies of the Software, and to permit persons to whom the
12 ;;; Software is furnished to do so, subject to the following conditions:
13
14 ;;; The above copyright notice and this permission notice shall be included in
15 ;;; all copies or substantial portions of the Software.
16
17 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 ;;; DEALINGS IN THE SOFTWARE.
24
25 ;;;; Commentary:
26
27 ;;;
28
29 ;;;; Code:
30
31 (in-package :cl-user)
32 (defpackage #:mustache-test-api
33 (:use #:cl #:prove))
34 (in-package :mustache-test-api)
35
36 (defclass unreadable-class ()
37 ())
38
39 (plan 14)
40
41 (is-type (mustache:version) 'string
42 "(mustache:version) is a version string")
43
44 (is-type (mustache:compile-template "string") 'function
45 "compile a string template")
46 (is-type (mustache:compile-template
47 (make-pathname
48 :directory (pathname-directory
49 #.(or *load-truename* *compile-file-truename*))
50 :name "test"
51 :type "mustache"))
52 'function
53 "compile a file template")
54
55 (is (with-output-to-string (mustache:*output-stream*)
56 (mustache:render "TEMPLATE"))
57 "TEMPLATE"
58 "render to mustache:*output-stream*")
59 (is (mustache:render* "TEMPLATE")
60 "TEMPLATE"
61 "render to string")
62 (is (with-output-to-string (out)
63 (mustache:render "TEMPLATE" nil out))
64 "TEMPLATE"
65 "render to out stream")
66
67 (mustache:define test-mustache-define "TEMPLATE")
68 (is (with-output-to-string (mustache:*output-stream*)
69 (test-mustache-define))
70 "TEMPLATE"
71 "mustache:define works")
72
73 (is (mustache:render* "{{#list}}{{item}}{{/list}}"
74 '((list . (((item . "a"))
75 ((item . "b"))
76 ((item . "c"))))))
77 "abc"
78 "render context from list")
79
80 (is (mustache:render* "{{var}}"
81 `((:var . ,(make-array 0 :element-type 'character
82 :adjustable t
83 :fill-pointer 0))))
84 ""
85 "render a string of type '(array character (*))")
86
87 (is (mustache:render* "{{var}}"
88 (let ((context (make-hash-table :test #'equal)))
89 (setf (gethash "VAR" context) "test")
90 context))
91 "test"
92 "use a hash-table as context.")
93
94 (is (mustache:render* "{{escape}}" '((escape . "<>&\"'")))
95 "&lt;&gt;&amp;&quot;&#39;"
96 "escape char")
97
98 (is (mustache:render* "{{var}}" '((var . "pass") (var . "fail")))
99 "pass"
100 "use alist as context, the first match should shadow the rest.")
101
102 (let ((unreadable-instance (make-instance 'unreadable-class)))
103 (is (mustache:render* "!!{{unknown-type}}!!"
104 `((unknown-type . ,unreadable-instance)))
105 (format nil "!!~a!!" (mustache::escape
106 (princ-to-string unreadable-instance)))
107 "escape non printable types"))
108
109
110 (is (mustache:render* "{{symbol}}"
111 '((symbol . symbol)))
112 "SYMBOL"
113 "print symbols")
114
115 (finalize)
116
117 ;;; test-api.lisp ends here
118
119 ;;; Local Variables:
120 ;;; mode: lisp
121 ;;; End: