Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable revive comments linting #11

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,6 @@ linters-settings:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
revive:
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: increment-decrement
- name: var-declaration
- name: package-comments
- name: range
- name: time-naming
- name: errorf
- name: unreachable-code
- name: redefines-builtin-id
staticcheck:
go: "1.21"
checks: [ "all", "-SA1019"]
Expand Down Expand Up @@ -161,3 +146,6 @@ severity:
- linters:
- dupl
severity: info

issues:
exclude-use-default: false
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ emailServiceMock.Setup(
}))
```

### Verifying Method Calls

You can verify that a method has been called using the `mock.Called()` method:

```go
// Arrange
mock := registrationservice.NewMock()

// Act
mock.Instance().Register("Mark")
mock.Instance().Register("Jim")

// Assert
t.True(mock.Called(registrationservice.Register("Mark")))
t.True(mock.Called(registrationservice.Register(kelpie.Any[string]()).Times(2)))
t.False(mock.Called(registrationservice.Register("Wendy")))
```

### Times

You can configure a method call to only match a certain number of times, or verify a method has been called a certain number of times using the `Times()`, `Once()` and `Never()` helpers:

```go
// Arrange
mock := registrationservice.NewMock()

// Act
mock.Instance().Register("Mark")
mock.Instance().Register("Jim")

// Assert
t.True(mock.Called(registrationservice.Register("Mark").Once()))
t.True(mock.Called(registrationservice.Register(kelpie.Any[string]()).Times(2)))
t.True(mock.Called(registrationservice.Register("Wendy").Never()))
```

## FAQ

### What makes Kelpie so magical
Expand Down
13 changes: 8 additions & 5 deletions cmd/kelpie/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main contains the Kelpie code generator.
package main

import (
Expand All @@ -16,13 +17,13 @@ import (
//go:embed "mock.go.tmpl"
var mockTemplate string

type GenerateCmd struct {
type generateCmd struct {
SourceFile string `short:"s" required:"" env:"GOFILE" help:"The Go source file containing the interface to mock."`
Interfaces []string `short:"i" required:"" help:"The names of the interfaces to mock."`
OutputDir string `short:"o" required:"" default:"mocks" help:"The directory to write the mock out to."`
}

func (g *GenerateCmd) Run() error {
func (g *generateCmd) Run() error {
file, err := os.Open(g.SourceFile)
if err != nil {
return errors.Wrap(err, "could not open file for parsing")
Expand All @@ -43,9 +44,11 @@ func (g *GenerateCmd) Run() error {
err := func() error {
outputDirectoryName := filepath.Join(g.OutputDir, i.PackageName)
if _, err := os.Stat(outputDirectoryName); os.IsNotExist(err) {
os.MkdirAll(outputDirectoryName, 0700)
if err := os.MkdirAll(outputDirectoryName, 0700); err != nil {
return errors.Wrap(err, "could not create directory for mock")
}
}
file, err := os.Create(filepath.Join(outputDirectoryName, fmt.Sprintf("%s.go", i.PackageName)))
file, err := os.Create(filepath.Clean(filepath.Join(outputDirectoryName, fmt.Sprintf("%s.go", i.PackageName))))
if err != nil {
return errors.Wrap(err, "could not open output file")
}
Expand All @@ -67,7 +70,7 @@ func (g *GenerateCmd) Run() error {
}

var cli struct {
Generate GenerateCmd `cmd:"" help:"Generate a mock."`
Generate generateCmd `cmd:"" help:"Generate a mock."`
}

func main() {
Expand Down
37 changes: 37 additions & 0 deletions examples/called_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examples

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/adamconnelly/kelpie"
"github.com/adamconnelly/kelpie/examples/mocks/registrationservice"
)

//go:generate go run ../cmd/kelpie generate --interfaces RegistrationService
type RegistrationService interface {
Register(name string) error
}

type CalledTests struct {
suite.Suite
}

func (t *CalledTests) Test_Called_VerifiesWhetherMethodHasBeenCalled() {
// Arrange
mock := registrationservice.NewMock()

// Act
mock.Instance().Register("Mark")
mock.Instance().Register("Jim")

// Assert
t.True(mock.Called(registrationservice.Register("Mark")))
t.True(mock.Called(registrationservice.Register(kelpie.Any[string]()).Times(2)))
t.False(mock.Called(registrationservice.Register("Wendy")))
}

func TestCalled(t *testing.T) {
suite.Run(t, new(CalledTests))
}
170 changes: 170 additions & 0 deletions examples/mocks/registrationservice/registrationservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions matcher.go → kelpie.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package kelpie contains helpers for matching arguments when configuring a mock.
package kelpie

import "github.com/adamconnelly/kelpie/mocking"
Expand Down
7 changes: 7 additions & 0 deletions mocking/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ type Matcher[T comparable] struct {
func (i Matcher[T]) IsMatch(other any) bool {
return i.MatchFn(other.(T))
}

// MethodMatcher is used to match a method call to an expectation.
type MethodMatcher struct {
MethodName string
ArgumentMatchers []ArgumentMatcher
Times *uint
}
Loading
Loading