Table-driven testing revisited
Now that we understand the basics of implementing generic code, we can turn our attention to testing it. The adoption of generics throughout the Go community is still in its beginning stages. We have already established that generic code is statically enforced by the compiler, but does this increase in flexibility lead to more complex test code?
We can continue our exploration using the generic GetSortedValues
function that we have implemented. Tests should now be written to assert the behavior of the function for a variety of input types and values. We can achieve this by using the table-driven testing technique that we explored in Chapter 4, Building Efficient Test Suites. The implementation of generic table-driven testing follows a series of steps.
Step 1 – defining generic test cases
We begin by creating a generic test case type to save our input map and values:
type testCase[K ~int, V comparable] struct { input ...