Skip to content

Commit

Permalink
Minor refactoring of logging capabilities for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remogatto committed Jan 18, 2014
1 parent 8835d68 commit b12c92c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
25 changes: 16 additions & 9 deletions log_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ import (
"unsafe"
)

var ctag *C.char = C.CString("Mandala")
const (
// The default logcat tag for the framework
MandalaLogcatTag = "Mandala"
)

func init() {
log.SetOutput(AndroidWriter{})
log.SetOutput(AndroidWriter{Tag: MandalaLogcatTag})
}

type AndroidWriter []byte
type AndroidWriter struct {
buf []byte
Tag string
}

func (buf AndroidWriter) Write(p []byte) (n int, err error) {
func (w AndroidWriter) Write(p []byte) (n int, err error) {
ctag := C.CString(w.Tag)
n = len(p)
err = nil
for nlidx := bytes.IndexByte(p, '\n'); nlidx != -1; nlidx = bytes.IndexByte(p, '\n') {
buf = append(buf, p[:nlidx]...)
w.buf = append(w.buf, p[:nlidx]...)
p = p[nlidx+1:]
buf = append(buf, 0)
cstr := (*C.char)(unsafe.Pointer(&buf[0]))
w.buf = append(w.buf, 0)
cstr := (*C.char)(unsafe.Pointer(&w.buf[0]))
C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
buf = buf[:0]
w.buf = w.buf[:0]
}
buf = append(buf, p...)
w.buf = append(w.buf, p...)
return
}
27 changes: 10 additions & 17 deletions test/src/testlib/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
package testlib

import (
"github.com/remogatto/mandala"
"github.com/remogatto/prettytest"
"log"
"path/filepath"
"github.com/remogatto/prettytest"
)

const formatTag = "%s\t"
Expand Down Expand Up @@ -49,29 +48,23 @@ type TDDFormatter struct {
log *log.Logger
}

func NewTDDFormatter() *TDDFormatter {
return &TDDFormatter{
log: log.New(mandala.AndroidWriter{}, "[mandala-test] ", 0),
}
}

func (formatter *TDDFormatter) PrintSuiteInfo(suite *prettytest.Suite) {
formatter.log.Printf("%s.%s:\n\n", suite.Package, suite.Name)
log.Printf("%s.%s:\n\n", suite.Package, suite.Name)
}

func (formatter *TDDFormatter) PrintStatus(testFunc *prettytest.TestFunc) {
callerName := testFunc.Name
switch testFunc.Status {
case prettytest.STATUS_FAIL:
formatter.log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelFAIL, callerName, len(testFunc.Assertions))
log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelFAIL, callerName, len(testFunc.Assertions))
case prettytest.STATUS_MUST_FAIL:
formatter.log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelMUSTFAIL, callerName, len(testFunc.Assertions))
log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelMUSTFAIL, callerName, len(testFunc.Assertions))
case prettytest.STATUS_PASS:
formatter.log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPASS, callerName, len(testFunc.Assertions))
log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPASS, callerName, len(testFunc.Assertions))
case prettytest.STATUS_PENDING:
formatter.log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPENDING, callerName, len(testFunc.Assertions))
log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPENDING, callerName, len(testFunc.Assertions))
case prettytest.STATUS_NO_ASSERTIONS:
formatter.log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelNOASSERTIONS, callerName, len(testFunc.Assertions))
log.Printf(formatTag+"%-30s(%d assertion(s))\n", labelNOASSERTIONS, callerName, len(testFunc.Assertions))

}
}
Expand All @@ -81,17 +74,17 @@ func (formatter *TDDFormatter) PrintErrorLog(logs []*prettytest.Error) {
currentTestFuncHeader := ""
for _, error := range logs {
if currentTestFuncHeader != error.TestFunc.Name {
formatter.log.Printf("\n%s:\n", error.TestFunc.Name)
log.Printf("\n%s:\n", error.TestFunc.Name)
}
filename := filepath.Base(error.Assertion.Filename)
formatter.log.Printf("\t(%s:%d) %s\n", filename, error.Assertion.Line, error.Assertion.ErrorMessage)
log.Printf("\t(%s:%d) %s\n", filename, error.Assertion.Line, error.Assertion.ErrorMessage)
currentTestFuncHeader = error.TestFunc.Name
}
}
}

func (formatter *TDDFormatter) PrintFinalReport(report *prettytest.FinalReport) {
formatter.log.Printf("%d tests, %d passed, %d failed, %d expected failures, %d pending, %d with no assertions\n",
log.Printf("%d tests, %d passed, %d failed, %d expected failures, %d pending, %d with no assertions\n",
report.Total(), report.Passed, report.Failed, report.ExpectedFailures, report.Pending, report.NoAssertions)
}

Expand Down
4 changes: 4 additions & 0 deletions window_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ type window struct {
eglState *platform.EGLState
}

// SwapBuffers swaps the surface with the display actually showing the
// result of rendering.
func (win *window) SwapBuffers() {
egl.SwapBuffers(win.eglState.Display, win.eglState.Surface)
}

// MakeContextCurrent bind the OpenGL context to the current thread.
func (win *window) MakeContextCurrent() {
if ok := egl.MakeCurrent(
win.eglState.Display,
Expand All @@ -30,6 +33,7 @@ func (win *window) MakeContextCurrent() {
}
}

// GetSize returns the dimension of the rendering surface.
func (win *window) GetSize() (int, int) {
return win.eglState.SurfaceWidth, win.eglState.SurfaceHeight
}
Expand Down

0 comments on commit b12c92c

Please sign in to comment.