-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DumpLexemes is now part of cptest and is tested. It also replaces LF …
…with block char if LF is colorful.
- Loading branch information
Showing
4 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cptest | ||
|
||
import ( | ||
"strings" | ||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
const ( | ||
AltLineFeedChar = '█' | ||
) | ||
|
||
func DumpLexemes(xms []RichText, color aurora.Color) string { | ||
var str strings.Builder | ||
|
||
x := 0 | ||
for _, xm := range xms { | ||
if x != 0 && xm.Str != "\n" { | ||
str.WriteRune(' ') | ||
} | ||
|
||
if xm.Str == "\n" { | ||
x = -1 | ||
|
||
if xm.Colorful() { | ||
str.WriteString(aurora.Colorize(string(AltLineFeedChar), color).String()) | ||
str.WriteRune('\n') | ||
x++ | ||
continue | ||
} | ||
} | ||
|
||
str.WriteString(xm.Colorize(color)) | ||
x++ | ||
} | ||
|
||
return str.String() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cptest_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kuredoro/cptest" | ||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
func TestDumpLexemes(t *testing.T) { | ||
t.Run("single lexeme", func(t *testing.T) { | ||
xms := []cptest.RichText{ | ||
{"foo", []bool{false, false, false}}, | ||
} | ||
|
||
got := cptest.DumpLexemes(xms, aurora.RedFg) | ||
want := "foo" | ||
|
||
cptest.AssertText(t, got, want) | ||
}) | ||
|
||
t.Run("multiple on one line", func(t *testing.T) { | ||
xms := []cptest.RichText{ | ||
{"foo", []bool{false, false, false}}, | ||
{"bar", []bool{true, true, true}}, | ||
} | ||
|
||
got := cptest.DumpLexemes(xms, aurora.BoldFm) | ||
want := "foo " + aurora.Bold("bar").String() | ||
|
||
cptest.AssertText(t, got, want) | ||
}) | ||
|
||
t.Run("with one new line", func(t *testing.T) { | ||
xms := []cptest.RichText{ | ||
{"foo", []bool{false, false, false}}, | ||
{"bar", []bool{true, true, true}}, | ||
{"\n", []bool{false}}, | ||
} | ||
|
||
got := cptest.DumpLexemes(xms, aurora.BoldFm) | ||
want := "foo " + aurora.Bold("bar").String() + "\n" | ||
|
||
cptest.AssertText(t, got, want) | ||
}) | ||
|
||
t.Run("multiple lines", func(t *testing.T) { | ||
xms := []cptest.RichText{ | ||
{"foo", []bool{false, false, false}}, | ||
{"bar", []bool{true, true, true}}, | ||
{"\n", []bool{false}}, | ||
{"bar", []bool{false, false, false}}, | ||
{"foo", []bool{true, true, true}}, | ||
{"\n", []bool{false}}, | ||
} | ||
|
||
got := cptest.DumpLexemes(xms, aurora.BoldFm) | ||
want := "foo " + aurora.Bold("bar").String() + "\n" | ||
want += "bar " + aurora.Bold("foo").String() + "\n" | ||
|
||
cptest.AssertText(t, got, want) | ||
}) | ||
|
||
t.Run("colorized new line", func(t *testing.T) { | ||
xms := []cptest.RichText{ | ||
{"foo", []bool{false, false, false}}, | ||
{"\n", []bool{true}}, | ||
{"\n", []bool{true}}, | ||
{"bar", []bool{true, true, true}}, | ||
{"\n", []bool{false}}, | ||
} | ||
|
||
got := cptest.DumpLexemes(xms, aurora.BoldFm) | ||
|
||
colorizedLF := aurora.Bold(string(cptest.AltLineFeedChar)).String() + "\n" | ||
want := "foo" + colorizedLF + colorizedLF + aurora.Bold("bar").String() + "\n" | ||
|
||
cptest.AssertText(t, got, want) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters