Skip to content

Commit

Permalink
test: ✅ add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Equationzhao committed May 9, 2024
1 parent e2a7f73 commit 3de3480
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/align/left_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package align

import (
"testing"

"github.com/zeebo/assert"
)

func TestIsLeft(t *testing.T) {
Register("name")
assert.Equal(t, true, IsLeft("name"))
assert.Equal(t, false, IsLeft("name1"))
}

func TestIsLeftHeaderFooter(t *testing.T) {
RegisterHeaderFooter("name")
assert.Equal(t, true, IsLeftHeaderFooter("name"))
assert.Equal(t, false, IsLeftHeaderFooter("name1"))
}
86 changes: 86 additions & 0 deletions internal/util/safeSlice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package util

import (
"sync"
"testing"
)

func TestSlice_AppendTo(t *testing.T) {
s := NewSlice[int](10)
s.AppendTo(1)
s.AppendTo(2)
s.AppendTo(3)
if s.Len() != 3 {
t.Errorf("AppendTo failed")
}
}

func TestSlice_At(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
if s.At(1) != 2 {
t.Errorf("At failed")
}
}

func TestSlice_Clear(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
s.Clear()
if s.Len() != 0 {
t.Errorf("Clear failed")
}
}

func TestSlice_GetRaw(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
gotRaw := s.GetRaw()
if len(*gotRaw) != 3 {
t.Errorf("GetRaw failed")
}
if gotRaw != &s.data {
t.Errorf("GetRaw failed")
}
}

func TestSlice_GetCopy(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
copied := s.GetCopy()
if len(copied) != 3 {
t.Errorf("GetCopy failed")
}
if &copied == &s.data {
t.Errorf("GetCopy failed")
}
}

func TestSlice_Len(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
if s.Len() != 3 {
t.Errorf("Len failed")
}
}

func TestSlice_Set(t *testing.T) {
s := Slice[int]{
data: []int{1, 2, 3},
m: sync.RWMutex{},
}
s.Set(1, 4)
if s.At(1) != 4 {
t.Errorf("Set failed")
}
}
13 changes: 13 additions & 0 deletions internal/util/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import (
"testing"
)

func TestSafeSet(t *testing.T) {
set := NewSet[string]()
set.Add("name")
if !set.Contains("name") {
t.Errorf("Add failed")
}
}
86 changes: 86 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,89 @@ func TestSplitNumberAndUnit(t *testing.T) {
})
}
}

func TestMakeLink(t *testing.T) {
link := MakeLink("abs", "name")
if link != "\033]8;;abs\033\\name\033]8;;\033\\" {
t.Errorf("MakeLink failed")
}
}

func TestRemoveSep(t *testing.T) {
sep := RemoveSep("a/b/c")
if sep != "a/b/c" {
t.Errorf("RemoveSep failed")
}
sep = RemoveSep("a/b/c/")
if sep != "a/b/c" {
t.Errorf("RemoveSep failed")
}
}

func TestEscape(t *testing.T) {
type args struct {
a string
}
tests := []struct {
name string
args args
want string
}{
{
name: "tab",
args: args{
a: "\t",
},
want: "\x1b[7m\\t\x1b[27m",
},
{
name: "carriage return",
args: args{
a: "\r",
},
want: "\x1b[7m\\r\x1b[27m",
},
{
name: "line feed",
args: args{
a: "\n",
},
want: "\x1b[7m\\n\x1b[27m",
},
{
name: "double quote",
args: args{
a: "\"",
},
want: "\x1b[7m\\\"\x1b[27m",
},
{
name: "backslash",
args: args{
a: "\\",
},
want: "\x1b[7m\\\\\x1b[27m",
},
{
name: "single quote",
args: args{
a: "'",
},
want: "'",
},
{
name: "normal",
args: args{
a: "normal",
},
want: "normal",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Escape(tt.args.a); got != tt.want {
t.Errorf("Escape() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3de3480

Please sign in to comment.