]> git.sur5r.net Git - i3/i3/blob - testcases/t/187-commands-parser.t
Bugfix: Properly parse commands like “move workspace torrent” (Thanks rasi)
[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 torrent; ' .
56    'move workspace to output LVDS1; ' .
57    'move workspace 3: foobar; ' .
58    'move workspace "3: foobar"; ' .
59    'move workspace "3: foobar, baz"; '),
60    "cmd_move_con_to_workspace_name(3)\n" .
61    "cmd_move_con_to_workspace_name(3)\n" .
62    "cmd_move_con_to_workspace_name(3)\n" .
63    "cmd_move_con_to_workspace_name(foobar)\n" .
64    "cmd_move_con_to_workspace_name(torrent)\n" .
65    "cmd_move_workspace_to_output(LVDS1)\n" .
66    "cmd_move_con_to_workspace_name(3: foobar)\n" .
67    "cmd_move_con_to_workspace_name(3: foobar)\n" .
68    "cmd_move_con_to_workspace_name(3: foobar, baz)",
69    'move ok');
70
71 is(parser_calls('move workspace 3: foobar, nop foo'),
72    "cmd_move_con_to_workspace_name(3: foobar)\n" .
73    "cmd_nop(foo)",
74    'multiple ops (move workspace 3: foobar, nop foo) ok');
75
76 is(parser_calls(
77    'exec i3-sensible-terminal; ' .
78    'exec --no-startup-id i3-sensible-terminal'),
79    "cmd_exec((null), i3-sensible-terminal)\n" .
80    "cmd_exec(--no-startup-id, i3-sensible-terminal)",
81    'exec ok');
82
83 is(parser_calls(
84    'resize shrink left; ' .
85    'resize shrink left 25 px; ' .
86    'resize shrink left 25 px or 33 ppt; ' .
87    'resize shrink left 25'),
88    "cmd_resize(shrink, left, 10, 10)\n" .
89    "cmd_resize(shrink, left, 25, 10)\n" .
90    "cmd_resize(shrink, left, 25, 33)\n" .
91    "cmd_resize(shrink, left, 25, 10)",
92    'simple resize ok');
93
94 is(parser_calls('resize shrink left 25 px or 33 ppt,'),
95    'cmd_resize(shrink, left, 25, 33)',
96    'trailing comma resize ok');
97
98 is(parser_calls('resize shrink left 25 px or 33 ppt;'),
99    'cmd_resize(shrink, left, 25, 33)',
100    'trailing semicolon resize ok');
101
102 is(parser_calls('[con_mark=yay] focus'),
103    "cmd_criteria_add(con_mark, yay)\n" .
104    "cmd_focus()",
105    'criteria focus ok');
106
107 is(parser_calls("[con_mark=yay con_mark=bar] focus"),
108    "cmd_criteria_add(con_mark, yay)\n" .
109    "cmd_criteria_add(con_mark, bar)\n" .
110    "cmd_focus()",
111    'criteria focus ok');
112
113 is(parser_calls("[con_mark=yay\tcon_mark=bar] focus"),
114    "cmd_criteria_add(con_mark, yay)\n" .
115    "cmd_criteria_add(con_mark, bar)\n" .
116    "cmd_focus()",
117    'criteria focus ok');
118
119 is(parser_calls("[con_mark=yay\tcon_mark=bar]\tfocus"),
120    "cmd_criteria_add(con_mark, yay)\n" .
121    "cmd_criteria_add(con_mark, bar)\n" .
122    "cmd_focus()",
123    'criteria focus ok');
124
125 is(parser_calls('[con_mark="yay"] focus'),
126    "cmd_criteria_add(con_mark, yay)\n" .
127    "cmd_focus()",
128    'quoted criteria focus ok');
129
130 # Make sure trailing whitespace is stripped off: While this is not an issue for
131 # commands being parsed due to the configuration, people might send IPC
132 # commands with leading or trailing newlines.
133 is(parser_calls("workspace test\n"),
134    'cmd_workspace_name(test)',
135    'trailing whitespace stripped off ok');
136
137 is(parser_calls("\nworkspace test"),
138    'cmd_workspace_name(test)',
139    'trailing whitespace stripped off ok');
140
141 ################################################################################
142 # 2: Verify that the parser spits out the right error message on commands which
143 # are not ok.
144 ################################################################################
145
146 is(parser_calls('unknown_literal'),
147    "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" .
148    "ERROR: Your command: unknown_literal\n" .
149    "ERROR:               ^^^^^^^^^^^^^^^",
150    'error for unknown literal ok');
151
152 is(parser_calls('move something to somewhere'),
153    "ERROR: Expected one of these tokens: 'window', 'container', 'to', 'workspace', 'output', 'scratchpad', 'left', 'right', 'up', 'down', 'position', 'absolute'\n" .
154    "ERROR: Your command: move something to somewhere\n" .
155    "ERROR:                    ^^^^^^^^^^^^^^^^^^^^^^",
156    'error for unknown literal ok');
157
158 ################################################################################
159 # 3: Verify that escaping of double quotes works correctly
160 ################################################################################
161
162 is(parser_calls('workspace "foo"'),
163    'cmd_workspace_name(foo)',
164    'Command with simple double quotes ok');
165
166 is(parser_calls('workspace "foo'),
167    'cmd_workspace_name(foo)',
168    'Command without ending double quotes ok');
169
170 is(parser_calls('workspace "foo \"bar"'),
171    'cmd_workspace_name(foo "bar)',
172    'Command with escaped double quotes ok');
173
174 done_testing;