Write some tests for the style wrapper

This commit is contained in:
Kovid Goyal 2022-08-27 14:15:43 +05:30
parent 91c61478dd
commit 42a8ca0842
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 73 additions and 20 deletions

30
tools/utils/style/api.go Normal file
View File

@ -0,0 +1,30 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package style
import (
"fmt"
"strings"
)
type Context struct {
AllowEscapeCodes bool
}
func (self *Context) SprintFunc(spec string) func(args ...interface{}) string {
p := prefix_for_spec(spec)
s := suffix_for_spec(spec)
return func(args ...interface{}) string {
body := fmt.Sprint(args...)
if !self.AllowEscapeCodes {
return body
}
b := strings.Builder{}
b.Grow(len(p) + len(body) + len(s))
b.WriteString(p)
b.WriteString(body)
b.WriteString(s)
return b.String()
}
}

View File

@ -27,7 +27,7 @@ func (self bool_value) as_sgr(start, end string, prefix, suffix []string) ([]str
start, end = end, start start, end = end, start
} }
prefix = append(prefix, start) prefix = append(prefix, start)
suffix = append(suffix, start) suffix = append(suffix, end)
} }
return prefix, suffix return prefix, suffix
} }
@ -89,7 +89,7 @@ func (self color_type) as_sgr(number_base int, prefix, suffix []string) ([]strin
prefix = append(prefix, fmt.Sprintf("%d:5:%d", number_base+8, num)) prefix = append(prefix, fmt.Sprintf("%d:5:%d", number_base+8, num))
} }
} else { } else {
prefix = append(prefix, fmt.Sprintf("%d:2:%d", number_base+8, self.val.Red, self.val.Green, self.val.Blue)) prefix = append(prefix, fmt.Sprintf("%d:2:%d:%d:%d", number_base+8, self.val.Red, self.val.Green, self.val.Blue))
} }
return prefix, suffix return prefix, suffix
} }
@ -230,7 +230,7 @@ func (self *underline_value) from_string(val string) bool {
case "dashed": case "dashed":
ans = dashed_underline ans = dashed_underline
} }
if ans != nil_underline { if ans == nil_underline {
return false return false
} }
self.is_set = true self.is_set = true
@ -240,9 +240,9 @@ func (self *underline_value) from_string(val string) bool {
func (self underline_value) as_sgr(prefix, suffix []string) ([]string, []string) { func (self underline_value) as_sgr(prefix, suffix []string) ([]string, []string) {
if self.is_set { if self.is_set {
s, e := "0", "0" s, e := "4:0", "4:0"
if self.style != no_underline { if self.style != no_underline {
s = strconv.Itoa(int(self.style)) s = "4:" + strconv.Itoa(int(self.style))
} }
prefix = append(prefix, s) prefix = append(prefix, s)
suffix = append(suffix, e) suffix = append(suffix, e)
@ -352,18 +352,3 @@ func suffix_for_spec(spec string) string {
} }
return sb.String() return sb.String()
} }
func Styler(spec string) func(args ...interface{}) string {
p := prefix_for_spec(spec)
s := suffix_for_spec(spec)
return func(args ...interface{}) string {
body := fmt.Sprint(args...)
b := strings.Builder{}
b.Grow(len(p) + len(body) + len(s))
b.WriteString(p)
b.WriteString(body)
b.WriteString(s)
return b.String()
}
}

View File

@ -0,0 +1,38 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package style
import (
"fmt"
"testing"
)
var _ = fmt.Print
func TestANSIStyleContext(t *testing.T) {
var ctx = Context{AllowEscapeCodes: false}
sprint := ctx.SprintFunc("bold")
if sprint("test") != "test" {
t.Fatal("AllowEscapeCodes=false not respected")
}
ctx.AllowEscapeCodes = true
if sprint("test") == "test" {
t.Fatal("AllowEscapeCodes=true not respected")
}
}
func TestANSIStyleSprint(t *testing.T) {
var ctx = Context{AllowEscapeCodes: true}
test := func(spec string, prefix string, suffix string) {
actual := ctx.SprintFunc(spec)(" ")
expected := prefix + " " + suffix
if actual != expected {
t.Fatalf("Formatting with spec: %s failed expected != actual: %#v != %#v", spec, expected, actual)
}
}
test("bold", "\x1b[1m", "\x1b[22m")
test("bold fg=red u=curly", "\x1b[1;4:3;31m", "\x1b[22;4:0;39m")
}