gitee.com/go-spring2/spring-base@v1.1.3/cache/result_test.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cache_test 18 19 import ( 20 "testing" 21 22 "gitee.com/go-spring2/spring-base/assert" 23 "gitee.com/go-spring2/spring-base/cache" 24 ) 25 26 func TestValueResult(t *testing.T) { 27 28 t.Run("", func(t *testing.T) { 29 r := cache.NewValueResult(map[string]string{ 30 "a": "123", 31 }) 32 b, err := r.JSON() 33 assert.Nil(t, err) 34 assert.Equal(t, b, `{"a":"123"}`) 35 var m map[string]string 36 err = r.Load(m) 37 assert.Error(t, err, "value should be ptr and not nil") 38 err = r.Load(&m) 39 assert.Nil(t, err) 40 assert.Equal(t, m, map[string]string{ 41 "a": "123", 42 }) 43 var s []string 44 err = r.Load(&s) 45 assert.Error(t, err, "load type \\(\\[\\]string\\) but expect type \\(map\\[string\\]string\\)") 46 }) 47 48 t.Run("", func(t *testing.T) { 49 r := cache.NewValueResult([]string{ 50 "abc", 51 }) 52 b, err := r.JSON() 53 assert.Nil(t, err) 54 assert.Equal(t, b, `["abc"]`) 55 var s []string 56 err = r.Load(&s) 57 assert.Nil(t, err) 58 assert.Equal(t, s, []string{ 59 "abc", 60 }) 61 }) 62 63 t.Run("", func(t *testing.T) { 64 r := cache.NewValueResult(complex(1.0, 0.5)) 65 _, err := r.JSON() 66 assert.Error(t, err, "json: unsupported type: complex128") 67 }) 68 } 69 70 func TestJSONResult(t *testing.T) { 71 72 t.Run("", func(t *testing.T) { 73 r := cache.NewJSONResult(`{"a":"123"}`) 74 b, err := r.JSON() 75 assert.Nil(t, err) 76 assert.Equal(t, b, `{"a":"123"}`) 77 var m map[string]string 78 err = r.Load(m) 79 assert.Error(t, err, "json: Unmarshal\\(non-pointer map\\[string\\]string\\)") 80 err = r.Load(&m) 81 assert.Nil(t, err) 82 assert.Equal(t, m, map[string]string{ 83 "a": "123", 84 }) 85 var s []string 86 err = r.Load(&s) 87 assert.Error(t, err, "json: cannot unmarshal object into Go value of type \\[\\]string") 88 }) 89 90 t.Run("", func(t *testing.T) { 91 r := cache.NewJSONResult(`["abc"]`) 92 b, err := r.JSON() 93 assert.Nil(t, err) 94 assert.Equal(t, b, `["abc"]`) 95 var s []string 96 err = r.Load(&s) 97 assert.Nil(t, err) 98 assert.Equal(t, s, []string{ 99 "abc", 100 }) 101 }) 102 }