...

Source file src/github.com/sethvargo/go-password/password/mock.go

Documentation: github.com/sethvargo/go-password/password

     1  package password
     2  
     3  // Built-time checks that the generators implement the interface.
     4  var _ PasswordGenerator = (*mockGenerator)(nil)
     5  
     6  type mockGenerator struct {
     7  	result string
     8  	err    error
     9  }
    10  
    11  // NewMockGenerator creates a new generator that satisfies the PasswordGenerator
    12  // interface. If an error is provided, the error is returned. If a result if
    13  // provided, the result is always returned, regardless of what parameters are
    14  // passed into the Generate or MustGenerate methods.
    15  //
    16  // This function is most useful for tests where you want to have predicable
    17  // results for a transitive resource that depends on go-password.
    18  func NewMockGenerator(result string, err error) *mockGenerator {
    19  	return &mockGenerator{
    20  		result: result,
    21  		err:    err,
    22  	}
    23  }
    24  
    25  // Generate returns the mocked result or error.
    26  func (g *mockGenerator) Generate(int, int, int, bool, bool) (string, error) {
    27  	if g.err != nil {
    28  		return "", g.err
    29  	}
    30  	return g.result, nil
    31  }
    32  
    33  // MustGenerate returns the mocked result or panics if an error was given.
    34  func (g *mockGenerator) MustGenerate(int, int, int, bool, bool) string {
    35  	if g.err != nil {
    36  		panic(g.err)
    37  	}
    38  	return g.result
    39  }
    40  

View as plain text