2021-01-23 20:29:22 +00:00
|
|
|
package term
|
2021-01-09 17:54:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2021-01-09 18:48:29 +00:00
|
|
|
"github.com/mickael-menu/zk/core/style"
|
2021-01-10 15:39:10 +00:00
|
|
|
"github.com/mickael-menu/zk/util/test/assert"
|
2021-01-09 17:54:50 +00:00
|
|
|
)
|
|
|
|
|
2021-01-23 20:29:22 +00:00
|
|
|
func createTerminal() *Terminal {
|
2021-01-09 17:54:50 +00:00
|
|
|
color.NoColor = false // Otherwise the color codes are not injected during tests
|
2021-01-23 20:15:17 +00:00
|
|
|
return New()
|
2021-01-09 17:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStyleNoRule(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
res, err := createTerminal().Style("Hello")
|
2021-01-09 17:54:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, res, "Hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStyleOneRule(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
res, err := createTerminal().Style("Hello", style.Rule("red"))
|
2021-01-09 17:54:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, res, "\033[31mHello\033[0m")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStyleMultipleRule(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
res, err := createTerminal().Style("Hello", style.Rule("red"), style.Rule("bold"))
|
2021-01-09 17:54:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, res, "\033[31;1mHello\033[0m")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStyleUnknownRule(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
_, err := createTerminal().Style("Hello", style.Rule("unknown"))
|
2021-01-09 17:54:50 +00:00
|
|
|
assert.Err(t, err, "unknown styling rule: unknown")
|
|
|
|
}
|
|
|
|
|
2021-01-23 14:24:08 +00:00
|
|
|
func TestStyleEmptyString(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
res, err := createTerminal().Style("", style.Rule("bold"))
|
2021-01-23 14:24:08 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, res, "")
|
|
|
|
}
|
|
|
|
|
2021-01-09 17:54:50 +00:00
|
|
|
func TestStyleAllRules(t *testing.T) {
|
2021-01-23 20:29:22 +00:00
|
|
|
styler := createTerminal()
|
2021-01-09 17:54:50 +00:00
|
|
|
test := func(rule string, expected string) {
|
2021-01-09 18:48:29 +00:00
|
|
|
res, err := styler.Style("Hello", style.Rule(rule))
|
2021-01-09 17:54:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, res, "\033["+expected+"Hello\033[0m")
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:48:29 +00:00
|
|
|
test("title", "1;33m")
|
2021-01-10 11:29:57 +00:00
|
|
|
test("path", "4;36m")
|
2021-01-19 20:05:16 +00:00
|
|
|
test("term", "31m")
|
2021-01-23 20:15:17 +00:00
|
|
|
test("emphasis", "1;36m")
|
2021-02-21 09:38:51 +00:00
|
|
|
test("understate", "2m")
|
2021-01-09 18:48:29 +00:00
|
|
|
|
2021-01-09 17:54:50 +00:00
|
|
|
test("bold", "1m")
|
|
|
|
test("faint", "2m")
|
|
|
|
test("italic", "3m")
|
|
|
|
test("underline", "4m")
|
2021-01-19 19:51:28 +00:00
|
|
|
test("blink", "5m")
|
|
|
|
test("reverse", "7m")
|
2021-01-09 17:54:50 +00:00
|
|
|
test("hidden", "8m")
|
|
|
|
test("strikethrough", "9m")
|
|
|
|
|
|
|
|
test("black", "30m")
|
|
|
|
test("red", "31m")
|
|
|
|
test("green", "32m")
|
|
|
|
test("yellow", "33m")
|
|
|
|
test("blue", "34m")
|
|
|
|
test("magenta", "35m")
|
|
|
|
test("cyan", "36m")
|
|
|
|
test("white", "37m")
|
|
|
|
|
|
|
|
test("black-bg", "40m")
|
|
|
|
test("red-bg", "41m")
|
|
|
|
test("green-bg", "42m")
|
|
|
|
test("yellow-bg", "43m")
|
|
|
|
test("blue-bg", "44m")
|
|
|
|
test("magenta-bg", "45m")
|
|
|
|
test("cyan-bg", "46m")
|
|
|
|
test("white-bg", "47m")
|
|
|
|
|
|
|
|
test("bright-black", "90m")
|
|
|
|
test("bright-red", "91m")
|
|
|
|
test("bright-green", "92m")
|
|
|
|
test("bright-yellow", "93m")
|
|
|
|
test("bright-blue", "94m")
|
|
|
|
test("bright-magenta", "95m")
|
|
|
|
test("bright-cyan", "96m")
|
|
|
|
test("bright-white", "97m")
|
|
|
|
|
|
|
|
test("bright-black-bg", "100m")
|
|
|
|
test("bright-red-bg", "101m")
|
|
|
|
test("bright-green-bg", "102m")
|
|
|
|
test("bright-yellow-bg", "103m")
|
|
|
|
test("bright-blue-bg", "104m")
|
|
|
|
test("bright-magenta-bg", "105m")
|
|
|
|
test("bright-cyan-bg", "106m")
|
|
|
|
test("bright-white-bg", "107m")
|
|
|
|
}
|