]> git.sur5r.net Git - i3/i3/blob - testcases/t/187-commands-parser.t
37deb94256e7708556d03cb676593fdf49245b37
[i3/i3] / testcases / t / 187-commands-parser.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Please read the following documents before working on tests:
5 # • http://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • http://build.i3wm.org/docs/ipc.html
12 #   (or docs/ipc)
13 #
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 #   (unless you are already familiar with Perl)
16 #
17 # Tests the standalone parser binary to see if it calls the right code when
18 # confronted with various commands, if it prints proper error messages for
19 # wrong commands and if it terminates in every case.
20 #
21 use i3test i3_autostart => 0;
22
23 sub parser_calls {
24     my ($command) = @_;
25
26     # TODO: use a timeout, so that we can error out if it doesn’t terminate
27     # TODO: better way of passing arguments
28     my $stdout = qx(../test.commands_parser '$command' 2>&1 >&-);
29
30     # Filter out all debugging output.
31     my @lines = split("\n", $stdout);
32     @lines = grep { not /^# / } @lines;
33
34     # The criteria management calls are irrelevant and not what we want to test
35     # in the first place.
36     @lines = grep { !(/cmd_criteria_init()/ || /cmd_criteria_match_windows/) } @lines;
37     return join("\n", @lines);
38 }
39
40 ################################################################################
41 # 1: First that the parser properly recognizes commands which are ok.
42 ################################################################################
43
44 # The first call has only a single command, the following ones are consolidated
45 # for performance.
46 is(parser_calls('move workspace 3'),
47    'cmd_move_con_to_workspace_name(3)',
48    'single number (move workspace 3) ok');
49
50 is(parser_calls(
51    'move to workspace 3; ' .
52    'move window to workspace 3; ' .
53    'move container to workspace 3; ' .
54    'move workspace foobar; ' .
55    'move workspace 3: foobar; ' .
56    'move workspace "3: foobar"; ' .
57    'move workspace "3: foobar, baz"; '),
58    "cmd_move_con_to_workspace_name(3)\n" .
59    "cmd_move_con_to_workspace_name(3)\n" .
60    "cmd_move_con_to_workspace_name(3)\n" .
61    "cmd_move_con_to_workspace_name(foobar)\n" .
62    "cmd_move_con_to_workspace_name(3: foobar)\n" .
63    "cmd_move_con_to_workspace_name(3: foobar)\n" .
64    "cmd_move_con_to_workspace_name(3: foobar, baz)",
65    'move ok');
66
67 is(parser_calls('move workspace 3: foobar, nop foo'),
68    "cmd_move_con_to_workspace_name(3: foobar)\n" .
69    "cmd_nop(foo)",
70    'multiple ops (move workspace 3: foobar, nop foo) ok');
71
72 is(parser_calls(
73    'exec i3-sensible-terminal; ' .
74    'exec --no-startup-id i3-sensible-terminal'),
75    "cmd_exec((null), i3-sensible-terminal)\n" .
76    "cmd_exec(--no-startup-id, i3-sensible-terminal)",
77    'exec ok');
78
79 is(parser_calls(
80    'resize shrink left; ' .
81    'resize shrink left 25 px; ' .
82    'resize shrink left 25 px or 33 ppt; ' .
83    'resize shrink left 25'),
84    "cmd_resize(shrink, left, 10, 10)\n" .
85    "cmd_resize(shrink, left, 25, 10)\n" .
86    "cmd_resize(shrink, left, 25, 33)\n" .
87    "cmd_resize(shrink, left, 25, 10)",
88    'simple resize ok');
89
90 is(parser_calls('resize shrink left 25 px or 33 ppt,'),
91    'cmd_resize(shrink, left, 25, 33)',
92    'trailing comma resize ok');
93
94 is(parser_calls('resize shrink left 25 px or 33 ppt;'),
95    'cmd_resize(shrink, left, 25, 33)',
96    'trailing semicolon resize ok');
97
98 is(parser_calls('[con_mark=yay] focus'),
99    "cmd_criteria_add(con_mark, yay)\n" .
100    "cmd_focus()",
101    'criteria focus ok');
102
103 is(parser_calls("[con_mark=yay con_mark=bar] focus"),
104    "cmd_criteria_add(con_mark, yay)\n" .
105    "cmd_criteria_add(con_mark, bar)\n" .
106    "cmd_focus()",
107    'criteria focus ok');
108
109 is(parser_calls("[con_mark=yay\tcon_mark=bar] focus"),
110    "cmd_criteria_add(con_mark, yay)\n" .
111    "cmd_criteria_add(con_mark, bar)\n" .
112    "cmd_focus()",
113    'criteria focus ok');
114
115 is(parser_calls("[con_mark=yay\tcon_mark=bar]\tfocus"),
116    "cmd_criteria_add(con_mark, yay)\n" .
117    "cmd_criteria_add(con_mark, bar)\n" .
118    "cmd_focus()",
119    'criteria focus ok');
120
121 is(parser_calls('[con_mark="yay"] focus'),
122    "cmd_criteria_add(con_mark, yay)\n" .
123    "cmd_focus()",
124    'quoted criteria focus ok');
125
126 # Make sure trailing whitespace is stripped off: While this is not an issue for
127 # commands being parsed due to the configuration, people might send IPC
128 # commands with leading or trailing newlines.
129 is(parser_calls("workspace test\n"),
130    'cmd_workspace_name(test)',
131    'trailing whitespace stripped off ok');
132
133 is(parser_calls("\nworkspace test"),
134    'cmd_workspace_name(test)',
135    'trailing whitespace stripped off ok');
136
137 ################################################################################
138 # 2: Verify that the parser spits out the right error message on commands which
139 # are not ok.
140 ################################################################################
141
142 is(parser_calls('unknown_literal'),
143    "ERROR: Expected one of these tokens: <end>, '[', 'move', 'exec', 'exit', 'restart', 'reload', 'border', 'layout', 'append_layout', 'workspace', 'focus', 'kill', 'open', 'fullscreen', 'split', 'floating', 'mark', 'resize', 'rename', 'nop', 'scratchpad', 'mode'\n" .
144    "ERROR: Your command: unknown_literal\n" .
145    "ERROR:               ^^^^^^^^^^^^^^^",
146    'error for unknown literal ok');
147
148 is(parser_calls('move something to somewhere'),
149    "ERROR: Expected one of these tokens: 'window', 'container', 'to', 'workspace', 'output', 'scratchpad', 'left', 'right', 'up', 'down', 'position', 'absolute'\n" .
150    "ERROR: Your command: move something to somewhere\n" .
151    "ERROR:                    ^^^^^^^^^^^^^^^^^^^^^^",
152    'error for unknown literal ok');
153
154 ################################################################################
155 # 3: Verify that escaping of double quotes works correctly
156 ################################################################################
157
158 is(parser_calls('workspace "foo"'),
159    'cmd_workspace_name(foo)',
160    'Command with simple double quotes ok');
161
162 is(parser_calls('workspace "foo'),
163    'cmd_workspace_name(foo)',
164    'Command without ending double quotes ok');
165
166 is(parser_calls('workspace "foo \"bar"'),
167    'cmd_workspace_name(foo "bar)',
168    'Command with escaped double quotes ok');
169
170 done_testing;