<commit_before>package httplog

import (
	"fmt"
	"io"
	"testing"

	"github.com/pkg/errors"
)

func TestAddError(t *testing.T) {
	var got string
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() { logPrint = old }()

	const want = `[error] whoops err="EOF"`

	entry := fallbackLogger{}
	entry.AddError(io.EOF)
	entry.Error("whoops")

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func TestAddErrorWrapped(t *testing.T) {
	var got string
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() { logPrint = old }()

	const want = `[error] whoops err="unexpected eof: EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:c:11, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:b:10, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:a:9, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:TestAddErrorWrapped:41"`

	entry := fallbackLogger{}
	entry.AddError(a())
	entry.Error("whoops")

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func recoverPanic(f func()) (got string) {
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() {
		if perr := recover(); perr != nil {
			perror, ok := perr.(error)
			if !ok {
				perror = fmt.Errorf("%v", perr)
			}

			perror = errors.WithStack(perror)

			entry := fallbackLogger{}
			entry.AddError(perror)
			entry.Error("panic recover")
		}
		logPrint = old
	}()

	f()

	return
}

func TestPanic(t *testing.T) {
	const want = `[error] panic recover err="EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic.func2:59, ` +
		`runtime/panic.go:gopanic:458, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:cPanic:15, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:bPanic:14, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:aPanic:13, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic:68, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:TestPanic:83"`

	got := recoverPanic(aPanic)

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func TestWrappedPanic(t *testing.T) {
	const want = `[error] panic recover err="unexpected eof: EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic.func2:59, ` +
		`runtime/panic.go:gopanic:458, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:cPanicWrapped:19, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:bPanicWrapped:18, ` +<commit_msg>test: update expected output for go1.8.3<commit_after>package httplog

import (
	"fmt"
	"io"
	"testing"

	"github.com/pkg/errors"
)

func TestAddError(t *testing.T) {
	var got string
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() { logPrint = old }()

	const want = `[error] whoops err="EOF"`

	entry := fallbackLogger{}
	entry.AddError(io.EOF)
	entry.Error("whoops")

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func TestAddErrorWrapped(t *testing.T) {
	var got string
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() { logPrint = old }()

	const want = `[error] whoops err="unexpected eof: EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:c:11, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:b:10, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:a:9, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:TestAddErrorWrapped:41"`

	entry := fallbackLogger{}
	entry.AddError(a())
	entry.Error("whoops")

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func recoverPanic(f func()) (got string) {
	old := logPrint
	logPrint = func(v ...interface{}) { got = fmt.Sprint(v...) }
	defer func() {
		if perr := recover(); perr != nil {
			perror, ok := perr.(error)
			if !ok {
				perror = fmt.Errorf("%v", perr)
			}

			perror = errors.WithStack(perror)

			entry := fallbackLogger{}
			entry.AddError(perror)
			entry.Error("panic recover")
		}
		logPrint = old
	}()

	f()

	return
}

func TestPanic(t *testing.T) {
	const want = `[error] panic recover err="EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic.func2:59, ` +
		`runtime/panic.go:gopanic:489, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:cPanic:15, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:bPanic:14, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:aPanic:13, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic:68, ` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:TestPanic:83"`

	got := recoverPanic(aPanic)

	if want != got {
		t.Errorf("\nwant:\n\t%s\ngot:\n\t%s", want, got)
	}
}

func TestWrappedPanic(t *testing.T) {
	const want = `[error] panic recover err="unexpected eof: EOF" stacktrace="` +
		`github.com/judwhite/httplog/fallbackLogger_test.go:recoverPanic.func2:59, ` +
		`runtime/panic.go:gopanic:489, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:cPanicWrapped:19, ` +
		`github.com/judwhite/httplog/fallbackLogger_helpers_test.go:bPanicWrapped:18, ` +
{'fraction_non_alphanumeric': 0.1736460078168621, 'fraction_numerical': 0.012841987716359575, 'mean_word_length': 4.363273453093813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2321290', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2169, 'n_words': 319}