Theta-Dev
a9db2aed02
All checks were successful
continuous-integration/drone/push Build is passing
add UseExt options to gotry_generate
69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package try
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestStrHelper_noThrow(t *testing.T) {
|
|
s := String(noThrow())
|
|
if s != "test" {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestStrHelper_throw(t *testing.T) {
|
|
var err Err
|
|
defer Return(&err)
|
|
|
|
String(throw())
|
|
|
|
t.Fail() // If everything works we are never here
|
|
}
|
|
|
|
func TestStrStrHelper(t *testing.T) {
|
|
s1, s2 := StrStr(twoStrNoThrow())
|
|
if s1 != "test" || s2 != "test" {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func Example_copyFile() {
|
|
copyFile := func(src, dst string) (err Err) {
|
|
defer Annotate(&err, fmt.Sprintf("copy %s %s", src, dst))
|
|
|
|
// These helpers are as fast as Check() calls
|
|
r := File(os.Open(src))
|
|
defer Check(r.Close())
|
|
|
|
w := File(os.Create(dst))
|
|
defer Handle(&err, func() {
|
|
os.Remove(dst)
|
|
})
|
|
defer Check(w.Close())
|
|
Empty(io.Copy(w, r))
|
|
return nil
|
|
}
|
|
|
|
err := copyFile("/notfound/path/file.go", "/notfound/path/file.bak")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
//nolint:lll
|
|
// Output: copy /notfound/path/file.go /notfound/path/file.bak: open /notfound/path/file.go: no such file or directory
|
|
}
|
|
|
|
func BenchmarkTry_StringHelper(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
_ = String(noThrow())
|
|
}
|
|
}
|
|
|
|
func BenchmarkTry_XHelper(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
_ = X(noThrow()).(string)
|
|
}
|
|
}
|