blob: 62d9c3911733de4b2f5093a8a8dacef3eef0d869 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package format
18
19import (
20 "fmt"
21 "strings"
22 "testing"
23)
24
25type TestSubStructure struct {
26 Value string
27}
28
29type TestStructure struct {
30 Field1 string
31 Field2 *string
32 Field3 bool
33 Field4 int
34 Field5 []string
35 Field6 [][]string
36 Field7 TestSubStructure
37 Field8 []TestSubStructure
38 Field9 *TestSubStructure
39}
40
41func generateTestData(rows int) []TestStructure {
42 data := make([]TestStructure, rows)
43
44 abc := "abc"
45 for i := 0; i < rows; i += 1 {
46 data[i].Field1 = fmt.Sprintf("0x%05x", i)
47 data[i].Field2 = &abc
48 if i%2 == 0 {
49 data[i].Field3 = true
50 }
51 data[i].Field4 = i
52 data[i].Field5 = []string{"a", "b", "c", "d"}
53 data[i].Field6 = [][]string{{"x", "y", "z"}}
54 data[i].Field7.Value = "abc"
55 data[i].Field8 = []TestSubStructure{{Value: "abc"}}
56 data[i].Field9 = &TestSubStructure{Value: "abc"}
57 }
58 return data
59}
60
61func TestTableFormat(t *testing.T) {
62 expected := "" +
63 "FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6 VALUE FIELD8 FIELD9\n" +
64 "0x00000 abc true 0 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
65 "0x00001 abc false 1 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
66 "0x00002 abc true 2 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
67 "0x00003 abc false 3 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
68 "0x00004 abc true 4 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
69 "0x00005 abc false 5 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
70 "0x00006 abc true 6 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
71 "0x00007 abc false 7 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
72 "0x00008 abc true 8 [a b c d] [[x y z]] abc [{abc}] {abc}\n" +
73 "0x00009 abc false 9 [a b c d] [[x y z]] abc [{abc}] {abc}\n"
74 got := &strings.Builder{}
75 format := Format("table{{.Field1}}\t{{.Field2}}\t{{.Field3}}\t{{.Field4}}\t{{.Field5}}\t{{.Field6}}\t{{.Field7.Value}}\t{{.Field8}}\t{{.Field9}}")
76 data := generateTestData(10)
77 err := format.Execute(got, true, data)
78 if err != nil {
79 t.Errorf("%s: unexpected error result: %s", t.Name(), err)
80 }
81 if got.String() != expected {
82 t.Logf("RECEIVED:\n%s\n", got.String())
83 t.Logf("EXPECTED:\n%s\n", expected)
84 t.Errorf("%s: expected and received did not match", t.Name())
85 }
86}
87
88func TestNoTableFormat(t *testing.T) {
89 expected := "" +
90 "0x00000,abc,true,0,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
91 "0x00001,abc,false,1,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
92 "0x00002,abc,true,2,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
93 "0x00003,abc,false,3,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
94 "0x00004,abc,true,4,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
95 "0x00005,abc,false,5,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
96 "0x00006,abc,true,6,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
97 "0x00007,abc,false,7,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
98 "0x00008,abc,true,8,[a b c d],[[x y z]],abc,[{abc}],{abc}\n" +
99 "0x00009,abc,false,9,[a b c d],[[x y z]],abc,[{abc}],{abc}\n"
100 got := &strings.Builder{}
101 format := Format("{{.Field1}},{{.Field2}},{{.Field3}},{{.Field4}},{{.Field5}},{{.Field6}},{{.Field7.Value}},{{.Field8}},{{.Field9}}")
102 data := generateTestData(10)
103 err := format.Execute(got, false, data)
104 if err != nil {
105 t.Errorf("%s: unexpected error result: %s", t.Name(), err)
106 }
107 if got.String() != expected {
108 t.Logf("RECEIVED:\n%s\n", got.String())
109 t.Logf("EXPECTED:\n%s\n", expected)
110 t.Errorf("%s: expected and received did not match", t.Name())
111 }
112}
113
114func TestTableSingleFormat(t *testing.T) {
115 expected := "" +
116 "FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6 VALUE FIELD8 FIELD9\n" +
117 "0x00000 abc true 0 [a b c d] [[x y z]] abc [{abc}] {abc}\n"
118 got := &strings.Builder{}
119 format := Format("table{{.Field1}}\t{{.Field2}}\t{{.Field3}}\t{{.Field4}}\t{{.Field5}}\t{{.Field6}}\t{{.Field7.Value}}\t{{.Field8}}\t{{.Field9}}")
120 data := generateTestData(1)
121 err := format.Execute(got, true, data[0])
122 if err != nil {
123 t.Errorf("%s: unexpected error result: %s", t.Name(), err)
124 }
125 if got.String() != expected {
126 t.Logf("RECEIVED:\n%s\n", got.String())
127 t.Logf("EXPECTED:\n%s\n", expected)
128 t.Errorf("%s: expected and received did not match", t.Name())
129 }
130}
131
132func TestNoTableSingleFormat(t *testing.T) {
133 expected := "0x00000,abc,true,0,[a b c d],[[x y z]],abc,[{abc}],{abc}\n"
134 got := &strings.Builder{}
135 format := Format("{{.Field1}},{{.Field2}},{{.Field3}},{{.Field4}},{{.Field5}},{{.Field6}},{{.Field7.Value}},{{.Field8}},{{.Field9}}")
136 data := generateTestData(1)
137 err := format.Execute(got, false, data[0])
138 if err != nil {
139 t.Errorf("%s: unexpected error result: %s", t.Name(), err)
140 }
141 if got.String() != expected {
142 t.Logf("RECEIVED:\n%s\n", got.String())
143 t.Logf("EXPECTED:\n%s\n", expected)
144 t.Errorf("%s: expected and received did not match", t.Name())
145 }
146}
147
148func TestBadFormat(t *testing.T) {
149 format := Format("table{{.Field1}\t{{.Field2}}\t{{.Field3}}\t{{.Field4}}\t{{.Field5}}\t{{.Field6}}\t{{.Field7.Value}}\t{{.Field8}}\t{{.Field9}}")
150 got := &strings.Builder{}
151 data := generateTestData(10)
152 err := format.Execute(got, true, data)
153 if err == nil {
154 t.Errorf("%s: expected error (bad format) got none", t.Name())
155 }
156}