Skip to content

Commit

Permalink
Add Mock template variable
Browse files Browse the repository at this point in the history
This is a string that will be either `Mock` or `mock`, depending on the exported-ness of the interface.
  • Loading branch information
LandonTClipp committed Apr 20, 2023
1 parent ad74577 commit b38b6b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/configuration.md
Expand Up @@ -97,11 +97,12 @@ Parameter Descriptions

| name | description |
|------|-------------|
| InterfaceDir | The path of the original interface being mocked. This can be used as `#!yaml dir: "{{.InterfaceDir}}"` to place your mocks adjacent to the original interface. This should not be used for external interfaces. |
| InterfaceDir | The path of the original interface being mocked. This can be used as <br>`#!yaml dir: "{{.InterfaceDir}}"` to place your mocks adjacent to the original interface. This should not be used for external interfaces. |
| InterfaceName | The name of the original interface being mocked |
| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName` |
| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` |
| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` |
| Mock | A string that is `Mock` if the interface is exported, or `mock` if it is not exported. Useful when setting the name of your mock to something like: <br>`#!yaml mockname: "{{.Mock}}{{.InterfaceName}}"`<br> This way, the mock name will retain the exported-ness of the original interface.
| MockName | The name of the mock that will be generated. Note that this is simply the `mockname` configuration variable |
| PackageName | The name of the package from the original interface |
| PackagePath | The fully qualified package path of the original interface |
10 changes: 10 additions & 0 deletions pkg/outputter.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"go/ast"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,13 +137,21 @@ func (*FileOutputStreamProvider) underscoreCaseName(caseName string) string {
func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interface) error {
log := zerolog.Ctx(ctx)

isExported := ast.IsExported(iface.Name)
var mock string
if isExported {
mock = "Mock"
} else {
mock = "mock"
}

Check warning on line 146 in pkg/outputter.go

View check run for this annotation

Codecov / codecov/patch

pkg/outputter.go#L145-L146

Added lines #L145 - L146 were not covered by tests
// data is the struct sent to the template parser
data := struct {
InterfaceDir string
InterfaceName string
InterfaceNameCamel string
InterfaceNameLowerCamel string
InterfaceNameSnake string
Mock string
MockName string
PackageName string
PackagePath string
Expand All @@ -152,6 +161,7 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac
InterfaceNameCamel: strcase.ToCamel(iface.Name),
InterfaceNameLowerCamel: strcase.ToLowerCamel(iface.Name),
InterfaceNameSnake: strcase.ToSnake(iface.Name),
Mock: mock,
MockName: c.MockName,
PackageName: iface.Pkg.Name(),
PackagePath: iface.Pkg.Path(),
Expand Down

0 comments on commit b38b6b0

Please sign in to comment.