Test::Device::Chip::Adapter - unit testing on
use Test::More; use Test::Device::Chip::Adapter; use Future::AsyncAwait; my $adapter = Test::Device::Chip::Adapter->new; $chip_under_test->mount( $adapter ); # An actual test $adapter->expect_readwrite( "123" ) ->returns( "45" ); is( await $chip->do_thing( "123" ), "45", 'result of ->do_thing' ); $adapter->check_and_clear( '->do_thing' );
This package provides a concrete implementation of Device::Chip::Adapter convenient for using in a unit-test script used to test a Device::Chip instance. It operates in an "expect-and-check" style of mocking, requiring the test script to declare upfront what methods are expected to be called, and what values they return.
Futures returned by this module will not yield results immediately; they must be awaited by a toplevel
await keyword or invoking the
->get method. This ensures that unit tests correctly perform the required asynchronisation.
Each of the actual methods to be used by the Device::Chip under test has an associated expectation method, whose name is prefixed
expect_. Each returns an expectation object, which has additional methods to control the behaviour of that invocation.
$exp = $adapter->expect_write_gpios( \%gpios ) $exp = $adapter->expect_read_gpios( \@gpios ) $exp = $adapter->expect_tris_gpios( \@gpios ) $exp = $adapter->expect_write( $bytes ) $exp = $adapter->expect_read( $len ) $exp = $adapter->expect_write_then_read( $bytes, $len ) $exp = $adapter->expect_readwrite( $bytes_out ) $exp = $adapter->expect_assert_ss $exp = $adapter->expect_release_ss $exp = $adapter->expect_readwrite_no_ss( $bytes_out ) $exp = $adapter->expect_write_no_ss( $bytes )
The returned expectation object allows the test script to specify what such an invocation should return or throw.
$exp->returns( $bytes_in ) $exp->fails( $failure )
Expectations for an atomic I²C transaction are performed inline, using the following additional methods:
This class has the methods available on Device::Chip::Adapter, which would normally be used by the chip instance under test. The following additional methods would be used by the unit test script controlling it.
$adapter->check_and_clear( $name )
Checks that by now, every expected method has indeed been called, and emits a new test output line via Test::Builder. Regardless, the expectations are also cleared out ready for the start of the next test.
Paul Evans <firstname.lastname@example.org>