go-testdoc runs your code documentation examples during normal test time.
go get -u github.com/Eun/go-testdoc
This example will run the go code inside the Example
and Examples
section using yaegi.
// example.go file
package example
// IsLoggedIn returns true if the current user has access to the service.
//
// Example:
// if IsLoggedIn() {
// fmt.Println("You are logged in")
// }
func IsLoggedIn() bool {
return true
}
// CurrentUser returns the current username of the logged in user.
//
// Examples:
// fmt.Println(CurrentUser())
//
// if IsLoggedIn() {
// fmt.Println(CurrentUser())
// }
func CurrentUser() string {
return "Joe Doe"
}
// example_test.go file
package example_test
import (
"testing"
"github.com/Eun/go-testdoc"
)
func TestDocumentation(t *testing.T) {
testdoc.TestCodeDocumentation(t, &testdoc.Options{
// Test for this folder
Path: ".",
// Test the `example` package
PkgName: "example",
// Execute code inside the `Example` and `Examples` sections
Sections: []string{"Example", "Examples"},
Imports: []testdoc.Import{
// Import some standard packages we need
{Name: "", Path: "fmt"},
// Import the current package so we can call the functions.
{Name: ".", Path: "./"},
},
})
}