OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // adapted from github.com/golang/appengine/datastore | 5 // adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package datastore | 7 package datastore |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | |
10 "strings" | 11 "strings" |
11 "testing" | 12 "testing" |
12 | 13 |
13 . "github.com/smartystreets/goconvey/convey" | 14 . "github.com/smartystreets/goconvey/convey" |
15 "gopkg.in/yaml.v2" | |
14 ) | 16 ) |
15 | 17 |
16 var indexDefinitionTests = []struct { | 18 var indexDefinitionTests = []struct { |
17 » id *IndexDefinition | 19 » id *IndexDefinition |
18 » builtin bool | 20 » builtin bool |
19 » compound bool | 21 » compound bool |
20 » str string | 22 » str string |
21 » yaml []string | 23 » yaml []string |
24 » serialized []string // without the leading hyphen, for instance | |
iannucci
2016/01/13 19:12:20
I don't understand this comment
nishanths (utexas)
2016/01/14 21:12:56
My bad. Meant: not inside an array (YAML arrays ha
| |
22 }{ | 25 }{ |
23 { | 26 { |
24 id: &IndexDefinition{Kind: "kind"}, | 27 id: &IndexDefinition{Kind: "kind"}, |
25 builtin: true, | 28 builtin: true, |
26 str: "B:kind", | 29 str: "B:kind", |
27 }, | 30 }, |
28 | 31 |
29 { | 32 { |
30 id: &IndexDefinition{ | 33 id: &IndexDefinition{ |
31 Kind: "kind", | 34 Kind: "kind", |
(...skipping 24 matching lines...) Expand all Loading... | |
56 compound: true, | 59 compound: true, |
57 str: "C:Kind|A/prop/-other", | 60 str: "C:Kind|A/prop/-other", |
58 yaml: []string{ | 61 yaml: []string{ |
59 "- kind: Kind", | 62 "- kind: Kind", |
60 " ancestor: yes", | 63 " ancestor: yes", |
61 " properties:", | 64 " properties:", |
62 " - name: prop", | 65 " - name: prop", |
63 " - name: other", | 66 " - name: other", |
64 " direction: desc", | 67 " direction: desc", |
65 }, | 68 }, |
69 serialized: []string{ | |
70 " kind: Kind", | |
dnj
2016/01/13 16:16:40
You should be able to avoid repeating "yaml" by Ma
nishanths (utexas)
2016/01/14 21:12:56
Thanks! Much cleaner. :)
| |
71 " ancestor: yes", | |
72 " properties:", | |
73 " - name: prop", | |
74 " - name: other", | |
75 " direction: desc", | |
76 }, | |
66 }, | 77 }, |
67 } | 78 } |
68 | 79 |
69 func TestIndexDefinition(t *testing.T) { | 80 func TestIndexDefinition(t *testing.T) { |
70 t.Parallel() | 81 t.Parallel() |
71 | 82 |
72 Convey("Test IndexDefinition", t, func() { | 83 Convey("Test IndexDefinition", t, func() { |
73 for _, tc := range indexDefinitionTests { | 84 for _, tc := range indexDefinitionTests { |
74 Convey(tc.str, func() { | 85 Convey(tc.str, func() { |
75 So(tc.id.String(), ShouldEqual, tc.str) | 86 So(tc.id.String(), ShouldEqual, tc.str) |
76 So(tc.id.Builtin(), ShouldEqual, tc.builtin) | 87 So(tc.id.Builtin(), ShouldEqual, tc.builtin) |
77 So(tc.id.Compound(), ShouldEqual, tc.compound) | 88 So(tc.id.Compound(), ShouldEqual, tc.compound) |
78 yaml, _ := tc.id.YAMLString() | 89 yaml, _ := tc.id.YAMLString() |
79 So(yaml, ShouldEqual, strings.Join(tc.yaml, "\n" )) | 90 So(yaml, ShouldEqual, strings.Join(tc.yaml, "\n" )) |
80 }) | 91 }) |
81 } | 92 } |
82 }) | 93 }) |
94 | |
95 Convey("Test MarshalYAML/UnmarshalYAML", t, func() { | |
96 for _, tc := range indexDefinitionTests { | |
97 Convey(fmt.Sprintf("serializable index definition `%s` i s marshaled and unmarshaled correctly", tc.str), func() { | |
98 if tc.serialized != nil { | |
99 // marshal | |
100 _, err := yaml.Marshal(tc.id) | |
101 So(err, ShouldBeNil) | |
102 | |
103 // unmarshal | |
104 var id IndexDefinition | |
105 yaml.Unmarshal([]byte(strings.Join(tc.se rialized, "\n")), &id) | |
106 So(&id, ShouldResemble, tc.id) | |
107 } | |
108 }) | |
109 } | |
110 }) | |
83 } | 111 } |
OLD | NEW |