Improved tests

This commit is contained in:
Eduard Urbach 2024-03-05 19:10:27 +01:00
parent aae983fd5b
commit 13f4c5fb0f
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 19 additions and 12 deletions

View File

@ -24,6 +24,7 @@ func RGB(r Component, g Component, b Component) Color {
func (c Color) Fprint(writer io.Writer, text string) { func (c Color) Fprint(writer io.Writer, text string) {
if !Terminal { if !Terminal {
fmt.Fprint(writer, text) fmt.Fprint(writer, text)
return
} }
fmt.Fprintf(writer, format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) fmt.Fprintf(writer, format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
@ -33,6 +34,7 @@ func (c Color) Fprint(writer io.Writer, text string) {
func (c Color) Print(text string) { func (c Color) Print(text string) {
if !Terminal { if !Terminal {
fmt.Print(text) fmt.Print(text)
return
} }
fmt.Printf(format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) fmt.Printf(format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
@ -42,6 +44,7 @@ func (c Color) Print(text string) {
func (c Color) Println(text string) { func (c Color) Println(text string) {
if !Terminal { if !Terminal {
fmt.Println(text) fmt.Println(text)
return
} }
fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)

View File

@ -1,7 +1,7 @@
package color_test package color_test
import ( import (
"os" "io"
"testing" "testing"
"git.akyoto.dev/go/assert" "git.akyoto.dev/go/assert"
@ -9,6 +9,16 @@ import (
) )
func TestColors(t *testing.T) { func TestColors(t *testing.T) {
color.Terminal = true
testColors(t)
}
func TestNoColors(t *testing.T) {
color.Terminal = false
testColors(t)
}
func testColors(t *testing.T) {
colors := map[string]color.Color{ colors := map[string]color.Color{
"black": color.RGB(0.0, 0.0, 0.0), "black": color.RGB(0.0, 0.0, 0.0),
"white": color.RGB(1.0, 1.0, 1.0), "white": color.RGB(1.0, 1.0, 1.0),
@ -26,14 +36,8 @@ func TestColors(t *testing.T) {
assert.True(t, value.G <= 1.0) assert.True(t, value.G <= 1.0)
assert.True(t, value.B <= 1.0) assert.True(t, value.B <= 1.0)
color.Terminal = true value.Fprint(io.Discard, name)
value.Fprint(os.Stdout, name)
value.Print(name) value.Print(name)
value.Println(name) value.Println(name)
color.Terminal = false
value.Fprint(os.Stdout, "-")
value.Print("-")
value.Println("-")
} }
} }

View File

@ -16,7 +16,7 @@ go get git.akyoto.dev/go/color
```go ```go
red := color.RGB(1.0, 0.0, 0.0) red := color.RGB(1.0, 0.0, 0.0)
fmt.Println(red.Format("red text")) red.Println("red text")
``` ```
## Tests ## Tests
@ -29,8 +29,8 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkRGB-12 1000000000 0.3131 ns/op 0 B/op 0 allocs/op BenchmarkRGB-12 1000000000 0.3139 ns/op 0 B/op 0 allocs/op
BenchmarkFprint-12 6455497 186.2 ns/op 0 B/op 0 allocs/op BenchmarkFprint-12 25758495 45.38 ns/op 0 B/op 0 allocs/op
``` ```
## License ## License

View File

@ -7,7 +7,7 @@ import (
// These constants represent the escape codes needed to display color in terminals. // These constants represent the escape codes needed to display color in terminals.
const ( const (
format = "\x1b[38;2;%d;%d;%dm%s\x1b[0m" format = "\x1b[38;2;%d;%d;%dm%s\x1b[0m"
formatLine = format + "\n" formatLine = "\x1b[38;2;%d;%d;%dm%s\n\x1b[0m"
) )
// Terminal is a boolean that indicates if we're in a terminal with truecolor support. // Terminal is a boolean that indicates if we're in a terminal with truecolor support.