Muster
muster is for API developers to automatically generate Must functions for any function that returns a value and an error.
Sometimes it's annoying to create test structs when you need to populate a field with a function that returns (value, error).
Install 📥
go get github.com/joshcarp/muster
Problem 🔥
func TestSomeComplicatedFunc(t *testing.T){
value, _ := someFunction("blah") // This is annoying
testCase := SomeComplicatedStruct{
Foo: "Bar"
ComplicatedField:value
}
//...
}
A better alternative to this is if there was a mustSomethingFunction(string)interface{}
that panics if there is an error:
func TestSomeComplicatedFunc(t *testing.T){
testCase := SomeComplicatedStruct{
Foo: "Bar"
ComplicatedField: mustSomeFunction("blah") // Much cleaner
}
//...
}
Example 🔧
type Foo struct {
}
func Blah(s int, a Foo) (int, error) {
return 0, fmt.Errorf("")
}
then run muster .
or muster <filename>.go
or cat <filename> | muster --stream > output.txt
func MustBlah(s int, a Foo) int {
val, err := Blah(s, a)
if err != nil {
panic(err)
}
return val
}
Features 💯
- Specify more than one file
- Input from stdin
- Works with methods
- Works with external types
- Include regex
- Exclude regex
- Variadic functions