Adding Unit Tests
Learn how to add unit tests.
Although the autogenerated units tests were useful in finding whether if the gas requirements of our functions were being fulfilled, we are yet to test the actual logic of our functions. We can add more unit tests by simply adding more mappings to the tests variable.
To start, lets define the tests that we want to write for each function within our Calculator precompile:
add
: check that add(1, 2) returns 3nextTwo
: check that nextTwo(1) returns 2, 3repeat
: check that repeat(2, "EGS") returns "EGSEGS"
With this in mind, lets add the three units tests to the tests variable! Below is a code excerpt which shows the three unit tests incorporated:
var (
expectedNextTwoOutcome, _ = PackNextTwoOutput(NextTwoOutput{big.NewInt(2), big.NewInt(3)})
expectedRepeatOutcome, _ = PackRepeatOutput("EGSEGS")
expectedAddOutcome = common.LeftPadBytes(big.NewInt(3).Bytes(), common.HashLength)
tests = map[string]testutils.PrecompileTest{
"insufficient gas for add should fail": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
// CUSTOM CODE STARTS HERE
// populate test input here
testInput := AddInput{big.NewInt(1), big.NewInt(1)}
input, err := PackAdd(testInput)
require.NoError(t, err)
return input
},
SuppliedGas: AddGasCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
},
"insufficient gas for nextTwo should fail": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
// CUSTOM CODE STARTS HERE
// set test input to a value here
// var testInput *big.Int
testInput := big.NewInt(1)
input, err := PackNextTwo(testInput)
require.NoError(t, err)
return input
},
SuppliedGas: NextTwoGasCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
},
"insufficient gas for repeat should fail": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
// CUSTOM CODE STARTS HERE
// populate test input here
testInput := RepeatInput{big.NewInt(1), "EGS"}
input, err := PackRepeat(testInput)
require.NoError(t, err)
return input
},
SuppliedGas: RepeatGasCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
},
"testing add": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
value1 := big.NewInt(1)
value2 := big.NewInt(2)
testInput := AddInput{value1, value2}
input, err := PackAdd(testInput)
require.NoError(t, err)
return input
},
SuppliedGas: AddGasCost,
ReadOnly: true,
ExpectedRes: expectedAddOutcome,
},
"testing nextTwo": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
testInput := big.NewInt(1)
input, err := PackNextTwo(testInput)
require.NoError(t, err)
return input
},
SuppliedGas: NextTwoGasCost,
ReadOnly: true,
ExpectedRes: expectedNextTwoOutcome,
},
"testing repeat": {
Caller: common.Address{1},
InputFn: func(t testing.TB) []byte {
baseString := "EGS"
timesToRepeat := big.NewInt(2)
input, err := PackRepeat(RepeatInput{timesToRepeat, baseString})
require.NoError(t, err)
return input
},
SuppliedGas: RepeatGasCost,
ReadOnly: true,
ExpectedRes: expectedRepeatOutcome,
},
}
)
Edit on GitHub
Last updated on 1/9/2025