;; -*- mode: Scheme; -*- ;; ;; This file is part of Protocol Buffers for CHICKEN ;; Copyright (c) 2013 by Thomas Chust. All rights reserved. ;; ;; Permission is hereby granted, free of charge, to any person ;; obtaining a copy of this software and associated documentation ;; files (the Software), to deal in the Software without restriction, ;; including without limitation the rights to use, copy, modify, ;; merge, publish, distribute, sublicense, and/or sell copies of the ;; Software, and to permit persons to whom the Software is furnished ;; to do so, subject to the following conditions: ;; ;; The above copyright notice and this permission notice shall be ;; included in all copies or substantial portions of the Software. ;; ;; THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS ;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN ;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ;; SOFTWARE. (define (message-rtd? v) (and (rtd? v) (message-info? (prop:protobuf #f v)))) (define msg (make-address-book #:person (list (make-person #:id 42 #:name "Jane Doe" #:phone (list (make-person:phone-number #:number "+12-3456-7890"))) (make-person #:id 23 #:name "Johannes Mustermann" #:email "joe@example.com" #:phone (list (make-person:phone-number #:number "+67-876743724-8751751" #:type 'mobile) (make-person:phone-number #:number "+60-9848752576-987832" #:type 'work)))))) (define (test-structure msg) (test-assert (message? msg)) (test-assert (address-book? msg)) (let ([persons (address-book-person msg '())]) (test 2 (length persons)) (let ([jane (first persons)] [joe (second persons)]) (test-assert (person? jane)) (test 42 (person-id jane)) (test "Jane Doe" (person-name jane)) (test (void) (person-email jane)) (test "jane@example.com" (person-email jane "jane@example.com")) (let ([phones (person-phone jane)]) (test 1 (length phones)) (let ([phone (car phones)]) (test "+12-3456-7890" (person:phone-number-number phone)) (test 'home (person:phone-number-type phone)))) (test-assert (person? joe)) (test 23 (person-id joe)) (test "Johannes Mustermann" (person-name joe)) (test "joe@example.com" (person-email joe)) (test "joe@example.com" (person-email joe "whatever@example.com")) (let ([phones (person-phone joe)]) (test 2 (length phones)) (let ([phone (first phones)]) (test "+67-876743724-8751751" (person:phone-number-number phone)) (test 'mobile (person:phone-number-type phone))) (let ([phone (second phones)]) (test "+60-9848752576-987832" (person:phone-number-number phone)) (test 'work (person:phone-number-type phone))))))) (define (test-main) (test-group "protocol buffers" (test-group "reflection" (test-assert (message-rtd? person)) (test-assert (enum-info? person:phone-type)) (test-assert (message-rtd? person:phone-number)) (test-assert (message-rtd? address-book)) (test 'work ((enum-info-integer->enum person:phone-type) 2)) (test 1 ((enum-info-enum->integer person:phone-type) 'home))) (test-group "original message" (test-structure msg)) (test-group "roundtrip message" (test-structure (call-with-input-string (call-with-output-string (cut serialize msg <>)) (cut deserialize address-book <>)))) (test-group "modification" (set! (address-book-person msg) (cdr (address-book-person msg))) (test 23 (person-id (car (address-book-person msg))))) )) ;; vim: set ai et ts=8 sts=2 sw=2 ft=scheme: ;;