- Examples: Integer With No Message
- Examples: Float32 With Unformatted Message
- Examples: String With Formatted 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.gopackage 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
Here the failing test has its fourth number highlighted as changed while the passing test produced no output.
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.gopackage 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
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.
This example shows a string Got/Wnt test with a formatted message.
cat ./string_with_formatted_message/example_test.gopackage 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
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.