reply_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/pkg/errors"
  20. )
  21. type valueError struct {
  22. v interface{}
  23. err error
  24. }
  25. func ve(v interface{}, err error) valueError {
  26. return valueError{v, err}
  27. }
  28. var replyTests = []struct {
  29. name interface{}
  30. actual valueError
  31. expected valueError
  32. }{
  33. {
  34. "ints([v1, v2])",
  35. ve(Ints([]interface{}{[]byte("4"), []byte("5")}, nil)),
  36. ve([]int{4, 5}, nil),
  37. },
  38. {
  39. "ints(nil)",
  40. ve(Ints(nil, nil)),
  41. ve([]int(nil), ErrNil),
  42. },
  43. {
  44. "strings([v1, v2])",
  45. ve(Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  46. ve([]string{"v1", "v2"}, nil),
  47. },
  48. {
  49. "strings(nil)",
  50. ve(Strings(nil, nil)),
  51. ve([]string(nil), ErrNil),
  52. },
  53. {
  54. "byteslices([v1, v2])",
  55. ve(ByteSlices([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  56. ve([][]byte{[]byte("v1"), []byte("v2")}, nil),
  57. },
  58. {
  59. "byteslices(nil)",
  60. ve(ByteSlices(nil, nil)),
  61. ve([][]byte(nil), ErrNil),
  62. },
  63. {
  64. "values([v1, v2])",
  65. ve(Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  66. ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
  67. },
  68. {
  69. "values(nil)",
  70. ve(Values(nil, nil)),
  71. ve([]interface{}(nil), ErrNil),
  72. },
  73. {
  74. "float64(1.0)",
  75. ve(Float64([]byte("1.0"), nil)),
  76. ve(float64(1.0), nil),
  77. },
  78. {
  79. "float64(nil)",
  80. ve(Float64(nil, nil)),
  81. ve(float64(0.0), ErrNil),
  82. },
  83. {
  84. "uint64(1)",
  85. ve(Uint64(int64(1), nil)),
  86. ve(uint64(1), nil),
  87. },
  88. {
  89. "uint64(-1)",
  90. ve(Uint64(int64(-1), nil)),
  91. ve(uint64(0), ErrNegativeInt),
  92. },
  93. }
  94. func TestReply(t *testing.T) {
  95. for _, rt := range replyTests {
  96. if errors.Cause(rt.actual.err) != rt.expected.err {
  97. t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
  98. continue
  99. }
  100. if !reflect.DeepEqual(rt.actual.v, rt.expected.v) {
  101. t.Errorf("%s=%+v, want %+v", rt.name, rt.actual.v, rt.expected.v)
  102. }
  103. }
  104. }
  105. // dial wraps DialDefaultServer() with a more suitable function name for examples.
  106. func dial() (Conn, error) {
  107. return DialDefaultServer()
  108. }
  109. func ExampleBool() {
  110. c, err := dial()
  111. if err != nil {
  112. fmt.Println(err)
  113. return
  114. }
  115. defer c.Close()
  116. c.Do("SET", "foo", 1)
  117. exists, _ := Bool(c.Do("EXISTS", "foo"))
  118. fmt.Printf("%#v\n", exists)
  119. // Output:
  120. // true
  121. }
  122. func ExampleInt() {
  123. c, err := dial()
  124. if err != nil {
  125. fmt.Println(err)
  126. return
  127. }
  128. defer c.Close()
  129. c.Do("SET", "k1", 1)
  130. n, _ := Int(c.Do("GET", "k1"))
  131. fmt.Printf("%#v\n", n)
  132. n, _ = Int(c.Do("INCR", "k1"))
  133. fmt.Printf("%#v\n", n)
  134. // Output:
  135. // 1
  136. // 2
  137. }
  138. func ExampleInts() {
  139. c, err := dial()
  140. if err != nil {
  141. fmt.Println(err)
  142. return
  143. }
  144. defer c.Close()
  145. c.Do("SADD", "set_with_integers", 4, 5, 6)
  146. ints, _ := Ints(c.Do("SMEMBERS", "set_with_integers"))
  147. fmt.Printf("%#v\n", ints)
  148. // Output:
  149. // []int{4, 5, 6}
  150. }
  151. func ExampleString() {
  152. c, err := dial()
  153. if err != nil {
  154. fmt.Println(err)
  155. return
  156. }
  157. defer c.Close()
  158. c.Do("SET", "hello", "world")
  159. s, err := String(c.Do("GET", "hello"))
  160. fmt.Printf("%#v\n", s)
  161. // Output:
  162. // "world"
  163. }