1 ;;;; elisp-compiler.test --- Test the compiler for Elisp.
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-elisp-compiler)
21 :use-module (test-suite lib)
22 :use-module (system base compile)
23 :use-module (language elisp runtime))
26 ; Macros to handle the compilation conveniently.
28 (define-syntax compile-test
29 (syntax-rules (pass-if pass-if-exception)
30 ((_ (pass-if test-name exp))
31 (pass-if test-name (compile 'exp #:from 'elisp #:to 'value)))
32 ((_ (pass-if-equal test-name result exp))
33 (pass-if test-name (equal? result
34 (compile 'exp #:from 'elisp #:to 'value))))
35 ((_ (pass-if-exception test-name exc exp))
36 (pass-if-exception test-name exc
37 (compile 'exp #:from 'elisp #:to 'value)))))
39 (define-syntax with-test-prefix/compile
41 ((_ section-name exp ...)
42 (with-test-prefix section-name (compile-test exp) ...))))
45 ; Test control structures.
46 ; ========================
48 (with-test-prefix/compile "Sequencing"
50 (pass-if-equal "progn" 1
55 (with-test-prefix/compile "Conditionals"
57 (pass-if-equal "succeeding if" 1
59 (pass-if-equal "failing if" 3
66 (pass-if-equal "empty cond" nil-value
68 (pass-if-equal "all failing cond" nil-value
70 (pass-if-equal "only condition" 5
72 (pass-if-equal "succeeding cond value" 42
73 (cond (nil) (t 42) (t 0)))
74 (pass-if-equal "succeeding cond side-effect" 42
76 (cond (nil) (t (setq a 42) 1) (t (setq a 0)))
79 (with-test-prefix/compile "Combining Conditions"
81 (pass-if-equal "empty and" t-value (and))
82 (pass-if-equal "failing and" nil-value (and 1 2 nil 3))
83 (pass-if-equal "succeeding and" 3 (and 1 2 3))
85 (pass-if-equal "empty or" nil-value (or))
86 (pass-if-equal "failing or" nil-value (or nil nil nil))
87 (pass-if-equal "succeeding or" 1 (or nil 1 nil 2 nil 3)))
89 (with-test-prefix/compile "Iteration"
91 (pass-if-equal "failing while" 0
93 (while nil (setq a 1))
95 (pass-if-equal "running while" 120
99 (setq prod (* i prod))
104 ; Test handling of variables.
105 ; ===========================
107 (with-test-prefix/compile "Variable Setting/Referencing"
109 ; TODO: Check for variable-void error
111 (pass-if-equal "setq and reference" 6
117 (with-test-prefix/compile "Let and Let*"
119 (pass-if-equal "let without value" nil-value
121 (pass-if-equal "basic let" 0
126 (pass-if-equal "let*" 1
132 (pass-if "local scope"
140 (with-test-prefix/compile "defconst and defvar"
142 (pass-if-equal "defconst without docstring" 3.141
146 (pass-if-equal "defconst value" 'pi
147 (defconst pi 3.141 "Pi"))
149 (pass-if-equal "defvar without value" 42
153 (pass-if-equal "defvar on already defined variable" 42
155 (defvar a 1 "Some docstring is also ok")
157 ; FIXME: makunbound a!
158 (pass-if-equal "defvar on undefined variable" 1
161 (pass-if-equal "defvar value" 'a
165 ; Functions and lambda expressions.
166 ; =================================
168 (with-test-prefix/compile "Lambda Expressions"
170 (pass-if-equal "required arguments" 3
171 ((lambda (a b c) c) 1 2 3))
173 (pass-if-equal "optional argument" 3
174 ((function (lambda (a &optional b c) c)) 1 2 3))
175 (pass-if-equal "optional missing" nil-value
176 ((lambda (&optional a) a)))
178 (pass-if-equal "rest argument" '(3 4 5)
179 ((lambda (a b &rest c) c) 1 2 3 4 5))
180 (pass-if-equal "rest missing" nil-value
181 ((lambda (a b &rest c) c) 1 2)))
183 (with-test-prefix/compile "Function Definitions"
185 (pass-if-equal "defun" 3
186 (progn (defun test (a b) (+ a b))
188 (pass-if-equal "defun value" 'test
189 (defun test (a b) (+ a b))))
191 (with-test-prefix/compile "Calling Functions"
193 (pass-if-equal "recursion" 120
194 (progn (defun factorial (n prod)
197 (factorial (1- n) (* prod n))))
200 (pass-if "dynamic scoping"