Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey committed Nov 12, 2015
1 parent 350c3d5 commit 7864803
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 44 deletions.
44 changes: 0 additions & 44 deletions output.go
Expand Up @@ -3,7 +3,6 @@ package xlog
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"log/syslog"
Expand Down Expand Up @@ -170,14 +169,6 @@ func newJSONSyslogOutput(network, address string, prio syslog.Priority, tag stri
return NewJSONOutput(s), nil
}

const (
red = 31
green = 32
yellow = 33
blue = 34
gray = 37
)

type consoleOutput struct {
w io.Writer
}
Expand Down Expand Up @@ -244,41 +235,6 @@ func (o consoleOutput) Write(fields map[string]interface{}) error {
return err
}

func colorPrint(w io.Writer, s string, color int) {
//w.Write([]byte{0x1b, '[', byte('0' + color), 'm'})
fmt.Fprintf(w, "\x1b[%dm", color)
w.Write([]byte(s))
w.Write([]byte("\x1b[0m"))
}

func needsQuotedValueRune(r rune) bool {
return r <= ' ' || r == '=' || r == '"'
}

func writeValue(w io.Writer, v interface{}) (err error) {
switch v := v.(type) {
case nil:
_, err = w.Write([]byte("null"))
case string:
if strings.IndexFunc(v, needsQuotedValueRune) != -1 {
var b []byte
b, err = json.Marshal(v)
if err == nil {
w.Write(b)
}
} else {
_, err = w.Write([]byte(v))
}
case error:
s := v.Error()
err = writeValue(w, s)
default:
s := fmt.Sprint(v)
err = writeValue(w, s)
}
return
}

type logfmtOutput struct {
w io.Writer
}
Expand Down
53 changes: 53 additions & 0 deletions util.go
@@ -0,0 +1,53 @@
package xlog

import (
"encoding/json"
"fmt"
"io"
"strings"
)

type color int

const (
red color = 31
green color = 32
yellow color = 33
blue color = 34
gray color = 37
)

func colorPrint(w io.Writer, s string, c color) {
w.Write([]byte{0x1b, '[', byte('0' + c/10), byte('0' + c%10), 'm'})
w.Write([]byte(s))
w.Write([]byte("\x1b[0m"))
}

func needsQuotedValueRune(r rune) bool {
return r <= ' ' || r == '=' || r == '"'
}

// writeValue writes a value on the writer in a logfmt compatible way
func writeValue(w io.Writer, v interface{}) (err error) {
switch v := v.(type) {
case nil:
_, err = w.Write([]byte("null"))
case string:
if strings.IndexFunc(v, needsQuotedValueRune) != -1 {
var b []byte
b, err = json.Marshal(v)
if err == nil {
w.Write(b)
}
} else {
_, err = w.Write([]byte(v))
}
case error:
s := v.Error()
err = writeValue(w, s)
default:
s := fmt.Sprint(v)
err = writeValue(w, s)
}
return
}
56 changes: 56 additions & 0 deletions util_test.go
@@ -0,0 +1,56 @@
package xlog

import (
"bytes"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestColorPrint(t *testing.T) {
buf := &bytes.Buffer{}
colorPrint(buf, "test", red)
assert.Equal(t, "\x1b[31mtest\x1b[0m", buf.String())
buf.Reset()
colorPrint(buf, "test", green)
assert.Equal(t, "\x1b[32mtest\x1b[0m", buf.String())
buf.Reset()
colorPrint(buf, "test", yellow)
assert.Equal(t, "\x1b[33mtest\x1b[0m", buf.String())
buf.Reset()
colorPrint(buf, "test", blue)
assert.Equal(t, "\x1b[34mtest\x1b[0m", buf.String())
buf.Reset()
colorPrint(buf, "test", gray)
assert.Equal(t, "\x1b[37mtest\x1b[0m", buf.String())
}

func TestNeedsQuotedValueRune(t *testing.T) {
assert.True(t, needsQuotedValueRune('='))
assert.True(t, needsQuotedValueRune('"'))
assert.True(t, needsQuotedValueRune(' '))
assert.False(t, needsQuotedValueRune('a'))
assert.False(t, needsQuotedValueRune('\''))
}

func TestWriteValue(t *testing.T) {
buf := &bytes.Buffer{}
write := func(v interface{}) string {
buf.Reset()
err := writeValue(buf, v)
if err == nil {
return buf.String()
}
return ""
}
assert.Equal(t, `foobar`, write(`foobar`))
assert.Equal(t, `"foo=bar"`, write(`foo=bar`))
assert.Equal(t, `"foo bar"`, write(`foo bar`))
assert.Equal(t, `"foo\"bar"`, write(`foo"bar`))
assert.Equal(t, `"foo\nbar"`, write("foo\nbar"))
assert.Equal(t, `null`, write(nil))
assert.Equal(t, `"2000-01-02 03:04:05 +0000 UTC"`, write(time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)))
assert.Equal(t, `"error \"with quote\""`, write(errors.New(`error "with quote"`)))
}

0 comments on commit 7864803

Please sign in to comment.