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