From a4e03d9f2ac71db9c5ffc8817bf57e8c1931b218 Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Thu, 26 Dec 2024 23:26:39 +1100 Subject: [PATCH] consolidate into single test --- test/pokesay_test.go | 148 +++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/test/pokesay_test.go b/test/pokesay_test.go index 18a2c1a..b6921b6 100644 --- a/test/pokesay_test.go +++ b/test/pokesay_test.go @@ -124,76 +124,87 @@ func TestUnicodeStringLength(test *testing.T) { // Test ANSI tokenisation ------------------------------------------------------ func TestTokeniseANSIString(test *testing.T) { - // purple fg, red bg - line := "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m" - - expected := [][]pokesay.ANSILineToken{ + testCases := []struct { + name string + input string + expected [][]pokesay.ANSILineToken + }{ { - pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "}, - pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XX "}, - pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + name: "Single line with no colour", + input: " ▄▄ ▄▄", + expected: [][]pokesay.ANSILineToken{ + { + pokesay.ANSILineToken{Colour: "", Text: " ▄▄ ▄▄"}, + }, + }, }, - } - result := pokesay.TokeniseANSIString(line) - Assert(expected, result, test) -} - -func TestTokeniseANSIStringLines(test *testing.T) { - // line 1 : purple fg, line 2: red bg - msg := "\x1b[38;5;160m▄ \x1b[38;5;46m▄\n▄ \x1b[38;5;190m▄" - - expected := [][]pokesay.ANSILineToken{ - { // Line 1 - pokesay.ANSILineToken{Colour: "\x1b[38;5;160m", Text: "▄ "}, - pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄"}, - pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + // purple fg, red bg + { + name: "Single line with fg and bg", + input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m", + expected: [][]pokesay.ANSILineToken{ + { + pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "}, + pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XX "}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + }, + }, }, - { // Line 2 - pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄ "}, - pokesay.ANSILineToken{Colour: "\x1b[38;5;190m", Text: "▄"}, - pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + { + name: "Multi-line", + // line 1 : purple fg, line 2: red bg + input: "\x1b[38;5;160m▄ \x1b[38;5;46m▄\n▄ \x1b[38;5;190m▄", + expected: [][]pokesay.ANSILineToken{ + { // Line 1 + pokesay.ANSILineToken{Colour: "\x1b[38;5;160m", Text: "▄ "}, + pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄"}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + }, + { // Line 2 + pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄ "}, + pokesay.ANSILineToken{Colour: "\x1b[38;5;190m", Text: "▄"}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + }, + }, }, - } - result := pokesay.TokeniseANSIString(msg) - Assert(expected, result, test) - -} - -func TestTokeniseANSILineWithTrailingSpaces(test *testing.T) { - // The AAA has a purple fg - // The XX has a red bg - line := " \x1b[38;5;129mAAA \x1b[48;5;160m XY \x1b[0m " - - // The AAA should still have a purple fg - // The XX should still have a red bg - expected := [][]pokesay.ANSILineToken{ { - pokesay.ANSILineToken{Colour: "", Text: " "}, - pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "}, - pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XY "}, - pokesay.ANSILineToken{Colour: "\x1b[0m", Text: " "}, - pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + name: "Multi-line with trailing spaces", + // The AAA has a purple fg + // The XX has a red bg + input: " \x1b[38;5;129mAAA \x1b[48;5;160m XY \x1b[0m ", + // The AAA should still have a purple fg + // The XX should still have a red bg + expected: [][]pokesay.ANSILineToken{ + { + pokesay.ANSILineToken{Colour: "", Text: " "}, + pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "}, + pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XY "}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: " "}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + }, + }, + }, + { + name: "Lines with colour continuation (spaces)", + // purple fg, red bg + // the 4 spaces after AAA should have a purple fg, and no bg + input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m", + // expected := "\x1b[0m\x1b[48;5;160m\x1b[38;5;129m XX \x1b[38;5;129m\x1b[49m AAA\x1b[0m" + expected: [][]pokesay.ANSILineToken{ + { + pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "}, + pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XX "}, + pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""}, + }, + }, }, } - result := pokesay.TokeniseANSIString(line) - // fmt.Printf("expected: %#v\n", expected) - // fmt.Printf("result: %#v\n", result) - - Assert(expected, result, test) -} - -func TestTokeniseANSIStringWithBG(test *testing.T) { - // purple fg, red bg - // the 4 spaces after AAA should have a purple fg, and no bg - line := "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m" - - expected := "\x1b[0m\x1b[48;5;160m\x1b[38;5;129m XX \x1b[38;5;129m\x1b[49m AAA\x1b[0m" - result := pokesay.TokeniseANSIString(line) - - fmt.Printf("expected: %#v\n", expected) - fmt.Printf("result: %#v\n", result) - - Assert(expected, result, test) + for _, tc := range testCases { + test.Run(tc.name, func(test *testing.T) { + result := pokesay.TokeniseANSIString(tc.input) + Assert(tc.expected, result, test) + }) + } } // Test ANSI line reversal ----------------------------------------------------- @@ -245,17 +256,6 @@ func TestFlipHorizontalLineWithTrailingSpaces(test *testing.T) { Assert(expected, result, test) } -func TestTokeniseANSIStringWithNoColour(test *testing.T) { - msg := " ▄▄ ▄▄" - expected := [][]pokesay.ANSILineToken{ - { - pokesay.ANSILineToken{Colour: "", Text: " ▄▄ ▄▄"}, - }, - } - result := pokesay.TokeniseANSIString(msg) - Assert(expected, result, test) -} - func TestReverseUnicodeString(test *testing.T) { msg := " ▄▄ ▄▄" expected := "▄▄ ▄▄ "