serialise-test.ss
;;;
;;; Time-stamp: <06/01/03 14:32:20 nhw>
;;;
;;; Copyright (C) 2005 by Noel Welsh.
;;;

;;; This library is free software; you can redistribute it
;;; and/or modify it under the terms of the GNU Lesser
;;; General Public License as published by the Free Software
;;; Foundation; either version 2.1 of the License, or (at
;;; your option) any later version.

;;; This library is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the
;;; implied warranty of MERCHANTABILITY or FITNESS FOR A
;;; PARTICULAR PURPOSE.  See the GNU Lesser General Public
;;; License for more details.

;;; You should have received a copy of the GNU Lesser
;;; General Public License along with this library; if not,
;;; write to the Free Software Foundation, Inc., 59 Temple
;;; Place, Suite 330, Boston, MA 02111-1307 USA

;;; Author: Noel Welsh <noelwelsh@yahoo.com>, Matt Jadud <jadudm@gmail.com>
;;
;;
;; Commentary:

(module serialise-test mzscheme
  
  (require (planet "test.ss" ("schematics" "schemeunit.plt" 1 1)))
  (require "base.ss"
           "serialise.ss"
           "util.ss")
  
  (provide serialise-tests)

  ;; assert-invertible : any sxml
  ;;
  ;; Assert that serialisation and deserialisation produces
  ;; exactly the same result
  (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 "&lt;&amp;"))))

     (make-test-case
      "All entity instances are encoded"
      (assert-invertible
       "<hello&<there&< <&&"
       '(value (string "&lt;hello&amp;&lt;there&amp;&lt; &lt;&amp;&amp;"))))
     
     (make-test-case
      "base64 encoded correctly"
      (assert-invertible
       #"Scheme Rules!"
       '(value (base64 "U2NoZW1lIFJ1bGVzIQ=="))))

     ))
  ;; end of invertible-tests

  (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 "&lt;&amp;")))))
     ))
  ;; end of serialisation tests
  
  (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 "&lt;&amp;")))
                       "<&")))

     (make-test-case
      "All entity instances are decoded"
      (assert-equal?
       (deserialise
        '(value
          (string "&lt;hello&amp;&lt;there&amp;&lt; &lt;&amp;&amp;")))
       "<hello&<there&< <&&"))
     ))
  ;; end of deserialisation-tests

  (define serialise-tests
    (make-test-suite
     "Serialise tests"
     invertible-tests
     serialisation-tests
     deserialisation-tests))
  
  )