1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package tests
21
22import (
23	"testing"
24
25	"github.com/apache/thrift/lib/go/test/gopath/src/constoptionalfielda"
26	"github.com/apache/thrift/lib/go/test/gopath/src/constoptionalfieldb"
27)
28
29func TestConstOptionalField(t *testing.T) {
30	c := constoptionalfieldb.CONSTANTS[0]
31
32	t.Run("foo", func(t *testing.T) {
33		const expected = constoptionalfielda.Foo_One
34		if *c.OptFoo != expected {
35			t.Errorf("Expected %v, got %v", expected, *c.OptFoo)
36		}
37		if *c.AFoo != constoptionalfielda.TypedefAFoo(expected) {
38			t.Errorf("Typedef a expected %v, got %v", expected, *c.AFoo)
39		}
40		if *c.BFoo != constoptionalfieldb.TypedefBFoo(expected) {
41			t.Errorf("Typedef b expected %v, got %v", expected, *c.BFoo)
42		}
43	})
44
45	t.Run("bool", func(t *testing.T) {
46		const expected = true
47		if *c.OptBool != expected {
48			t.Errorf("Expected %v, got %v", expected, *c.OptBool)
49		}
50		if *c.ABool != constoptionalfielda.TypedefABool(expected) {
51			t.Errorf("Typedef a expected %v, got %v", expected, *c.ABool)
52		}
53		if *c.BBool != constoptionalfieldb.TypedefBBool(expected) {
54			t.Errorf("Typedef b expected %v, got %v", expected, *c.BBool)
55		}
56	})
57
58	t.Run("i8", func(t *testing.T) {
59		const expected = 8
60		if *c.OptI8 != expected {
61			t.Errorf("Expected %v, got %v", expected, *c.OptI8)
62		}
63		if *c.AI8 != constoptionalfielda.TypedefAI8(expected) {
64			t.Errorf("Typedef a expected %v, got %v", expected, *c.AI8)
65		}
66		if *c.BI8 != constoptionalfieldb.TypedefBI8(expected) {
67			t.Errorf("Typedef b expected %v, got %v", expected, *c.BI8)
68		}
69	})
70
71	t.Run("i16", func(t *testing.T) {
72		const expected = 16
73		if *c.OptI16 != expected {
74			t.Errorf("Expected %v, got %v", expected, *c.OptI16)
75		}
76		if *c.AI16 != constoptionalfielda.TypedefAI16(expected) {
77			t.Errorf("Typedef a expected %v, got %v", expected, *c.AI16)
78		}
79		if *c.BI16 != constoptionalfieldb.TypedefBI16(expected) {
80			t.Errorf("Typedef b expected %v, got %v", expected, *c.BI16)
81		}
82	})
83
84	t.Run("i32", func(t *testing.T) {
85		const expected = 32
86		if *c.OptI32 != expected {
87			t.Errorf("Expected %v, got %v", expected, *c.OptI32)
88		}
89		if *c.AI32 != constoptionalfielda.TypedefAI32(expected) {
90			t.Errorf("Typedef a expected %v, got %v", expected, *c.AI32)
91		}
92		if *c.BI32 != constoptionalfieldb.TypedefBI32(expected) {
93			t.Errorf("Typedef b expected %v, got %v", expected, *c.BI32)
94		}
95	})
96
97	t.Run("i64", func(t *testing.T) {
98		const expected = 64
99		if *c.OptI64 != expected {
100			t.Errorf("Expected %v, got %v", expected, *c.OptI64)
101		}
102		if *c.AI64 != constoptionalfielda.TypedefAI64(expected) {
103			t.Errorf("Typedef a expected %v, got %v", expected, *c.AI64)
104		}
105		if *c.BI64 != constoptionalfieldb.TypedefBI64(expected) {
106			t.Errorf("Typedef b expected %v, got %v", expected, *c.BI64)
107		}
108	})
109
110	t.Run("double", func(t *testing.T) {
111		// To avoid the annoyance of comparing float numbers,
112		// we convert all floats to int in this test.
113		const expected = 1234
114		if int(*c.OptDouble) != expected {
115			t.Errorf("Expected %v, got %v", expected, *c.OptDouble)
116		}
117		if int(*c.ADouble) != expected {
118			t.Errorf("Typedef a expected %v, got %v", expected, *c.ADouble)
119		}
120		if int(*c.BDouble) != expected {
121			t.Errorf("Typedef b expected %v, got %v", expected, *c.BDouble)
122		}
123	})
124
125	t.Run("string", func(t *testing.T) {
126		const expected = "string"
127		if *c.OptString != expected {
128			t.Errorf("Expected %q, got %q", expected, *c.OptString)
129		}
130		if *c.AString != constoptionalfielda.TypedefAString(expected) {
131			t.Errorf("Typedef a expected %q, got %q", expected, *c.AString)
132		}
133		if *c.BString != constoptionalfieldb.TypedefBString(expected) {
134			t.Errorf("Typedef b expected %q, got %q", expected, *c.BString)
135		}
136	})
137
138	t.Run("binary", func(t *testing.T) {
139		const expected = "binary"
140		if string(c.OptBinary) != expected {
141			t.Errorf("Expected %q, got %q", expected, c.OptBinary)
142		}
143		if string(c.ABinary) != expected {
144			t.Errorf("Typedef a expected %q, got %q", expected, c.ABinary)
145		}
146		if string(c.BBinary) != expected {
147			t.Errorf("Typedef b expected %q, got %q", expected, c.BBinary)
148		}
149	})
150}
151