;;; Weeb --- Web microframework for GNU Guile ;;; Copyright © 2026 Jakob Kreuze ;;; ;;; This file is part of Weeb. ;;; ;;; Weeb is free software: you can redistribute it and/or modify it ;;; under the terms of the GNU Affero General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; Weeb 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 Affero General ;;; Public License for more details. ;;; ;;; You should have received a copy of the GNU Affero General Public ;;; License along with Weeb. If not, see . (use-modules (srfi srfi-64) (weeb struct)) (test-runner-current (test-runner-simple)) (test-begin "weeb-struct") (test-group "instantiation" (define-struct x y) (let ((p (make-struct 10 20))) (test-eqv 10 ((record-accessor 'x) p)) (test-eqv 20 ((record-accessor 'y) p)))) (test-group "with-accessors" (define-struct name age) (let ((p (make-struct "Alice" 30))) (with-accessors (p name age) (test-eqv "Alice" name) (test-eqv 30 age)))) (test-group "call-with-slots" (define-struct x y) (let ((c (make-struct 5 15))) (call-with-slots c (lambda (x y) (test-eqv 5 x) (test-eqv 15 y))))) (test-group "instance-of?" (define-struct species) (define-struct breed) (let ((my-dog (make-struct "Labrador"))) (test-assert (instance-of? my-dog )) (test-assert (not (instance-of? my-dog ))))) (test-group "error-handling" (define-struct ) (test-error #t (make-struct 1))) (test-group "json-serialization" (define-struct x y) (define-struct name age active) (define-struct ) (let ((p (make-struct 10 20))) (test-equal (struct->json-string p) "{\"x\":10,\"y\":20}")) (let ((p (make-struct 10 20))) (test-equal p (json-string->struct (record-type-descriptor p) (struct->json-string p)))) (let ((u (make-struct "Bob" 42 #t))) (test-equal (struct->json-string u) "{\"name\":\"Bob\",\"age\":42,\"active\":true}")) (let ((u (make-struct "Bob" 42 #t))) (test-equal u (json-string->struct (record-type-descriptor u) (struct->json-string u)))) (let ((e (make-struct ))) (test-equal (struct->json-string e) "{}")) (let ((e (make-struct ))) (test-equal (json-string->struct (record-type-descriptor e) "{}") e)) (let ((u (make-struct "Alice" 30 #f))) (let* ((json (struct->json-string u)) (u2 (json-string->struct (record-type-descriptor u) json))) (test-equal u u2))) (let ((p (make-struct 1.5 2.5))) (test-equal (json-string->struct (record-type-descriptor p) (struct->json-string p)) p)) (let ((u (make-struct "" 0 #f))) (test-equal (json-string->struct (record-type-descriptor u) (struct->json-string u)) u)) (let* ((p1 (make-struct 1 2)) (p2 (make-struct 3 4)) (json1 (struct->json-string p1)) (json2 (struct->json-string p2))) (test-equal (json-string->struct (record-type-descriptor p1) json1) p1) (test-equal (json-string->struct (record-type-descriptor p2) json2) p2))) (test-end "weeb-struct")