Skip to content

Commit

Permalink
DumpLexemes is now part of cptest and is tested. It also replaces LF …
Browse files Browse the repository at this point in the history
…with block char if LF is colorful.
  • Loading branch information
kuredoro committed Jan 8, 2021
1 parent 8fd0a6c commit 3bac01f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 19 deletions.
21 changes: 2 additions & 19 deletions cmd/cptest/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"strings"
"time"

"github.com/kuredoro/cptest"
Expand All @@ -25,22 +24,6 @@ func RunPrinter(id int) {
fmt.Printf("=== RUN\tTest %d\n", id)
}

func DumpLexemes(xms []cptest.RichText) string {
var str strings.Builder

for _, xm := range xms {
if xm.Str == "\n" {
str.WriteRune('\n')
continue
}

str.WriteString(xm.Colorize(DiffColor))
str.WriteRune(' ')
}

return str.String()
}

func VerboseResultPrinter(b *cptest.TestingBatch, test cptest.Test, id int) {
verdict := b.Verdicts[id]

Expand All @@ -50,12 +33,12 @@ func VerboseResultPrinter(b *cptest.TestingBatch, test cptest.Test, id int) {
if verdict != cptest.OK {
fmt.Printf("Input:\n%s\n", test.Input)

fmt.Printf("Answer:\n%s\n", DumpLexemes(b.RichAnswers[id]))
fmt.Printf("Answer:\n%s\n", cptest.DumpLexemes(b.RichAnswers[id], DiffColor))

if verdict == cptest.RE {
fmt.Printf("Stderr:\n%s\n", b.Outs[id])
} else if verdict == cptest.WA {
fmt.Printf("Output:\n%s\n", DumpLexemes(b.RichOuts[id]))
fmt.Printf("Output:\n%s\n", cptest.DumpLexemes(b.RichOuts[id], DiffColor))
} else if verdict == cptest.IE {
fmt.Printf("Error:\n%v\n\n", b.Errs[id])
}
Expand Down
38 changes: 38 additions & 0 deletions dump_lexemes.go
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()
}

80 changes: 80 additions & 0 deletions dump_lexemes_test.go
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)
})
}
10 changes: 10 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,13 @@ func AssertEnrichedLexSequence(t *testing.T, got, want []RichText) {
AssertRichText(t, RichText{"", []bool{}}, want[i])
}
}

func AssertText(t *testing.T, got, want string) {
t.Helper()

if got != want {
got = strings.ReplaceAll(got, "\n", "\\n")
want = strings.ReplaceAll(want, "\n", "\\n")
t.Errorf("got text '%s', want '%s'", got, want)
}
}

0 comments on commit 3bac01f

Please sign in to comment.