Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 186 lines (145 sloc) 4.7 kB
5378653 @sroccaserra Added tools.el
authored
1 ;;
2 ;;
3 ;;
4
5 (require 'tools)
6
3b1da06 @sroccaserra small tool changes
authored
7 ;;;;;;;;;;;;
8 ;; Sequences
9
5378653 @sroccaserra Added tools.el
authored
10 (deftest "seq returns the sequence itself"
11 (assert-equal '(1)
3b1da06 @sroccaserra small tool changes
authored
12 (sequence '(1)))
5378653 @sroccaserra Added tools.el
authored
13 (assert-equal [1]
3b1da06 @sroccaserra small tool changes
authored
14 (sequence [1]))
5378653 @sroccaserra Added tools.el
authored
15 (assert-equal "1"
3b1da06 @sroccaserra small tool changes
authored
16 (sequence "1")))
5378653 @sroccaserra Added tools.el
authored
17
18 (deftest "seq turns non-sequences into a list"
19 (assert-equal '(1)
3b1da06 @sroccaserra small tool changes
authored
20 (sequence 1))
5378653 @sroccaserra Added tools.el
authored
21 (assert-equal '(toto)
3b1da06 @sroccaserra small tool changes
authored
22 (sequence 'toto)))
23
24 (deftest "I can substract two vectors"
25 (assert-equal ["ABC"]
26 (seq-difference ["ABC" "DEF"] ["DEF"])))
27
28 (deftest "retrieving a random element"
29 (assert-equal "ABC"
30 (random-elt ["ABC"])))
31
32 ;;;;;;;;;;;;;
33 ;; Functional
5378653 @sroccaserra Added tools.el
authored
34
35 (deftest "currying the plus function"
36 (assert-equal 5
37 (funcall (partial + 2) 3)))
38
39 (deftest "using curry in map"
40 (assert-equal [2 4 6]
41 (map 'vector (partial * 2) [1 2 3])))
42
43 (deftest "using curry in filter"
44 (assert-equal [2 3]
45 (remove-if-not (partial < 1) [1 2 3])))
46
3b1da06 @sroccaserra small tool changes
authored
47 (lexdef lexdef-test (x)
48 (lambda () x))
49
50 (defun defun-test (x)
51 (lambda () x))
52
53 (deftest "lexdef has lexical scoping"
54 (let ((lexical (lexdef-test 1))
55 (dynamic (defun-test 1))
56 (x 2))
57 (assert-equal 1
58 (funcall lexical))
59 (assert-equal 2
60 (funcall dynamic))))
61
62 ;;;;;;;;;;;;;;
63 ;; Trush tests
64
5378653 @sroccaserra Added tools.el
authored
65 (deftest "applying trush to one form"
66 (assert-equal 5.0
67 (-> 25 sqrt)))
68
69 (deftest "applying trush to two forms"
70 (assert-equal 3.0
71 (-> 25 sqrt (- 2))))
72
73 (deftest "chaining trush combinators"
74 (assert-equal "-2.0"
75 (-> 25 sqrt (->> (- 3) number-to-string))))
76
77 (deftest "threading three forms"
78 (assert-equal '(* 6 (+ 4 5 (- 2 3 x)))
79 (macroexpand '(->> x (- 2 3) (+ 4 5) (* 6)))))
80
81 (deftest "comment returns nil"
82 (assert-equal nil
83 (comment 3)))
84
85 (deftest "comment does nothing"
86 (let ((x 1))
87 (setq x 2)
88 (comment
89 (setq x 3))
90 (assert-equal 2 x)))
91
3b1da06 @sroccaserra small tool changes
authored
92 ;;;;;;;;
93 ;; Maths
94
5378653 @sroccaserra Added tools.el
authored
95 (deftest "variance of a const list returns 0"
96 (->> [[1 1 1]
97 [2 2 2 2 2]
98 [3 3 3 3 3 3 3]]
99 (mapcar (lambda (v)
100 (assert-equal 0.0 (variance v))))))
101
102 (deftest "variance of a simple list"
103 (assert-that (lambda (x) (< (eval x) 0.001))
104 (- (/ 2 3.0)
105 (variance [1 2 3]))))
106
107 ;;;
108 ;; Alist functions
109
110 (deftest "I can get a value by key in an alist"
111 (let ((h '((a . 1)(b . 2))))
112 (assert-equal 1
113 (alist-get h 'a))
114 (assert-equal 2
115 (alist-get h 'b))
116 (assert-nil (alist-get h 'z))
117 (assert-equal 26
118 (alist-get h 'z 26))))
119
120 (deftest "I can remove a value in an alist"
121 (let ((h '((a . 1)(b . 2)(a . 3))))
122 (assert-nil (-> h (alist-remove 'a) (alist-get 'a)))))
123
124 (deftest "I can set a value in an alist"
125 (let ((h '((a . 1)(b . 2)(a . 3))))
126 (assert-equal '((a . 5)(b . 2)) (-> h (alist-set 'a 5)))
127 (assert-equal '((a . 1)(b . 2)(a . 3)) h)))
128
129 ;;;;;;;;;;
130 ;; Strings
131
3b1da06 @sroccaserra small tool changes
authored
132 (deftest "string-equal can compare strings to symbols"
133 (assert-t (string= 'hello "hello"))
134 (assert-error "string" (string= 1 "1")))
135
5378653 @sroccaserra Added tools.el
authored
136 (deftest "an empty string is empty, a non empty string is not empty"
137 (assert-nil (string-empty-p "hello"))
3b1da06 @sroccaserra small tool changes
authored
138 (assert-t (string-empty-p (copy-seq "")))
139 (assert-t (string-empty-p nil))
140 (assert-error "string" (string-empty-p 1)))
5378653 @sroccaserra Added tools.el
authored
141
142 (deftest "I can tell if a sring is blank"
3b1da06 @sroccaserra small tool changes
authored
143 (assert-t (string-not-empty-p "hello"))
144 (assert-nil (string-not-empty-p nil))
145 (assert-nil (string-not-empty-p ""))
146 (assert-error "string" (string-not-empty-p 1)))
147
148 (deftest "symbols are non empty"
149 (assert-nil (string-empty-p 'hello)))
ee4e6fe @sroccaserra added destructuring-bind tests
authored
150
151 ;;;;;;;;;;;;;;;;;;;;;
152 ;; Destructuring bind
153
154 (deftest "destructuring nested lists"
155 (destructuring-bind (a (b c)) '(1 (2 3))
156 (assert-equal 1 a)
157 (assert-equal 2 b)
158 (assert-equal 3 c)))
159
160 (deftest "destructuring alists overlooks keys"
161 (destructuring-bind ((x . a)(y . b)) '((y . 2)(x . 1))
162 (assert-equal 2 a)
163 (assert-equal 'y x)))
164
165 (deftest "destructuring can use nested keywords"
166 (destructuring-bind (a (x &key y z)) '(1 (2 :z 4 :y 3))
167 (assert-equal 1 a)
168 (assert-equal 2 x)
169 (assert-equal 3 y)
170 (assert-equal 4 z)))
171
172 (deftest "destructuring can't use hashtables"
173 (let ((h (make-hash-table)))
174 (puthash :a 1 h)
175 (assert-error "listp" (destructuring-bind (&key a) h))))
176
177 (deftest "destructuring can't use vectors"
178 (assert-error "argument" (destructuring-bind [a] [1]))
179 (assert-error "argument" (destructuring-bind (a [b]) (1 [2]))))
180
cda56f2 @sroccaserra fixed a destructuring-bind test
authored
181 (defun destructest (a (b c))
ee4e6fe @sroccaserra added destructuring-bind tests
authored
182 (values a b c))
183
cda56f2 @sroccaserra fixed a destructuring-bind test
authored
184 (deftest "defun can't destructure"
185 (assert-error "function" (destructest '(1 (2 3)))))
Something went wrong with that request. Please try again.