diff options
| author | Arun Isaac | 2025-10-03 19:37:42 +0100 |
|---|---|---|
| committer | Arun Isaac | 2025-11-16 22:43:00 +0000 |
| commit | 9e12ccfdfd77891746120cab201a65f5486c057a (patch) | |
| tree | 3b7e60a2a6f3cb7d9164cf0eff055f97a1afba15 /tests | |
| parent | 7d116c6efefd94dbe82b3f7dc33d4017052559da (diff) | |
| download | ravanan-9e12ccfdfd77891746120cab201a65f5486c057a.tar.gz ravanan-9e12ccfdfd77891746120cab201a65f5486c057a.tar.lz ravanan-9e12ccfdfd77891746120cab201a65f5486c057a.zip | |
tests: Canonicalize JSON objects to compare them.
Compare JSON objects by canonicalizing them and then using test-equal. This is better than using json=? and test-assert since this passes on the actual and expected values to the SRFI-64 runner.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/reader.scm | 267 |
1 files changed, 125 insertions, 142 deletions
diff --git a/tests/reader.scm b/tests/reader.scm index f3bcdd2..1142add 100644 --- a/tests/reader.scm +++ b/tests/reader.scm @@ -16,8 +16,7 @@ ;;; You should have received a copy of the GNU General Public License ;;; along with ravanan. If not, see <https://www.gnu.org/licenses/>. -(use-modules (srfi srfi-1) - (srfi srfi-64) +(use-modules (srfi srfi-64) (ice-9 filesystem) (ice-9 match) (web uri) @@ -34,151 +33,135 @@ (define normalize-input (@@ (ravanan reader) normalize-input)) -(define (json=? tree1 tree2) - (cond - ;; Arrays - ((vector? tree1) - (lset= json=? - (vector->list tree1) - (vector->list tree2))) - ;; Dictionaries - ((list? tree1) - (lset= (match-lambda* - (((key1 . value1) (key2 . value2)) - (and (string=? key1 key2) - (json=? value1 value2)))) - tree1 - tree2)) - ;; Atoms - (else - (equal? tree1 tree2)))) - (test-begin "reader") (test-equal "Coerce number to number" 37 (coerce-type 37 'number)) -(test-assert "Normalize File type formal input" - (json=? '(("type" . "File") - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #t))))) - (normalize-formal-input - '(("type" . "File") - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize File array type formal input" - (json=? '(("type" - ("type" . "array") - ("items" . "File")) - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #t))))) - (normalize-formal-input - '(("type" - ("type" . "array") - ("items" . "File")) - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize array of File arrays type formal input" - (json=? '(("type" - ("type" . "array") - ("items" . (("type" . "array") - ("items" . "File")))) - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #t))))) - (normalize-formal-input - '(("type" - ("type" . "array") - ("items" . (("type" . "array") - ("items" . "File")))) - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize File type formal output" - (json=? '(("type" . "File") - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #f))))) - (normalize-formal-output - '(("type" . "File") - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize File array type formal output" - (json=? '(("type" - ("type" . "array") - ("items" . "File")) - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #f))))) - (normalize-formal-output - '(("type" - ("type" . "array") - ("items" . "File")) - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize array of File arrays type formal output" - (json=? '(("type" - ("type" . "array") - ("items" . (("type" . "array") - ("items" . "File")))) - ("id" . "foo") - ("secondaryFiles" . #((("pattern" . ".bai") - ("required" . #f))))) - (normalize-formal-output - '(("type" - ("type" . "array") - ("items" . (("type" . "array") - ("items" . "File")))) - ("id" . "foo") - ("secondaryFiles" . #(".bai")))))) - -(test-assert "Normalize inputs with only location" - (call-with-temporary-directory - (lambda (dir) - (json=? (let ((path (expand-file-name "foo" dir))) - `(("class" . "File") - ("location" . ,(uri->string (build-uri 'file #:path path))) - ("path" . ,path) - ("basename" . "foo") - ("nameroot" . "foo") - ("nameext" . "") - ("size" . 0) - ("checksum" . "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709"))) - (call-with-current-directory dir - (lambda () - ;; Create an actual file called "foo" so that canonicalize-path - ;; works. - (call-with-output-file "foo" - (const #t)) - (normalize-input '(("class" . "File") - ("location" . "foo"))))))))) - -(test-assert "Normalize inputs with only path" - (call-with-temporary-directory - (lambda (dir) - (json=? (let ((path (expand-file-name "foo" dir))) - `(("class" . "File") - ("location" . ,(uri->string (build-uri 'file #:path path))) - ("path" . ,path) - ("basename" . "foo") - ("nameroot" . "foo") - ("nameext" . "") - ("size" . 0) - ("checksum" . "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709"))) - (call-with-current-directory dir - (lambda () - ;; Create an actual file called "foo" so that canonicalize-path - ;; works. - (call-with-output-file "foo" - (const #t)) - (normalize-input '(("class" . "File") - ("path" . "foo"))))))))) +(test-equal "Normalize File type formal input" + (canonicalize-json '(("type" . "File") + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #t)))))) + (canonicalize-json (normalize-formal-input + '(("type" . "File") + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(test-equal "Normalize File array type formal input" + (canonicalize-json '(("type" + ("type" . "array") + ("items" . "File")) + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #t)))))) + (canonicalize-json (normalize-formal-input + '(("type" + ("type" . "array") + ("items" . "File")) + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(test-equal "Normalize array of File arrays type formal input" + (canonicalize-json '(("type" + ("type" . "array") + ("items" . (("type" . "array") + ("items" . "File")))) + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #t)))))) + (canonicalize-json (normalize-formal-input + '(("type" + ("type" . "array") + ("items" . (("type" . "array") + ("items" . "File")))) + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(test-equal "Normalize File type formal output" + (canonicalize-json '(("type" . "File") + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #f)))))) + (canonicalize-json (normalize-formal-output + '(("type" . "File") + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(test-equal "Normalize File array type formal output" + (canonicalize-json '(("type" + ("type" . "array") + ("items" . "File")) + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #f)))))) + (canonicalize-json (normalize-formal-output + '(("type" + ("type" . "array") + ("items" . "File")) + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(test-equal "Normalize array of File arrays type formal output" + (canonicalize-json '(("type" + ("type" . "array") + ("items" . (("type" . "array") + ("items" . "File")))) + ("id" . "foo") + ("secondaryFiles" . #((("pattern" . ".bai") + ("required" . #f)))))) + (canonicalize-json (normalize-formal-output + '(("type" + ("type" . "array") + ("items" . (("type" . "array") + ("items" . "File")))) + ("id" . "foo") + ("secondaryFiles" . #(".bai")))))) + +(call-with-temporary-directory + (lambda (dir) + (test-equal "Normalize inputs with only location" + (canonicalize-json + (let ((path (expand-file-name "foo" dir))) + `(("class" . "File") + ("location" . ,(uri->string (build-uri 'file #:path path))) + ("path" . ,path) + ("basename" . "foo") + ("nameroot" . "foo") + ("nameext" . "") + ("size" . 0) + ("checksum" . "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709")))) + (canonicalize-json + (call-with-current-directory dir + (lambda () + ;; Create an actual file called "foo" so that canonicalize-path works. + (call-with-output-file "foo" + (const #t)) + (normalize-input '(("class" . "File") + ("location" . "foo"))))))))) + +(call-with-temporary-directory + (lambda (dir) + (test-equal "Normalize inputs with only path" + (canonicalize-json + (let ((path (expand-file-name "foo" dir))) + `(("class" . "File") + ("location" . ,(uri->string (build-uri 'file #:path path))) + ("path" . ,path) + ("basename" . "foo") + ("nameroot" . "foo") + ("nameext" . "") + ("size" . 0) + ("checksum" . "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709")))) + (canonicalize-json + (call-with-current-directory dir + (lambda () + ;; Create an actual file called "foo" so that canonicalize-path + ;; works. + (call-with-output-file "foo" + (const #t)) + (normalize-input '(("class" . "File") + ("path" . "foo"))))))))) (test-end "reader") |
