]> git.sur5r.net Git - i3/i3/blob - testcases/lib/i3test/Test.pm
tests: make cmp_float a real test instruction
[i3/i3] / testcases / lib / i3test / Test.pm
1 package i3test::Test;
2 # vim:ts=4:sw=4:expandtab
3
4 use base 'Test::Builder::Module';
5
6 our @EXPORT = qw(
7     is_num_children
8     cmp_float
9 );
10
11 my $CLASS = __PACKAGE__;
12
13 =head1 NAME
14
15 i3test::Test - Additional test instructions for use in i3 testcases
16
17 =head1 SYNOPSIS
18
19   use i3test;
20
21   my $ws = fresh_workspace;
22   is_num_children($ws, 0, 'no containers on this workspace yet');
23   cmd 'open';
24   is_num_children($ws, 1, 'one container after "open"');
25
26   done_testing;
27
28 =head1 DESCRIPTION
29
30 This module provides convenience methods for i3 testcases. If you notice that a
31 certain pattern is present in 5 or more test cases, it should most likely be
32 moved into this module.
33
34 =head1 EXPORT
35
36 =head2 is_num_children($workspace, $expected, $test_name)
37
38 Gets the number of children on the given workspace and verifies that they match
39 the expected amount of children.
40
41   is_num_children('1', 0, 'no containers on workspace 1 at startup');
42
43 =cut
44
45 sub is_num_children {
46     my ($workspace, $num_children, $name) = @_;
47     my $tb = $CLASS->builder;
48
49     my $con = i3test::get_ws($workspace);
50     $tb->ok(defined($con), "Workspace $workspace exists");
51     if (!defined($con)) {
52         $tb->skip("Workspace does not exist, skipping is_num_children");
53         return;
54     }
55
56     my $got_num_children = scalar @{$con->{nodes}};
57
58     $tb->is_num($got_num_children, $num_children, $name);
59 }
60
61 =head2 cmp_float($a, $b)
62
63 Compares floating point numbers C<$a> and C<$b> and returns true if they differ
64 less then 1e-6.
65
66   $tmp = fresh_workspace;
67
68   open_window for (1..4);
69
70   cmd 'resize grow width 10 px or 25 ppt';
71
72   ($nodes, $focus) = get_ws_content($tmp);
73   ok(cmp_float($nodes->[0]->{percent}, 0.166666666666667), 'first window got 16%');
74   ok(cmp_float($nodes->[1]->{percent}, 0.166666666666667), 'second window got 16%');
75   ok(cmp_float($nodes->[2]->{percent}, 0.166666666666667), 'third window got 16%');
76   ok(cmp_float($nodes->[3]->{percent}, 0.50), 'fourth window got 50%');
77
78 =cut
79 sub cmp_float {
80   my ($a, $b, $name) = @_;
81   my $tb = $CLASS->builder;
82
83   $tb->cmp_ok(abs($a - $b), '<', 1e-6, $name);
84 }
85
86 =head1 AUTHOR
87
88 Michael Stapelberg <michael@i3wm.org>
89
90 =cut
91
92 1