Skip to content

Commit

Permalink
consolidate into single test
Browse files Browse the repository at this point in the history
  • Loading branch information
tmck-code committed Dec 26, 2024
1 parent b3ebbe7 commit a4e03d9
Showing 1 changed file with 74 additions and 74 deletions.
148 changes: 74 additions & 74 deletions test/pokesay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------
Expand Down Expand Up @@ -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 := "▄▄ ▄▄ "
Expand Down

0 comments on commit a4e03d9

Please sign in to comment.