(module serialise-test mzscheme
(require (planet "test.ss" ("schematics" "schemeunit.plt" 1 1)))
(require "base.ss"
"serialise.ss"
"util.ss")
(provide serialise-tests)
(define-assertion (assert-invertible scheme xml)
(with-assertion-info
(('message "Conversion from Scheme to XML failed"))
(assert-equal? (serialise scheme)
xml))
(with-assertion-info
(('message "Conversion from XML to Scheme failed"))
(assert-equal? (deserialise xml)
scheme)))
(define-assertion (assert-invertible-hash scheme xml)
(with-assertion-info
(('message "Conversion from Scheme to XML failed"))
(assert-equal? (serialise scheme)
xml))
(with-assertion-info
(('message "Conversion from XML to Scheme failed"))
(assert-hash-table-equal? (deserialise xml)
scheme)))
(define invertible-tests
(make-test-suite
"All tests for invertible serialisation"
(make-test-case
"Normal integers serialised correctly"
(assert-invertible 1 '(value (int "1")))
(assert-invertible 123456 '(value (int "123456")))
(assert-invertible -1234 '(value (int "-1234")))
(assert-invertible (expt 2 31) '(value (int "2147483648")))
(assert-invertible (* -1 (expt 2 31))
'(value (int "-2147483648"))))
(make-test-case
"Boolean serialised correctly"
(assert-invertible #t '(value (boolean "1")))
(assert-invertible #f '(value (boolean "0"))))
(make-test-case
"String serialised correctly"
(assert-invertible "hello world"
'(value (string "hello world"))))
(make-test-case
"Empty string serialised correctly"
(assert-invertible "" '(value (string ""))))
(make-test-case
"Doubles serialised correctly"
(assert-invertible 1.2 '(value (double "1.2"))))
(make-test-case
"Date serialised correctly"
(assert-invertible
(make-date 35 27 9 7 12 2005 3 340 #f 0)
'(value (dateTime.iso8601 "20051207T09:27:35"))))
(make-test-case
"Date components are padded to two digits"
(assert-invertible
(make-date 5 4 3 2 1 2005 0 1 #f 0)
'(value (dateTime.iso8601 "20050102T03:04:05"))))
(make-test-case
"Hash-table serialised correctly"
(assert-invertible-hash
#hash((a . 1) (b . "2") (c . 3.0))
'(value (struct
(member (name "b") (value (string "2")))
(member (name "a") (value (int "1")))
(member (name "c") (value (double "3.0")))))))
(make-test-case
"Recursive hash-table serialised correctly"
(assert-invertible-hash
#hash((a . #hash((b . 2))))
'(value (struct
(member (name "a")
(value (struct
(member (name "b")
(value (int "2"))))))))))
(make-test-case
"List serialised as array"
(assert-invertible '(1 2 3 4)
`(value (array (data (value (int "1"))
(value (int "2"))
(value (int "3"))
(value (int "4")))))))
(make-test-case
"Hetergenous list serialised correctly"
(assert-invertible
'(1 2.0 "3")
`(value (array (data (value (int "1"))
(value (double "2.0"))
(value (string "3")))))))
(make-test-case
"Recursive list serialised correctly"
(assert-invertible
'((1 (2)))
'(value (array
(data
(value
(array
(data
(value (int "1"))
(value
(array
(data (value (int "2")))))))))))))
(make-test-case
"(list) array encoded correctly"
(assert-invertible '() '(value (array (data))))
(assert-invertible
'(())
'(value (array (data (value (array (data)))))))
(assert-invertible '(1 2 3 4 5)
'(value (array
(data
(value (int "1"))
(value (int "2"))
(value (int "3"))
(value (int "4"))
(value (int "5"))))))
(assert-invertible '(1 "two" 3.3)
'(value (array
(data
(value (int "1"))
(value (string "two"))
(value (double "3.3"))))))
(assert-invertible
'("")
'(value (array (data (value (string "")))))))
(make-test-case
"String containing < and & is encoded correctly"
(assert-invertible "<&" '(value (string "<&"))))
(make-test-case
"All entity instances are encoded"
(assert-invertible
"<hello&<there&< <&&"
'(value (string "<hello&<there&< <&&"))))
(make-test-case
"base64 encoded correctly"
(assert-invertible
#"Scheme Rules!"
'(value (base64 "U2NoZW1lIFJ1bGVzIQ=="))))
))
(define serialisation-tests
(make-test-suite
"Serialisation tests"
(make-test-case
"Out-of-range doubles throw exception"
(assert-exn exn:xmlrpc?
(lambda ()
(serialise +inf.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise -inf.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise +nan.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise -nan.0))))
(make-test-case
"Out of range integer throws exception"
(assert-exn exn:xmlrpc?
(lambda ()
(serialise +inf.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise -inf.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise +nan.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise -nan.0)))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise (expt 2 32))))
(assert-exn exn:xmlrpc?
(lambda ()
(serialise (- (expt 2 40))))))
(make-test-case
"(vector) array encoded correctly"
(assert-equal? (serialise (make-vector 0))
'(value (array (data))))
(assert-equal? (serialise (list->vector '(1 2 3 4 5)))
'(value (array
(data
(value (int "1"))
(value (int "2"))
(value (int "3"))
(value (int "4"))
(value (int "5"))))))
(assert-equal? (serialise (list->vector '(1 "two" 3.3)))
'(value (array
(data
(value (int "1"))
(value (string "two"))
(value (double "3.3"))))))
(assert-equal? (serialise (list->vector '("")))
'(value (array (data (value (string "")))))))
(make-test-case
"String encoding is parameterised"
(parameterize
((encode-string #f))
(assert-equal? (serialise "<&")
'(value (string "<&"))))
(parameterize
((encode-string #t))
(assert-equal? (serialise "<&")
'(value (string "<&")))))
))
(define deserialisation-tests
(make-test-suite
"Deserialisation tests"
(make-test-case
"Deserialisation default to string"
(assert-equal? (deserialise '(value "Foo"))
"Foo"))
(make-test-case
"Deserialisation of empty value defaults to empty string"
(assert-equal? (deserialise '(value))
""))
(make-test-case
"Deserialisation of dateTime raises exn:xmlrpc on badly formatted data"
(assert-exn
exn:xmlrpc?
(lambda ()
(deserialise
'(value (dateTime.iso8601 "990101T09:27:35"))))))
(make-test-case
"Deserialisation raises exn:xmlrpc on error"
(assert-exn exn:xmlrpc?
(lambda ()
(deserialise '(some crap)))))
(make-test-case
"Incorrect struct raises exn:xmlrpc"
(assert-exn exn:xmlrpc?
(lambda ()
(deserialise
'(value (struct
(member (name "a")))))))
(assert-exn exn:xmlrpc?
(lambda ()
(deserialise
'(value (struct
(member (value "2"))))))))
(make-test-case
"Deserialisation of empty string is correct"
(assert-equal? (deserialise '(value (string)))
""))
(make-test-case
"String decoding is parameterised"
(parameterize
((decode-string #f))
(assert-equal? (deserialise '(value (string "<&")))
"<&"))
(parameterize
((decode-string #t))
(assert-equal? (deserialise '(value (string "<&")))
"<&")))
(make-test-case
"All entity instances are decoded"
(assert-equal?
(deserialise
'(value
(string "<hello&<there&< <&&")))
"<hello&<there&< <&&"))
))
(define serialise-tests
(make-test-suite
"Serialise tests"
invertible-tests
serialisation-tests
deserialisation-tests))
)