test clifmt/Title + Warn + Info

This commit is contained in:
Adrien Marquès 2018-11-13 16:22:57 +01:00
parent b4d6b8d8ff
commit 6cdbf07d37
3 changed files with 52 additions and 4 deletions

View File

@ -9,7 +9,9 @@ func escape(in string) string {
out := make([]rune, 0) out := make([]rune, 0)
for _, char := range in { for _, char := range in {
if char == '\n' { if char == '\\' {
out = append(out, []rune("\\\\")...)
} else if char == '\n' {
out = append(out, []rune("\\n")...) out = append(out, []rune("\\n")...)
} else if char == '\r' { } else if char == '\r' {
out = append(out, []rune("\\r")...) out = append(out, []rune("\\r")...)

View File

@ -8,6 +8,7 @@ import (
var titleIndex = 0 var titleIndex = 0
var alignOffset = 40 var alignOffset = 40
var defaultPrinter = fmt.Printf
// Warn returns a red warning ASCII sign. If a string is given // Warn returns a red warning ASCII sign. If a string is given
// as argument, it will print it after the warning sign // as argument, it will print it after the warning sign
@ -32,7 +33,7 @@ func Info(s ...string) string {
// Title prints a formatted title (auto-indexed from local counted) // Title prints a formatted title (auto-indexed from local counted)
func Title(s string) { func Title(s string) {
titleIndex++ titleIndex++
fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false)) defaultPrinter("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false))
} }
@ -68,11 +69,11 @@ func Align(s string) {
} }
// 3. print string // 3. print string
fmt.Printf("%s", s) defaultPrinter("%s", s)
// 4. print trailing spaces // 4. print trailing spaces
for i := size; i < offset; i++ { for i := size; i < offset; i++ {
fmt.Printf(" ") defaultPrinter(" ")
} }
} }

View File

@ -0,0 +1,45 @@
package clifmt
import (
"fmt"
"testing"
)
var lastPrint = ""
func mockupPrinter(format string, args ...interface{}) (int, error) {
lastPrint = fmt.Sprintf(format, args...)
return 0, nil
}
func TestSpecial(t *testing.T) {
defaultPrinter = mockupPrinter
tests := []struct {
Pre func()
Processed string
Expect string
}{
{nil, Warn(), "\033[0;31m/!\\\033[0m"},
{nil, Warn("any text"), "\033[0;31m/!\\\033[0m any text"},
{nil, Info(), "\033[0;34m(!)\033[0m"},
{nil, Info("any text"), "\033[0;34m(!)\033[0m any text"},
{func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |1| any text \033[0;33m<<\033[0m\n"},
{func() { Title("any text") }, "", "\n\033[0;33m>>\033[0m |2| any text \033[0;33m<<\033[0m\n"},
}
for i, test := range tests {
if test.Pre != nil {
test.Pre()
test.Processed = lastPrint
}
if test.Processed != test.Expect {
t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Expect), escape(test.Processed))
}
}
}