File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ package testcase
2+
3+ import (
4+ "github.com/adamluzsi/testcase/sandbox"
5+ )
6+
7+ func Sandbox (fn func ()) sandbox.RunOutcome {
8+ return sandbox .Run (fn )
9+ }
Original file line number Diff line number Diff line change 1+ package testcase_test
2+
3+ import (
4+ "fmt"
5+ "runtime"
6+ "testing"
7+
8+ "github.com/adamluzsi/testcase"
9+ "github.com/adamluzsi/testcase/assert"
10+ "github.com/adamluzsi/testcase/random"
11+ "github.com/adamluzsi/testcase/sandbox"
12+ )
13+
14+ func ExampleSandbox () {
15+ stb := & testcase.StubTB {}
16+ outcome := testcase .Sandbox (func () {
17+ // some test helper function calls fatal, which cause runtime.Goexit after marking the test failed.
18+ stb .FailNow ()
19+ })
20+
21+ fmt .Println ("The sandbox run has finished without an issue" , outcome .OK )
22+ fmt .Println ("runtime.Goexit was called:" , outcome .Goexit )
23+ fmt .Println ("panic value:" , outcome .PanicValue )
24+ }
25+
26+ func TestSandbox_smoke (t * testing.T ) {
27+ var out sandbox.RunOutcome
28+ out = testcase .Sandbox (func () {})
29+ assert .True (t , out .OK )
30+ assert .Nil (t , out .PanicValue )
31+
32+ out = testcase .Sandbox (func () { runtime .Goexit () })
33+ assert .False (t , out .OK )
34+ assert .Nil (t , out .PanicValue )
35+
36+ expectedPanicValue := random .New (random.CryptoSeed {}).Error ()
37+ out = testcase .Sandbox (func () { panic (expectedPanicValue ) })
38+ assert .False (t , out .OK )
39+ assert .Equal [any ](t , expectedPanicValue , out .PanicValue )
40+ }
You can’t perform that action at this time.
0 commit comments