Skip to content

Commit

Permalink
now decorates with correct values
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat Ryer authored and Mat Ryer committed Nov 14, 2014
1 parent 70e131d commit 668c682
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 47 deletions.
43 changes: 43 additions & 0 deletions decorate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package is

import (
"bytes"
"fmt"
"runtime"
"strings"
)

// decorate prefixes the string with the file and line of the call site
// and inserts the final newline if needed and indentation tabs for formatting.
// this function was copied from the testing framework.
func decorate(s string) string {
_, file, line, ok := runtime.Caller(4) // decorate + log + public function.
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
buf := new(bytes.Buffer)
// Every line is indented at least one tab.
buf.WriteByte('\t')
fmt.Fprintf(buf, "%s:%d: ", file, line)
lines := strings.Split(s, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
// Second and subsequent lines are indented an extra tab.
buf.WriteString("\n\t\t")
}
buf.WriteString(line)
}
buf.WriteByte('\n')
return buf.String()
}
48 changes: 36 additions & 12 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package is
import (
"fmt"
"reflect"
"sync"
)

// I represents the is interface.
Expand All @@ -24,13 +25,27 @@ type I interface {
// failures.
// testing.T satisfied this interface.
type T interface {
Fatal(...interface{})
Fatalf(string, ...interface{})
FailNow()
}

// i represents an implementation of interface I.
type i struct {
t T
t T
last string
l sync.Mutex
}

func (i *i) Log(args ...interface{}) {
i.l.Lock()
i.last = fmt.Sprint(args...)
fmt.Print(decorate(i.last))
i.l.Unlock()
}
func (i *i) Logf(format string, args ...interface{}) {
i.l.Lock()
i.last = fmt.Sprint(fmt.Sprintf(format, args...))
fmt.Print(decorate(i.last))
i.l.Unlock()
}

// OK asserts that the specified objects are all OK.
Expand All @@ -44,7 +59,8 @@ func (i *i) OK(o ...interface{}) {
// considered equal. Non strict.
func (i *i) Equal(a, b interface{}) {
if !areEqual(a, b) {
i.t.Fatalf("%v != %v", a, b)
i.Logf("%v != %v", a, b)
i.t.FailNow()
}
}

Expand All @@ -59,7 +75,8 @@ func (i *i) Panic(fn func()) {
fn()
}()
if r == nil {
i.t.Fatal("expected panic")
i.Log("expected panic")
i.t.FailNow()
}
}

Expand All @@ -74,7 +91,8 @@ func (i *i) PanicWith(m string, fn func()) {
fn()
}()
if r != m {
i.t.Fatalf("expected panic: \"%s\"", m)
i.Logf("expected panic: \"%s\"", m)
i.t.FailNow()
}
}

Expand Down Expand Up @@ -109,29 +127,35 @@ func (i *i) isOK(o interface{}) {
co()
}()
if r != nil {
i.t.Fatalf("unexpected panic: %v", r)
i.Logf("unexpected panic: %v", r)
i.t.FailNow()
}
case error:
if co != nil {
i.t.Fatal("unexpected error: " + co.Error())
i.Log("unexpected error: " + co.Error())
i.t.FailNow()
}
case string:
if len(co) == 0 {
i.t.Fatal("unexpected \"\"")
i.Log("unexpected \"\"")
i.t.FailNow()
}
case bool:
// false
if co == false {
i.t.Fatal("unexpected false")
i.Log("unexpected false")
i.t.FailNow()
return
}
}
if isNil(o) {
i.t.Fatal("unexpected nil")
i.Log("unexpected nil")
i.t.FailNow()
return
}
if o == 0 {
i.t.Fatal("unexpected zero")
i.Log("unexpected zero")
i.t.FailNow()
}
}

Expand Down
64 changes: 29 additions & 35 deletions is_test.go
Original file line number Diff line number Diff line change
@@ -1,123 +1,117 @@
package is_test
package is

import (
"errors"
"fmt"
"testing"

"github.com/metabition/is"
)

type mockT struct {
fail string
failed bool
}

func (m *mockT) Fatal(a ...interface{}) {
m.fail = fmt.Sprint(a...)
}
func (m *mockT) Fatalf(f string, a ...interface{}) {
m.fail = fmt.Sprintf(f, a...)
func (m *mockT) FailNow() {
m.failed = true
}
func (m *mockT) Failed() bool {
return len(m.fail) > 0
return m.failed
}

func TestIs(t *testing.T) {

for _, test := range []struct {
N string
F func(is is.I)
F func(is I)
Fail string
}{
// is.OK
{
N: "OK(false)",
F: func(is is.I) {
F: func(is I) {
is.OK(false)
},
Fail: "unexpected false",
}, {
N: "OK(true)",
F: func(is is.I) {
F: func(is I) {
is.OK(true)
},
Fail: "",
}, {
N: "OK(nil)",
F: func(is is.I) {
F: func(is I) {
is.OK(nil)
},
Fail: "unexpected nil",
}, {
N: "OK(1,2,3)",
F: func(is is.I) {
F: func(is I) {
is.OK(1, 2, 3)
},
}, {
N: "OK(0)",
F: func(is is.I) {
F: func(is I) {
is.OK(0)
},
Fail: "unexpected zero",
}, {
N: "OK(1)",
F: func(is is.I) {
F: func(is I) {
is.OK(1)
},
}, {
N: "OK(\"\")",
F: func(is is.I) {
F: func(is I) {
is.OK("")
},
Fail: "unexpected \"\"",
}, {
N: "OK(errors.New(\"an error\"))",
F: func(is is.I) {
F: func(is I) {
is.OK(errors.New("an error"))
},
Fail: "unexpected error: an error",
}, {
N: "OK(func) panic",
F: func(is is.I) {
F: func(is I) {
is.OK(func() {
panic("panic message")
})
},
Fail: "unexpected panic: panic message",
}, {
N: "OK(func) no panic",
F: func(is is.I) {
F: func(is I) {
is.OK(func() {})
},
},
// is.Panic
{
N: "PanicWith(\"panic message\", func(){ panic() })",
F: func(is is.I) {
F: func(is I) {
is.PanicWith("panic message", func() {
panic("panic message")
})
},
},
{
N: "PanicWith(\"panic message\", func(){ /* no panic */ })",
F: func(is is.I) {
F: func(is I) {
is.PanicWith("panic message", func() {
})
},
Fail: "expected panic: \"panic message\"",
},
{
N: "Panic(func(){ panic() })",
F: func(is is.I) {
F: func(is I) {
is.Panic(func() {
panic("panic message")
})
},
},
{
N: "Panic(func(){ /* no panic */ })",
F: func(is is.I) {
F: func(is I) {
is.Panic(func() {
})
},
Expand All @@ -126,35 +120,35 @@ func TestIs(t *testing.T) {
// is.Equal
{
N: "Equal(1,1)",
F: func(is is.I) {
F: func(is I) {
is.Equal(1, 1)
},
}, {
N: "Equal(1,2)",
F: func(is is.I) {
F: func(is I) {
is.Equal(1, 2)
},
Fail: "1 != 2",
}, {
N: "Equal(1,nil)",
F: func(is is.I) {
F: func(is I) {
is.Equal(1, nil)
},
Fail: "1 != <nil>",
}, {
N: "Equal(nil,1)",
F: func(is is.I) {
F: func(is I) {
is.Equal(nil, 1)
},
Fail: "<nil> != 1",
}, {
N: "Equal(false,false)",
F: func(is is.I) {
F: func(is I) {
is.Equal(false, false)
},
}, {
N: "Equal(map1,map2)",
F: func(is is.I) {
F: func(is I) {
is.Equal(
map[string]interface{}{"package": "is"},
map[string]interface{}{"package": "is"},
Expand All @@ -163,7 +157,7 @@ func TestIs(t *testing.T) {
}} {

tt := new(mockT)
is := is.New(tt)
is := New(tt)
var rec interface{}

func() {
Expand All @@ -177,8 +171,8 @@ func TestIs(t *testing.T) {
if !tt.Failed() {
t.Errorf("%s should fail", test.N)
}
if test.Fail != tt.fail {
t.Errorf("expected fail \"%s\" but was \"%s\".", test.Fail, tt.fail)
if test.Fail != is.(*i).last {
t.Errorf("expected fail \"%s\" but was \"%s\".", test.Fail, is.(*i).last)
}
}

Expand Down

0 comments on commit 668c682

Please sign in to comment.