Skip to content

Latest commit

 

History

History
314 lines (263 loc) · 12.5 KB

File metadata and controls

314 lines (263 loc) · 12.5 KB

Panic examples

Contents

Examples: No Panic

cat ./no_panic/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Passing test.
func Test_PASS_NoPanic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.Panic(
        func() {
            // Exit function without panicking.
        },
        "", // Permits a NoPanic wnt to be calculated by the test.
    )
}

// Failing test.
func Test_FAIL_NoPanic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.Panic(
        func() {
            panic("this is the panic generated")
        },
        // Expecting no panic to be thrown
        "",
    )
}
go test -v -cover ./no_panic

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲NoPanic}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲NoPanic ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲NoPanic}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:27: ͏ ͏unexpected ͏ ͏panic:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}{\color{green}{this ͏ ͏is ͏ ͏the ͏ ͏panic ͏ ͏generated}}}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲NoPanic ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/panic/no ̲ ̲panic ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Contents

Examples: No Panic Helper

cat ./no_panic_helper/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Passing test.
func Test_PASS_NoPanicHelper(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.NoPanic(
        func() {
            // Exit function without panicking.
        },
    )
}

// Failing test.
func Test_FAIL_NoPanicHelper(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.NoPanic(
        func() {
            panic("panic message") // Unexpected Panic
        },
    )
}
go test -v -cover ./no_panic_helper

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲NoPanicHelper}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲NoPanicHelper ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲NoPanicHelper}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:26: ͏ ͏unexpected ͏ ͏panic:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}{\color{green}{panic ͏ ͏message}}}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲NoPanicHelper ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/panic/no ̲ ̲panic ̲ ̲helper ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Contents

Examples: Panic

cat ./panic/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Passing test.
func Test_PASS_Panic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.Panic(
        func() {
            panic("expected panic message")
        },
        "expected panic message",
    )
}

// Failing test.
func Test_FAIL_Panic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    // Failure.  The invoked function's panic message
    // is not what is expected.
    chk.Panic(
        func() {
            panic("this is the panic generated")
        },
        "this is the panic wanted",
    )
}
go test -v -cover ./panic

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲Panic}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲Panic ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲Panic}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:29: ͏ ͏unexpected ͏ ͏panic:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}this ͏ ͏is ͏ ͏the ͏ ͏panic ͏ ͏{\color{darkturquoise}{genera}}ted}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}this ͏ ͏is ͏ ͏the ͏ ͏panic ͏ ͏{\color{darkturquoise}{wan}}ted}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲Panic ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/panic/panic ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Contents

Examples: Blank Panic

cat ./blank_panic/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Passing test.
func Test_PASS_BlankPanic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.Panic(
        func() {
            panic("") // Blank panic message.
        },
        sztest.BlankPanicMessage, // Panic without message is expected.
        // Message returned in place of an empty string representing
        // that an empty ("") panic was issued by the function.
    )
}

// Failing test.
func Test_FAIL_BlankPanic(t *testing.T) {
    chk := sztest.CaptureNothing(t)
    defer chk.Release()

    chk.Panic(
        func() {
            panic("") // Empty panic message will be flagged.
        },
        "",
        // sztest.BlankPanicMessage will be returned.
    )
}
go test -v -cover ./blank_panic

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲BlankPanic}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲BlankPanic ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲BlankPanic}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:29: ͏ ͏unexpected ͏ ͏panic:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}{\color{green}{sztest.BlankPanicMessage}}}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲BlankPanic ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/panic/blank ̲ ̲panic ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Contents