]> git.sur5r.net Git - i3/i3/blob - testcases/t/187-commands-parser.t
Display i3-nagbar when commands lead to an error
[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>&1 >&-);
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 # The first call has only a single command, the following ones are consolidated
32 # for performance.
33 is(parser_calls('move workspace 3'),
34    'cmd_move_con_to_workspace_name(3)',
35    'single number (move workspace 3) ok');
36
37 is(parser_calls(
38    'move to workspace 3; ' .
39    'move window to workspace 3; ' .
40    'move container to workspace 3; ' .
41    'move workspace foobar; ' .
42    'move workspace 3: foobar; ' .
43    'move workspace "3: foobar"; ' .
44    'move workspace "3: foobar, baz"; '),
45    "cmd_move_con_to_workspace_name(3)\n" .
46    "cmd_move_con_to_workspace_name(3)\n" .
47    "cmd_move_con_to_workspace_name(3)\n" .
48    "cmd_move_con_to_workspace_name(foobar)\n" .
49    "cmd_move_con_to_workspace_name(3: foobar)\n" .
50    "cmd_move_con_to_workspace_name(3: foobar)\n" .
51    "cmd_move_con_to_workspace_name(3: foobar, baz)",
52    'move ok');
53
54 is(parser_calls('move workspace 3: foobar, nop foo'),
55    "cmd_move_con_to_workspace_name(3: foobar)\n" .
56    "cmd_nop(foo)",
57    'multiple ops (move workspace 3: foobar, nop foo) ok');
58
59 is(parser_calls(
60    'exec i3-sensible-terminal; ' .
61    'exec --no-startup-id i3-sensible-terminal'),
62    "cmd_exec((null), i3-sensible-terminal)\n" .
63    "cmd_exec(--no-startup-id, i3-sensible-terminal)",
64    'exec ok');
65
66 is(parser_calls(
67    'resize shrink left; ' .
68    'resize shrink left 25 px; ' .
69    'resize shrink left 25 px or 33 ppt; ' .
70    'resize shrink left 25'),
71    "cmd_resize(shrink, left, 10, 10)\n" .
72    "cmd_resize(shrink, left, 25, 10)\n" .
73    "cmd_resize(shrink, left, 25, 33)\n" .
74    "cmd_resize(shrink, left, 25, 10)",
75    'simple resize ok');
76
77 is(parser_calls('resize shrink left 25 px or 33 ppt,'),
78    'cmd_resize(shrink, left, 25, 33)',
79    'trailing comma resize ok');
80
81 is(parser_calls('resize shrink left 25 px or 33 ppt;'),
82    'cmd_resize(shrink, left, 25, 33)',
83    'trailing semicolon resize ok');
84
85 is(parser_calls('[con_mark=yay] focus'),
86    "cmd_criteria_add(con_mark, yay)\n" .
87    "cmd_focus()",
88    'criteria focus ok');
89
90 is(parser_calls("[con_mark=yay con_mark=bar] focus"),
91    "cmd_criteria_add(con_mark, yay)\n" .
92    "cmd_criteria_add(con_mark, bar)\n" .
93    "cmd_focus()",
94    'criteria focus ok');
95
96 is(parser_calls("[con_mark=yay\tcon_mark=bar] focus"),
97    "cmd_criteria_add(con_mark, yay)\n" .
98    "cmd_criteria_add(con_mark, bar)\n" .
99    "cmd_focus()",
100    'criteria focus ok');
101
102 is(parser_calls("[con_mark=yay\tcon_mark=bar]\tfocus"),
103    "cmd_criteria_add(con_mark, yay)\n" .
104    "cmd_criteria_add(con_mark, bar)\n" .
105    "cmd_focus()",
106    'criteria focus ok');
107
108 is(parser_calls('[con_mark="yay"] focus'),
109    "cmd_criteria_add(con_mark, yay)\n" .
110    "cmd_focus()",
111    'quoted criteria focus ok');
112
113 # Make sure trailing whitespace is stripped off: While this is not an issue for
114 # commands being parsed due to the configuration, people might send IPC
115 # commands with leading or trailing newlines.
116 is(parser_calls("workspace test\n"),
117    'cmd_workspace_name(test)',
118    'trailing whitespace stripped off ok');
119
120 is(parser_calls("\nworkspace test"),
121    'cmd_workspace_name(test)',
122    'trailing whitespace stripped off ok');
123
124 ################################################################################
125 # 2: Verify that the parser spits out the right error message on commands which
126 # are not ok.
127 ################################################################################
128
129 is(parser_calls('unknown_literal'),
130    "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" .
131    "ERROR: Your command: unknown_literal\n" .
132    "ERROR:               ^^^^^^^^^^^^^^^",
133    'error for unknown literal ok');
134
135 is(parser_calls('move something to somewhere'),
136    "ERROR: Expected one of these tokens: 'window', 'container', 'to', 'workspace', 'output', 'scratchpad', 'left', 'right', 'up', 'down', 'position', 'absolute'\n" .
137    "ERROR: Your command: move something to somewhere\n" .
138    "ERROR:                    ^^^^^^^^^^^^^^^^^^^^^^",
139    'error for unknown literal ok');
140
141 ################################################################################
142 # 3: Verify that escaping of double quotes works correctly
143 ################################################################################
144
145 is(parser_calls('workspace "foo"'),
146    'cmd_workspace_name(foo)',
147    'Command with simple double quotes ok');
148
149 is(parser_calls('workspace "foo'),
150    'cmd_workspace_name(foo)',
151    'Command without ending double quotes ok');
152
153 is(parser_calls('workspace "foo \"bar"'),
154    'cmd_workspace_name(foo "bar)',
155    'Command with escaped double quotes ok');
156
157 done_testing;