The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Tk - Testing Tk widgets.

SYNOPSIS

 use Test::More tests => 3;
 
 createapp(
 );
 
 @tests = (
    [sub { return 1 }, 1, 'A demo test'],
 );
 
 $app->MainLoop;

DESCRIPTION

This module aims to assist in the testing of Perl/T kwidgets.

createapp creates a MainWindow widget and places it in the variable $app. It sets a timer with delay $delay to start the internal test routine.

After MainLoop is called and the timer is done, testing is done using the @tests array. When testing is done it destroys the MainWindow and continues the test script.

You can set a command line parameter show to test command on the command line. eg perl -Mblib t/My-Test.t show. The application will not terminate so you can visually inspect it.

It will perform two tests. You need to account for these when you set your number of tests.

EXPORT

$app

Holds the reference to the MainWindow object.

$delay

Default value 100. The delay time between creating the test app and start of the testing. You may want to increase this value in case all tests succeed but your test program still throws an error.

$mwclass

Default value Tk::MainWindow; You can set it to a derived class if you like.

@tests

Each element of @tests should contain a list of three elements.

A reference to a sub.

The sub should return the expected value for the test to succeed.

Expected value.

This can be a simple scalare but also the reference to a list or a hash. You may even specify a complexer data structure.

Description.

A brief description of the test so you know which test passed or failed.

$show

By default 0. Is set when the show option is given at the command line. You can overwrite this by setting or clearing this yourself.

createapp@options

Creates a MainWindow object and sets the countdown timer for testing. You still must call $app->MainWindow yourself.

hashcompare(\%hash1, \%hash2)

Returns true of both hashes have an identical set of keys and all values are equal. If a value is a reference to a list it will call listcompare. If a value is a reference to a hash it will call hashcompare.

listcompare(\@list1, \@list2)

Returns true of both lists have are of equal size and content. If a list element is a reference to a list it will call listcompare. If a list element is a reference to a hash it will call hashcompare.

SEE ALSO

Test::More

AUTHOR

Hans Jeuken, <hanje at cpan dot org@>

TODO

This should also work for Tcl::pTk widgets. However, the testing of this module during install is done with Tk. So this is set as a prerequisite. A duplicate module with slightly different defaults for Tcl::pTk is thinkable.

COPYRIGHT AND LICENSE

Copyright (C) 2023 by Hans Jeuken

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.34.0 or, at your option, any later version of Perl 5 you may have available.