From 11ecc2f1e1f6fab82d6cfc5b0e217b4755dfc2ce Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Thu, 15 Sep 2016 16:32:49 -0400 Subject: [PATCH] string-split-lens: add tests where inputs would violate the lens laws fixes https://github.com/jackfirth/lens/issues/261 --- lens-data/lens/private/string/string-split.rkt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lens-data/lens/private/string/string-split.rkt b/lens-data/lens/private/string/string-split.rkt index b42959c..762191d 100644 --- a/lens-data/lens/private/string/string-split.rkt +++ b/lens-data/lens/private/string/string-split.rkt @@ -35,6 +35,8 @@ (module+ test (define ws-lens (string-split-lens #px"\\s+")) + (check-equal? (lens-view ws-lens "a b c") '("a" "b" "c")) + (check-equal? (lens-set ws-lens "a b c" '("d" "e" "f")) "d e f") (check-equal? (lens-view ws-lens " foo bar baz \r\n\t") '("" "foo" "bar" "baz" "")) (check-equal? (lens-set ws-lens " foo bar baz \r\n\t" '("a" "b" "c" "d" "e")) @@ -43,14 +45,28 @@ '("a" "b" "c" "d" "e")) (check-equal? (lens-set ws-lens "a b c d \r\n\te" '("" "foo" "bar" "baz" "")) " foo bar baz \r\n\t") + ;; this input would violate the lens laws + (check-exn (regexp (regexp-quote "expected a string not matching #px\"\\\\s+\", given: \"e f\"")) + (λ () + (lens-set ws-lens "a b c" '("d" "e f" "g")))) + (define newline-lens (string-split-lens "\n")) (check-equal? (lens-view newline-lens "a,b\nc,d\ne,f,g") '("a,b" "c,d" "e,f,g")) (check-equal? (lens-set newline-lens "a,b\nc,d\ne,f,g" '("1" "2" "3")) "1\n2\n3") + ;; this input would violate the lens laws + (check-exn (regexp (regexp-quote "expected a string not matching \"\\n\", given: \"2\\n2.5\"")) + (λ () + (lens-set newline-lens "a,b\nc,d\ne,f,g" '("1" "2\n2.5" "3")))) + (define comma-lens (string-split-lens #\,)) (check-equal? (lens-view comma-lens "a,b,c") '("a" "b" "c")) (check-equal? (lens-set comma-lens "a,b,c" '("1" "2" "3")) "1,2,3") + ;; this input would violate the lens laws + (check-exn (regexp (regexp-quote "expected a string not matching #\\,, given: \"2,2.5\"")) + (λ () + (lens-set comma-lens "a,b,c" '("1" "2,2.5" "3")))) )