Skip to content

Instantly share code, notes, and snippets.

View petergtz's full-sized avatar
😊

Peter Götz petergtz

😊
View GitHub Profile
func CopyFile(src, dst string) fails_with error {
r := os.Open(src) on_error err fail fmt.Errorf("in Open %s %s: %v", src, dst, err)
defer r.Close() on_error {}
w := os.Create(dst) on_error err fail fmt.Errorf("in Create %s %s: %v", src, dst, err)
io.Copy(w, r) on_error err {
w.Close() on_error {} // explicitly ignore error
os.Remove(dst) on_error {} // explicitly ignore error
fail fmt.Errorf("in Copy %s %s: %v", src, dst, err)
// Stubbing:
When(updater.NotifyUploadSucceeded(AnyString(), AnyString(), AnyString())).
ThenReturn(fmt.Errorf("Some error"))
// Exercise object under test:
handler.AddOrReplace(responseWriter, ..., map[string]string{"identifier": "someguid"})
// Verification:
blobstore.VerifyWasCalledOnce().Put(EqString("someguid"), AnyReadSeeker())
updater.VerifyWasCalled(Never()).NotifyUploadFailed(AnyString(), AnyError())
phoneBook := NewMockPhoneBook()
// Stubbing:
When(phoneBook.GetPhoneNumber(AnyString())).Then(func(params []Param) ReturnValues {
return []ReturnValue{fmt.Sprintf("1-800-CALL-%v", strings.ToUpper(params[0]))}
},
// Prints "1-800-CALL-DAN":
fmt.Println(phoneBook.GetPhoneNumber("Dan"))
When(phoneBook.GetPhoneNumber(AnyString())).ThenReturn("123-456-789")
// Interface:
type PhoneBook interface {
GetPhoneNumber(name string) string
}
// Test:
// creating the mock
phoneBook := NewMockPhoneBook()
@petergtz
petergtz / resource_handler_test_snippet.go
Last active April 22, 2018 19:23
An example of how to use Pegomock's InOrderContext
// Create mocks:
blobstore := NewMockBlobstore()
updater := NewMockUpdater()
// Create object under test:
handler := NewResourceHandlerWithUpdater(blobstore, updater, ...)
...
// Make request against the Bits-Service resource handler:
handler.AddOrReplace(responseWriter, ..., map[string]string{"identifier": "someguid"})
virtualGuestService, e := softlayerClient.GetSoftLayer_Virtual_Guest_Service()
vms, e := virtualGuestService.GetVMs()
if e != nil {
return cli.NewExitError(e.Error(), 1)
}
@petergtz
petergtz / error_handling_example.go
Last active August 2, 2017 22:02
A simple example of a new error handling concept in Go
func Caller(arg int) {
result := SomeFunction(arg) on_error e {
fmt.Printf("Wrong input: %v", e)
return
}
fmt.Printf("Result: %v", result)
}
func SomeFunction(arg int) int fails_with error {
if arg == 42 {
@petergtz
petergtz / softlayer_snippet_with_new_error_handling.go
Created July 31, 2017 21:47
Some Go code with new error handling
virtualGuestService := softlayerClient.GetSoftLayer_Virtual_Guest_Service() or_on_error e {
fail_with cli.NewExitError(e.Error(), 1)
}
server := cliContext.GlobalString("server")
port := cliContext.GlobalInt("port")
if server == "" {
fail_with cli.NewExitError("server not provided as input flag", 1)
}
@petergtz
petergtz / softlayer_snippet.go
Created July 31, 2017 20:41
Some Go code with a semantic error in the error handling
virtualGuestService, e := softlayerClient.GetSoftLayer_Virtual_Guest_Service()
if e != nil {
return cli.NewExitError(e.Error(), 1)
}
server := cliContext.GlobalString("server")
port := cliContext.GlobalInt("port")
if server == "" {
return cli.NewExitError(e.Error(), 1)