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