Skip to content

Commit

Permalink
internal: tests moved to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Nov 15, 2020
1 parent d04f689 commit 1b323c2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Parse(format string) string {
resParts = append(resParts, string(tempToken))
tempToken = nil
}

inFormatGroup = true
inFormat = false
inText = false
Expand Down Expand Up @@ -81,7 +82,6 @@ func Parse(format string) string {
}

if s == '}' && index+1 < len(format) && format[index+1] == '}' {

inFormatGroup = false
inFormat = false
inText = true
Expand Down Expand Up @@ -142,7 +142,7 @@ func Parse(format string) string {
}

func groupStyle(format string, token string, style string) string {
styler, err := styleBuilder(style)
styler, err := StyleBuilder(style)
if err != nil {
log.Fatalf("Error parse style string in '%s' format string: %v", format, err)
}
Expand Down
8 changes: 3 additions & 5 deletions internal/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/gookit/color"
)

func styleBuilder(styleString string) (func(string) string, error) {
// StyleBuilder is a function that returns a callback function that will style
// the supplied string according to the supplied format string.
func StyleBuilder(styleString string) (func(string) string, error) {
if styleString == "" {
return nil, fmt.Errorf("style string is empty")
}
Expand Down Expand Up @@ -62,10 +64,6 @@ func styleBuilder(styleString string) (func(string) string, error) {
outFun := func(text string) string { return text }

for index, fun := range hexColorFuncs {
if index < 0 || index >= len(hexColors) {
continue
}

clr := hexColors[index]
outFun = fun(clr, outFun)
}
Expand Down
19 changes: 19 additions & 0 deletions tests/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests

import (
"testing"

"github.com/i582/cfmt"
)

func TestParse(t *testing.T) {
cfmt.Println("{{correct group}}::red|underline")
cfmt.Println("{{correct group}}::red|underline and {{other}}::red")
cfmt.Println("{{error group}} ")
cfmt.Println("{{overline group}}::overline")
cfmt.Println("{{reverse group}}::reverse")
cfmt.Println("{{faint group}}::faint")
cfmt.Println("{{blink group}}::blink")
cfmt.Println("{{hex color group}}::#ff00ff")
cfmt.Println("{{background color hex group}}::bg#ffff00")
}
19 changes: 17 additions & 2 deletions internal/style_test.go → tests/style_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package internal
package tests

import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/i582/cfmt"
"github.com/i582/cfmt/internal"
)

type StyleBuilderSuite struct {
Expand All @@ -12,6 +15,10 @@ type StyleBuilderSuite struct {
}

func TestStyleBuilder(t *testing.T) {
cfmt.RegisterStyle("code", func(s string) string {
return cfmt.Sprintf("{{%s}}::red|underline", s)
})

suites := []StyleBuilderSuite{
{
Value: "red",
Expand All @@ -29,6 +36,14 @@ func TestStyleBuilder(t *testing.T) {
Value: "bg#ff00ff|bold",
Error: "",
},
{
Value: "code|underline|blink",
Error: "",
},
{
Value: "overline|reverse|faint",
Error: "",
},
{
Value: "bg#ff0|bold",
Error: "invalid hex: length of hex color must be 6",
Expand All @@ -48,7 +63,7 @@ func TestStyleBuilder(t *testing.T) {
}

for _, suite := range suites {
_, err := styleBuilder(suite.Value)
_, err := internal.StyleBuilder(suite.Value)
if err == nil && suite.Error != "" {
t.Error(cmp.Diff("", suite.Error))
continue
Expand Down

0 comments on commit 1b323c2

Please sign in to comment.