1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 18:18:51 +03:00
mal/tests/step8_macros.mal

165 lines
2.1 KiB
Plaintext

;; Testing non-macro function
(not (= 1 1))
;=>false
;;; This should fail if it is a macro
(not (= 1 2))
;=>true
;; Testing trivial macros
(defmacro! one (fn* () 1))
(one)
;=>1
(defmacro! two (fn* () 2))
(two)
;=>2
;; Testing unless macros
(defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
(unless false 7 8)
;=>7
(unless true 7 8)
;=>8
(defmacro! unless2 (fn* (pred a b) `(if (not ~pred) ~a ~b)))
(unless2 false 7 8)
;=>7
(unless2 true 7 8)
;=>8
;; Testing macroexpand
(macroexpand (unless2 2 3 4))
;=>(if (not 2) 3 4)
;; Testing evaluation of macro result
(defmacro! identity (fn* (x) x))
(let* (a 123) (identity a))
;=>123
;; Testing nth, first and rest functions
(nth '(1) 0)
;=>1
(nth '(1 2) 1)
;=>2
(def! x "x")
(def! x (nth '(1 2) 2))
x
;=>"x"
(first '())
;=>nil
(first '(6))
;=>6
(first '(7 8 9))
;=>7
(rest '())
;=>()
(rest '(6))
;=>()
(rest '(7 8 9))
;=>(8 9)
;; Testing or macro
(or)
;=>nil
(or 1)
;=>1
(or 1 2 3 4)
;=>1
(or false 2)
;=>2
(or false nil 3)
;=>3
(or false nil false false nil 4)
;=>4
(or false nil 3 false nil 4)
;=>3
(or (or false 4))
;=>4
;; Testing cond macro
(cond)
;=>nil
(cond true 7)
;=>7
(cond true 7 true 8)
;=>7
(cond false 7 true 8)
;=>8
(cond false 7 false 8 "else" 9)
;=>9
(cond false 7 (= 2 2) 8 "else" 9)
;=>8
(cond false 7 false 8 false 9)
;=>nil
;; Testing EVAL in let*
(let* (x (or nil "yes")) x)
;=>"yes"
;;
;; -------- Optional Functionality --------
;; Testing nth, first, rest with vectors
(nth [1] 0)
;=>1
(nth [1 2] 1)
;=>2
(def! x "x")
(def! x (nth [1 2] 2))
x
;=>"x"
(first [])
;=>nil
(first nil)
;=>nil
(first [10])
;=>10
(first [10 11 12])
;=>10
(rest [])
;=>()
(rest nil)
;=>()
(rest [10])
;=>()
(rest [10 11 12])
;=>(11 12)
;; Testing EVAL in vector let*
(let* [x (or nil "yes")] x)
;=>"yes"
;;
;; Loading core.mal
(load-file "../core.mal")
;; Testing -> macro
(-> 7)
;=>7
(-> (list 7 8 9) first)
;=>7
(-> (list 7 8 9) (first))
;=>7
(-> (list 7 8 9) first (+ 7))
;=>14
(-> (list 7 8 9) rest (rest) first (+ 7))
;=>16
;; Testing ->> macro
(->> "L")
;=>"L"
(->> "L" (str "A") (str "M"))
;=>"MAL"
(->> [4] (concat [3]) (concat [2]) rest (concat [1]))
;=>(1 3 4)