This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When(phoneBook.GetPhoneNumber(AnyString())).ThenReturn("123-456-789") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface: | |
type PhoneBook interface { | |
GetPhoneNumber(name string) string | |
} | |
// Test: | |
// creating the mock | |
phoneBook := NewMockPhoneBook() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
virtualGuestService, e := softlayerClient.GetSoftLayer_Virtual_Guest_Service() | |
vms, e := virtualGuestService.GetVMs() | |
if e != nil { | |
return cli.NewExitError(e.Error(), 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder