;;;; json-utils-test.scm (import test) (test-begin "json-utils") (import json-utils) ;; (import (srfi-69)) ; (test-group "json-undefined" (define +orig-undef+ (json-undefined)) (parameterize ((json-undefined "")) (test-assert "specific object is undefined" (json-undefined? (json-undefined))) ) (test-assert (json-undefined? +orig-undef+)) ) ; (define jd1 (vector "a" 2 2.0)) (define jd2 '((a . 1) (b . 2))) (define jd3 (alist->hash-table jd2)) (define jd4 (vector jd1 2 jd3)) (define jd5 `((jd1 . ,jd1) (2nd . 2) (jd3 . ,jd3))) (test-group "json-count" (test 3 (json-count jd1)) (test 2 (json-count jd2)) (test 2 (json-count jd3)) ) (test-group "json-ref" (test "string index key" 2.0 (json-ref jd1 "2")) (test "symbol index key" 2.0 (json-ref jd1 '2)) (test "non-fix index" 2.0 (json-ref jd4 1/2 2.0)) (test 2 (json-ref jd2 'b)) (test 2 (json-ref jd3 'b)) (test 2.0 (json-ref jd4 0 2)) (test 2 (json-ref jd4 2 'b)) ) (test-group "numerics preserved" (test 2 (json-ref jd1 1)) (test-assert (fixnum? (json-ref jd1 1))) (test 2.0 (json-ref jd1 2)) (test-assert (flonum? (json-ref jd1 2))) ) (test-group "Array HOF" (test '((2 . 2.0) (1 . 2) (0 . "a")) (json-fold jd1 (lambda (k v a) `((,k . ,v) . ,a)) '())) (test '((2 . 2.0) (1 . 2) (0 . "a")) (json-foldl jd1 (lambda (a k v) `((,k . ,v) . ,a)) '())) (test '((2 . 2.0) (1 . 2) (0 . "a")) (json-map jd1 (lambda (k v) `(,k . ,v)))) (let ((+seen+ #f)) (json-for-each jd1 (lambda (k v) (set! +seen+ #t))) (test-assert "json-for-each" +seen+)) ) (test-group "Object HOF" (test "AL json-fold" '((b . 2) (a . 1)) (json-fold jd2 (lambda (k v a) `((,k . ,v) . ,a)) '())) (test "AL json-foldl" '((b . 2) (a . 1)) (json-foldl jd2 (lambda (a k v) `((,k . ,v) . ,a)) '())) (test "AL json-map" '((a . 1) (b . 2)) (json-map jd2 (lambda (k v) `(,k . ,v)))) (let ((+seen+ #f)) (json-for-each jd2 (lambda (k v) (set! +seen+ #t))) (test-assert "AL jjson-for-each" +seen+)) (let () (import (chicken sort)) (define (symbol< a b) (stringstring a) (symbol->string b))) (define (key< a b) (symbol< (car a) (car b))) (test "HT json-fold" '((a . 1) (b . 2)) (sort (json-fold jd3 (lambda (k v a) `((,k . ,v) . ,a)) '()) key<)) (test "HT json-foldl" '((a . 1) (b . 2)) (sort (json-foldl jd3 (lambda (a k v) `((,k . ,v) . ,a)) '()) key<)) (test "HT json-map" '((a . 1) (b . 2)) (sort (json-map jd3 (lambda (k v) `(,k . ,v))) key<)) (let ((+seen+ #f)) (json-for-each jd3 (lambda (k v) (set! +seen+ #t))) (test-assert "HT json-for-each" +seen+)) ) ) ;; (test-end "json-utils") (test-exit)