Skip to content

Commit

Permalink
gofmted.
Browse files Browse the repository at this point in the history
go fmted cmd files.
  • Loading branch information
kuredoro committed Jul 4, 2021
1 parent e56fb4d commit 92b9ab2
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 283 deletions.
68 changes: 34 additions & 34 deletions cmd/cptest/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ func (e *Executable) Run(r io.Reader) (cptest.ProcessResult, error) {
cmd := exec.Command(e.Path)
cmd.Stdin = r

stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stderrPipe, err := cmd.StderrPipe()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

err = cmd.Start()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stdErr, err := ioutil.ReadAll(stderrPipe)
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stdOut, err := ioutil.ReadAll(stdoutPipe)
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

out := cptest.ProcessResult{
ExitCode: 0,
Stdout: string(stdOut),
Stderr: string(stdErr),
}

err = cmd.Wait()
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stderrPipe, err := cmd.StderrPipe()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

err = cmd.Start()
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stdErr, err := ioutil.ReadAll(stderrPipe)
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

stdOut, err := ioutil.ReadAll(stdoutPipe)
if err != nil {
return cptest.ProcessResult{}, fmt.Errorf("executable: %v", err)
}

out := cptest.ProcessResult{
ExitCode: 0,
Stdout: string(stdOut),
Stderr: string(stdErr),
}

err = cmd.Wait()

if ee, ok := err.(*exec.ExitError); ok {
out.ExitCode = ee.ExitCode()
return out, nil
out.ExitCode = ee.ExitCode()
return out, nil
}

if err != nil {
Expand Down
44 changes: 22 additions & 22 deletions cmd/cptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ import (
"github.com/alexflint/go-arg"
"github.com/kuredoro/cptest"
"github.com/logrusorgru/aurora"
"github.com/mattn/go-colorable"
"github.com/mattn/go-colorable"
)

var wd = "."

var stdout = colorable.NewColorableStdout()

type appArgs struct {
Inputs string `arg:"-i" default:"inputs.txt" help:"file with tests"`
Executable string `arg:"positional,required"`
NoColors bool `arg:"--no-colors" help:"disable colored output"`
Inputs string `arg:"-i" default:"inputs.txt" help:"file with tests"`
Executable string `arg:"positional,required"`
NoColors bool `arg:"--no-colors" help:"disable colored output"`
}

var args appArgs

func (appArgs) Description() string {
return `Feed programs fixed inputs, compare their outputs against expected ones.
return `Feed programs fixed inputs, compare their outputs against expected ones.
Author: @kuredoro
User manual: https://github.com/kuredoro/cptest
`
}

func (appArgs) Version() string {
return "cptest 1.02a"
return "cptest 1.02a"
}

func init() {
arg.MustParse(&args)

if args.NoColors {
cptest.Au = aurora.NewAurora(false)
}

verdictStr = map[cptest.Verdict]aurora.Value{
cptest.OK: cptest.Au.Bold("OK").Green(),
cptest.IE: cptest.Au.Bold("IE").Bold(),
cptest.WA: cptest.Au.Bold("WA").Red(),
cptest.RE: cptest.Au.Bold("RE").Magenta(),
cptest.TL: cptest.Au.Bold("TL").Yellow(),
}
arg.MustParse(&args)

if args.NoColors {
cptest.Au = aurora.NewAurora(false)
}

verdictStr = map[cptest.Verdict]aurora.Value{
cptest.OK: cptest.Au.Bold("OK").Green(),
cptest.IE: cptest.Au.Bold("IE").Bold(),
cptest.WA: cptest.Au.Bold("WA").Red(),
cptest.RE: cptest.Au.Bold("RE").Magenta(),
cptest.TL: cptest.Au.Bold("TL").Yellow(),
}
}

func main() {
Expand All @@ -57,7 +57,7 @@ func main() {
return
}

inputsPath := joinIfRelative(wd, args.Inputs)
inputsPath := joinIfRelative(wd, args.Inputs)

inputs, errs := readInputs(inputsPath)
if errs != nil {
Expand All @@ -68,7 +68,7 @@ func main() {
return
}

execPath := joinIfRelative(wd, args.Executable)
execPath := joinIfRelative(wd, args.Executable)
proc := &Executable{
Path: execPath,
}
Expand All @@ -82,7 +82,7 @@ func main() {

batch := cptest.NewTestingBatch(inputs, proc, swatch)

fmt.Printf("floating point precision: %d digit(s)\n", batch.Lx.Precision)
fmt.Printf("floating point precision: %d digit(s)\n", batch.Lx.Precision)

batch.TestStartCallback = runPrinter
batch.TestEndCallback = verboseResultPrinter
Expand Down
6 changes: 3 additions & 3 deletions cmd/cptest/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func verboseResultPrinter(b *cptest.TestingBatch, test cptest.Test, id int) {
fmt.Printf("Stderr:\n%s\n", b.Outs[id].Stderr)
} else if verdict == cptest.WA {
fmt.Fprintf(stdout, "Output:\n%s\n", cptest.DumpLexemes(b.RichOuts[id], diffColor))
if b.Outs[id].Stderr != "" {
fmt.Printf("Stderr:\n%s\n", b.Outs[id].Stderr)
}
if b.Outs[id].Stderr != "" {
fmt.Printf("Stderr:\n%s\n", b.Outs[id].Stderr)
}
} else if verdict == cptest.IE {
fmt.Printf("Error:\n%v\n\n", b.Errs[id])
}
Expand Down
37 changes: 18 additions & 19 deletions dump_lexemes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cptest

import (
"strings"
"github.com/logrusorgru/aurora"
"strings"
)

// AltLineFeed is the representation of LF in textual form, that replaces LF
Expand All @@ -16,27 +16,26 @@ const AltLineFeed = "\\n"
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(' ')
}
x := 0
for _, xm := range xms {
if x != 0 && xm.Str != "\n" {
str.WriteRune(' ')
}

if xm.Str == "\n" {
x = -1
if xm.Str == "\n" {
x = -1

if xm.Colorful() {
str.WriteString(Au.Colorize(AltLineFeed, color).String())
str.WriteRune('\n')
x++
continue
}
}
if xm.Colorful() {
str.WriteString(Au.Colorize(AltLineFeed, color).String())
str.WriteRune('\n')
x++
continue
}
}

str.WriteString(xm.Colorize(color))
x++
}
str.WriteString(xm.Colorize(color))
x++
}

return str.String()
}

138 changes: 69 additions & 69 deletions dump_lexemes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,73 @@ import (
)

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(cptest.AltLineFeed).String() + "\n"
want := "foo" + colorizedLF + colorizedLF + aurora.Bold("bar").String() + "\n"

cptest.AssertText(t, got, want)
})
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(cptest.AltLineFeed).String() + "\n"
want := "foo" + colorizedLF + colorizedLF + aurora.Bold("bar").String() + "\n"

cptest.AssertText(t, got, want)
})
}
Loading

0 comments on commit 92b9ab2

Please sign in to comment.