summaryrefslogtreecommitdiffstats
path: root/tests/common.tl
blob: 9f7e6eb3cd29913d7b0ad09d3a0dc1e8fc6ca9ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(defmacro error-to-sym (expr)
 ^(catch ,expr
    (error (cond) :error)))

(defmacro vtest (:env env expr expected)
  (if (mequal expected :error '':error)
    (catch
      (let ((expr-expn (expand expr env)))
        ^(ifa (not (equal (error-to-sym ,expr-expn) :error))
           (error "test case ~s failed: produced ~s; expected ~s"
                  ',expr it :error)))
      (error (exc)))
    (let ((expr-expn (expand expr env))
          (expval (gensym)))
      ^(let ((,expval ,expected))
         (ifa (not (equal ,expr-expn ,expval))
           (error "test case ~s failed: produced ~s; expected ~s"
                  ',expr it ,expval))))))

(defmacro test (expr expected)
  ^(vtest ,expr ',expected))

(defmacro stest (expr expected)
  ^(vtest ,^(tostring ,expr) ,expected))

(defmacro mtest (. pairs)
  ^(progn ,*(mapcar (op cons 'test) (tuples 2 pairs))))

(defun os-symbol ()
  (if (ignerr (dlsym (dlopen "libandroid.so") "AAsset_close"))
    :android
    (let ((u (uname)))
      [(orf (iff (f^ #/Linux/) (ret :linux))
            (iff (f^ #/SunOS/) (ret :solaris))
            (iff (f^ #/CYGWIN/) (ret :cygwin))
            (iff (f^ #/CYGNAL/) (ret :cygnal))
            (iff (f^ #/Darwin/) (ret :macos))
            (iff (f^ #/OpenBSD/) (ret :openbsd))
            (ret :unknown))
       u.sysname])))

(defun libc ()
  (caseql (os-symbol)
    ((:cygwin :cygnal) (dlopen "cygwin1.dll"))
    (t (dlopen nil))))

(defmacro macro-time-let (:env env bindings . body)
  (with-gensyms (invoke)
    ^(macrolet ((,invoke ()
                  (let ,bindings
                     (expand '(progn ,*body) ,env))))
       (,invoke))))