Skip to content

Commit

Permalink
Add tests for :any
Browse files Browse the repository at this point in the history
Includes a regression test for #8, any-hash-table
  • Loading branch information
gschjetne committed Aug 13, 2023
1 parent 1b739d2 commit f677097
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
15 changes: 14 additions & 1 deletion tests/encode-decode.lisp
Expand Up @@ -38,7 +38,10 @@
(is (= (get-number obj)
(get-number (obj-rt obj))))))

;; TODO: test hash table
(test hash-table
(for-all ((obj (gen-object)))
(is (equalp (get-hash-table obj)
(get-hash-table (obj-rt obj))))))

(test vector
(for-all ((obj (gen-object)))
Expand All @@ -60,6 +63,16 @@
(is (= (get-number (get-object obj))
(get-number (get-object (obj-rt obj)))))))

(test any
(for-all ((obj (gen-object)))
(is (equalp (get-any obj)
(get-any (obj-rt obj))))))

(test any-hash-table
(for-all ((obj (gen-object)))
(is (equalp (get-any-hash-table obj)
(get-any-hash-table (obj-rt obj))))))

(test inheritance-encode
(let ((child (make-instance 'child))
(parent-only (make-instance 'parent)))
Expand Down
37 changes: 33 additions & 4 deletions tests/tests.lisp
Expand Up @@ -35,6 +35,10 @@
:reader get-hash-table
:json-type :hash-table
:json-key "hash")
(any-hash :initarg :any-hash-table
:reader get-any-hash-table
:json-type :any
:json-key "anyHash")
(vector :initarg :vector
:reader get-vector
:json-type :vector
Expand All @@ -50,7 +54,11 @@
(object :initarg :object
:reader get-object
:json-type test-class
:json-key "obj"))
:json-key "obj")
(any :initarg :any
:reader get-any
:json-type :any
:json-key "any"))
(:metaclass json-serializable-class))

;;; as per https://github.com/gschjetne/json-mop/issues/1
Expand Down Expand Up @@ -86,24 +94,45 @@
(defun gen-bool ()
(lambda () (zerop (random 2))))

(defun gen-any (&key (choices (list (gen-string)
(gen-float)
(gen-vector))))
(lambda ()
(funcall (nth (random (length choices))
choices))))

(defun gen-hash-table (&key
(length (gen-integer :min 0 :max 10))
(keys (gen-string))
(elements (gen-integer)))
(lambda ()
(let ((hash-table (make-hash-table :test 'equal)))
(loop repeat (funcall length)
do (setf (gethash (funcall keys) hash-table)
(funcall elements)))
hash-table)))

(defun gen-object (&key
(string (gen-string))
(number (gen-float))
(hash-table (lambda () (make-hash-table)))
(hash-table (gen-hash-table))
(vector (gen-vector))
(list (gen-list))
(bool (gen-bool))
(object (lambda () (make-instance
'test-class
:number (funcall (gen-integer))))))
:number (funcall (gen-integer)))))
(any (gen-any)))
(lambda ()
(make-instance 'test-class
:string (funcall string)
:number (funcall number)
:hash-table (funcall hash-table)
:any-hash-table (funcall hash-table)
:vector (funcall vector)
:list (funcall list)
:bool (funcall bool)
:object (funcall object))))
:object (funcall object)
:any (funcall any))))

(def-suite test-all)

0 comments on commit f677097

Please sign in to comment.