-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbuttercup.el
2296 lines (2025 loc) · 92.2 KB
/
buttercup.el
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; buttercup.el --- Behavior-Driven Emacs Lisp Testing -*-lexical-binding:t-*-
;; Copyright (C) 2015-2017 Jorgen Schaefer <[email protected]>
;; Copyright (C) 2018-2024 Ola Nilsson <[email protected]>
;; Version: 1.36
;; Author: Jorgen Schaefer <[email protected]>
;; Maintainer: Ola Nilsson <[email protected]>
;; Package-Requires: ((emacs "24.4"))
;; URL: https://github.com/jorgenschaefer/emacs-buttercup
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Buttercup is a behavior-driven development framework for testing
;; Emacs Lisp code. It is heavily inspired by the Jasmine test
;; framework for JavaScript.
;; A test suite begins with a call to the Buttercup macro `describe` with
;; the first parameter describing the suite and the rest being the body
;; of code that implements the suite.
;; ;;; lexical binding is required -*- lexical-binding: t; -*-
;; (describe "A suite"
;; (it "contains a spec with an expectation"
;; (expect t :to-be t)))
;; The ideas for project were shamelessly taken from Jasmine
;; <https://jasmine.github.io>.
;; All the good ideas are theirs. All the problems are mine.
;;; Code:
(require 'cl-lib)
(require 'buttercup-compat)
(require 'format-spec)
(require 'ert nil t)
(require 'warnings)
;; A base error for all errors raised by buttercup.
(define-error 'buttercup-error-base "error")
;; Buttercup internals error, raised on internal implementation
;; inconsistencies.
(define-error 'buttercup-internals-error "Internal buttercup error"
'buttercup-error-base)
;; Raised when expanding `describe` macros whithout lexical-binding: t
(define-error 'buttercup-dynamic-binding-error "Lexical binding is not enabled"
'buttercup-error-base)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; wrapper function manipulation
;; Error for buttercup--enclosed-expr
(define-error 'buttercup-enclosed-expression-error
"Bad test expression"
'buttercup-internals-error)
(eval-and-compile
(if (fboundp 'oclosure-define) ;Emacs≥29
(oclosure-define (buttercup--thunk (:predicate buttercup--thunk-p))
"An elisp expression as a function and original code."
expr)
(defalias 'buttercup--thunk-p #'ignore
"Always return nil when Oclosures are not available.")))
(defun buttercup--enclosed-expr (fun)
"Given a FUN `buttercup-thunk', return its unevaluated expression.
For Emacs < 29:
The function MUST be byte-compiled or have one of the following
forms:
\(closure (ENVLIST) () (quote EXPR) EXPANDED)
\(lambda () (quote EXPR) EXPR)
and the return value will be EXPR, unevaluated. The quoted EXPR
is useful if EXPR is a macro call, in which case the `quote'
ensures access to the un-expanded form."
(if (buttercup--thunk-p fun)
(buttercup--thunk--expr fun)
(pcase fun
;; This should be the normal case, a closure with unknown enclosed
;; variables, empty arglist and a body containing
;; * the quoted original expression
;; * the stackframe marker
;; * the macroexpanded original expression
(`(closure ,(pred listp) nil
(quote ,expr) ,_expanded)
expr)
;; This a when FUN has not been evaluated.
;; Why does that happen?
;; A lambda with an empty arglist and a body containing
;; * the quoted original expression
;; * the stackframe marker
;; * the expanded expression
(`(lambda nil
(quote ,expr) ,_expanded)
expr)
;; This is when FUN has been byte compiled, as when the entire
;; test file has been byte compiled. Check that it has an empty
;; arglist, that is all that is possible at this point. The
;; return value is byte compiled code, not the original
;; expressions. Also what is possible at this point.
((and (pred byte-code-function-p) (guard (member (aref fun 0) '(nil 0))))
(aref fun 1))
;; Error
(_ (signal 'buttercup-enclosed-expression-error (format "Not a zero-arg one-expression closure: %S" fun))))))
(defun buttercup--expr-and-value (fun)
"Given a function, return its quoted expression and value.
FUN must be a zero-argument one-expression function, i.e.
something that satisfies `buttercup--wrapper-fun-p'. The return
value is `(cons EXPR VALUE)', where EXPR is the unevaluated
expression in the function, and VALUE is the result of calling
the function (thus evaluating EXPR in the proper lexical
environment)."
(cons (buttercup--enclosed-expr fun)
(funcall fun)))
(defun buttercup--wrapper-fun-p (fun)
"Return non-nil if FUN is a zero-arg one-expression function."
(condition-case nil
(prog1 t
(buttercup--enclosed-expr fun))
(error nil)))
;;;;;;;;;;;;;;;;;;;;
;;; Helper functions
(defun buttercup-format-spec (format specification)
"Return a string based on FORMAT and SPECIFICATION.
This is a wrapper around `format-spec', which see. This also adds
a call to `save-match-data', as `format-spec' modifies that."
(save-match-data
(format-spec format specification)))
(defun buttercup--simple-format (specification &rest format)
"Return a string based on SPECIFICATION and FORMAT.
A simpler version of `format-spec', which see.
If more than one FORMAT string is given they will be combined
before formatting replacements occur.
Does not support flags, width or precision.
The substitution for a specification character can be a function,
which is only supported in `format-spec' from Emacs 29.
Does not have the IGNORE-MISSING and SPLIT parameters."
(save-match-data
(with-temp-buffer
(apply #'insert format)
(goto-char 1)
(while (search-forward "%" nil t)
(cond
((= (following-char) ?%)
(delete-char 1))
((looking-at-p (rx alpha))
(let* ((char (following-char))
(begin (point))
(replacement (cdr (assq char specification)))
(text (if (functionp replacement) (funcall replacement) replacement)))
(insert-and-inherit text)
(delete-char 1)
(delete-region (1- begin) begin)))))
(buffer-string))))
;;;;;;;;;;
;;; expect
(define-error 'buttercup-failed "Buttercup test failed" 'buttercup-error-base)
(define-error 'buttercup-pending "Buttercup test is pending" 'buttercup-error-base)
(defun buttercup--wrap-expr (expr)
"Wrap EXPR in a `buttercup--thunk' to be used by `buttercup-expect'.
This function is only usable from within the buttercup `expect' macro."
(if (fboundp 'oclosure-lambda) ;Emacs≥29
`(oclosure-lambda (buttercup--thunk (expr ',expr)) ()
,expr)
`(lambda ()
(quote ,expr)
,expr)))
(defmacro expect (arg &optional matcher &rest args)
"Expect a condition to be true.
This macro knows two forms:
\(expect ARG :MATCHER [ARGS...])
Fail the current test if the MATCHER does not match these arguments.
The correct number of arguments depend on the MATCHER.
See `buttercup-define-matcher' for more information on matchers.
\(expect ARG)
Fail the current test if ARG is not true.
This is the same as (expect ARG :to-be-truthy).
All arguments can be a value or a single Lisp form. The evaluation of
the arguments is delayed until the containing spec is executed."
`(buttercup-expect ,(buttercup--wrap-expr arg)
,(or matcher :to-be-truthy)
,@(mapcar #'buttercup--wrap-expr args)))
(defun buttercup-expect (arg &optional matcher &rest args)
"The function for the `expect' macro.
See the macro documentation for details and the definition of
ARG, MATCHER and ARGS."
(cl-assert (cl-every #'buttercup--wrapper-fun-p (cons arg args)) t)
(pcase (buttercup--apply-matcher (or matcher :to-be-truthy) (cons arg args))
(`(,result . ,message) (unless result (buttercup-fail message)))
(result (unless result (buttercup-fail "Expected %S %S %s"
(buttercup--enclosed-expr arg)
matcher
(mapconcat (lambda (obj)
(format "%S" (funcall obj)))
args " "))))))
(defun buttercup-fail (format &rest args)
"Fail the current test with the given description.
This is the mechanism underlying `expect'. You can use it
directly if you want to write your own testing functionality.
FORMAT and ARGS are passed to `format'."
(signal 'buttercup-failed (apply #'format format args)))
(defun buttercup-skip (format &rest args)
"Skip the current test with the given description.
FORMAT and ARGS are passed to `format'."
(signal 'buttercup-pending (apply #'format format args)))
(defmacro assume (condition &optional message)
"Assume CONDITION for the current test.
Assume that CONDITION evaluates to non-nil in the current test.
If it evaluates to nil cancel the current test with MESSAGE. If
MESSAGE is omitted or nil show the condition form instead."
(let ((message (or message (format "%S => nil" condition))))
`(unless ,condition
(buttercup-skip "!! CANCELLED !! %s" ,message))))
(defmacro buttercup-define-matcher (matcher args &rest body)
"Define a matcher named MATCHER to be used in `expect'.
MATCHER is a keyword, for instance `:to-be'.
ARGS is a list of the elements to match together.
The BODY will receive ARGS as functions that can be called (using
`funcall') to get their values. BODY should return either a
simple boolean, or a cons cell of the form (RESULT . MESSAGE). If
RESULT is nil, MESSAGE should describe why the matcher failed. If
RESULT is non-nil, MESSAGE should describe why a negated matcher
failed.
\(fn MATCHER ARGS [DOCSTRING] BODY...)"
(declare (indent defun))
`(put ,matcher 'buttercup-matcher
(lambda ,args
,@body)))
(defun buttercup--function-as-matcher (fun)
"Wrap FUN in code to unpack function-wrapped arguments."
(cl-assert (functionp fun) t)
(lambda (&rest args)
(apply fun (mapcar #'funcall args))))
(defun buttercup--find-matcher-function (matcher)
"Return the matcher function for MATCHER."
(let ((matcher-prop
(when (symbolp matcher)
(get matcher 'buttercup-matcher))))
(cond
;; Use `buttercup-matcher' property if it's a function
((functionp matcher-prop)
matcher-prop)
(matcher-prop
(error "%S %S has a `buttercup-matcher' property that is not a function. Buttercup has been misconfigured"
(if (keywordp matcher) "Keyword" "Symbol") matcher))
;; Otherwise just use `matcher' as a function, wrapping it in
;; code to unpack function-wrapped arguments.
((functionp matcher)
(buttercup--function-as-matcher matcher))
(matcher (error "Not a test: `%S'" matcher))
;; If `matcher' is nil, then we just want a basic truth test
((null matcher)
(buttercup--find-matcher-function :to-be-truthy))
(t (error "This line should never run")))))
(defun buttercup--apply-matcher (matcher args)
"Apply MATCHER to ARGS.
ARGS is a list of functions that must be `funcall'ed to get their
values.
MATCHER is either a matcher keyword defined with
`buttercup-define-matcher', or a function."
(cl-assert (cl-every #'buttercup--wrapper-fun-p args) t)
(let ((function
(buttercup--find-matcher-function matcher)))
(apply function args)))
(cl-defmacro buttercup--test-expectation
(expression &key expect-match-phrase expect-mismatch-phrase)
"Wrapper for the common matcher case of two possible messages.
The logic for the return values of buttercup matchers can be
unintuitive, since the return value is a cons cell whose first
element is t for a mismatch and nil for a match. In the simple
case where there are only two possible
messages (EXPECT-MATCH-PHRASE for a match and
EXPECT-MISMATCH-PHRASE for a mismatch), this macro allows you to
simply specify those two phrases and the EXPRESSION to test."
(declare (indent 1))
(cl-assert expect-match-phrase)
(cl-assert expect-mismatch-phrase)
`(let ((value ,expression))
(if value
(cons t ,expect-mismatch-phrase)
(cons nil ,expect-match-phrase))))
(cl-defmacro buttercup-define-matcher-for-unary-function
(matcher function &key
expect-match-phrase expect-mismatch-phrase function-name)
"Shortcut to define a MATCHER for a 1-argument FUNCTION.
When the matcher is used, keyword arguments EXPECT-MATCH-PHRASE
and EXPECT-MISMATCH-PHRASE are used to construct the return
message. It may contain `%f', `%A', and `%a', which will be
replaced with the function name, the expression of the argument
the matcher was called on, and the value of that argument,
respectively. If not provided, the default EXPECT-MATCH-PHRASE
is:
Expected `%A' to match `%f', but instead it was `%a'.
Similarly, the default EXPECT-MISMATCH-PHRASE is:
Expected `%A' not to match `%f', but it was `%a'.
To include a literal `%' in either message, use `%%'.
If FUNCTION is passed as a lambda expression or other non-symbol, then
you must provide a keyword argument FUNCTION-NAME to be used in
the match/mismatch messages. Otherwise, FUNCTION-NAME will be
used instead of FUNCTION if both are non-nil SYMBOLS.
If FUNCTION (or FUNCTION-NAME) has an `ert-explainer' property,
this will be used to generate the default EXPECT-MATCH-PHRASE.
See also `buttercup-define-matcher'."
(declare (indent 2))
;; Use the ERT explainer for FUNCTION if available to generate the
;; default expect-match phrase.
(let ((explainer (or (when function-name
(get function-name 'ert-explainer))
(when (symbolp function)
(get function 'ert-explainer)))))
(cl-assert (symbolp function-name) t)
(cl-assert (functionp function) t)
(unless expect-match-phrase
(setq expect-match-phrase
(if explainer
;; %x is the undocumented substitution for the
;; explainer's output
"Expected `%A' to match `%f', but instead it was `%a' which did not match because: %x."
"Expected `%A' to match `%f', but instead it was `%a'.")))
(unless expect-mismatch-phrase
(setq expect-mismatch-phrase
"Expected `%A' not to match `%f', but it was `%a'."))
(when (and (null function-name)
;; Only need a function name if either phrase contains
;; an unescaped `%f'.
(string-match-p
"%f"
(replace-regexp-in-string
"%%" ""
(concat expect-match-phrase " "
expect-mismatch-phrase))))
(if (symbolp function)
(setq function-name (symbol-name function))
(error "The `:function-name' keyword is required if FUNCTION is not a symbol")))
`(buttercup-define-matcher ,matcher (arg)
(let* ((expr (buttercup--enclosed-expr arg))
(value (funcall arg))
(explanation (and ',explainer (funcall ',explainer arg)))
(spec (format-spec-make
?f ',function-name
?A (format "%S" expr)
?a (format "%S" value)
?x (format "%S" explanation))))
(buttercup--test-expectation (funcall ',function value)
:expect-match-phrase (buttercup-format-spec ,expect-match-phrase spec)
:expect-mismatch-phrase (buttercup-format-spec ,expect-mismatch-phrase spec))))))
(cl-defmacro buttercup-define-matcher-for-binary-function
(matcher function &key
expect-match-phrase expect-mismatch-phrase function-name)
"Shortcut to define a MATCHER for a 2-argument FUNCTION.
When the matcher is used, keyword arguments EXPECT-MATCH-PHRASE
and EXPECT-MISMATCH-PHRASE are used to construct the return
message. It may contain `%f', `%A', `%a', `%B', and `%b'. The
token `%f' will be replaced with the function name. `%A' and `%B'
will be replaced with the unevaluted expressions of the two
arguments passed to the function, while `%a' and `%b' will be
replaced with their values. not provided, the default
EXPECT-MATCH-PHRASE is:
Expected `%A' to be `%f' to `%b', but instead it was `%a'.
Similarly, the default EXPECT-MISMATCH-PHRASE is:
Expected `%A' not to be `%f' to `%b', but it was.
To include a literal `%' in either message, use `%%'.
If FUNCTION is passed as a lambda expression or other non-symbol, then
you must provide a keyword argument FUNCTION-NAME to be used in
the match/mismatch messages (unless neither one contains `%f').
If both are non-nil symbols, FUNCTION-NAME will be used instead
of FUNCTION in messages.
If FUNCTION (or FUNCTION-NAME) has an `ert-explainer' property,
this will be used to generate the default EXPECT-MATCH-PHRASE.
See also `buttercup-define-matcher'."
(declare (indent 2))
;; Use the ERT explainer for FUNCTION if available to generate the
;; default expect-match phrase.
(let ((explainer (or (when function-name
(get function-name 'ert-explainer))
(when (symbolp function)
(get function 'ert-explainer)))))
(cl-assert (symbolp function-name) t)
(cl-assert (functionp function) t)
(unless expect-match-phrase
(setq expect-match-phrase
(if explainer
;; %x is the undocumented substitution for the
;; explainer's output
"Expected `%A' to be `%f' to `%b', but instead it was `%a' which does not match because: %x."
"Expected `%A' to be `%f' to `%b', but instead it was `%a'.")))
(unless expect-mismatch-phrase
(setq expect-mismatch-phrase
"Expected `%A' not to be `%f' to `%b', but it was."))
(when (and (null function-name)
;; Only need a function name if either phrase contains
;; an unescaped `%f'.
(string-match-p
"%f"
(replace-regexp-in-string
"%%" ""
(concat expect-match-phrase " "
expect-mismatch-phrase))))
(if (symbolp function)
(setq function-name (symbol-name function))
(error "The `:function-name' keyword is required if FUNCTION is not a symbol")))
`(buttercup-define-matcher ,matcher (a b)
(cl-destructuring-bind
((a-expr . a) (b-expr . b))
(mapcar #'buttercup--expr-and-value (list a b))
(let* ((explanation (and ',explainer (funcall ',explainer a b)))
(spec (format-spec-make
?f ',function-name
?A (format "%S" a-expr)
?a (format "%S" a)
?B (format "%S" b-expr)
?b (format "%S" b)
?x (format "%S" explanation))))
(buttercup--test-expectation (funcall #',function a b)
:expect-match-phrase (buttercup-format-spec ,expect-match-phrase spec)
:expect-mismatch-phrase (buttercup-format-spec ,expect-mismatch-phrase spec)))))))
;;;;;;;;;;;;;;;;;;;;;
;;; Built-in matchers
(buttercup-define-matcher-for-unary-function :to-be-truthy identity
:expect-match-phrase "Expected `%A' to be non-nil, but instead it was nil."
:expect-mismatch-phrase "Expected `%A' to be nil, but instead it was `%a'.")
(buttercup-define-matcher-for-binary-function :to-be eq)
(buttercup-define-matcher-for-binary-function :to-equal equal)
(buttercup-define-matcher :not (obj matcher &rest args)
(let* ((matcher (funcall matcher))
(result (buttercup--apply-matcher matcher (cons obj args))))
(if (consp result)
(cons (not (car result))
(cdr result))
(not result))))
(buttercup-define-matcher :to-have-same-items-as (a b)
(cl-destructuring-bind
((a-expr . a) (b-expr . b))
(mapcar #'buttercup--expr-and-value (list a b))
(let* ((a-uniques (cl-set-difference a b :test #'equal))
(b-uniques (cl-set-difference b a :test #'equal))
(spec (format-spec-make
?A (format "%S" a-expr)
?a (format "%S" a)
?B (format "%S" b-expr)
?b (format "%S" b)
?m (format "%S" b-uniques)
?p (format "%S" a-uniques))))
(cond
((and a-uniques b-uniques)
(cons nil (buttercup-format-spec
"Expected `%A' to contain the same items as `%b', but `%m' are missing and `%p' are present unexpectedly."
spec)))
(a-uniques
(cons nil (buttercup-format-spec
"Expected `%A' to contain the same items as `%b', but `%p' are present unexpectedly."
spec)))
(b-uniques
(cons nil (buttercup-format-spec
"Expected `%A' to contain the same items as `%b', but `%m' are missing."
spec)))
(t
(cons t (buttercup-format-spec
"Expected `%A' not to have same items as `%b'"
spec)))))))
(buttercup-define-matcher :to-match (text regexp)
(cl-destructuring-bind
((text-expr . text) (regexp-expr . regexp))
(mapcar #'buttercup--expr-and-value (list text regexp))
(save-match-data
(let* (;; For string literals, just use them normally, but for
;; expressions, show both the expr and its string value
(text-is-literal (equal text-expr text))
(regexp-is-literal (equal regexp-expr regexp))
(text-desc
(if text-is-literal
text-expr
(format "`%S' with value %S"
text-expr text)))
(regexp-desc
(if regexp-is-literal
regexp-expr
(format "`%S' with value %S"
regexp-expr regexp)))
(match-p (string-match regexp text))
;; Get some more details about the match
(start
(when match-p
(match-beginning 0)))
(end
(when match-p
(match-end 0)))
(matched-substring
(when match-p
(substring text start end)))
(spec (format-spec-make
?T text-desc
?t (format "%S" text)
?R regexp-desc
?r (format "%S" regexp)
?m (format "%S" matched-substring)
?a start
?z end)))
(buttercup--test-expectation match-p
:expect-match-phrase
(buttercup-format-spec "Expected %T to match the regexp %r, but instead it was %t."
spec)
:expect-mismatch-phrase
(buttercup-format-spec "Expected %T not to match the regexp %r, but it matched the substring %m from position %a to %z."
spec))))))
(buttercup-define-matcher-for-binary-function
:to-be-in member
:expect-match-phrase "Expected `%A' to be an element of `%b', but it was `%a'."
:expect-mismatch-phrase "Expected `%A' not to be an element of `%b', but it was `%a'.")
(buttercup-define-matcher-for-binary-function
;; Reverse the args
:to-contain (lambda (a b) (member b a))
:expect-match-phrase "Expected `%A' to be a list containing `%b', but instead it was `%a'."
:expect-mismatch-phrase "Expected `%A' to be a list not containing `%b', but instead it was `%a'.")
(buttercup-define-matcher-for-binary-function
:to-be-less-than <
:expect-match-phrase "Expected `%A' < %b, but `%A' was %a."
:expect-mismatch-phrase "Expected `%A' >= %b, but `%A' was %a.")
(buttercup-define-matcher-for-binary-function
:to-be-greater-than >
:expect-match-phrase "Expected `%A' > %b, but `%A' was %a."
:expect-mismatch-phrase "Expected `%A' <= %b, but `%A' was %a.")
(buttercup-define-matcher-for-binary-function
:to-be-weakly-less-than <=
:expect-match-phrase "Expected `%A' <= %b, but `%A' was %a."
:expect-mismatch-phrase "Expected `%A' > %b, but `%A' was %a.")
(buttercup-define-matcher-for-binary-function
:to-be-weakly-greater-than >=
:expect-match-phrase "Expected `%A' >= %b, but `%A' was %a."
:expect-mismatch-phrase "Expected `%A' < %b, but `%A' was %a.")
(buttercup-define-matcher :to-be-close-to (a b precision)
"Verify that |A-B| < 10^-PRECISION.
\(expect A :to-be-close-to B PRECISION)"
(cl-destructuring-bind
(precision (a-expr . a) (_b-expr . b))
(cons (funcall precision)
(mapcar #'buttercup--expr-and-value (list a b)))
(let ((tolerance (expt 10.0 (- precision))))
(buttercup--test-expectation
(< (abs (- a b)) tolerance)
:expect-match-phrase
(format "Expected `%S' to be within %s of %s, but instead it was %s, with a difference of %s"
a-expr tolerance b a (abs (- a b)))
:expect-mismatch-phrase
(format "Expected `%S' to differ from %s by more than %s, but instead it was %s, with a difference of %s"
a-expr b tolerance a (abs (- a b)))))))
(buttercup-define-matcher :to-throw (expr &optional signal signal-args)
"Check that EXPR raises SIGNAL with SIGNAL-ARGS.
EXPR, SIGNAL, and SIGNAL-ARGS should all be buttercup-wrapped
objects. EXPR is the test code that will be evaluated. The signal
raised by EXPR must be SIGNAL or an error signal derived from
SIGNAL. If SIGNAL is nil match any error signal. If SIGNAL-ARGS
are given they must be `equal' to the arguments of the caught
signal. Do not consider the signal arguments if SIGNAL-ARGS is
nil."
(let ((expected-signal-symbol (and signal (funcall signal)))
(expected-signal-args (and signal-args (funcall signal-args)))
(unevaluated-expr (buttercup--enclosed-expr expr))
expr-value
thrown-signal)
(when (and (functionp unevaluated-expr)
(member (car unevaluated-expr) '(lambda closure)))
(display-warning
'buttercup
(buttercup-colorize
(format "Probable incorrect use of `:to-throw' matcher: pass an expression instead of a function: `%S'"
unevaluated-expr)
'yellow)))
;; Set the above variables
(condition-case err
(setq expr-value
(funcall expr))
(error
(setq thrown-signal err)
nil))
(buttercup--handle-to-throw thrown-signal
(cons expected-signal-symbol expected-signal-args)
unevaluated-expr expr-value)))
(defun buttercup--handle-to-throw (thrown-signal expected-signal unevaluated-expr expr-value)
"Handle the results of the :to-throw matcher.
This is a separate function for testability purposes.
THROWN-SIGNAL is the signal - a `cons' of symbol and arguments -
caught by `condition-case', or nil if no signal was raised.
EXPECTED-SIGNAL is a `cons' of the expected signal symbol and
arguments. The `cdr' can be nil if the `expect' statement did not
specify any expected arguments.
UNEVALUATED-EXPR is the Lisp sexp used before the :to-throw
matcher keyword in the `expect' statement.
EXPR-VALUE is the return value from the evaluation of
UNEVALUATED-EXPR if it did not raise any signal."
(let* ((thrown-signal-symbol (car thrown-signal))
(thrown-signal-args (cdr thrown-signal))
(expected-signal-symbol (car expected-signal))
(expected-signal-args (cdr expected-signal))
(matching-signal-symbol
(or (null expected-signal-symbol)
(memq expected-signal-symbol (get thrown-signal-symbol 'error-conditions))))
(explained-signal-args ; nil for matched, explained or t for mismatched
(when expected-signal-args
;; The ert-explainer for equal does an equal internally,
;; so avoid calling equal twice by calling the explainer
;; directly.
(funcall (or (get 'equal 'ert-explainer) (lambda (a b) (not (equal a b))))
thrown-signal-args expected-signal-args))))
(let*
((matched (and thrown-signal matching-signal-symbol (not explained-signal-args)))
;; Some of these replacement are always used, there is no
;; reason not to format them immediately. But e and t are not
;; always used and should be delayed. Use
;; buttercup--simple-format for formatting as format-spec
;; does not support functions until Emacs 29
(spec (format-spec-make
?E (format "`%S'" unevaluated-expr)
?e (lambda () (format "`%S'" expr-value))
?t (lambda () (format "%S" thrown-signal))
?S (lambda () (format "`%S'" thrown-signal-symbol))
?A (lambda ()
(if expected-signal-args
(format " with args `%S'" thrown-signal-args)
""))
?s (if expected-signal-symbol
(format "a child signal of `%S'" expected-signal-symbol)
"a signal")
?a (if expected-signal-args
(format " with args `%S'" expected-signal-args)
"")
?q (lambda () (format "%S" explained-signal-args))
)))
(cond
(matched ;; should be the most likely result
`(t . ,(buttercup--simple-format
spec "Expected %E not to throw %s%a, but it threw %S%A")))
((null thrown-signal) ; no signal raised
`(nil . ,(buttercup--simple-format
spec "Expected %E to throw %s%a, but instead it returned %e")))
((and explained-signal-args (not matching-signal-symbol)) ; neither symbol nor args matched
`(nil . ,(buttercup--simple-format
spec
"Expected %E to throw %s%a, but instead it threw %S%A")))
(explained-signal-args ; symbol matched
`(nil . ,(buttercup--simple-format
spec
"Expected %E to signal %s%a, but instead signalled%A which does not match because %q.")))
((not matching-signal-symbol) ; args matched
`(nil . ,(buttercup--simple-format
spec
"Expected %E to throw %s%a, but instead it threw %S%A")))
(t (error "`buttercup--handle-to-throw' could not handle args %S %S"
thrown-signal expected-signal))))))
(buttercup-define-matcher :to-have-been-called (spy)
"Check that SPY have been called at least once."
(cl-assert (symbolp (funcall spy)))
(if (spy-calls-all (funcall spy))
t
nil))
(buttercup-define-matcher :to-have-been-called-with (spy &rest args)
"Check that SPY has been called at least once with arguments ARGS."
(setq spy (funcall spy))
(cl-assert (symbolp spy))
(setq args (mapcar #'funcall args))
(let* ((calls (mapcar #'spy-context-args (spy-calls-all spy))))
(cond
((not calls)
(cons nil
(format "Expected `%s' to have been called with %S, but it was not called at all" spy args)))
((not (member args calls))
(cons nil
(format "Expected `%s' to have been called with %S, but it was called with %s"
spy
args
(mapconcat (lambda (args)
(format "%S" args))
calls
", "))))
(t
t))))
(buttercup-define-matcher :to-have-been-called-times (spy number)
"Check that SPY has been called exactly NUMBER times."
(setq spy (funcall spy)
number (funcall number))
(cl-assert (symbolp spy))
(let* ((call-count (spy-calls-count spy)))
(cond
((= number call-count)
(cons t
(format "Expected `%s' to not have been called exactly %s %s, but it was."
spy number (if (= number 1) "time" "times"))))
(t
(cons nil
(format "Expected `%s' to have been called %s %s, but it was called %s %s"
spy
number (if (= number 1) "time" "times")
call-count (if (= call-count 1) "time" "times")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Suite and spec data structures
(cl-defstruct buttercup-suite-or-spec
;; The name of this specific suite
description
;; The parent of this suite, another suite
parent
;; One of: passed failed pending
(status 'passed)
failure-description
failure-stack
time-started
time-ended)
(cl-defstruct (buttercup-suite (:include buttercup-suite-or-spec))
;; Any children of this suite, both suites and specs
children
;; Closure to run before and after each spec in this suite and its
;; children
before-each
after-each
;; Likewise, but before and after all specs.
before-all
after-all)
(cl-defstruct (buttercup-spec (:include buttercup-suite-or-spec))
;; The closure to run for this spec
function)
(defun buttercup-suite-add-child (parent child)
"Add a CHILD suite to a PARENT suite.
Return CHILD."
(setf (buttercup-suite-children parent)
(append (buttercup-suite-children parent)
(list child)))
(setf (buttercup-suite-or-spec-parent child) parent)
child)
(defun buttercup-suite-or-spec-parents (suite-or-spec)
"Return a list of parents of SUITE-OR-SPEC."
(when (buttercup-suite-or-spec-parent suite-or-spec)
(cons (buttercup-suite-or-spec-parent suite-or-spec)
(buttercup-suite-or-spec-parents (buttercup-suite-or-spec-parent suite-or-spec)))))
(define-obsolete-function-alias 'buttercup-suite-parents
#'buttercup-suite-or-spec-parents "emacs-buttercup 1.10")
(define-obsolete-function-alias 'buttercup-spec-parents
#'buttercup-suite-or-spec-parents "emacs-buttercup 1.10")
(defun buttercup-suites-total-specs-defined (suite-list)
"Return the number of specs defined in all suites in SUITE-LIST."
(length (buttercup--specs suite-list)))
(defun buttercup-suites-total-specs-status (suite-list status)
"Return the number of specs in SUITE-LIST marked with STATUS."
(cl-count status (buttercup--specs suite-list) :key #'buttercup-spec-status))
(defun buttercup-suites-total-specs-pending (suite-list)
"Return the number of specs marked as pending in all suites in SUITE-LIST."
(buttercup-suites-total-specs-status suite-list 'pending))
(defun buttercup-suites-total-specs-failed (suite-list)
"Return the number of failed specs in all suites in SUITE-LIST."
(buttercup-suites-total-specs-status suite-list 'failed))
(defun buttercup--specs (spec-or-suite-list)
"Return a flat list of all specs in SPEC-OR-SUITE-LIST."
(let (specs)
(dolist (spec-or-suite spec-or-suite-list specs)
(if (buttercup-spec-p spec-or-suite)
(setq specs (append specs (list spec-or-suite)))
(setq specs (append specs (buttercup--specs
(buttercup-suite-children spec-or-suite))))))))
(defun buttercup--specs-and-suites (spec-or-suite-list)
"Return a flat list of all specs and suites in SPEC-OR-SUITE-LIST."
(let ((specs-and-suites nil))
(dolist (spec-or-suite spec-or-suite-list specs-and-suites)
(setq specs-and-suites (append specs-and-suites
(list spec-or-suite)))
(when (buttercup-suite-p spec-or-suite)
(setq specs-and-suites
(append specs-and-suites
(buttercup--specs-and-suites
(buttercup-suite-children spec-or-suite))))))))
(defun buttercup-suite-full-name (suite)
"Return the full name of SUITE, which includes the names of the parents."
(mapconcat #'buttercup-suite-description
(nreverse (cons suite (buttercup-suite-or-spec-parents suite)))
" "))
(defun buttercup-spec-full-name (spec)
"Return the full name of SPEC, which includes the full name of its suite."
(let ((parent (buttercup-spec-parent spec)))
(if parent
(concat (buttercup-suite-full-name parent)
" "
(buttercup-spec-description spec))
(buttercup-spec-description spec))))
(defun buttercup--full-spec-names (spec-or-suite-list)
"Return full names of all specs in SPEC-OR-SUITE-LIST."
(cl-loop
for x in (buttercup--specs spec-or-suite-list)
collect (buttercup-spec-full-name x)))
(defun buttercup--find-duplicate-spec-names (spec-or-suite-list)
"Return duplicate full spec names among SPEC-OR-SUITE-LIST."
(let ((seen '())
(duplicates '()))
(dolist (name (buttercup--full-spec-names spec-or-suite-list)
(nreverse duplicates))
(if (member name seen)
(push name duplicates)
(push name seen)))))
(defun buttercup--set-start-time (suite-or-spec)
"Set time-started of SUITE-OR-SPEC to `current-time'."
(setf (buttercup-suite-or-spec-time-started suite-or-spec) (current-time)))
(defun buttercup--set-end-time (suite-or-spec)
"Set time-ended of SUITE-OR-SPEC to `current-time'."
(setf (buttercup-suite-or-spec-time-ended suite-or-spec) (current-time)))
(defun buttercup-elapsed-time (suite-or-spec)
"Get elapsed time of SUITE-OR-SPEC."
;; time-subtract does not handle nil arguments until Emacs 25.1
(time-subtract
(or (buttercup-suite-or-spec-time-ended suite-or-spec) (current-time))
(or (buttercup-suite-or-spec-time-started suite-or-spec) (current-time))))
(defun buttercup-elapsed-time-string (suite-or-spec)
"Convert the elapsed time for SUITE-OR-SPEC to a short string."
(seconds-to-string (float-time (buttercup-elapsed-time suite-or-spec))))
(defun buttercup--indented-description (suite-or-spec)
"Return the description of SUITE-OR-SPEC indented according to level.
The indentaion is two spaces per parent."
(let ((level (length (buttercup-suite-or-spec-parents suite-or-spec))))
(concat (make-string (* 2 level) ?\s) (buttercup-suite-or-spec-description suite-or-spec))))
(defun buttercup--spec-mark-pending (spec description &optional description-for-now)
"Mark SPEC as pending with DESCRIPTION.
If DESCRIPTION-FOR-NOW is non-nil, set the spec
`pending-description' to that value for now, it will be reset to
DESCRIPTION when the spec is run. Return SPEC."
(setf (buttercup-spec-function spec)
(lambda () (signal 'buttercup-pending description))
(buttercup-spec-status spec) 'pending)
(when description-for-now
(setf (buttercup-spec-failure-description spec) description-for-now))
spec)
;;;;;;;;;;;;;;;;;;;;
;;; Suites: describe
(defvar buttercup-suites nil
"The list of all currently defined Buttercup suites.")
(defvar buttercup--current-suite nil
"The suite currently being defined.
Do not set this globally. It is let-bound by the `describe'
form.")
(defmacro describe (description &rest body)
"Describe a test suite.
DESCRIPTION is a string. BODY is a sequence of instructions,
mainly calls to `describe', `it' and `before-each'."
(declare (indent 1) (debug (&define sexp def-body)))
(unless lexical-binding
(signal 'buttercup-dynamic-binding-error
"buttercup requires `lexical-binding' to be t"))
(let ((new-body
(cond
((eq (elt body 0) :var)
`((let ,(elt body 1)
,@(cddr body))))
((eq (elt body 0) :var*)
`((let* ,(elt body 1)
,@(cddr body))))
(t body))))
(if (or (memq :var new-body)
(memq :var* new-body))
`(error "buttercup: :var(*) found in invalid position of describe form \"%s\"" ,description)
`(buttercup-describe ,description (lambda () ,@new-body)))))
(defun buttercup-describe (description body-function)
"Function to handle a `describe' form.
DESCRIPTION has the same meaning as in `describe'. BODY-FUNCTION
is a function containing the body instructions passed to
`describe'."
(let* ((enclosing-suite buttercup--current-suite)
(buttercup--current-suite (make-buttercup-suite
:description description)))
(condition-case nil
(funcall body-function)
(buttercup-pending
(setf (buttercup-suite-status buttercup--current-suite)
'pending)))
(if enclosing-suite
(buttercup-suite-add-child enclosing-suite
buttercup--current-suite)
;; At top level, warn about duplicate spec names
(let ((dups (buttercup--find-duplicate-spec-names
(list buttercup--current-suite))))
(when dups
;; TODO: Use `buttercup--warn'
(display-warning
'buttercup
(format "Found duplicate spec names in suite: %S"
(delete-dups dups)))))
(setq buttercup-suites (append buttercup-suites
(list buttercup--current-suite)))
buttercup--current-suite)))
;;;;;;;;;;;;;
;;; Specs: it