aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-03-11 03:42:56 -0300
committerEuAndreh <eu@euandre.org>2022-03-11 03:42:56 -0300
commit9884dadd952116f8da3c7d731b498c599948087c (patch)
treefa9e2b8f3cf95f5a9cedd97a53889a53bd0fa6f9 /src
parentTODOs.md: Add #td-37e4373e-64ee-eab5-99fb-4126939126d7 (diff)
downloadtoph-9884dadd952116f8da3c7d731b498c599948087c.tar.gz
toph-9884dadd952116f8da3c7d731b498c599948087c.tar.xz
src/curth0.scm: Finish tests for heredocs
Fix the case when a single #" was used.
Diffstat (limited to 'src')
-rw-r--r--src/curth0.scm241
1 files changed, 227 insertions, 14 deletions
diff --git a/src/curth0.scm b/src/curth0.scm
index 13a3ec8..c56263f 100644
--- a/src/curth0.scm
+++ b/src/curth0.scm
@@ -1,13 +1,14 @@
(define-module (curth0)
#:use-module ((language tree-il) #:prefix tree-il:)
+ #:use-module ((ice-9 pretty-print) #:prefix pp:)
#:use-module ((srfi srfi-1) #:prefix s1:)
#:use-module ((srfi srfi-64) #:prefix t:)
#:export (->
->>))
-
-(define (expand l)
- (tree-il:tree-il->scheme (macroexpand l)))
+(define (tap x)
+ (pp:pretty-print x)
+ x)
(define-syntax ->
(syntax-rules ()
@@ -28,6 +29,9 @@
((_ x f rest ...) (->> (f x) rest ...))))
(define (test-thread-macro)
+ (define (expand l)
+ (tree-il:tree-il->scheme (macroexpand l)))
+
(t:test-group "-> and ->>"
(t:test-equal '#f (expand '(->)))
(t:test-equal '#f (expand '(->>)))
@@ -80,7 +84,7 @@
(non-blank? (list prev #t n))
(changed? (list prev #t n))
(#:else (list curr #f (+ 1 n))))))
- (list #nil #f 0)
+ '(#nil #f 0)
chars))
(define (test-extract-single-line-indentation)
@@ -169,7 +173,15 @@
(t:test-equal '((#\space 1) (#\space 2) (#\space 3))
(extract-line-indentations
- '(" " " " " ")))))
+ '(" " " " " ")))
+
+ (t:test-equal '((#\space 1) (#\tab 1) (#\space 3))
+ (extract-line-indentations
+ '(" " " " " ")))
+
+ (t:test-equal '((#nil 0) (#\space 3) (#\tab 2))
+ (extract-line-indentations
+ '("no spaces" " with spaces" " with tabs")))))
(define (maximum-indentation lines)
(let* ((line-indentations (extract-line-indentations lines))
@@ -177,7 +189,41 @@
(different-indents? (not (= 1 (length (s1:delete-duplicates chars))))))
(if different-indents?
0
- (apply min (map s1:third line-indentations)))))
+ (apply min (map s1:second line-indentations)))))
+
+(define (test-maximum-indentation)
+ (t:test-group "maximum-indentation"
+ (t:test-equal 0
+ (maximum-indentation
+ '()))
+
+ (t:test-equal 0
+ (maximum-indentation
+ '("")))
+
+ (t:test-equal 0
+ (maximum-indentation
+ '("" "" "")))
+
+ (t:test-equal 0
+ (maximum-indentation
+ '("" " " "")))
+
+ (t:test-equal 1
+ (maximum-indentation
+ '(" " " " " ")))
+
+ (t:test-equal 2
+ (maximum-indentation
+ '(" a" " b" " c")))
+
+ (t:test-equal 1
+ (maximum-indentation
+ '(" space space" " space tab" " space space tab")))
+
+ (t:test-equal 0
+ (maximum-indentation
+ '(" space space" " space tab" "none")))))
(define (trim-indentation s)
(let* ((lines (string-split s #\newline))
@@ -192,25 +238,108 @@
lines)
"\n")))
+(define (test-trim-indentation)
+ (t:test-group "trim-indentation"
+ (t:test-equal ""
+ (trim-indentation ""))
+
+ (t:test-equal "alltogether"
+ (trim-indentation "alltogether"))
+
+ (t:test-equal "with spaces between"
+ (trim-indentation "with spaces between"))
+
+ (t:test-equal "with spaces around "
+ (trim-indentation " with spaces around "))
+
+ (t:test-equal "with multiple spaces "
+ (trim-indentation " with multiple spaces "))
+
+ (t:test-equal "tabs "
+ (trim-indentation " tabs "))
+
+ (t:test-equal "tabs and spaces "
+ (trim-indentation " tabs and spaces "))
+
+ (t:test-equal "\n"
+ (trim-indentation "\n"))
+
+ (t:test-equal "\n\n\nmultiple empty lines\n\n\n"
+ (trim-indentation "\n\n\nmultiple empty lines\n\n\n"))
+
+ (t:test-equal "\n\nmixed\n indentations\n"
+ (trim-indentation "\n\nmixed\n indentations\n"))
+
+ (t:test-equal "\n\n\n \n lines with only spaces\n"
+ (trim-indentation "\n\n \n \n lines with only spaces\n"))
+
+ (t:test-equal "\n\n mixed spaces\n and tabs"
+ (trim-indentation "\n\n mixed spaces\n and tabs"))
+
+ (t:test-equal "\nstripped\n tabs"
+ (trim-indentation "\n stripped\n tabs"))
+
+ (t:test-equal "
+#!/bin/sh
+set -eu
+
+ret=0
+if cmd; then
+ echo 'Done!'
+ ret=1
+fi
+exit $ret
+"
+ (trim-indentation "
+ #!/bin/sh
+ set -eu
+
+ ret=0
+ if cmd; then
+ echo 'Done!'
+ ret=1
+ fi
+ exit $ret
+"))))
+
+
(define (non-quote-chars? chars)
(let ((non-quote-chars (filter (lambda (c)
(not (equal? #\" c)))
chars)))
- (if (< 0 (length non-quote-chars))
- non-quote-chars
- #f)))
+ (< 0 (length non-quote-chars))))
+
+(define (test-non-quote-chars?)
+ (t:test-group "non-quote-chars?"
+ (t:test-equal #f
+ (non-quote-chars?
+ '()))
+
+ (t:test-equal #f
+ (non-quote-chars?
+ '(#\")))
+
+ (t:test-equal #f
+ (non-quote-chars?
+ '(#\" #\" #\")))
+
+ (t:test-equal #t
+ (non-quote-chars?
+ '(#\" #\" #\-)))
+
+ (t:test-equal #t
+ (non-quote-chars?
+ '(#\a #\")))))
(define (multiline-string-reader _char port)
- "FIXME:
- - remove the need to indent the last line with the rest of the content.
- How does Perl, Ruby, Python, sh do it?"
(let ((multiline? #f)
(chars '()))
(do ((curr (read-char port)
(read-char port)))
((equal? #\newline curr))
(set! chars (cons curr chars)))
- (when (equal? #\- (car chars))
+ (when (and (not (null? chars))
+ (equal? #\- (car chars)))
(set! multiline? #t)
(set! chars (cdr chars)))
(let ((non-quote-chars (non-quote-chars? chars)))
@@ -244,11 +373,95 @@
(read-hash-extend #\" multiline-string-reader)
+(define (test-multiline-string-reader)
+ (t:test-group "multiline-string-reader"
+ (t:test-equal ""
+ #""
+""#)
+
+ (t:test-equal " "
+ #""
+ ""#)
+
+ (t:test-equal " "
+ #""
+ ""#)
+
+ (t:test-equal ""
+ #""-
+ ""#)
+
+ (t:test-equal " some\n text"
+ #""
+ some
+ text""#)
+
+ (t:test-equal "some\ntext"
+ #""-
+ some
+ text""#)
+
+ (t:test-equal " indented\ntext"
+ #""-
+ indented
+ text""#)
+
+ (t:test-equal "with\nnewline\n"
+ #""-
+ with
+ newline
+ ""#)
+
+ (t:test-equal " unindented\n newline\n"
+ #""-
+ unindented
+ newline
+ ""#)
+
+ (t:test-equal "multiple quotes: #\"\"\"inside\"\"\"#\n"
+ #""""-
+ multiple quotes: #"""inside"""#
+ """"#)
+
+ (t:test-equal " indented"
+ #"
+ indented"#)
+
+ (t:test-equal "unindented"
+ #""-
+ unindented""#)
+
+ (t:test-equal "#!/bin/sh
+set -eu
+
+some-cmd
+echo \"$SOMETHING\"# here is a valid glued comment
+if cmd; then
+ echo 'Indentation here!'
+ echo \"\"# anothe valid comment, glued to an empty string
+fi
+"
+ #"""-
+ #!/bin/sh
+ set -eu
+
+ some-cmd
+ echo "$SOMETHING"# here is a valid glued comment
+ if cmd; then
+ echo 'Indentation here!'
+ echo ""# anothe valid comment, glued to an empty string
+ fi
+ """#)))
+
(define test-fns
(list
test-thread-macro
test-extract-single-line-indentation
- test-extract-line-indentations))
+ test-extract-line-indentations
+ test-maximum-indentation
+ test-trim-indentation
+ test-non-quote-chars?
+ test-multiline-string-reader))
(define (unit-tests)
(t:test-begin "curth0-tests")