summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/unit/moduleapi/reply.tcl
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
commit5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch)
tree1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/tests/unit/moduleapi/reply.tcl
parentc7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff)
downloadcrep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/tests/unit/moduleapi/reply.tcl')
-rw-r--r--examples/redis-unstable/tests/unit/moduleapi/reply.tcl152
1 files changed, 152 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/unit/moduleapi/reply.tcl b/examples/redis-unstable/tests/unit/moduleapi/reply.tcl
new file mode 100644
index 0000000..af012f2
--- /dev/null
+++ b/examples/redis-unstable/tests/unit/moduleapi/reply.tcl
@@ -0,0 +1,152 @@
1set testmodule [file normalize tests/modules/reply.so]
2
3start_server {tags {"modules external:skip"}} {
4 r module load $testmodule
5
6 # test all with hello 2/3
7 for {set proto 2} {$proto <= 3} {incr proto} {
8 if {[lsearch $::denytags "resp3"] >= 0} {
9 if {$proto == 3} {continue}
10 } elseif {$::force_resp3} {
11 if {$proto == 2} {continue}
12 }
13 r hello $proto
14
15 test "RESP$proto: RM_ReplyWithString: an string reply" {
16 # RedisString
17 set string [r rw.string "Redis"]
18 assert_equal "Redis" $string
19 # C string
20 set string [r rw.cstring]
21 assert_equal "A simple string" $string
22 }
23
24 test "RESP$proto: RM_ReplyWithBigNumber: an string reply" {
25 assert_equal "123456778901234567890" [r rw.bignumber "123456778901234567890"]
26 }
27
28 test "RESP$proto: RM_ReplyWithInt: an integer reply" {
29 assert_equal 42 [r rw.int 42]
30 }
31
32 test "RESP$proto: RM_ReplyWithDouble: a float reply" {
33 assert_equal 3.141 [r rw.double 3.141]
34 }
35
36 test "RESP$proto: RM_ReplyWithDouble: inf" {
37 if {$proto == 2} {
38 assert_equal "inf" [r rw.double inf]
39 assert_equal "-inf" [r rw.double -inf]
40 } else {
41 # TCL convert inf to different results on different platforms, e.g. inf on mac
42 # and Inf on others, so use readraw to verify the protocol
43 r readraw 1
44 assert_equal ",inf" [r rw.double inf]
45 assert_equal ",-inf" [r rw.double -inf]
46 r readraw 0
47 }
48 }
49
50 test "RESP$proto: RM_ReplyWithDouble: NaN" {
51 if {$proto == 2} {
52 assert_equal "nan" [r rw.double 0 0]
53 assert_equal "nan" [r rw.double]
54 } else {
55 # TCL won't convert nan into a double, use readraw to verify the protocol
56 r readraw 1
57 assert_equal ",nan" [r rw.double 0 0]
58 assert_equal ",nan" [r rw.double]
59 r readraw 0
60 }
61 }
62
63 set ld 0.00000000000000001
64 test "RESP$proto: RM_ReplyWithLongDouble: a float reply" {
65 if {$proto == 2} {
66 # here the response gets to TCL as a string
67 assert_equal $ld [r rw.longdouble $ld]
68 } else {
69 # TCL doesn't support long double and the test infra converts it to a
70 # normal double which causes precision loss. so we use readraw instead
71 r readraw 1
72 assert_equal ",$ld" [r rw.longdouble $ld]
73 r readraw 0
74 }
75 }
76
77 test "RESP$proto: RM_ReplyWithVerbatimString: a string reply" {
78 assert_equal "bla\nbla\nbla" [r rw.verbatim "bla\nbla\nbla"]
79 }
80
81 test "RESP$proto: RM_ReplyWithArray: an array reply" {
82 assert_equal {0 1 2 3 4} [r rw.array 5]
83 }
84
85 test "RESP$proto: RM_ReplyWithMap: an map reply" {
86 set res [r rw.map 3]
87 if {$proto == 2} {
88 assert_equal {0 0 1 1.5 2 3} $res
89 } else {
90 assert_equal [dict create 0 0.0 1 1.5 2 3.0] $res
91 }
92 }
93
94 test "RESP$proto: RM_ReplyWithSet: an set reply" {
95 assert_equal {0 1 2} [r rw.set 3]
96 }
97
98 test "RESP$proto: RM_ReplyWithAttribute: an set reply" {
99 if {$proto == 2} {
100 catch {[r rw.attribute 3]} e
101 assert_match "Attributes aren't supported by RESP 2" $e
102 } else {
103 r readraw 1
104 set res [r rw.attribute 3]
105 assert_equal [r read] {:0}
106 assert_equal [r read] {,0}
107 assert_equal [r read] {:1}
108 assert_equal [r read] {,1.5}
109 assert_equal [r read] {:2}
110 assert_equal [r read] {,3}
111 assert_equal [r read] {+OK}
112 r readraw 0
113 }
114 }
115
116 test "RESP$proto: RM_ReplyWithBool: a boolean reply" {
117 assert_equal {0 1} [r rw.bool]
118 }
119
120 test "RESP$proto: RM_ReplyWithNull: a NULL reply" {
121 assert_equal {} [r rw.null]
122 }
123
124 test "RESP$proto: RM_ReplyWithError: an error reply" {
125 catch {r rw.error} e
126 assert_match "An error" $e
127 }
128
129 test "RESP$proto: RM_ReplyWithErrorFormat: error format reply" {
130 catch {r rw.error_format "An error: %s" foo} e
131 assert_match "An error: foo" $e ;# Should not be used by a user, but compatible with RM_ReplyError
132
133 catch {r rw.error_format "-ERR An error: %s" foo2} e
134 assert_match "-ERR An error: foo2" $e ;# Should not be used by a user, but compatible with RM_ReplyError (There are two hyphens, TCL removes the first one)
135
136 catch {r rw.error_format "-WRONGTYPE A type error: %s" foo3} e
137 assert_match "-WRONGTYPE A type error: foo3" $e ;# Should not be used by a user, but compatible with RM_ReplyError (There are two hyphens, TCL removes the first one)
138
139 catch {r rw.error_format "ERR An error: %s" foo4} e
140 assert_match "ERR An error: foo4" $e
141
142 catch {r rw.error_format "WRONGTYPE A type error: %s" foo5} e
143 assert_match "WRONGTYPE A type error: foo5" $e
144 }
145
146 r hello 2
147 }
148
149 test "Unload the module - replywith" {
150 assert_equal {OK} [r module unload replywith]
151 }
152}