|
| 1 | +package templates |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPad(t *testing.T) { |
| 10 | + type args struct { |
| 11 | + width int |
| 12 | + stringIn string |
| 13 | + padString string |
| 14 | + } |
| 15 | + |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + args args |
| 19 | + want string |
| 20 | + }{ |
| 21 | + {"Pad empty string with space", args{10, "", ""}, " "}, |
| 22 | + {"Pad empty string with minus", args{10, "", "-"}, "----------"}, |
| 23 | + {"Pad with space", args{10, "test", ""}, " test "}, |
| 24 | + {"Pad with space 2", args{10, "hello", ""}, " hello "}, |
| 25 | + {"Pad with minus", args{10, "test", "-"}, "---test---"}, |
| 26 | + {"Pad with space and zero width", args{0, "test", ""}, "test"}, |
| 27 | + {"Pad with space and smaller width", args{3, "test", ""}, "test"}, |
| 28 | + {"Pad with space and same width", args{4, "test", ""}, "test"}, |
| 29 | + {"Pad wide charactors with space", args{10, "宽字符", ""}, " 宽字符 "}, |
| 30 | + {"Pad wide charactors with space 2", args{10, "宽字符A", ""}, " 宽字符A "}, |
| 31 | + {"Pad wide charactors with space and zero width", args{0, "宽字符", ""}, "宽字符"}, |
| 32 | + {"Pad wide charactors with space and smaller width", args{5, "宽字符", ""}, "宽字符"}, |
| 33 | + {"Pad wide charactors with space and same width", args{6, "宽字符", ""}, "宽字符"}, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + var got string |
| 39 | + if tt.args.stringIn == "" && tt.args.padString == "" { |
| 40 | + got = pad(tt.args.width) |
| 41 | + } else if tt.args.padString == "" { |
| 42 | + got = pad(tt.args.width, tt.args.stringIn) |
| 43 | + } else { |
| 44 | + got = pad(tt.args.width, tt.args.stringIn, tt.args.padString) |
| 45 | + } |
| 46 | + assert.Equal(t, tt.want, got) |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestPadLeft(t *testing.T) { |
| 52 | + type args struct { |
| 53 | + width int |
| 54 | + stringIn string |
| 55 | + padString string |
| 56 | + } |
| 57 | + |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + args args |
| 61 | + want string |
| 62 | + }{ |
| 63 | + {"PadLeft empty string with space", args{10, "", ""}, " "}, |
| 64 | + {"PadLeft empty string with minus", args{10, "", "-"}, "----------"}, |
| 65 | + {"PadLeft with space", args{10, "test", ""}, " test"}, |
| 66 | + {"PadLeft with space 2", args{10, "hello", ""}, " hello"}, |
| 67 | + {"PadLeft with minus", args{10, "test", "-"}, "------test"}, |
| 68 | + {"PadLeft with space and zero width", args{0, "test", ""}, "test"}, |
| 69 | + {"PadLeft with space and smaller width", args{3, "test", ""}, "test"}, |
| 70 | + {"PadLeft with space and same width", args{4, "test", ""}, "test"}, |
| 71 | + {"PadLeft wide charactors with space", args{10, "宽字符", ""}, " 宽字符"}, |
| 72 | + {"PadLeft wide charactors with space 2", args{10, "宽字符A", ""}, " 宽字符A"}, |
| 73 | + {"PadLeft wide charactors with space and zero width", args{0, "宽字符", ""}, "宽字符"}, |
| 74 | + {"PadLeft wide charactors with space and smaller width", args{5, "宽字符", ""}, "宽字符"}, |
| 75 | + {"PadLeft wide charactors with space and same width", args{6, "宽字符", ""}, "宽字符"}, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + var got string |
| 81 | + if tt.args.stringIn == "" && tt.args.padString == "" { |
| 82 | + got = padLeft(tt.args.width) |
| 83 | + } else if tt.args.padString == "" { |
| 84 | + got = padLeft(tt.args.width, tt.args.stringIn) |
| 85 | + } else { |
| 86 | + got = padLeft(tt.args.width, tt.args.stringIn, tt.args.padString) |
| 87 | + } |
| 88 | + assert.Equal(t, tt.want, got) |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestPadRight(t *testing.T) { |
| 94 | + type args struct { |
| 95 | + width int |
| 96 | + stringIn string |
| 97 | + padString string |
| 98 | + } |
| 99 | + |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + args args |
| 103 | + want string |
| 104 | + }{ |
| 105 | + {"PadRight empty string with space", args{10, "", ""}, " "}, |
| 106 | + {"PadRight empty string with minus", args{10, "", "-"}, "----------"}, |
| 107 | + {"PadRight with space", args{10, "test", ""}, "test "}, |
| 108 | + {"PadRight with space 2", args{10, "hello", ""}, "hello "}, |
| 109 | + {"PadRight with minus", args{10, "test", "-"}, "test------"}, |
| 110 | + {"PadRight with space and zero width", args{0, "test", ""}, "test"}, |
| 111 | + {"PadRight with space and smaller width", args{3, "test", ""}, "test"}, |
| 112 | + {"PadRight with space and same width", args{4, "test", ""}, "test"}, |
| 113 | + {"PadRight wide charactors with space", args{10, "宽字符", ""}, "宽字符 "}, |
| 114 | + {"PadRight wide charactors with space 2", args{10, "宽字符A", ""}, "宽字符A "}, |
| 115 | + {"PadRight wide charactors with space and zero width", args{0, "宽字符", ""}, "宽字符"}, |
| 116 | + {"PadRight wide charactors with space and smaller width", args{5, "宽字符", ""}, "宽字符"}, |
| 117 | + {"PadRight wide charactors with space and same width", args{6, "宽字符", ""}, "宽字符"}, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + var got string |
| 123 | + if tt.args.stringIn == "" && tt.args.padString == "" { |
| 124 | + got = padRight(tt.args.width) |
| 125 | + } else if tt.args.padString == "" { |
| 126 | + got = padRight(tt.args.width, tt.args.stringIn) |
| 127 | + } else { |
| 128 | + got = padRight(tt.args.width, tt.args.stringIn, tt.args.padString) |
| 129 | + } |
| 130 | + assert.Equal(t, tt.want, got) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestPadRightX(t *testing.T) { |
| 136 | + type args struct { |
| 137 | + width int |
| 138 | + stringIn string |
| 139 | + padString string |
| 140 | + } |
| 141 | + |
| 142 | + tests := []struct { |
| 143 | + name string |
| 144 | + args args |
| 145 | + want string |
| 146 | + }{ |
| 147 | + {"PadRightX empty string with space", args{10, "", " "}, " "}, |
| 148 | + {"PadRightX empty string with minus", args{10, "", "-"}, "----------"}, |
| 149 | + {"PadRightX empty string with minus and plus", args{10, "", "-+"}, "-+-+-+-+-+"}, |
| 150 | + {"PadRightX empty string with minus plus and dot", args{10, "", "-+."}, "-+.-+.-+.-"}, |
| 151 | + {"PadRightX with space", args{10, "test", " "}, "test "}, |
| 152 | + {"PadRightX with space 2", args{10, "hello", " "}, "hello "}, |
| 153 | + {"PadRightX with minus", args{10, "test", "-"}, "test------"}, |
| 154 | + {"PadRightX with space and zero width", args{0, "test", " "}, "test"}, |
| 155 | + {"PadRightX with space and smaller width", args{3, "test", " "}, "test"}, |
| 156 | + {"PadRightX with space and same width", args{4, "test", " "}, "test"}, |
| 157 | + {"PadRightX wide charactors with space", args{10, "宽字符", " "}, "宽字符 "}, |
| 158 | + {"PadRightX wide charactors with space 2", args{10, "宽字符A", " "}, "宽字符A "}, |
| 159 | + {"PadRightX wide charactors with space and zero width", args{0, "宽字符", " "}, "宽字符"}, |
| 160 | + {"PadRightX wide charactors with space and smaller width", args{5, "宽字符", " "}, "宽字符"}, |
| 161 | + {"PadRightX wide charactors with space and same width", args{6, "宽字符", " "}, "宽字符"}, |
| 162 | + } |
| 163 | + |
| 164 | + for _, tt := range tests { |
| 165 | + t.Run(tt.name, func(t *testing.T) { |
| 166 | + got := padRightX(tt.args.stringIn, tt.args.padString, tt.args.width) |
| 167 | + assert.Equal(t, tt.want, got) |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestStringOr(t *testing.T) { |
| 173 | + type args struct { |
| 174 | + stringA string |
| 175 | + stringB string |
| 176 | + padding []int |
| 177 | + } |
| 178 | + |
| 179 | + tests := []struct { |
| 180 | + name string |
| 181 | + args args |
| 182 | + want string |
| 183 | + }{ |
| 184 | + {"StringOr", args{"a", "b", []int{10}}, "a "}, |
| 185 | + {"StringOr empty string a and b", args{"", "", []int{10}}, " "}, |
| 186 | + {"StringOr empty string a", args{"", "b", []int{10}}, "b "}, |
| 187 | + {"StringOr empty string b", args{"a", "", []int{10}}, "a "}, |
| 188 | + {"StringOr without padding", args{"a", "b", nil}, "a"}, |
| 189 | + {"StringOr with zero width", args{"test", "b", []int{0}}, "test"}, |
| 190 | + {"StringOr with smaller width", args{"test", "b", []int{3}}, "test"}, |
| 191 | + {"StringOr with same width", args{"test", "b", []int{4}}, "test"}, |
| 192 | + {"StringOr wide charactors", args{"宽字符", "", []int{10}}, "宽字符 "}, |
| 193 | + {"StringOr wide charactors 2", args{"宽字符A", "", []int{10}}, "宽字符A "}, |
| 194 | + {"StringOr wide charactors with zero width", args{"宽字符", "", []int{0}}, "宽字符"}, |
| 195 | + {"StringOr wide charactors with smaller width", args{"宽字符", "", []int{5}}, "宽字符"}, |
| 196 | + {"StringOr wide charactors with same width", args{"宽字符", "", []int{6}}, "宽字符"}, |
| 197 | + } |
| 198 | + |
| 199 | + for _, tt := range tests { |
| 200 | + t.Run(tt.name, func(t *testing.T) { |
| 201 | + got := stringOr(tt.args.stringA, tt.args.stringB, tt.args.padding...) |
| 202 | + assert.Equal(t, tt.want, got) |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
0 commit comments