Skip to content

Latest commit

 

History

History
308 lines (251 loc) · 12.1 KB

File metadata and controls

308 lines (251 loc) · 12.1 KB

Error Examples

Contents

Examples: No Error

cat ./no_error/example_test.go
package example

import (
    "errors"
    "testing"

    "github.com/dancsecs/sztest"
)

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

    err := error(nil)

    chk.Err(
        err,
        "", // Empty string represents nil error.  Can be calculated.
    )
}

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

    err := errors.New("unexpected error")

    chk.Err(
        err,
        "", // Empty string represents nil error.  Can be calculated.
    )
}
go test -v -cover ./no_error

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

Contents

Examples: No Error Helper

cat ./no_error_helper/example_test.go
package example

import (
    "errors"
    "testing"

    "github.com/dancsecs/sztest"
)

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

    err := error(nil)

    chk.NoErr(err)
}

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

    err := errors.New("unexpected error")

    chk.NoErr(err)
}
go test -v -cover ./no_error_helper

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

Contents

Examples: Error

cat ./Error/example_test.go
package example

import (
    "errors"
    "testing"

    "github.com/dancsecs/sztest"
)

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

    err := errors.New("error condition")

    chk.Err(
        err,
        "error condition",
    )
}

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

    err := errors.New("error condition generated")
    chk.Err(
        err,
        "error condition wanted",
    )
}
go test -v -cover ./Error

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

Contents

Examples: Blank Error

cat ./blank_error/example_test.go
package example

import (
    "errors"
    "testing"

    "github.com/dancsecs/sztest"
)

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

    err := errors.New("")

    chk.Err(
        // blank "" error will be replaced with "sztest.EmptyErrorMessage"
        err,
        sztest.BlankErrorMessage,
    )
}

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

    err := errors.New("")
    chk.Err(
        // blank "" error will be replaced with "sztest.EmptyErrorMessage"
        err,
        "Error message wanted",
    )
}
go test -v -cover ./blank_error

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲BlankError}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲BlankError ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲BlankError}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:30: ͏ ͏unexpected ͏ ͏err:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}{\color{green}{sztest.Blank}}Error{\color{darkturquoise}{M}}essage}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}Error{\color{darkturquoise}{ ͏ ͏m}}essage{\color{red}{ ͏ ͏wanted}}}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲BlankError ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/error/blank ̲ ̲error ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Contents