| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- package convey
- import (
- "strconv"
- "testing"
- "time"
- )
- func TestSingleScope(t *testing.T) {
- output := prepare()
- Convey("hi", t, func() {
- output += "done"
- })
- expectEqual(t, "done", output)
- }
- func TestSingleScopeWithMultipleConveys(t *testing.T) {
- output := prepare()
- Convey("1", t, func() {
- output += "1"
- })
- Convey("2", t, func() {
- output += "2"
- })
- expectEqual(t, "12", output)
- }
- func TestNestedScopes(t *testing.T) {
- output := prepare()
- Convey("a", t, func() {
- output += "a "
- Convey("bb", func() {
- output += "bb "
- Convey("ccc", func() {
- output += "ccc | "
- })
- })
- })
- expectEqual(t, "a bb ccc | ", output)
- }
- func TestNestedScopesWithIsolatedExecution(t *testing.T) {
- output := prepare()
- Convey("a", t, func() {
- output += "a "
- Convey("aa", func() {
- output += "aa "
- Convey("aaa", func() {
- output += "aaa | "
- })
- Convey("aaa1", func() {
- output += "aaa1 | "
- })
- })
- Convey("ab", func() {
- output += "ab "
- Convey("abb", func() {
- output += "abb | "
- })
- })
- })
- expectEqual(t, "a aa aaa | a aa aaa1 | a ab abb | ", output)
- }
- func TestSingleScopeWithConveyAndNestedReset(t *testing.T) {
- output := prepare()
- Convey("1", t, func() {
- output += "1"
- Reset(func() {
- output += "a"
- })
- })
- expectEqual(t, "1a", output)
- }
- func TestPanicingReset(t *testing.T) {
- output := prepare()
- Convey("1", t, func() {
- output += "1"
- Reset(func() {
- panic("nooo")
- })
- Convey("runs since the reset hasn't yet", func() {
- output += "a"
- })
- Convey("but this doesnt", func() {
- output += "nope"
- })
- })
- expectEqual(t, "1a", output)
- }
- func TestSingleScopeWithMultipleRegistrationsAndReset(t *testing.T) {
- output := prepare()
- Convey("reset after each nested convey", t, func() {
- Convey("first output", func() {
- output += "1"
- })
- Convey("second output", func() {
- output += "2"
- })
- Reset(func() {
- output += "a"
- })
- })
- expectEqual(t, "1a2a", output)
- }
- func TestSingleScopeWithMultipleRegistrationsAndMultipleResets(t *testing.T) {
- output := prepare()
- Convey("each reset is run at end of each nested convey", t, func() {
- Convey("1", func() {
- output += "1"
- })
- Convey("2", func() {
- output += "2"
- })
- Reset(func() {
- output += "a"
- })
- Reset(func() {
- output += "b"
- })
- })
- expectEqual(t, "1ab2ab", output)
- }
- func Test_Failure_AtHigherLevelScopePreventsChildScopesFromRunning(t *testing.T) {
- output := prepare()
- Convey("This step fails", t, func() {
- So(1, ShouldEqual, 2)
- Convey("this should NOT be executed", func() {
- output += "a"
- })
- })
- expectEqual(t, "", output)
- }
- func Test_Panic_AtHigherLevelScopePreventsChildScopesFromRunning(t *testing.T) {
- output := prepare()
- Convey("This step panics", t, func() {
- Convey("this happens, because the panic didn't happen yet", func() {
- output += "1"
- })
- output += "a"
- Convey("this should NOT be executed", func() {
- output += "2"
- })
- output += "b"
- panic("Hi")
- output += "nope"
- })
- expectEqual(t, "1ab", output)
- }
- func Test_Panic_InChildScopeDoes_NOT_PreventExecutionOfSiblingScopes(t *testing.T) {
- output := prepare()
- Convey("This is the parent", t, func() {
- Convey("This step panics", func() {
- panic("Hi")
- output += "1"
- })
- Convey("This sibling should execute", func() {
- output += "2"
- })
- })
- expectEqual(t, "2", output)
- }
- func Test_Failure_InChildScopeDoes_NOT_PreventExecutionOfSiblingScopes(t *testing.T) {
- output := prepare()
- Convey("This is the parent", t, func() {
- Convey("This step fails", func() {
- So(1, ShouldEqual, 2)
- output += "1"
- })
- Convey("This sibling should execute", func() {
- output += "2"
- })
- })
- expectEqual(t, "2", output)
- }
- func TestResetsAreAlwaysExecutedAfterScope_Panics(t *testing.T) {
- output := prepare()
- Convey("This is the parent", t, func() {
- Convey("This step panics", func() {
- panic("Hi")
- output += "1"
- })
- Convey("This sibling step does not panic", func() {
- output += "a"
- Reset(func() {
- output += "b"
- })
- })
- Reset(func() {
- output += "2"
- })
- })
- expectEqual(t, "2ab2", output)
- }
- func TestResetsAreAlwaysExecutedAfterScope_Failures(t *testing.T) {
- output := prepare()
- Convey("This is the parent", t, func() {
- Convey("This step fails", func() {
- So(1, ShouldEqual, 2)
- output += "1"
- })
- Convey("This sibling step does not fail", func() {
- output += "a"
- Reset(func() {
- output += "b"
- })
- })
- Reset(func() {
- output += "2"
- })
- })
- expectEqual(t, "2ab2", output)
- }
- func TestSkipTopLevel(t *testing.T) {
- output := prepare()
- SkipConvey("hi", t, func() {
- output += "This shouldn't be executed!"
- })
- expectEqual(t, "", output)
- }
- func TestSkipNestedLevel(t *testing.T) {
- output := prepare()
- Convey("hi", t, func() {
- output += "yes"
- SkipConvey("bye", func() {
- output += "no"
- })
- })
- expectEqual(t, "yes", output)
- }
- func TestSkipNestedLevelSkipsAllChildLevels(t *testing.T) {
- output := prepare()
- Convey("hi", t, func() {
- output += "yes"
- SkipConvey("bye", func() {
- output += "no"
- Convey("byebye", func() {
- output += "no-no"
- })
- })
- })
- expectEqual(t, "yes", output)
- }
- func TestIterativeConveys(t *testing.T) {
- output := prepare()
- Convey("Test", t, func() {
- for x := 0; x < 10; x++ {
- y := strconv.Itoa(x)
- Convey(y, func() {
- output += y
- })
- }
- })
- expectEqual(t, "0123456789", output)
- }
- func TestClosureVariables(t *testing.T) {
- output := prepare()
- i := 0
- Convey("A", t, func() {
- i = i + 1
- j := i
- output += "A" + strconv.Itoa(i) + " "
- Convey("B", func() {
- k := j
- j = j + 1
- output += "B" + strconv.Itoa(k) + " "
- Convey("C", func() {
- output += "C" + strconv.Itoa(k) + strconv.Itoa(j) + " "
- })
- Convey("D", func() {
- output += "D" + strconv.Itoa(k) + strconv.Itoa(j) + " "
- })
- })
- Convey("C", func() {
- output += "C" + strconv.Itoa(j) + " "
- })
- })
- output += "D" + strconv.Itoa(i) + " "
- expectEqual(t, "A1 B1 C12 A2 B2 D23 A3 C3 D3 ", output)
- }
- func TestClosureVariablesWithReset(t *testing.T) {
- output := prepare()
- i := 0
- Convey("A", t, func() {
- i = i + 1
- j := i
- output += "A" + strconv.Itoa(i) + " "
- Reset(func() {
- output += "R" + strconv.Itoa(i) + strconv.Itoa(j) + " "
- })
- Convey("B", func() {
- output += "B" + strconv.Itoa(j) + " "
- })
- Convey("C", func() {
- output += "C" + strconv.Itoa(j) + " "
- })
- })
- output += "D" + strconv.Itoa(i) + " "
- expectEqual(t, "A1 B1 R11 A2 C2 R22 D2 ", output)
- }
- func TestWrappedSimple(t *testing.T) {
- prepare()
- output := resetTestString{""}
- Convey("A", t, func() {
- func() {
- output.output += "A "
- Convey("B", func() {
- output.output += "B "
- Convey("C", func() {
- output.output += "C "
- })
- })
- Convey("D", func() {
- output.output += "D "
- })
- }()
- })
- expectEqual(t, "A B C A D ", output.output)
- }
- type resetTestString struct {
- output string
- }
- func addReset(o *resetTestString, f func()) func() {
- return func() {
- Reset(func() {
- o.output += "R "
- })
- f()
- }
- }
- func TestWrappedReset(t *testing.T) {
- prepare()
- output := resetTestString{""}
- Convey("A", t, addReset(&output, func() {
- output.output += "A "
- Convey("B", func() {
- output.output += "B "
- })
- Convey("C", func() {
- output.output += "C "
- })
- }))
- expectEqual(t, "A B R A C R ", output.output)
- }
- func TestWrappedReset2(t *testing.T) {
- prepare()
- output := resetTestString{""}
- Convey("A", t, func() {
- Reset(func() {
- output.output += "R "
- })
- func() {
- output.output += "A "
- Convey("B", func() {
- output.output += "B "
- Convey("C", func() {
- output.output += "C "
- })
- })
- Convey("D", func() {
- output.output += "D "
- })
- }()
- })
- expectEqual(t, "A B C R A D R ", output.output)
- }
- func TestInfiniteLoopWithTrailingFail(t *testing.T) {
- done := make(chan int)
- go func() {
- Convey("This fails", t, func() {
- Convey("and this is run", func() {
- So(true, ShouldEqual, true)
- })
- /* And this prevents the whole block to be marked as run */
- So(false, ShouldEqual, true)
- })
- done <- 1
- }()
- select {
- case <-done:
- return
- case <-time.After(1 * time.Millisecond):
- t.Fail()
- }
- }
- func TestOutermostResetInvokedForGrandchildren(t *testing.T) {
- output := prepare()
- Convey("A", t, func() {
- output += "A "
- Reset(func() {
- output += "rA "
- })
- Convey("B", func() {
- output += "B "
- Reset(func() {
- output += "rB "
- })
- Convey("C", func() {
- output += "C "
- Reset(func() {
- output += "rC "
- })
- })
- Convey("D", func() {
- output += "D "
- Reset(func() {
- output += "rD "
- })
- })
- })
- })
- expectEqual(t, "A B C rC rB rA A B D rD rB rA ", output)
- }
- func TestFailureOption(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureHalts, func() {
- output += "A "
- So(true, ShouldEqual, true)
- output += "B "
- So(false, ShouldEqual, true)
- output += "C "
- })
- expectEqual(t, "A B ", output)
- }
- func TestFailureOption2(t *testing.T) {
- output := prepare()
- Convey("A", t, func() {
- output += "A "
- So(true, ShouldEqual, true)
- output += "B "
- So(false, ShouldEqual, true)
- output += "C "
- })
- expectEqual(t, "A B ", output)
- }
- func TestFailureOption3(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureContinues, func() {
- output += "A "
- So(true, ShouldEqual, true)
- output += "B "
- So(false, ShouldEqual, true)
- output += "C "
- })
- expectEqual(t, "A B C ", output)
- }
- func TestFailureOptionInherit(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureContinues, func() {
- output += "A1 "
- So(false, ShouldEqual, true)
- output += "A2 "
- Convey("B", func() {
- output += "B1 "
- So(true, ShouldEqual, true)
- output += "B2 "
- So(false, ShouldEqual, true)
- output += "B3 "
- })
- })
- expectEqual(t, "A1 A2 B1 B2 B3 ", output)
- }
- func TestFailureOptionInherit2(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureHalts, func() {
- output += "A1 "
- So(false, ShouldEqual, true)
- output += "A2 "
- Convey("B", func() {
- output += "A1 "
- So(true, ShouldEqual, true)
- output += "A2 "
- So(false, ShouldEqual, true)
- output += "A3 "
- })
- })
- expectEqual(t, "A1 ", output)
- }
- func TestFailureOptionInherit3(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureHalts, func() {
- output += "A1 "
- So(true, ShouldEqual, true)
- output += "A2 "
- Convey("B", func() {
- output += "B1 "
- So(true, ShouldEqual, true)
- output += "B2 "
- So(false, ShouldEqual, true)
- output += "B3 "
- })
- })
- expectEqual(t, "A1 A2 B1 B2 ", output)
- }
- func TestFailureOptionNestedOverride(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureContinues, func() {
- output += "A "
- So(false, ShouldEqual, true)
- output += "B "
- Convey("C", FailureHalts, func() {
- output += "C "
- So(true, ShouldEqual, true)
- output += "D "
- So(false, ShouldEqual, true)
- output += "E "
- })
- })
- expectEqual(t, "A B C D ", output)
- }
- func TestFailureOptionNestedOverride2(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureHalts, func() {
- output += "A "
- So(true, ShouldEqual, true)
- output += "B "
- Convey("C", FailureContinues, func() {
- output += "C "
- So(true, ShouldEqual, true)
- output += "D "
- So(false, ShouldEqual, true)
- output += "E "
- })
- })
- expectEqual(t, "A B C D E ", output)
- }
- func TestMultipleInvocationInheritance(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureHalts, func() {
- output += "A1 "
- So(true, ShouldEqual, true)
- output += "A2 "
- Convey("B", FailureContinues, func() {
- output += "B1 "
- So(true, ShouldEqual, true)
- output += "B2 "
- So(false, ShouldEqual, true)
- output += "B3 "
- })
- Convey("C", func() {
- output += "C1 "
- So(true, ShouldEqual, true)
- output += "C2 "
- So(false, ShouldEqual, true)
- output += "C3 "
- })
- })
- expectEqual(t, "A1 A2 B1 B2 B3 A1 A2 C1 C2 ", output)
- }
- func TestMultipleInvocationInheritance2(t *testing.T) {
- output := prepare()
- Convey("A", t, FailureContinues, func() {
- output += "A1 "
- So(true, ShouldEqual, true)
- output += "A2 "
- So(false, ShouldEqual, true)
- output += "A3 "
- Convey("B", FailureHalts, func() {
- output += "B1 "
- So(true, ShouldEqual, true)
- output += "B2 "
- So(false, ShouldEqual, true)
- output += "B3 "
- })
- Convey("C", func() {
- output += "C1 "
- So(true, ShouldEqual, true)
- output += "C2 "
- So(false, ShouldEqual, true)
- output += "C3 "
- })
- })
- expectEqual(t, "A1 A2 A3 B1 B2 A1 A2 A3 C1 C2 C3 ", output)
- }
- func TestSetDefaultFailureMode(t *testing.T) {
- output := prepare()
- SetDefaultFailureMode(FailureContinues) // the default is normally FailureHalts
- defer SetDefaultFailureMode(FailureHalts)
- Convey("A", t, func() {
- output += "A1 "
- So(true, ShouldBeFalse)
- output += "A2 "
- })
- expectEqual(t, "A1 A2 ", output)
- }
- func prepare() string {
- testReporter = newNilReporter()
- return ""
- }
|