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