Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions ansi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package aec_test

import (
"testing"

"github.com/morikuni/aec"
)

func TestANSIWith(t *testing.T) {
t.Parallel()

tests := []struct {
doc string
got aec.ANSI
want string
}{
{
doc: "single",
got: aec.Bold.With(),
want: "\x1b[1m",
},
{
doc: "multiple",
got: aec.Bold.With(aec.RedF, aec.Right(2)),
want: "\x1b[1m\x1b[31m\x1b[2C",
},
{
doc: "cursor",
got: aec.Up(2).With(),
want: "\x1b[2A",
},
{
doc: "cursor then sgr",
got: aec.Up(2).With(aec.Bold),
want: aec.Up(2).String() + aec.Bold.String(),
},
}

for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
if got := tc.got.String(); got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}

func TestANSIApply(t *testing.T) {
t.Parallel()

tests := []struct {
doc string
ansi aec.ANSI
in string
want string
}{
{
doc: "single",
ansi: aec.Bold,
in: "hello",
want: "\x1b[1mhello\x1b[0m",
},
{
doc: "combined",
ansi: aec.Bold.With(aec.RedF),
in: "hello",
want: "\x1b[1m\x1b[31mhello\x1b[0m",
},
{
doc: "combined multiple",
ansi: aec.Bold.With(aec.RedF, aec.Right(2)),
in: "hello",
want: "\x1b[1m\x1b[31m\x1b[2Chello\x1b[0m",
},
{
doc: "empty string",
ansi: aec.Bold,
in: "",
want: "\x1b[1m\x1b[0m",
},
}

for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
if got := tc.ansi.Apply(tc.in); got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}

func TestApply(t *testing.T) {
t.Parallel()

tests := []struct {
doc string
got string
want string
}{
{
doc: "no ansi",
got: aec.Apply("hello"),
want: "hello",
},
{
doc: "empty string",
got: aec.Apply("", aec.Bold),
want: "\x1b[1m\x1b[0m",
},
{
doc: "single",
got: aec.Apply("hello", aec.Bold),
want: "\x1b[1mhello\x1b[0m",
},
{
doc: "multiple",
got: aec.Apply("hello", aec.Bold, aec.RedF, aec.Right(2)),
want: "\x1b[1m\x1b[31m\x1b[2Chello\x1b[0m",
},
}

for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
if tc.got != tc.want {
t.Errorf("got %q, want %q", tc.got, tc.want)
}
})
}
}
76 changes: 76 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package aec_test

import (
"testing"

"github.com/morikuni/aec"
)

func TestNewBuilder(t *testing.T) {
t.Parallel()

got := aec.NewBuilder(aec.Up(2), aec.Bold).ANSI.String()
want := aec.Up(2).With(aec.Bold).String()
if got != want {
t.Fatalf("builder ANSI mismatch: got %q, want %q", got, want)
}
}

func TestEmptyBuilderChain(t *testing.T) {
t.Parallel()

got := aec.EmptyBuilder.Right(2).RedF().Bold().ANSI.String()
want := aec.Right(2).With(aec.RedF, aec.Bold).String()
if got != want {
t.Fatalf("builder ANSI mismatch: got %q, want %q", got, want)
}
}

func TestBuilderApply(t *testing.T) {
t.Parallel()

got := aec.EmptyBuilder.Bold().RedF().ANSI.Apply("hello")
want := aec.Bold.With(aec.RedF).Apply("hello")
if got != want {
t.Fatalf("builder Apply mismatch: got %q, want %q", got, want)
}
}

func TestBuilderFixtures(t *testing.T) {
t.Parallel()

tests := []struct {
doc string
got string
want string
}{
{
doc: "empty",
got: aec.EmptyBuilder.ANSI.String(),
want: "",
},
{
doc: "bold",
got: aec.EmptyBuilder.Bold().ANSI.String(),
want: "\x1b[1m",
},
{
doc: "right then red foreground",
got: aec.EmptyBuilder.Right(2).RedF().ANSI.String(),
want: "\x1b[2C\x1b[31m",
},
{
doc: "apply",
got: aec.EmptyBuilder.Bold().ANSI.Apply("hello"),
want: "\x1b[1mhello\x1b[0m",
},
}

for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
if tc.got != tc.want {
t.Fatalf("got %q, want %q", tc.got, tc.want)
}
})
}
}