File: chalk_test.go

package info (click to toggle)
golang-github-ttacon-chalk 0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 164 kB
  • sloc: makefile: 5
file content (83 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package chalk

import (
	"fmt"
	"testing"
)

func TestColor(t *testing.T) {
	col := Color{1}
	if col.Value() != 1 {
		t.Errorf("col.Value() != 1, was %d", col.Value())
	}

	expected := "\u001b[31m"
	if col.String() != expected {
		t.Errorf("expected col.String() to be %q, was %q", expected, col.String())
	}

	expected = fmt.Sprintf("%shello%s", expected, ResetColor)
	if col.Color("hello") != expected {
		t.Errorf("expected col.Color() to be %q, was %q", expected, col.Color("hello"))
	}
}

func TestTextStyle(t *testing.T) {
	text := TextStyle{1, 22}

	expected := "\u001b[1m\u001b[22m"
	if text.String() != expected {
		t.Errorf("expected text.String() to be %q, was %q", expected, text.String())
	}

	expected = "\u001b[1mhello\u001b[22m"
	if text.TextStyle("hello") != expected {
		t.Errorf("expected text.TextStyle() to be %q, was %q", expected, text.String())
	}

	empty := TextStyle{}
	if empty.TextStyle("hello") != "hello" {
		t.Errorf("expected empty.TextStyle() to be %q, was %q",
			expected,
			empty.String())
	}
}

func TestStyle(t *testing.T) {
	colStyle := Red.NewStyle()
	expected := "\u001b[40m\u001b[31m"
	if colStyle.String() != expected {
		t.Errorf("expected colStyle.String() to be %q, was %q",
			expected,
			colStyle.String())
	}

	textStyle := Bold.NewStyle()
	expected = "\u001b[40m\u001b[30m\u001b[1mhello\u001b[22m\u001b[49m\u001b[39m"
	if textStyle.Style("hello") != expected {
		t.Errorf("expected textStyle.Style(\"hello\") to be %q, was %q",
			expected,
			textStyle.Style("hello"))
	}

	// reset it
	colStyle.Foreground(ResetColor)
	colStyle.Background(ResetColor)
	expected = "\u001b[49m\u001b[39m"
	if colStyle.String() != expected {
		t.Errorf("expected colStyle.String() to be %q, was %q",
			expected,
			colStyle.String())
	}

	builtStyle := colStyle.
		WithForeground(Red).
		WithBackground(Blue).
		WithTextStyle(Underline)
	expected = "\u001b[44m\u001b[31m\u001b[4mhello\u001b[24m\u001b[49m\u001b[39m"
	if builtStyle.Style("hello") != expected {
		t.Errorf("expected builtStyle.Style() to be %q, was %q",
			expected,
			builtStyle.Style("hello"))
	}
}