Skip to content

Latest commit

 

History

History
272 lines (225 loc) · 13.2 KB

File metadata and controls

272 lines (225 loc) · 13.2 KB

Got / Want Examples

Contents

Examples: Integer With No Message

This is the simplest form of a builtin Got/Wnt test. Just comparing the wanted value with the gotten value and an error registered if they are not equal. No additional context information is provided.

cat ./integer_with_no_message/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Example function being tested.
func addIntegers(int1, int2 int) int {
    return int1 + int2
}

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

    chk.Int(
        addIntegers(2, 3), // Got.
        5,                 // Want.
    )
}

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

    chk.Int(
        addIntegers(1237456, 1000), // Got.
        1237456,                    // Want.
    )
}
go test -v -cover ./integer_with_no_message

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲IntegerWithNoMessage}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲IntegerWithNoMessage ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲IntegerWithNoMessage}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:30: ͏ ͏unexpected ͏ ͏int:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}123{\color{darkturquoise}{8}}456}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}123{\color{darkturquoise}{7}}456}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲IntegerWithNoMessage ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/got ̲ ̲wnt/integer ̲ ̲with ̲ ̲no ̲ ̲message ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Here the failing test has its fourth number highlighted as changed while the passing test produced no output.

Contents

Examples: Float32 With Unformatted Message

This example shows the float Got/Wnt test with an unformatted message. Due to the nature of floats it is the only builtin type check that includes a tolerance factor. If the absolute value of the difference between the got and want values is less than the tolerance then the two floats will be considered equivalent.

cat ./float32_with_unformatted_message/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

// Example function being tested.
func fromFloat(f float32) float32 {
    return f / 2.0
}

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

    const valueToTest = 1.234567
    chk.Float32(
        fromFloat(valueToTest),                  // Got.
        0.617,                                   // Want.
        0.001,                                   // Tolerance.
        "function fromFloat(", valueToTest, ")", // Additional message.
    )
}

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

    const valueToTest = 2.468024
    chk.Float32(
        fromFloat(valueToTest),                  // Got.
        1.2356,                                  // Want.
        0.0005,                                  // Tolerance.
        "function fromFloat(", valueToTest, ")", // Additional message.
    )
}
go test -v -cover ./float32_with_unformatted_message

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲Float32WithUnformattedMessage}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲Float32WithUnformattedMessage ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲Fail ̲ ̲Float32WithUnformattedMessage}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:34: ͏ ͏unexpected ͏ ͏float32(+/- ͏ ͏0.0005000000237487257):}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\emph{function ͏ ͏fromFloat(2.468024)}}:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}1.23{\color{darkturquoise}{4012}}}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}1.23{\color{darkturquoise}{56}}}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲Fail ̲ ̲Float32WithUnformattedMessage ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/got ̲ ̲wnt/float32 ̲ ̲with ̲ ̲unformatted ̲ ̲message ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Here the thousandth fractional position is flagged as being out of tolerance and is highlighted as changed/different while the specific tolerance value used has been added to the type name. Finally the additional unformatted message is displayed just before the GOT: line.

Contents

Examples: String With Formatted Message

This example shows a string Got/Wnt test with a formatted message.

cat ./string_with_formatted_message/example_test.go
package example

import (
    "testing"

    "github.com/dancsecs/sztest"
)

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

    chk.Strf(
        "This string being tested.",
        "This string being tested.",
        // Optional formatted message.
        "%s message with %s information", "Formatted", "additional",
    )
}

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

    chk.Strf(
        "This (extra) is the Got string being tested.",
        "This is the Wnt string (missing) being tested.",
        // Optional formatted message.
        "%s message with %s information", "Formatted", "additional",
    )
}
go test -v -cover ./string_with_formatted_message

$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲StringWithFormattedMessage}}$
$\small{\texttt{‒‒‒ ͏ ͏PASS: ͏ ͏ ͏ ͏Test ̲ ̲PASS ̲ ̲StringWithFormattedMessage ͏ ͏(0.0s)}}$
$\small{\texttt{=== ͏ ͏RUN ͏ ͏ ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲StringWithFormattedMessage}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏example ̲ ̲test.go:27: ͏ ͏unexpected ͏ ͏string:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\emph{Formatted ͏ ͏message ͏ ͏with ͏ ͏additional ͏ ͏information}}:}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{magenta}{GOT: ͏ ͏}}This{\color{green}{ ͏ ͏(extra)}} ͏ ͏is ͏ ͏the ͏ ͏{\color{darkturquoise}{Go}}t ͏ ͏string ͏ ͏being ͏ ͏tested.}}$
$\small{\texttt{ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏{\color{cyan}{WNT: ͏ ͏}}This ͏ ͏is ͏ ͏the ͏ ͏{\color{darkturquoise}{Wn}}t ͏ ͏string{\color{red}{ ͏ ͏(missing)}} ͏ ͏being ͏ ͏tested.}}$
$\small{\texttt{‒‒‒ ͏ ͏FAIL: ͏ ͏ ͏ ͏Test ̲ ̲FAIL ̲ ̲StringWithFormattedMessage ͏ ͏(0.0s)}}$
$\small{\texttt{FAIL}}$
$\small{\texttt{coverage: ͏ ͏[no ͏ ͏statements]}}$
$\small{\texttt{FAIL ͏ ͏github.com/dancsecs/sztest/examples/got ̲ ̲wnt/string ̲ ̲with ̲ ̲formatted ̲ ̲message ͏ ͏0.0s}}$
$\small{\texttt{FAIL}}$

Here the got string has extra information (extra) not found in the want string while the want string has missing information (missing) not found in the got string. Then there is a changed area area between the got Go and the want Wnt. Finally the additional formatted message is displayed just before the GOT: line.

Contents