Added more tests

This commit is contained in:
Eduard Urbach 2024-03-12 15:23:08 +01:00
parent 6a802c3ad8
commit 0e7bc76a0c
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 91 additions and 55 deletions

View File

@ -4,9 +4,6 @@ import (
"fmt" "fmt"
) )
// Value is a type definition for the data type of a single color component.
type Value = float64
// Color represents an RGB color. // Color represents an RGB color.
type Color struct { type Color struct {
R Value R Value
@ -14,11 +11,6 @@ type Color struct {
B Value B Value
} }
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
func RGB(r Value, g Value, b Value) Color {
return Color{r, g, b}
}
// Print writes the text in the given color to standard output. // Print writes the text in the given color to standard output.
func (c Color) Print(args ...any) { func (c Color) Print(args ...any) {
if !Terminal { if !Terminal {

View File

@ -3,7 +3,6 @@ package color_test
import ( import (
"testing" "testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/color" "git.akyoto.dev/go/color"
) )
@ -48,35 +47,3 @@ func TestPrintln(t *testing.T) {
color.RGB(0, 1, 0).Println("green") color.RGB(0, 1, 0).Println("green")
color.RGB(0, 0, 1).Println("blue") color.RGB(0, 0, 1).Println("blue")
} }
func TestRGB(t *testing.T) {
color.Terminal = true
rgbColors := map[string]color.Color{
"black": color.RGB(0, 0, 0),
"white": color.RGB(1, 1, 1),
"gray": color.RGB(0.5, 0.5, 0.5),
"red": color.RGB(1, 0, 0),
"green": color.RGB(0, 1, 0),
"blue": color.RGB(0, 0, 1),
"cyan": color.RGB(0, 1, 1),
"yellow": color.RGB(1, 1, 0),
"orange": color.RGB(1, 0.5, 0),
"magenta": color.RGB(1, 0, 1),
}
for name, c := range rgbColors {
testColorRange(t, c)
c.Println("█ " + name)
}
}
func testColorRange(t *testing.T, c color.Color) {
assert.True(t, c.R >= 0.0)
assert.True(t, c.G >= 0.0)
assert.True(t, c.B >= 0.0)
assert.True(t, c.R <= 1.0)
assert.True(t, c.G <= 1.0)
assert.True(t, c.B <= 1.0)
}

View File

@ -18,7 +18,7 @@ func TestLCH(t *testing.T) {
"red": color.LCH(0.75, 1.0, 40), "red": color.LCH(0.75, 1.0, 40),
"orange": color.LCH(0.75, 1.0, 60), "orange": color.LCH(0.75, 1.0, 60),
"yellow": color.LCH(0.9, 1.0, 100), "yellow": color.LCH(0.9, 1.0, 100),
"green": color.LCH(0.75, 1.0, 150), "green": color.LCH(0.75, 1.0, 135),
"blue": color.LCH(0.75, 1.0, 260), "blue": color.LCH(0.75, 1.0, 260),
"cyan": color.LCH(0.75, 1.0, 210), "cyan": color.LCH(0.75, 1.0, 210),
"magenta": color.LCH(0.75, 1.0, 320), "magenta": color.LCH(0.75, 1.0, 320),

View File

@ -17,11 +17,16 @@ go get git.akyoto.dev/go/color
## Usage ## Usage
```go ```go
red := color.RGB(1.0, 0.0, 0.0) // ANSI
red.Println("red text") ansi.Red.Println("red text")
orange := color.LCH(0.7, 1.0, 65) // LCH
orange.Println("orange text") green := color.LCH(0.5, 1.0, 135)
green.Println("green text")
// RGB
blue := color.RGB(0, 0, 1)
blue.Println("blue text")
``` ```
## Tests ## Tests
@ -30,9 +35,9 @@ orange.Println("orange text")
PASS: TestPrint PASS: TestPrint
PASS: TestPrintf PASS: TestPrintf
PASS: TestPrintln PASS: TestPrintln
PASS: TestRGB
PASS: TestLCH PASS: TestLCH
PASS: TestLCHSpectrum PASS: TestLCHSpectrum
PASS: TestRGB
coverage: 100.0% of statements coverage: 100.0% of statements
``` ```

View File

@ -2,6 +2,11 @@ package color
import "math" import "math"
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
func RGB(r Value, g Value, b Value) Color {
return Color{r, g, b}
}
// inSRGB indicates whether the given color can be mapped to the sRGB color space. // inSRGB indicates whether the given color can be mapped to the sRGB color space.
func inSRGB(l Value, a Value, b Value, chroma Value) bool { func inSRGB(l Value, a Value, b Value, chroma Value) bool {
r, g, b := oklabToLinearRGB(l, a*chroma, b*chroma) r, g, b := oklabToLinearRGB(l, a*chroma, b*chroma)

40
RGB_test.go Normal file
View File

@ -0,0 +1,40 @@
package color_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/color"
)
func TestRGB(t *testing.T) {
color.Terminal = true
rgbColors := map[string]color.Color{
"black": color.RGB(0, 0, 0),
"white": color.RGB(1, 1, 1),
"gray": color.RGB(0.5, 0.5, 0.5),
"red": color.RGB(1, 0, 0),
"green": color.RGB(0, 1, 0),
"blue": color.RGB(0, 0, 1),
"cyan": color.RGB(0, 1, 1),
"yellow": color.RGB(1, 1, 0),
"orange": color.RGB(1, 0.5, 0),
"magenta": color.RGB(1, 0, 1),
}
for name, c := range rgbColors {
testColorRange(t, c)
c.Println("█ " + name)
}
}
func testColorRange(t *testing.T, c color.Color) {
assert.True(t, c.R >= 0.0)
assert.True(t, c.G >= 0.0)
assert.True(t, c.B >= 0.0)
assert.True(t, c.R <= 1.0)
assert.True(t, c.G <= 1.0)
assert.True(t, c.B <= 1.0)
}

4
Value.go Normal file
View File

@ -0,0 +1,4 @@
package color
// Value is a type definition for the data type of a single color component.
type Value = float64

View File

@ -7,17 +7,21 @@ import (
"git.akyoto.dev/go/color/ansi" "git.akyoto.dev/go/color/ansi"
) )
func Test(t *testing.T) {
color.Terminal = true
testColors()
}
func TestPrintRaw(t *testing.T) { func TestPrintRaw(t *testing.T) {
color.Terminal = false color.Terminal = false
testColors() testPrint()
testPrintf()
testPrintln()
} }
func testColors() { func TestPrint(t *testing.T) {
color.Terminal = true
testPrint()
testPrintf()
testPrintln()
}
func testPrint() {
ansi.Black.Print("Black\n") ansi.Black.Print("Black\n")
ansi.White.Print("White\n") ansi.White.Print("White\n")
ansi.Red.Print("Red\n") ansi.Red.Print("Red\n")
@ -26,7 +30,9 @@ func testColors() {
ansi.Yellow.Print("Yellow\n") ansi.Yellow.Print("Yellow\n")
ansi.Magenta.Print("Magenta\n") ansi.Magenta.Print("Magenta\n")
ansi.Cyan.Print("Cyan\n") ansi.Cyan.Print("Cyan\n")
}
func testPrintf() {
ansi.Black.Printf("%s\n", "Black") ansi.Black.Printf("%s\n", "Black")
ansi.White.Printf("%s\n", "White") ansi.White.Printf("%s\n", "White")
ansi.Red.Printf("%s\n", "Red") ansi.Red.Printf("%s\n", "Red")
@ -35,7 +41,9 @@ func testColors() {
ansi.Yellow.Printf("%s\n", "Yellow") ansi.Yellow.Printf("%s\n", "Yellow")
ansi.Magenta.Printf("%s\n", "Magenta") ansi.Magenta.Printf("%s\n", "Magenta")
ansi.Cyan.Printf("%s\n", "Cyan") ansi.Cyan.Printf("%s\n", "Cyan")
}
func testPrintln() {
ansi.Black.Println("Black") ansi.Black.Println("Black")
ansi.White.Println("White") ansi.White.Println("White")
ansi.Red.Println("Red") ansi.Red.Println("Red")

View File

@ -1,6 +1,9 @@
package tty package tty
import "golang.org/x/sys/unix"
// IsTerminal returns true if the file descriptor is a terminal. // IsTerminal returns true if the file descriptor is a terminal.
func IsTerminal(fd uintptr) bool { func IsTerminal(fd uintptr) bool {
return false _, err := unix.IoctlGetTermios(int(fd), unix.TIOCGETA)
return err == nil
} }

12
tty/Terminal_test.go Normal file
View File

@ -0,0 +1,12 @@
package tty_test
import (
"os"
"testing"
"git.akyoto.dev/go/color/tty"
)
func TestIsTerminal(t *testing.T) {
tty.IsTerminal(os.Stdout.Fd())
}