reply_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_test
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/garyburd/redigo/redis"
  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. "strings([v1, v2])",
  35. ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  36. ve([]string{"v1", "v2"}, nil),
  37. },
  38. {
  39. "strings(nil)",
  40. ve(redis.Strings(nil, nil)),
  41. ve([]string(nil), redis.ErrNil),
  42. },
  43. {
  44. "values([v1, v2])",
  45. ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  46. ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
  47. },
  48. {
  49. "values(nil)",
  50. ve(redis.Values(nil, nil)),
  51. ve([]interface{}(nil), redis.ErrNil),
  52. },
  53. {
  54. "float64(1.0)",
  55. ve(redis.Float64([]byte("1.0"), nil)),
  56. ve(float64(1.0), nil),
  57. },
  58. {
  59. "float64(nil)",
  60. ve(redis.Float64(nil, nil)),
  61. ve(float64(0.0), redis.ErrNil),
  62. },
  63. }
  64. func TestReply(t *testing.T) {
  65. for _, rt := range replyTests {
  66. if rt.actual.err != rt.expected.err {
  67. t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
  68. continue
  69. }
  70. if !reflect.DeepEqual(rt.actual.v, rt.expected.v) {
  71. t.Errorf("%s=%+v, want %+v", rt.name, rt.actual.v, rt.expected.v)
  72. }
  73. }
  74. }
  75. // dial wraps DialTestDB() with a more suitable function name for examples.
  76. func dial() (redis.Conn, error) {
  77. return redis.DialTestDB()
  78. }
  79. func ExampleBool() {
  80. c, err := dial()
  81. if err != nil {
  82. panic(err)
  83. }
  84. defer c.Close()
  85. c.Do("SET", "foo", 1)
  86. exists, _ := redis.Bool(c.Do("EXISTS", "foo"))
  87. fmt.Printf("%#v\n", exists)
  88. // Output:
  89. // true
  90. }
  91. func ExampleInt() {
  92. c, err := dial()
  93. if err != nil {
  94. panic(err)
  95. }
  96. defer c.Close()
  97. c.Do("SET", "k1", 1)
  98. n, _ := redis.Int(c.Do("GET", "k1"))
  99. fmt.Printf("%#v\n", n)
  100. n, _ = redis.Int(c.Do("INCR", "k1"))
  101. fmt.Printf("%#v\n", n)
  102. // Output:
  103. // 1
  104. // 2
  105. }
  106. func ExampleString() {
  107. c, err := dial()
  108. if err != nil {
  109. panic(err)
  110. }
  111. defer c.Close()
  112. c.Do("SET", "hello", "world")
  113. s, err := redis.String(c.Do("GET", "hello"))
  114. fmt.Printf("%#v\n", s)
  115. // Output:
  116. // "world"
  117. }