From 91b64c32ba86ac66656220b980d08882f83618a5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 20 Mar 2026 23:57:44 +0100 Subject: [PATCH 1/2] add tests for builder Signed-off-by: Sebastiaan van Stijn --- builder_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 builder_test.go diff --git a/builder_test.go b/builder_test.go new file mode 100644 index 0000000..643edab --- /dev/null +++ b/builder_test.go @@ -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) + } + }) + } +} From 7f462bc2ff762cfe6153aec923793464dab31da9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 22 Mar 2026 16:13:59 +0100 Subject: [PATCH 2/2] add tests for ANSI utilities Signed-off-by: Sebastiaan van Stijn --- ansi_test.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 ansi_test.go diff --git a/ansi_test.go b/ansi_test.go new file mode 100644 index 0000000..a7d7b71 --- /dev/null +++ b/ansi_test.go @@ -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) + } + }) + } +}