Debian Codegraph @

test-suite/tests/srfi-119.test

raw
1 ;;;; srfi-119.test --- Test suite for Guile's SRFI-119 reader. -*- scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 2023 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-srfi-119)
20 #:use-module (test-suite lib)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-26) ;; cut
23 #:use-module (language wisp))
24
25 (define (read-string s)
26 (with-input-from-string s read))
27
28 (define (with-read-options opts thunk)
29 (let ((saved-options (read-options)))
30 (dynamic-wind
31 (lambda ()
32 (read-options opts))
33 thunk
34 (lambda ()
35 (read-options saved-options)))))
36
37 (define (wisp->list str)
38 (wisp-scheme-read-string str))
39
40 (define (scheme->list str)
41 (with-input-from-string str
42 (λ ()
43 (let loop ((result '()))
44 (if (eof-object? (peek-char))
45 (reverse! result)
46 (loop (cons (read) result)))))))
47
48 (with-test-prefix "wisp-read-simple"
49 (pass-if-equal '((<= n 5))
50 (wisp->list "<= n 5"))
51 (pass-if-equal '(5)
52 (wisp->list ". 5"))
53 (pass-if-equal '((+ 1 (* 2 3)))
54 (wisp->list "+ 1 : * 2 3")))
55 (with-test-prefix "wisp-read-complex"
56 (pass-if-equal '(
57 (a b c d e
58 f g h
59 i j k)
60
61 (concat "I want "
62 (getwish from me)
63 " - " username)) (wisp->list "
64 a b c d e
65 . f g h
66 . i j k
67
68 concat \"I want \"
69 getwish from me
70 . \" - \" username
71 "))
72
73 (pass-if-equal
74 '(
75 (define (a b c)
76 (d e
77 (f)
78 (g h)
79 i))
80
81 (define (_)
82 (display "hello\n"))
83
84 (_)) (wisp->list "
85 define : a b c
86 _ d e
87 ___ f
88 ___ g h
89 __ . i
90
91 define : _
92 _ display \"hello\n\"
93
94 \\_"))
95
96 ;; nesting with pairs
97 (pass-if-equal '((1 . 2)(3 4 (5 . 6)))
98 (wisp->list "1 . 2\n3 4\n 5 . 6")))
99
100 (with-test-prefix "wisp-source-properties"
101 ;; has properties
102 (pass-if (every pair? (map source-properties (wisp->list "1 . 2\n3 4\n 5 . 6"))))
103 (pass-if (every pair? (map source-properties (wisp->list "1 2\n3 4\n 5 6"))))
104 ;; has the same properties
105 (pass-if-equal
106 (map source-properties (scheme->list "(1 . 2)\n(3 4\n (5 . 6))\n(1 4)\n\n(7 8)"))
107 (map (cut cons '(filename . #f) <>)
108 (map source-properties (wisp->list "1 . 2\n3 4\n 5 . 6\n1 4\n\n7 8")))))
109
110 (with-test-prefix "btest"
111 (pass-if-equal '((display "b")
112 (newline)
113
114
115 ) (wisp->list "
116 display \"b\"
117 newline
118 ")))
119
120 (with-test-prefix "continuation"
121 (pass-if-equal '((a b c d e
122 f g h
123 i j k)
124
125 (concat "I want "
126 (getwish from me)
127 " - " username)
128
129
130
131 ) (wisp->list "
132 a b c d e
133 . f g h
134 . i j k
135
136 concat \"I want \"
137 getwish from me
138 . \" - \" username
139
140 ")))
141
142 (with-test-prefix "dotted-pair"
143 (pass-if-equal '((use-modules ((ice-9 popen) #:select ((open-input-pipe . oip))))
144
145
146 ) (wisp->list "
147 use-modules : (ice-9 popen) #:select ((open-input-pipe . oip))
148 ")))
149
150 (with-test-prefix "example"
151 (pass-if-equal '((defun a (b c)
152 (let
153 (
154 (d "i am a string
155 do not break me!")
156 (
157 ; comment: 0
158 (f)
159 ; comment : 1
160 `(g )); comment " : " 2
161 (
162 (h (I am in brackets:
163 do not : change "me"))
164 i)))
165 ,('j k)
166
167 l
168
169 ; comment
170
171 (a c))
172
173 (defun b (:n o)
174 "second defun : with a docstring!"
175 (message "I am here")
176 t)
177
178 (defun c (e f)
179 ((g))
180 (
181 (h
182 (i))
183 (j))
184 '(())
185 (k)
186 l
187 (m))
188
189 (defun _ (:)
190
191 :)
192
193 (_ b)
194
195 (defun d ()
196 (let
197 ((a b)
198 (c d))))
199
200 (a (((c))))
201
202 (let
203 ((a b)
204 (c)))
205
206 (let ((a b)))
207
208 a
209
210
211
212 ) (wisp->list "
213 defun a (b c)
214 let
215 :
216 d \"i am a string
217 do not break me!\"
218 :
219 ; comment: 0
220 f
221 ; comment : 1
222 ` g ; comment \" : \" 2
223 :
224 h (I am in brackets:
225 do not : change \"me\")
226 . i
227 , 'j k
228
229 . l
230
231 ; comment
232
233 a c
234
235 defun b : :n o
236 . \"second defun : with a docstring!\"
237 message \"I am here\"
238 . t
239
240 defun c : e f
241 : g
242 :
243 h
244 i
245 j
246 ' :
247 k
248 . l
249 . : m
250
251 defun _ : \\:
252 __
253 __ . \\:
254
255 \\_ b
256
257 defun d :
258 let
259 : a b
260 c d
261
262 a : : : c
263
264 let
265 : a b
266 c
267
268 let : : a b
269
270 . a
271
272 ")))
273
274 (with-test-prefix "factorial"
275 (pass-if-equal '(;; short version
276 ; note: once you use one inline colon, all the following forms on that
277 ; line will get closed at the end of the line
278
279 (define (factorial n)
280 (if (zero? n)
281 1
282 (* n (factorial (- n 1)))))
283
284 (display (factorial 5 ))
285
286
287 ;; more vertical space, less colons
288 (define (factorial n)
289 (if (zero? n)
290 1
291 (* n
292 (factorial
293 (- n 1)))))
294
295 (display (factorial 5 ))
296
297
298
299 ) (wisp->list "
300 ;; short version
301 ; note: once you use one inline colon, all the following forms on that
302 ; line will get closed at the end of the line
303
304 define : factorial n
305 if : zero? n
306 . 1
307 * n : factorial : - n 1
308
309 display : factorial 5
310
311
312 ;; more vertical space, less colons
313 define : factorial n
314 if : zero? n
315 . 1
316 * n
317 factorial
318 - n 1
319
320 display : factorial 5
321
322 ")))
323
324 (with-test-prefix "fast-sum"
325 (pass-if-equal '((use-modules (srfi srfi-1))
326
327 ; only for the nice test
328 #!curly-infix
329
330 (define-syntax fast-sum
331 (syntax-rules (iota)
332 ((fast-sum (iota count start))
333 (+ 1
334 (apply -
335 (map (lambda (x) (/ {x * {x + 1} } 2))
336 (list {count + {start - 1}} start)))))
337 ((fast-sum e)
338 (apply + e))))
339
340
341 ) (wisp->list "
342 use-modules : srfi srfi-1
343
344 ; only for the nice test
345 . #!curly-infix
346
347 define-syntax fast-sum
348 syntax-rules : iota
349 : fast-sum : iota count start
350 + 1
351 apply -
352 map : lambda (x) : / {x * {x + 1} } 2
353 list {count + {start - 1}} start
354 : fast-sum e
355 apply + e
356 ")))
357
358 (with-test-prefix "flexible-parameter-list"
359 (pass-if-equal '(; Test using a . as first parameter on a line by prefixing it with a second .
360 (define
361 (a i
362 . b)
363 (unless (>= i (length b))
364 (display (number->string (length b )))
365 (display (list-ref b i))
366 (newline)
367 (apply a ( + i 1 ) b)))
368
369
370 (a 0 "123" "345" "567")
371
372
373 ) (wisp->list "
374 ; Test using a . as first parameter on a line by prefixing it with a second .
375 define
376 a i
377 . . b
378 unless : >= i : length b
379 display : number->string : length b
380 display : list-ref b i
381 newline
382 apply a ( + i 1 ) b
383
384
385 a 0 \"123\" \"345\" \"567\"
386 ")))
387
388 (with-test-prefix "hello"
389 (pass-if-equal '((define (hello who)
390 ;; include the newline
391 (format #t "~A ~A!\n"
392 "Hello" who))
393 (hello "Wisp")
394
395 ) (wisp->list "
396 define : hello who
397 ;; include the newline
398 format #t \"~A ~A!\\n\"
399 . \"Hello\" who
400 hello \"Wisp\"
401 ")))
402
403 (with-test-prefix "mtest"
404 (pass-if-equal '(#!/home/arne/wisp/wisp-multiline.sh !#
405
406 (display 1)
407
408
409 ) (wisp->list "
410 #!/home/arne/wisp/wisp-multiline.sh !#
411
412 display 1
413 ")))
414
415 (with-test-prefix "multiline-string"
416 (pass-if-equal '((display "
417 This is a
418 \"multi-line\"
419 string.
420 ")
421
422 ) (wisp->list "
423 display \"
424 This is a
425 \\\"multi-line\\\"
426 string.
427 \"
428 ")))
429
430 (with-test-prefix "namedlet"
431 (pass-if-equal '(#!/home/arne/wisp/wisp-multiline.sh
432 ; !#
433 (define (hello who)
434 (display who))
435
436 (let hello
437 ((who 0))
438 (if (= who 5)
439 (display who)
440 (hello (+ 1 who))))
441
442
443 ) (wisp->list "
444 #!/home/arne/wisp/wisp-multiline.sh
445 ; !#
446 define : hello who
447 display who
448
449 let hello
450 : who 0
451 if : = who 5
452 display who
453 hello : + 1 who
454 ")))
455
456 ;; the following is no error, but produces a warning because indentation is inconsistent.
457 (with-test-prefix "partial-indent"
458 (pass-if-equal '((write
459 (list
460 (+ 1 2)
461 (+ 2 3)))
462 (newline)
463
464 (write
465 (list
466 (+ 1 2
467 (+ 3 4))
468 (+ 2 3)))
469 (newline)
470
471
472 ) (wisp->list "
473 write
474 list
475 + 1 2
476 + 2 3
477 newline
478
479 write
480 list
481 + 1 2
482 + 3 4
483 + 2 3
484 newline
485 ")))
486
487 (with-test-prefix "quotecolon"
488 (pass-if-equal '(#!/home/arne/wisp/wisp-multiline.sh
489 ; !#
490 (define a 1 ); test whether ' : correctly gets turned into '(
491 ; and whether brackets in commments are treated correctly.
492
493 (define a '(1 2 3))
494
495 (define
496 (a b)
497 (c))
498
499 (define a (quasiquote ,(+ 2 2)))
500 ) (wisp->list "
501 #!/home/arne/wisp/wisp-multiline.sh
502 ; !#
503 define a 1 ; test whether ' : correctly gets turned into '(
504 ; and whether brackets in commments are treated correctly.
505
506 define a ' : 1 2 3
507
508 define
509 a b
510 c
511
512 define a : quasiquote , : + 2 2
513 ")))
514
515 (with-test-prefix "range"
516 (pass-if-equal '((import (rnrs))
517
518 (define range
519 (case-lambda
520 ((n ); one-argument syntax
521 (range 0 n 1))
522 ((n0 n ); two-argument syntax
523 (range n0 n 1))
524 ((n0 n s ); three-argument syntax
525 (assert
526 (and
527 (for-all number? (list n0 n s))
528 (not (zero? s))))
529 (let ((cmp (if (positive? s) >= <= )))
530 (let loop
531 ((i n0 )
532 (acc '()))
533 (if
534 (cmp i n )
535 (reverse acc)
536 (loop (+ i s) (cons i acc))))))))
537
538 (display (apply string-append "" (map number->string (range 5))))
539 (newline)
540
541 ) (wisp->list "
542 import : rnrs
543
544 define range
545 case-lambda
546 : n ; one-argument syntax
547 range 0 n 1
548 : n0 n ; two-argument syntax
549 range n0 n 1
550 : n0 n s ; three-argument syntax
551 assert
552 and
553 for-all number? : list n0 n s
554 not : zero? s
555 let : : cmp : if (positive? s) >= <=
556 let loop
557 : i n0
558 acc '()
559 if
560 cmp i n
561 reverse acc
562 loop (+ i s) (cons i acc)
563
564 display : apply string-append \"\" : map number->string : range 5
565 newline")))
566
567 (with-test-prefix "readable-tests"
568 (pass-if-equal '((define (fibfast n)
569 (if (< n 2))
570 n
571 (fibup n 2 1 0 ))
572
573 (define (fibup maxnum count n-1 n-2)
574 (if (= maxnum count)
575 (+ n-1 n-2)
576 (fibup maxnum
577 (+ count 1 )
578 (+ n-1 n-2 )
579 n-1)))
580
581 (define (factorial n)
582 (if (<= n 1)
583 1
584 (* n
585 (factorial (- n 1)))))
586
587 (define (gcd x y)
588 (if (= y 0))
589 x
590 (gcd y
591 (rem x y)))
592
593 (define (add-if-all-numbers lst)
594 (call/cc
595 (lambda (exit)
596 (let loop
597 (
598 (lst lst )
599 (sum 0))
600 (if (null? lst)
601 sum
602 (if (not (number? (car lst)))
603 (exit #f)
604 (+ (car lst)
605 (loop (cdr lst)))))))))
606
607 ) (wisp->list "
608 define : fibfast n
609 if : < n 2
610 . n
611 fibup n 2 1 0
612
613 define : fibup maxnum count n-1 n-2
614 if : = maxnum count
615 + n-1 n-2
616 fibup maxnum
617 + count 1
618 + n-1 n-2
619 . n-1
620
621 define : factorial n
622 if : <= n 1
623 . 1
624 * n
625 factorial : - n 1
626
627 define (gcd x y)
628 if (= y 0)
629 . x
630 gcd y
631 rem x y
632
633 define : add-if-all-numbers lst
634 call/cc
635 lambda : exit
636 let loop
637 :
638 lst lst
639 sum 0
640 if : null? lst
641 . sum
642 if : not : number? : car lst
643 exit #f
644 + : car lst
645 loop : cdr lst")))
646
647 (with-test-prefix "receive"
648 (pass-if-equal '((import (ice-9 receive) (srfi srfi-1))
649 (write
650 (receive (car cdr)
651 (car+cdr '(car . cdr))
652 car))
653 ) (wisp->list "
654 import (ice-9 receive) (srfi srfi-1)
655 write
656 receive : car cdr
657 car+cdr '(car . cdr)
658 . car
659 ")))
660
661 (with-test-prefix "self-referencial"
662 (pass-if-equal '(; http://stackoverflow.com/questions/23167464/scheme-self-reference-lambda-macro
663 ; because this is as cool as things get
664 (define-syntax slambda
665 (lambda (x)
666 (syntax-case x ()
667 ((slambda formals body0 body1 ...)
668 (with-syntax
669 ((self (datum->syntax #'slambda 'self)))
670 #'(letrec ((self (lambda formals body0 body1 ...)))
671 self))))))
672
673
674
675 (
676 (slambda (x) (+ x 1))
677 10)
678
679 ((slambda () self))
680
681
682
683 ) (wisp->list "
684 ; http://stackoverflow.com/questions/23167464/scheme-self-reference-lambda-macro
685 ; because this is as cool as things get
686 define-syntax slambda
687 lambda : x
688 syntax-case x :
689 : slambda formals body0 body1 ...
690 with-syntax
691 : self : datum->syntax #'slambda 'self
692 #' letrec : : self : lambda formals body0 body1 ...
693 . self
694
695
696
697 :
698 slambda (x) : + x 1
699 . 10
700
701 : slambda () self
702
703 ")))
704
705 (with-test-prefix "shebang"
706 (pass-if-equal '(#!/usr/bin/wisp.py # !#
707 ; This tests shebang lines
708
709
710 ) (wisp->list "
711 #!/usr/bin/wisp.py # !#
712 ; This tests shebang lines
713 ")))
714
715 (with-test-prefix "strangecomments"
716 (pass-if-equal '((use-modules (wisp-scheme))
717 ; works
718 (display
719 (call-with-input-string "foo ; bar\n ; nop \n\n; nup\n; nup \n \n\n\n foo : moo \"\n\" \n___ . goo . hoo" wisp-scheme-read-chunk))
720 (newline)
721 (display
722 (call-with-input-string "foo \n___ . goo . hoo" wisp-scheme-read-chunk))
723 (newline)
724
725 ) (wisp->list "
726 use-modules : wisp-scheme
727 ; works
728 display
729 call-with-input-string \"foo ; bar\\n ; nop \\n\\n; nup\\n; nup \\n \\n\\n\\n foo : moo \\\"\\n\\\" \\n___ . goo . hoo\" wisp-scheme-read-chunk
730 newline
731 display
732 call-with-input-string \"foo \\n___ . goo . hoo\" wisp-scheme-read-chunk
733 newline
734 ")))
735
736 (with-test-prefix "sublist"
737 (pass-if-equal '(; sublists allow to start single line function calls with a colon ( : ).
738
739 (defun a (b c)
740 (let ((e . f))
741 g))
742
743
744 ) (wisp->list "
745 ; sublists allow to start single line function calls with a colon ( : ).
746 ;
747 defun a : b c
748 let : : e . f
749 . g
750 ")))
751
752 (with-test-prefix "sxml"
753 (pass-if-equal '((use-modules (sxml simple))
754 (use-modules (ice-9 match))
755
756 ; define a template
757 (define template
758 (quote
759 (html
760 (head (title "test"))
761 (body
762 (h1 "test")
763 (message "the header")
764 (p "it " (em "works!")
765 (br)
766 (" it actually works!"))))))
767
768 ; transform it
769 (define template2
770 (let loop
771 ((l template))
772 (match l
773 (('message a ...)
774 `(p (@ (style "margin-left: 2em"))
775 (strong ,(map loop a))))
776 ((a ...)
777 (map loop a ))
778 (a
779 a))))
780
781 ; write xml to the output port
782 (sxml->xml template2)
783
784 (newline)
785
786
787 ) (wisp->list "
788 use-modules : sxml simple
789 use-modules : ice-9 match
790
791 ; define a template
792 define template
793 quote
794 html
795 head : title \"test\"
796 body
797 h1 \"test\"
798 message \"the header\"
799 p \"it \" : em \"works!\"
800 br
801 \" it actually works!\"
802
803 ; transform it
804 define template2
805 let loop
806 : l template
807 match l
808 : 'message a ...
809 ` p : @ : style \"margin-left: 2em\"
810 strong ,(map loop a)
811 : a ...
812 map loop a
813 a
814 . a
815
816 ; write xml to the output port
817 sxml->xml template2
818
819 newline
820 ")))
821
822 (with-test-prefix "syntax-colon"
823 (pass-if-equal '((let
824 (
825 (a 1)
826 (b 2))
827 (let
828 (
829 (
830 c 3))
831 (format #t "a: ~A, b: ~A, c: ~A"
832 a b c)))
833
834 ((a))
835
836 (define (hello)
837 (display "hello\n"))
838
839 (let
840 ((a 1)
841 (b 2))
842 (format #t "a: ~A, b: ~A"
843 a b))
844
845 (let ((a '())))
846
847 (let
848 ( ; foo
849 (a
850 '())))
851
852 (
853 (a))
854
855 (define (:)
856 (hello))
857
858 (:)
859
860
861 ) (wisp->list "
862 let
863 :
864 a 1
865 b 2
866 let
867 :
868 :
869 . c 3
870 format #t \"a: ~A, b: ~A, c: ~A\"
871 . a b c
872
873 : a
874
875 define : hello
876 display \"hello\\n\"
877
878 let
879 : a 1
880 b 2
881 format #t \"a: ~A, b: ~A\"
882 . a b
883
884 let : : a ' :
885
886 let
887 : ; foo
888 a
889 '
890
891 :
892 a
893
894 define : \\:
895 hello
896
897 \\:
898 ")))
899
900 (with-test-prefix "syntax-dot"
901 (pass-if-equal '((define (foo)
902 "bar")
903
904 (define (bar)
905 '(1
906 . 2 )); pair
907
908 (display (foo))
909 (newline)
910 (display (bar))
911 (newline)
912
913
914 ) (wisp->list "
915 define : foo
916 . \"bar\"
917
918 define : bar
919 ' 1
920 . . 2 ; pair
921
922 display : foo
923 newline
924 display : bar
925 newline
926 ")))
927
928 (with-test-prefix "syntax-empty"
929 (pass-if-equal '() (wisp->list "
930 ")))
931
932 (with-test-prefix "syntax-indent"
933 (pass-if-equal '((define
934 (hello who)
935 (format #t "Hello ~A\n" who))
936
937 (define
938 (let
939 (
940 (a 1)
941 (b 2)
942 (c 3))
943 (format #t "a: ~A, b: ~A, c: ~A"
944 (+ a 2)
945 b c)))
946
947
948
949 ) (wisp->list "define
950 hello who
951 format #t \"Hello ~A\\n\" who
952
953 define
954 let
955 :
956 a 1
957 b 2
958 c 3
959 format #t \"a: ~A, b: ~A, c: ~A\"
960 + a 2
961 . b c
962
963 ")))
964
965 (with-test-prefix "syntax-strings-parens"
966 (pass-if-equal '(; Test linebreaks in strings and brackets
967
968 "flubbub
969
970 flabbab"
971
972 (hrug (nadda
973 madda gadda "shoktom
974 mee" " sep
975 ka"
976 hadda)
977 (gom))
978
979 (flu)
980
981 (sum [foo
982 bar] barz {1 + [* 2 2]})
983
984 (mara {
985 li
986 +
987 lo - (mabba)
988 })
989 ) (wisp->list "
990 ; Test linebreaks in strings and brackets
991
992 . \"flubbub
993
994 flabbab\"
995
996 hrug (nadda
997 madda gadda \"shoktom
998 mee\" \" sep
999 ka\"
1000 hadda)
1001 gom
1002
1003 flu
1004
1005 sum [foo
1006 bar] barz {1 + [* 2 2]}
1007
1008 mara {
1009 li
1010 +
1011 lo - (mabba)
1012 }
1013 ")))
1014
1015 (with-test-prefix "syntax-underscore"
1016 (pass-if-equal '((define (a b c)
1017 (d e
1018 (f)
1019 (g h)
1020 i))
1021
1022 (define (_)
1023 (display "hello\n"))
1024
1025 (_)
1026
1027
1028 ) (wisp->list "
1029 define : a b c
1030 _ d e
1031 ___ f
1032 ___ g h
1033 __ . i
1034
1035 define : _
1036 _ display \"hello\\n\"
1037
1038 \\_
1039 ")))
1040