2018-09-05 19:56:22 +02:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"gopkg.in/check.v1"
|
2018-11-07 21:58:22 +01:00
|
|
|
"netbsd.org/pkglint/intqa"
|
2018-09-05 19:56:22 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
type Suite struct {
|
|
|
|
Tracer Tracer
|
|
|
|
}
|
2018-09-05 19:56:22 +02:00
|
|
|
|
|
|
|
var _ = check.Suite(new(Suite))
|
|
|
|
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (t *Tracer) onlyArguments(args ...interface{}) {
|
|
|
|
defer t.Call(args...)()
|
|
|
|
t.Stepf("Running %q", "code")
|
2018-09-05 19:56:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (t *Tracer) argumentsAndResult(arg0 string, arg1 int) (result string) {
|
|
|
|
defer t.Call(arg0, arg1, t.Result(&result))()
|
|
|
|
t.Stepf("Running %q", "code")
|
2018-09-05 19:56:22 +02:00
|
|
|
return "the result"
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (t *Tracer) argumentsAndResultWrong(arg0 string, arg1 int) (result string) {
|
|
|
|
defer t.Call(arg0, arg1, result)() // "result" is evaluated too early and only once.
|
|
|
|
t.Stepf("Running %q", "code")
|
2018-09-05 19:56:22 +02:00
|
|
|
return "the result"
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (s *Suite) Test_Tracer_Call__only_arguments(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
2018-09-05 19:56:22 +02:00
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer.onlyArguments("arg0", 1234)
|
2018-09-05 19:56:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Tracer).onlyArguments(\"arg0\", 1234)\n"+
|
2018-09-05 19:56:22 +02:00
|
|
|
"TRACE: 1 Running \"code\"\n"+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Tracer).onlyArguments(\"arg0\", 1234)\n")
|
2018-09-05 19:56:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (s *Suite) Test_Tracer_Call__arguments_and_result(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
2018-09-05 19:56:22 +02:00
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer.argumentsAndResult("arg0", 1234)
|
2018-09-05 19:56:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Tracer).argumentsAndResult(\"arg0\", 1234)\n"+
|
2018-09-05 19:56:22 +02:00
|
|
|
"TRACE: 1 Running \"code\"\n"+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Tracer).argumentsAndResult(\"arg0\", 1234, \"=>\", \"the result\")\n")
|
2018-09-05 19:56:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (s *Suite) Test_Tracer_Call__arguments_and_result_wrong(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
2018-09-05 19:56:22 +02:00
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer.argumentsAndResultWrong("arg0", 1234)
|
2018-09-05 19:56:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Tracer).argumentsAndResultWrong(\"arg0\", 1234, \"\")\n"+
|
2018-09-05 19:56:22 +02:00
|
|
|
"TRACE: 1 Running \"code\"\n"+
|
2018-11-07 21:58:22 +01:00
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Tracer).argumentsAndResultWrong(\"arg0\", 1234, \"\")\n")
|
2018-09-05 19:56:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 21:12:13 +02:00
|
|
|
func (s *Suite) Test__fixed_argument_variants(c *check.C) {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer := &s.Tracer
|
2018-10-09 21:12:13 +02:00
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
2018-11-07 21:58:22 +01:00
|
|
|
defer tracer.Call0()()
|
|
|
|
defer tracer.Call1("x")()
|
|
|
|
defer tracer.Call2("x", "y")()
|
|
|
|
tracer.Step1("step %s", "a")
|
|
|
|
tracer.Step2("step %s, %s", "a", "b")
|
2018-10-09 21:12:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1()\n"+
|
|
|
|
"TRACE: 1 + netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1(\"x\")\n"+
|
|
|
|
"TRACE: 1 2 + netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1(\"x\", \"y\")\n"+
|
|
|
|
"TRACE: 1 2 3 step a\n"+
|
|
|
|
"TRACE: 1 2 3 step a, b\n"+
|
|
|
|
"TRACE: 1 2 - netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1(\"x\", \"y\")\n"+
|
|
|
|
"TRACE: 1 - netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1(\"x\")\n"+
|
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Suite).Test__fixed_argument_variants.func1()\n")
|
|
|
|
}
|
|
|
|
|
2019-12-03 00:32:09 +01:00
|
|
|
func (s *Suite) Test_Tracer_Call__Stringer_arg(c *check.C) {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer := &s.Tracer
|
2018-10-09 21:12:13 +02:00
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
2019-12-03 00:32:09 +01:00
|
|
|
defer tracer.Call(stringer{}, &stringer{})()
|
2018-10-09 21:12:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
2019-12-03 00:32:09 +01:00
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Suite).Test_Tracer_Call__Stringer_arg.func1(It's a string, It's a string)\n"+
|
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Suite).Test_Tracer_Call__Stringer_arg.func1(It's a string, It's a string)\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) Test_Tracer_Call__GoStringer_arg(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
|
|
|
|
|
|
|
output := s.captureTracingOutput(func() {
|
|
|
|
defer tracer.Call(goStringer{}, &goStringer{})()
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Check(output, check.Equals, ""+
|
|
|
|
"TRACE: + netbsd.org/pkglint/trace.(*Suite).Test_Tracer_Call__GoStringer_arg.func1(\"It's a string\", \"It's a string\")\n"+
|
|
|
|
"TRACE: - netbsd.org/pkglint/trace.(*Suite).Test_Tracer_Call__GoStringer_arg.func1(\"It's a string\", \"It's a string\")\n")
|
2018-10-09 21:12:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (s *Suite) Test_Tracer_traceCall__panic(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
|
|
|
|
2018-10-09 21:12:13 +02:00
|
|
|
c.Check(
|
2018-11-07 21:58:22 +01:00
|
|
|
func() { tracer.traceCall() },
|
2018-10-09 21:12:13 +02:00
|
|
|
check.Panics,
|
|
|
|
"Internal pkglint error: calls to trace.Call must only occur in tracing mode")
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
func (s *Suite) Test_Tracer_Result__panic(c *check.C) {
|
|
|
|
tracer := &s.Tracer
|
|
|
|
|
2018-10-09 21:12:13 +02:00
|
|
|
c.Check(
|
2018-11-07 21:58:22 +01:00
|
|
|
func() { tracer.Result("invalid argument") },
|
2018-10-09 21:12:13 +02:00
|
|
|
check.Panics,
|
|
|
|
"Result must be called with a pointer to the result, not \"invalid argument\".")
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:56:22 +02:00
|
|
|
func (s *Suite) captureTracingOutput(action func()) string {
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer := &s.Tracer
|
|
|
|
|
2018-09-05 19:56:22 +02:00
|
|
|
out := bytes.Buffer{}
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer.Out = &out
|
|
|
|
tracer.Tracing = true
|
2018-09-05 19:56:22 +02:00
|
|
|
|
|
|
|
action()
|
|
|
|
|
2018-11-07 21:58:22 +01:00
|
|
|
tracer.Tracing = false
|
|
|
|
tracer.Out = nil
|
2018-09-05 19:56:22 +02:00
|
|
|
return out.String()
|
|
|
|
}
|
2018-10-09 21:12:13 +02:00
|
|
|
|
2019-12-03 00:32:09 +01:00
|
|
|
type stringer struct{}
|
2018-10-09 21:12:13 +02:00
|
|
|
|
2019-12-03 00:32:09 +01:00
|
|
|
func (stringer) String() string { return "It's a string" }
|
|
|
|
|
|
|
|
type goStringer struct{}
|
|
|
|
|
|
|
|
func (goStringer) GoString() string { return "\"It's a string\"" }
|
2018-11-07 21:58:22 +01:00
|
|
|
|
2019-12-03 00:32:09 +01:00
|
|
|
func Test__qa(t *testing.T) {
|
|
|
|
ck := intqa.NewQAChecker(t.Errorf)
|
2019-11-19 07:51:38 +01:00
|
|
|
ck.Configure("*", "*", "*", -intqa.EMissingTest)
|
2018-11-07 21:58:22 +01:00
|
|
|
ck.Check()
|
|
|
|
}
|