Skip to content

Commit

Permalink
test: added unit tests for new colors and Log method
Browse files Browse the repository at this point in the history
  • Loading branch information
Pho3b committed Dec 20, 2023
1 parent f2fb560 commit 3652cc0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions colors/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package colors

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIsColorValid(t *testing.T) {
assert.True(t, IsColorValid(Red))
assert.True(t, IsColorValid(Magenta))
assert.True(t, IsColorValid(Yellow))
assert.True(t, IsColorValid(Cyan))
assert.True(t, IsColorValid(Gray))
assert.True(t, IsColorValid(White))
assert.True(t, IsColorValid(Black))
assert.False(t, IsColorValid(" "))
assert.False(t, IsColorValid("lfdjlfkasd"))
assert.False(t, IsColorValid("\\testno"))
}
21 changes: 21 additions & 0 deletions logs/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logs
import (
"bytes"
"github.com/stretchr/testify/assert"
"gitlab.com/docebo/libraries/go/tiny-logger/colors"
"io"
"os"
"testing"
Expand Down Expand Up @@ -135,6 +136,26 @@ func TestLogger_Error(t *testing.T) {
assert.Contains(t, buf.String(), testLog)
}

func TestLogger_Log(t *testing.T) {
var buf bytes.Buffer
testLog := "my GENERIC log test"
originalStdOut := os.Stdout
r, w, _ := os.Pipe()

os.Stdout = w
logger := NewLogger()
logger.Log("jldfald", testLog)
logger.Log(colors.Black, testLog)
logger.Log(colors.Cyan, testLog)

_ = w.Close()
_, _ = io.Copy(&buf, r)
os.Stdout = originalStdOut
assert.Contains(t, buf.String(), colors.White)
assert.Contains(t, buf.String(), colors.Black)
assert.Contains(t, buf.String(), colors.Cyan)
}

func TestLogger_BuildingMethods(t *testing.T) {
logger := NewLogger()
assert.IsType(t, &Logger{}, logger)
Expand Down
20 changes: 20 additions & 0 deletions logs/package_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logs
import (
"bytes"
"github.com/stretchr/testify/assert"
"gitlab.com/docebo/libraries/go/tiny-logger/colors"
"io"
"os"
"testing"
Expand Down Expand Up @@ -123,3 +124,22 @@ func TestPackageLogger_Error(t *testing.T) {
os.Stderr = originalStdErr
assert.Contains(t, buf.String(), testLog)
}

func TestPackageLogger_Log(t *testing.T) {
var buf bytes.Buffer
testLog := "my GENERIC log test"
originalStdOut := os.Stdout
r, w, _ := os.Pipe()

os.Stdout = w
Log("jldfald", testLog)
Log(colors.Black, testLog)
Log(colors.Cyan, testLog)

_ = w.Close()
_, _ = io.Copy(&buf, r)
os.Stdout = originalStdOut
assert.Contains(t, buf.String(), colors.White)
assert.Contains(t, buf.String(), colors.Black)
assert.Contains(t, buf.String(), colors.Cyan)
}

0 comments on commit 3652cc0

Please sign in to comment.