| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://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, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package memdocstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
|
| 21 |
"github.com/google/go-cmp/cmp" |
| 22 |
"gocloud.dev/docstore/drivertest" |
| 23 |
) |
| 24 |
|
| 25 |
type aStruct struct { |
| 26 |
X int |
| 27 |
embed |
| 28 |
Z *bool |
| 29 |
W uint |
| 30 |
T time.Time |
| 31 |
L []int |
| 32 |
F float32 |
| 33 |
B []byte |
| 34 |
} |
| 35 |
|
| 36 |
type embed struct { |
| 37 |
Y string |
| 38 |
} |
| 39 |
|
| 40 |
func TestEncodeDoc(t *testing.T) { |
| 41 |
var b bool = true |
| 42 |
tm := time.Now() |
| 43 |
for _, test := range []struct { |
| 44 |
in any |
| 45 |
want storedDoc |
| 46 |
}{ |
| 47 |
{ |
| 48 |
in: map[string]any{ |
| 49 |
"x": map[int]any{ |
| 50 |
1: "a", |
| 51 |
2: 17, |
| 52 |
3: []float32{1.0, 2.5}, |
| 53 |
4: map[string]bool{"false": false, "true": true}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
want: storedDoc{ |
| 57 |
"x": map[string]any{ |
| 58 |
"1": "a", |
| 59 |
"2": int64(17), |
| 60 |
"3": []any{float64(1.0), float64(2.5)}, |
| 61 |
"4": map[string]any{"false": false, "true": true}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
in: &aStruct{ |
| 67 |
X: 3, |
| 68 |
embed: embed{Y: "y"}, |
| 69 |
Z: &b, |
| 70 |
W: 33, |
| 71 |
T: tm, |
| 72 |
L: []int{4, 5}, |
| 73 |
F: 2.5, |
| 74 |
B: []byte("abc"), |
| 75 |
}, |
| 76 |
want: storedDoc{ |
| 77 |
"X": int64(3), |
| 78 |
"Y": "y", |
| 79 |
"Z": true, |
| 80 |
"W": int64(33), |
| 81 |
"T": tm, |
| 82 |
"L": []any{int64(4), int64(5)}, |
| 83 |
"F": 2.5, |
| 84 |
"B": []byte("abc"), |
| 85 |
}, |
| 86 |
}, |
| 87 |
} { |
| 88 |
doc := drivertest.MustDocument(test.in) |
| 89 |
got, err := encodeDoc(doc) |
| 90 |
if err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
if diff := cmp.Diff(got, test.want); diff != "" { |
| 94 |
t.Errorf("%+v: %s", test.in, diff) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestDecodeDoc(t *testing.T) { |
| 100 |
var b bool = true |
| 101 |
tm := time.Now() |
| 102 |
for _, test := range []struct { |
| 103 |
in storedDoc |
| 104 |
val any |
| 105 |
want any |
| 106 |
}{ |
| 107 |
{ |
| 108 |
storedDoc{ |
| 109 |
"x": map[string]any{ |
| 110 |
"1": "a", |
| 111 |
"2": int64(17), |
| 112 |
"3": []any{float64(1.0), float64(2.5)}, |
| 113 |
"4": map[string]any{"false": false, "true": true}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
map[string]any{}, |
| 117 |
map[string]any{ |
| 118 |
"x": map[string]any{ |
| 119 |
"1": "a", |
| 120 |
"2": int64(17), |
| 121 |
"3": []any{1.0, 2.5}, |
| 122 |
"4": map[string]any{"false": false, "true": true}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
storedDoc{ |
| 128 |
"X": int64(3), |
| 129 |
"Y": "y", |
| 130 |
"Z": true, |
| 131 |
"W": int64(33), |
| 132 |
"T": tm, |
| 133 |
"L": []any{int64(4), int64(5)}, |
| 134 |
"F": 2.5, |
| 135 |
"B": []byte("abc"), |
| 136 |
}, |
| 137 |
&aStruct{}, |
| 138 |
&aStruct{ |
| 139 |
X: 3, |
| 140 |
embed: embed{Y: "y"}, |
| 141 |
Z: &b, |
| 142 |
W: 33, |
| 143 |
T: tm, |
| 144 |
L: []int{4, 5}, |
| 145 |
F: 2.5, |
| 146 |
B: []byte("abc"), |
| 147 |
}, |
| 148 |
}, |
| 149 |
} { |
| 150 |
got := test.val |
| 151 |
doc := drivertest.MustDocument(test.val) |
| 152 |
if err := decodeDoc(test.in, doc, nil); err != nil { |
| 153 |
t.Fatal(err) |
| 154 |
} |
| 155 |
if diff := cmp.Diff(got, test.want, cmp.AllowUnexported(aStruct{})); diff != "" { |
| 156 |
t.Errorf("%+v: %s", test.in, diff) |
| 157 |
} |
| 158 |
} |
| 159 |
} |