| 1 | package helper |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | const testLineLength = 64 |
| 11 | |
| 12 | var lineSplittingTestCases = map[string]struct { |
| 13 | input string |
| 14 | splitOutput []string |
| 15 | nonSplitOutput []string |
| 16 | }{ |
| 17 | "Short single-line message": { |
| 18 | input: "short", |
| 19 | splitOutput: []string{"short"}, |
| 20 | nonSplitOutput: []string{"short"}, |
| 21 | }, |
| 22 | "Long single-line message": { |
| 23 | input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 24 | splitOutput: []string{ |
| 25 | "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", |
| 26 | "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", |
| 27 | " labore et dolore magna aliqua.", |
| 28 | }, |
| 29 | nonSplitOutput: []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}, |
| 30 | }, |
| 31 | "Short multi-line message": { |
| 32 | input: "I\ncan't\nget\nno\nsatisfaction!", |
| 33 | splitOutput: []string{ |
| 34 | "I", |
| 35 | "can't", |
| 36 | "get", |
| 37 | "no", |
| 38 | "satisfaction!", |
| 39 | }, |
| 40 | nonSplitOutput: []string{ |
| 41 | "I", |
| 42 | "can't", |
| 43 | "get", |
| 44 | "no", |
| 45 | "satisfaction!", |
| 46 | }, |
| 47 | }, |
| 48 | "Long multi-line message": { |
| 49 | input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" + |
| 50 | "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" + |
| 51 | "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n" + |
| 52 | "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", |
| 53 | splitOutput: []string{ |
| 54 | "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", |
| 55 | "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", |
| 56 | " labore et dolore magna aliqua.", |
| 57 | "Ut enim ad minim veniam, quis nostrud exercita <clipped message>", |
| 58 | "tion ullamco laboris nisi ut aliquip ex ea com <clipped message>", |
| 59 | "modo consequat.", |
| 60 | "Duis aute irure dolor in reprehenderit in volu <clipped message>", |
| 61 | "ptate velit esse cillum dolore eu fugiat nulla <clipped message>", |
| 62 | " pariatur.", |
| 63 | "Excepteur sint occaecat cupidatat non proident <clipped message>", |
| 64 | ", sunt in culpa qui officia deserunt mollit an <clipped message>", |
| 65 | "im id est laborum.", |
| 66 | }, |
| 67 | nonSplitOutput: []string{ |
| 68 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 69 | "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", |
| 70 | "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.", |
| 71 | "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", |
| 72 | }, |
| 73 | }, |
| 74 | "Message ending with new-line.": { |
| 75 | input: "Newline ending\n", |
| 76 | splitOutput: []string{"Newline ending"}, |
| 77 | nonSplitOutput: []string{"Newline ending"}, |
| 78 | }, |
| 79 | "Long message containing UTF-8 multi-byte runes": { |
| 80 | input: "不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說", |
| 81 | splitOutput: []string{ |
| 82 | "不布人個我此而及單石業喜資富下 <clipped message>", |
| 83 | "我河下日沒一我臺空達的常景便物 <clipped message>", |
| 84 | "沒為……子大我別名解成?生賣的 <clipped message>", |
| 85 | "全直黑,我自我結毛分洲了世當, <clipped message>", |
| 86 | "是政福那是東;斯說", |
| 87 | }, |
| 88 | nonSplitOutput: []string{"不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說"}, |
| 89 | }, |
| 90 | "Long message, clip three-byte rune after two bytes": { |
| 91 | input: "x 人人生而自由,在尊嚴和權利上一律平等。 他們都具有理性和良知,應該以兄弟情誼的精神對待彼此。", |
| 92 | splitOutput: []string{ |
| 93 | "x 人人生而自由,在尊嚴和權利上 <clipped message>", |
| 94 | "一律平等。 他們都具有理性和良知 <clipped message>", |
| 95 | ",應該以兄弟情誼的精神對待彼此。", |
| 96 | }, |
| 97 | nonSplitOutput: []string{"x 人人生而自由,在尊嚴和權利上一律平等。 他們都具有理性和良知,應該以兄弟情誼的精神對待彼此。"}, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | func TestGetSubLines(t *testing.T) { |
| 102 | for testname, testcase := range lineSplittingTestCases { |
| 103 | splitLines := GetSubLines(testcase.input, testLineLength, "") |
| 104 | assert.Equalf(t, testcase.splitOutput, splitLines, "'%s' testcase should give expected lines with splitting.", testname) |
| 105 | for _, splitLine := range splitLines { |
| 106 | byteLength := len([]byte(splitLine)) |
| 107 | assert.True(t, byteLength <= testLineLength, "Splitted line '%s' of testcase '%s' should not exceed the maximum byte-length (%d vs. %d).", splitLine, testcase, byteLength, testLineLength) |
| 108 | } |
| 109 | |
| 110 | nonSplitLines := GetSubLines(testcase.input, 0, "") |
| 111 | assert.Equalf(t, testcase.nonSplitOutput, nonSplitLines, "'%s' testcase should give expected lines without splitting.", testname) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestConvertWebPToPNG(t *testing.T) { |
| 116 | if os.Getenv("LOCAL_TEST") == "" { |
| 117 | t.Skip() |
| 118 | } |
| 119 | |
| 120 | input, err := os.ReadFile("test.webp") |
| 121 | if err != nil { |
| 122 | t.Fail() |
| 123 | } |
| 124 | |
| 125 | d := &input |
| 126 | err = ConvertWebPToPNG(d) |
| 127 | if err != nil { |
| 128 | t.Fail() |
| 129 | } |
| 130 | |
| 131 | err = os.WriteFile("test.png", *d, 0o644) //nolint:gosec |
| 132 | if err != nil { |
| 133 | t.Fail() |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | var clippingOrSplittingTestCases = map[string]struct { |
| 138 | inputText string |
| 139 | clipSplitLength int |
| 140 | clippingMessage string |
| 141 | splitMax int |
| 142 | expectedOutput []string |
| 143 | }{ |
| 144 | "Short single-line message, split 3": { |
| 145 | inputText: "short", |
| 146 | clipSplitLength: 20, |
| 147 | clippingMessage: "?!?!", |
| 148 | splitMax: 3, |
| 149 | expectedOutput: []string{"short"}, |
| 150 | }, |
| 151 | "Short single-line message, split 1": { |
| 152 | inputText: "short", |
| 153 | clipSplitLength: 20, |
| 154 | clippingMessage: "?!?!", |
| 155 | splitMax: 1, |
| 156 | expectedOutput: []string{"short"}, |
| 157 | }, |
| 158 | "Short single-line message, split 0": { |
| 159 | // Mainly check that we don't crash. |
| 160 | inputText: "short", |
| 161 | clipSplitLength: 20, |
| 162 | clippingMessage: "?!?!", |
| 163 | splitMax: 0, |
| 164 | expectedOutput: []string{"short"}, |
| 165 | }, |
| 166 | "Long single-line message, noclip": { |
| 167 | inputText: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 168 | clipSplitLength: 50, |
| 169 | clippingMessage: "?!?!", |
| 170 | splitMax: 10, |
| 171 | expectedOutput: []string{ |
| 172 | "Lorem ipsum dolor sit amet, consectetur adipiscing", |
| 173 | " elit, sed do eiusmod tempor incididunt ut labore ", |
| 174 | "et dolore magna aliqua.", |
| 175 | }, |
| 176 | }, |
| 177 | "Long single-line message, noclip tight": { |
| 178 | inputText: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 179 | clipSplitLength: 50, |
| 180 | clippingMessage: "?!?!", |
| 181 | splitMax: 3, |
| 182 | expectedOutput: []string{ |
| 183 | "Lorem ipsum dolor sit amet, consectetur adipiscing", |
| 184 | " elit, sed do eiusmod tempor incididunt ut labore ", |
| 185 | "et dolore magna aliqua.", |
| 186 | }, |
| 187 | }, |
| 188 | "Long single-line message, clip custom": { |
| 189 | inputText: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 190 | clipSplitLength: 50, |
| 191 | clippingMessage: "?!?!", |
| 192 | splitMax: 2, |
| 193 | expectedOutput: []string{ |
| 194 | "Lorem ipsum dolor sit amet, consectetur adipiscing", |
| 195 | " elit, sed do eiusmod tempor incididunt ut lab?!?!", |
| 196 | }, |
| 197 | }, |
| 198 | "Long single-line message, clip built-in": { |
| 199 | inputText: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", |
| 200 | clipSplitLength: 50, |
| 201 | clippingMessage: "", |
| 202 | splitMax: 2, |
| 203 | expectedOutput: []string{ |
| 204 | "Lorem ipsum dolor sit amet, consectetur adipiscing", |
| 205 | " elit, sed do eiusmod tempor inc <clipped message>", |
| 206 | }, |
| 207 | }, |
| 208 | "Short multi-line message": { |
| 209 | inputText: "I\ncan't\nget\nno\nsatisfaction!", |
| 210 | clipSplitLength: 50, |
| 211 | clippingMessage: "", |
| 212 | splitMax: 2, |
| 213 | expectedOutput: []string{"I\ncan't\nget\nno\nsatisfaction!"}, |
| 214 | }, |
| 215 | "Long message containing UTF-8 multi-byte runes": { |
| 216 | inputText: "人人生而自由,在尊嚴和權利上一律平等。 他們都具有理性和良知,應該以兄弟情誼的精神對待彼此。", |
| 217 | clipSplitLength: 50, |
| 218 | clippingMessage: "", |
| 219 | splitMax: 10, |
| 220 | expectedOutput: []string{ |
| 221 | "人人生而自由,在尊嚴和權利上一律", // Note: only 48 bytes! |
| 222 | "平等。 他們都具有理性和良知,應該", // Note: only 49 bytes! |
| 223 | "以兄弟情誼的精神對待彼此。", |
| 224 | }, |
| 225 | }, |
| 226 | } |
| 227 | |
| 228 | func TestClipOrSplitMessage(t *testing.T) { |
| 229 | for testname, testcase := range clippingOrSplittingTestCases { |
| 230 | actualOutput := ClipOrSplitMessage(testcase.inputText, testcase.clipSplitLength, testcase.clippingMessage, testcase.splitMax) |
| 231 | assert.Equalf(t, testcase.expectedOutput, actualOutput, "'%s' testcase should give expected lines with clipping+splitting.", testname) |
| 232 | for _, splitLine := range testcase.expectedOutput { |
| 233 | byteLength := len([]byte(splitLine)) |
| 234 | assert.True(t, byteLength <= testcase.clipSplitLength, "Splitted line '%s' of testcase '%s' should not exceed the maximum byte-length (%d vs. %d).", splitLine, testname, testcase.clipSplitLength, byteLength) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |