aboutsummaryrefslogtreecommitdiff
path: root/tests/struct.scm
blob: 4eef204c2b896f48dbf9169b6d81b7caf86d2cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;;; Weeb --- Web microframework for GNU Guile
;;; Copyright © 2026 Jakob Kreuze <zerodaysfordays@sdf.org>
;;;
;;; 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 <https://www.gnu.org/licenses/>.

(use-modules (srfi srfi-64)
             (weeb struct))

(test-runner-current (test-runner-simple))

(test-begin "weeb-struct")

(test-group "instantiation"
  (define-struct <point> x y)
  (let ((p (make-struct <point> 10 20)))
    (test-eqv 10 ((record-accessor <point> 'x) p))
    (test-eqv 20 ((record-accessor <point> 'y) p))))

(test-group "with-accessors"
  (define-struct <person> name age)
  (let ((p (make-struct <person> "Alice" 30)))
    (with-accessors (p name age)
      (test-eqv "Alice" name)
      (test-eqv 30 age))))

(test-group "call-with-slots"
  (define-struct <coord> x y)
  (let ((c (make-struct <coord> 5 15)))
    (call-with-slots c
      (lambda (x y)
        (test-eqv 5 x)
        (test-eqv 15 y)))))

(test-group "instance-of?"
  (define-struct <animal> species)
  (define-struct <dog> breed)
  (let ((my-dog (make-struct <dog> "Labrador")))
    (test-assert (instance-of? my-dog <dog>))
    (test-assert (not (instance-of? my-dog <animal>)))))

(test-group "error-handling"
  (define-struct <empty>)
  (test-error #t (make-struct <empty> 1)))

(test-group "json-serialization"
  (define-struct <point> x y)
  (define-struct <user> name age active)
  (define-struct <empty>)

  (let ((p (make-struct <point> 10 20)))
    (test-equal (struct->json-string p) "{\"x\":10,\"y\":20}"))

  (let ((p (make-struct <point> 10 20)))
    (test-equal p (json-string->struct (record-type-descriptor p) (struct->json-string p))))

  (let ((u (make-struct <user> "Bob" 42 #t)))
    (test-equal (struct->json-string u) "{\"name\":\"Bob\",\"age\":42,\"active\":true}"))

  (let ((u (make-struct <user> "Bob" 42 #t)))
    (test-equal u (json-string->struct (record-type-descriptor u) (struct->json-string u))))

  (let ((e (make-struct <empty>)))
    (test-equal (struct->json-string e) "{}"))

  (let ((e (make-struct <empty>)))
    (test-equal (json-string->struct (record-type-descriptor e) "{}") e))

  (let ((u (make-struct <user> "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 <point> 1.5 2.5)))
    (test-equal (json-string->struct (record-type-descriptor p) (struct->json-string p)) p))

  (let ((u (make-struct <user> "" 0 #f)))
    (test-equal (json-string->struct (record-type-descriptor u) (struct->json-string u)) u))

  (let* ((p1 (make-struct <point> 1 2))
         (p2 (make-struct <point> 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")

© 2015 - 2026 Jakob L. Kreuze