File: mock.go

package info (click to toggle)
golang-github-sethvargo-go-password 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package password

// Built-time checks that the generators implement the interface.
var _ PasswordGenerator = (*mockGenerator)(nil)

type mockGenerator struct {
	result string
	err    error
}

// NewMockGenerator creates a new generator that satisfies the PasswordGenerator
// interface. If an error is provided, the error is returned. If a result if
// provided, the result is always returned, regardless of what parameters are
// passed into the Generate or MustGenerate methods.
//
// This function is most useful for tests where you want to have predicable
// results for a transitive resource that depends on go-password.
func NewMockGenerator(result string, err error) *mockGenerator {
	return &mockGenerator{
		result: result,
		err:    err,
	}
}

// Generate returns the mocked result or error.
func (g *mockGenerator) Generate(int, int, int, bool, bool) (string, error) {
	if g.err != nil {
		return "", g.err
	}
	return g.result, nil
}

// MustGenerate returns the mocked result or panics if an error was given.
func (g *mockGenerator) MustGenerate(int, int, int, bool, bool) string {
	if g.err != nil {
		panic(g.err)
	}
	return g.result
}