1// Copyright 2015 Google Inc. All rights reserved.
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//     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,
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
15package proptools
16
17import (
18	"fmt"
19	"testing"
20)
21
22var typeEqualTestCases = []struct {
23	in1 interface{}
24	in2 interface{}
25	out bool
26}{
27	{
28		// Matching structs
29		in1: struct{ S1 string }{},
30		in2: struct{ S1 string }{},
31		out: true,
32	},
33	{
34		// Mismatching structs
35		in1: struct{ S1 string }{},
36		in2: struct{ S2 string }{},
37		out: false,
38	},
39	{
40		// Matching pointer to struct
41		in1: &struct{ S1 string }{},
42		in2: &struct{ S1 string }{},
43		out: true,
44	},
45	{
46		// Mismatching pointer to struct
47		in1: &struct{ S1 string }{},
48		in2: &struct{ S2 string }{},
49		out: false,
50	},
51	{
52		// Matching embedded structs
53		in1: struct{ S struct{ S1 string } }{},
54		in2: struct{ S struct{ S1 string } }{},
55		out: true,
56	},
57	{
58		// Misatching embedded structs
59		in1: struct{ S struct{ S1 string } }{},
60		in2: struct{ S struct{ S2 string } }{},
61		out: false,
62	},
63	{
64		// Matching embedded pointer to struct
65		in1: &struct{ S *struct{ S1 string } }{S: &struct{ S1 string }{}},
66		in2: &struct{ S *struct{ S1 string } }{S: &struct{ S1 string }{}},
67		out: true,
68	},
69	{
70		// Mismatching embedded pointer to struct
71		in1: &struct{ S *struct{ S1 string } }{S: &struct{ S1 string }{}},
72		in2: &struct{ S *struct{ S2 string } }{S: &struct{ S2 string }{}},
73		out: false,
74	},
75	{
76		// Matching embedded nil pointer to struct
77		in1: &struct{ S *struct{ S1 string } }{},
78		in2: &struct{ S *struct{ S1 string } }{},
79		out: true,
80	},
81	{
82		// Mismatching embedded nil pointer to struct
83		in1: &struct{ S *struct{ S1 string } }{},
84		in2: &struct{ S *struct{ S2 string } }{},
85		out: false,
86	},
87	{
88		// Mismatching nilitude embedded  pointer to struct
89		in1: &struct{ S *struct{ S1 string } }{S: &struct{ S1 string }{}},
90		in2: &struct{ S *struct{ S1 string } }{},
91		out: true,
92	},
93	{
94		// Matching embedded interface to pointer to struct
95		in1: &struct{ S interface{} }{S: &struct{ S1 string }{}},
96		in2: &struct{ S interface{} }{S: &struct{ S1 string }{}},
97		out: true,
98	},
99	{
100		// Mismatching embedded interface to pointer to struct
101		in1: &struct{ S interface{} }{S: &struct{ S1 string }{}},
102		in2: &struct{ S interface{} }{S: &struct{ S2 string }{}},
103		out: false,
104	},
105	{
106		// Matching embedded nil interface to pointer to struct
107		in1: &struct{ S interface{} }{},
108		in2: &struct{ S interface{} }{},
109		out: true,
110	},
111	{
112		// Mismatching nilitude embedded  interface to pointer to struct
113		in1: &struct{ S interface{} }{S: &struct{ S1 string }{}},
114		in2: &struct{ S interface{} }{},
115		out: false,
116	},
117	{
118		// Matching pointer to non-struct
119		in1: struct{ S1 *string }{S1: StringPtr("test1")},
120		in2: struct{ S1 *string }{S1: StringPtr("test2")},
121		out: true,
122	},
123	{
124		// Matching nilitude pointer to non-struct
125		in1: struct{ S1 *string }{S1: StringPtr("test1")},
126		in2: struct{ S1 *string }{},
127		out: true,
128	},
129	{
130		// Mismatching pointer to non-struct
131		in1: struct{ S1 *string }{},
132		in2: struct{ S2 *string }{},
133		out: false,
134	},
135}
136
137func TestTypeEqualProperties(t *testing.T) {
138	for _, testCase := range typeEqualTestCases {
139		testString := fmt.Sprintf("%#v, %#v -> %t", testCase.in1, testCase.in2, testCase.out)
140
141		got := TypeEqual(testCase.in1, testCase.in2)
142
143		if got != testCase.out {
144			t.Errorf("test case: %s", testString)
145			t.Errorf("incorrect output")
146			t.Errorf("  expected: %t", testCase.out)
147			t.Errorf("       got: %t", got)
148		}
149	}
150}
151