Test::Device::Chip::Adapter - unit testing on Device::Chip


 use Test::More;
 use Test::Device::Chip::Adapter;

 my $adapter = Test::Device::Chip::Adapter->new;

 $chip_under_test->mount( $adapter );

 # An actual test
 $adapter->expect_readwrite( "123" )
    ->returns( "45" );

 is( $chip->do_thing( "123" )->get, "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.


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 )


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.


  • Handle list_gpios method

  • Handle configure


Paul Evans <>