Skip to content

Commit

Permalink
feat: support common test methods (#94)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
tkrop committed Oct 20, 2024
1 parent 428d035 commit 0d047d6
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 41 deletions.
133 changes: 103 additions & 30 deletions internal/mock/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ func methodsMockIFaceFunc(mocktest, test, mock string) []*Method {
}, {
Name: "err", Type: "error",
}},
Variadic: false,
}, {
Name: "CallC",
Params: []*Param{{
Name: "test", Type: aliasType(test, "Context"),
}},
Results: []*Param{},
Variadic: false,
Results: []*Param{},
}}
}

Expand Down Expand Up @@ -172,13 +170,25 @@ var (
pathTest, pathTesting, pathMock)

methodsTestTest = []*Method{{
Name: "Cleanup",
Params: []*Param{
{Name: "cleanup", Type: "func()"},
},
Results: []*Param{},
}, {
Name: "Deadline",
Params: []*Param{},
Results: []*Param{
{Name: "deadline", Type: "time.Time"},
{Name: "ok", Type: "bool"},
},
Variadic: false,
}, {
Name: "Error",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: true,
}, {
Name: "Errorf",
Params: []*Param{
Expand All @@ -188,10 +198,24 @@ var (
Results: []*Param{},
Variadic: true,
}, {
Name: "FailNow",
Params: []*Param{},
Name: "Fail",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "FailNow",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "Failed",
Params: []*Param{},
Results: []*Param{{Type: "bool"}},
}, {
Name: "Fatal",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: false,
Variadic: true,
}, {
Name: "Fatalf",
Params: []*Param{
Expand All @@ -201,36 +225,76 @@ var (
Results: []*Param{},
Variadic: true,
}, {
Name: "Helper",
Params: []*Param{},
Results: []*Param{},
Variadic: false,
Name: "Helper",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "Name",
Params: []*Param{},
Results: []*Param{{Type: "string"}},
Variadic: false,
Name: "Log",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: true,
}, {
Name: "Parallel",
Params: []*Param{},
Name: "Logf",
Params: []*Param{
{Name: "format", Type: "string"},
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: false,
Variadic: true,
}, {
Name: "Name",
Params: []*Param{},
Results: []*Param{{Type: "string"}},
}, {
Name: "Parallel",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "Setenv",
Params: []*Param{
{Name: "key", Type: "string"},
{Name: "value", Type: "string"},
},
Results: []*Param{},
}, {
Name: "Skip",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: true,
}, {
Name: "SkipNow",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "Skipf",
Params: []*Param{
{Name: "format", Type: "string"},
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: false,
Variadic: true,
}, {
Name: "TempDir",
Params: []*Param{},
Results: []*Param{{Type: "string"}},
Variadic: false,
Name: "Skipped",
Params: []*Param{},
Results: []*Param{{Type: "bool"}},
}, {
Name: "TempDir",
Params: []*Param{},
Results: []*Param{{Type: "string"}},
}}

methodsTestReporter = []*Method{{
Name: "Error",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: true,
}, {
Name: "Errorf",
Params: []*Param{
{Name: "format", Type: "string"},
Expand All @@ -239,10 +303,20 @@ var (
Results: []*Param{},
Variadic: true,
}, {
Name: "FailNow",
Params: []*Param{},
Name: "Fail",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "FailNow",
Params: []*Param{},
Results: []*Param{},
}, {
Name: "Fatal",
Params: []*Param{
{Name: "args", Type: "[]any"},
},
Results: []*Param{},
Variadic: false,
Variadic: true,
}, {
Name: "Fatalf",
Params: []*Param{
Expand All @@ -252,10 +326,9 @@ var (
Results: []*Param{},
Variadic: true,
}, {
Name: "Panic",
Params: []*Param{{Name: "arg", Type: "any"}},
Results: []*Param{},
Variadic: false,
Name: "Panic",
Params: []*Param{{Name: "arg", Type: "any"}},
Results: []*Param{},
}}

methodsGoMockTestReporter = []*Method{{
Expand Down
48 changes: 46 additions & 2 deletions test/caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ type Caller struct {
path string
}

// Error is the caller reporter function to capture the callers file and line
// number of the `Error` call.
func (c *Caller) Error(_ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
}

// Errorf is the caller reporter function to capture the callers file and line
// number of the `Errorf` call.
func (c *Caller) Errorf(_ string, _ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
}

// Fatal is the caller reporter function to capture the callers file and line
// number of the `Fatal` call.
func (c *Caller) Fatal(_ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}

// Fatalf is the caller reporter function to capture the callers file and line
// number of the `Fatalf` call.
func (c *Caller) Fatalf(_ string, _ ...any) {
Expand All @@ -35,6 +50,14 @@ func (c *Caller) Fatalf(_ string, _ ...any) {
panic("finished") // prevents goexit.
}

// Fail is the caller reporter function to capture the callers file and line
// number of the `Fail` call.
func (c *Caller) Fail() {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}

// FailNow is the caller reporter function to capture the callers file and line
// number of the `FailNow` call.
func (c *Caller) FailNow() {
Expand All @@ -43,6 +66,8 @@ func (c *Caller) FailNow() {
panic("finished") // prevents goexit.
}

// Panic is the caller reporter function to capture the callers file and line
// number of the `Panic` call.
func (c *Caller) Panic(_ any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
Expand All @@ -67,14 +92,26 @@ func getCaller(call func(t test.Reporter)) string {
}

var (
// CallerError provides the file with line number of the `Error` call.
CallerError = getCaller(func(t test.Reporter) {
t.Error("fail")
})
// CallerErrorf provides the file with line number of the `Errorf` call.
CallerErrorf = getCaller(func(t test.Reporter) {
t.Errorf("fail")
})
// CallerFatal provides the file with line number of the `Fatal` call.
CallerFatal = getCaller(func(t test.Reporter) {
t.Fatal("fail")
})
// CallerFatalf provides the file with line number of the `Fatalf` call.
CallerFatalf = getCaller(func(t test.Reporter) {
t.Fatalf("fail")
})
// CallerFail provides the file with line number of the `Fail` call.
CallerFail = getCaller(func(t test.Reporter) {
t.Fail()
})
// CallerFailNow provides the file with line number of the `FailNow` call.
CallerFailNow = getCaller(func(t test.Reporter) {
t.FailNow()
Expand All @@ -92,10 +129,17 @@ var (
}
return dir
}()
// CallerTestError provides the file with the line number of the `Error`
// call in the test context implementation.
CallerTestError = path.Join(SourceDir, "context.go:356")
// CallerReporterErrorf provides the file with the line number of the
// `Errorf` call in the test reporter/validator implementation.
CallerReporterError = path.Join(SourceDir, "gomock.go:60")

// CallerTestErrorf provides the file with the line number of the `Errorf`
// call in the test context implementation.
CallerTestErrorf = path.Join(SourceDir, "context.go:276")
CallerTestErrorf = path.Join(SourceDir, "context.go:374")
// CallerReporterErrorf provides the file with the line number of the
// `Errorf` call in the test reporter/validator implementation.
CallerReporterErrorf = path.Join(SourceDir, "gomock.go:61")
CallerReporterErrorf = path.Join(SourceDir, "gomock.go:82")
)
Loading

0 comments on commit 0d047d6

Please sign in to comment.