]> git.sur5r.net Git - i3/i3/blob - testcases/t/201-config-parser.t
Bugfix: parse outputs as "word", not "string", to ignore trailing whitespace (Thanks...
[i3/i3] / testcases / t / 201-config-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 use IPC::Run qw(run);
23
24 sub parser_calls {
25     my ($command) = @_;
26
27     my $stdout;
28     run [ '../test.config_parser', $command ],
29         '>&-',
30         '2>', \$stdout;
31     # TODO: use a timeout, so that we can error out if it doesn’t terminate
32
33     # Filter out all debugging output.
34     my @lines = split("\n", $stdout);
35     @lines = grep { not /^# / } @lines;
36
37     # The criteria management calls are irrelevant and not what we want to test
38     # in the first place.
39     @lines = grep { !(/cfg_criteria_init/ || /cfg_criteria_pop_state/) } @lines;
40     return join("\n", @lines) . "\n";
41 }
42
43 my $config = <<'EOT';
44 mode "meh" {
45     bindsym Mod1 + Shift +   x resize grow
46     bindcode Mod1+44 resize shrink
47     bindsym --release Mod1+x exec foo
48 }
49 EOT
50
51 my $expected = <<'EOT';
52 cfg_enter_mode(meh)
53 cfg_mode_binding(bindsym, Mod1,Shift, x, (null), resize grow)
54 cfg_mode_binding(bindcode, Mod1, 44, (null), resize shrink)
55 cfg_mode_binding(bindsym, Mod1, x, --release, exec foo)
56 EOT
57
58 is(parser_calls($config),
59    $expected,
60    'single number (move workspace 3) ok');
61
62 ################################################################################
63 # exec and exec_always
64 ################################################################################
65
66 $config = <<'EOT';
67 exec geeqie
68 exec --no-startup-id /tmp/foo.sh
69 exec_always firefox
70 exec_always --no-startup-id /tmp/bar.sh
71 EOT
72
73 $expected = <<'EOT';
74 cfg_exec(exec, (null), geeqie)
75 cfg_exec(exec, --no-startup-id, /tmp/foo.sh)
76 cfg_exec(exec_always, (null), firefox)
77 cfg_exec(exec_always, --no-startup-id, /tmp/bar.sh)
78 EOT
79
80 is(parser_calls($config),
81    $expected,
82    'exec okay');
83
84 ################################################################################
85 # for_window
86 ################################################################################
87
88 $config = <<'EOT';
89 for_window [class="^Chrome"] floating enable
90 EOT
91
92 $expected = <<'EOT';
93 cfg_criteria_add(class, ^Chrome)
94 cfg_for_window(floating enable)
95 EOT
96
97 is(parser_calls($config),
98    $expected,
99    'for_window okay');
100
101 ################################################################################
102 # assign
103 ################################################################################
104
105 $config = <<'EOT';
106 assign [class="^Chrome"] 4
107 assign [class="^Chrome"] named workspace
108 assign [class="^Chrome"] "quoted named workspace"
109 assign [class="^Chrome"] → "quoted named workspace"
110 EOT
111
112 $expected = <<'EOT';
113 cfg_criteria_add(class, ^Chrome)
114 cfg_assign(4)
115 cfg_criteria_add(class, ^Chrome)
116 cfg_assign(named workspace)
117 cfg_criteria_add(class, ^Chrome)
118 cfg_assign(quoted named workspace)
119 cfg_criteria_add(class, ^Chrome)
120 cfg_assign(quoted named workspace)
121 EOT
122
123 is(parser_calls($config),
124    $expected,
125    'for_window okay');
126
127 ################################################################################
128 # floating_minimum_size / floating_maximum_size
129 ################################################################################
130
131 $config = <<'EOT';
132 floating_minimum_size 80x55
133 floating_minimum_size 80    x  55  
134 floating_maximum_size 73 x 10
135 EOT
136
137 $expected = <<'EOT';
138 cfg_floating_minimum_size(80, 55)
139 cfg_floating_minimum_size(80, 55)
140 cfg_floating_maximum_size(73, 10)
141 EOT
142
143 is(parser_calls($config),
144    $expected,
145    'floating_minimum_size ok');
146
147 ################################################################################
148 # floating_modifier
149 ################################################################################
150
151 $config = <<'EOT';
152 floating_modifier Mod1
153 floating_modifier mOd1
154 EOT
155
156 $expected = <<'EOT';
157 cfg_floating_modifier(Mod1)
158 cfg_floating_modifier(Mod1)
159 EOT
160
161 is(parser_calls($config),
162    $expected,
163    'floating_modifier ok');
164
165 ################################################################################
166 # default_orientation
167 ################################################################################
168
169 $config = <<'EOT';
170 default_orientation horizontal
171 default_orientation vertical
172 default_orientation auto
173 EOT
174
175 $expected = <<'EOT';
176 cfg_default_orientation(horizontal)
177 cfg_default_orientation(vertical)
178 cfg_default_orientation(auto)
179 EOT
180
181 is(parser_calls($config),
182    $expected,
183    'default_orientation ok');
184
185 ################################################################################
186 # workspace_layout
187 ################################################################################
188
189 $config = <<'EOT';
190 workspace_layout default
191 workspace_layout stacked
192 workspace_layout stacking
193 workspace_layout tabbed
194 EOT
195
196 $expected = <<'EOT';
197 cfg_workspace_layout(default)
198 cfg_workspace_layout(stacked)
199 cfg_workspace_layout(stacking)
200 cfg_workspace_layout(tabbed)
201 EOT
202
203 is(parser_calls($config),
204    $expected,
205    'workspace_layout ok');
206
207 ################################################################################
208 # workspace assignments, with trailing whitespace (ticket #921)
209 ################################################################################
210
211 $config = <<'EOT';
212 workspace "3" output DP-1 
213 workspace "3" output            VGA-1   
214 EOT
215
216 $expected = <<'EOT';
217 cfg_workspace(3, DP-1)
218 cfg_workspace(3, VGA-1)
219 EOT
220
221 is(parser_calls($config),
222    $expected,
223    'workspace assignment ok');
224
225 ################################################################################
226 # new_window
227 ################################################################################
228
229 $config = <<'EOT';
230 new_window 1pixel
231 new_window normal
232 new_window none
233 new_float 1pixel
234 new_float normal
235 new_float none
236 EOT
237
238 $expected = <<'EOT';
239 cfg_new_window(new_window, 1pixel, -1)
240 cfg_new_window(new_window, normal, 2)
241 cfg_new_window(new_window, none, -1)
242 cfg_new_window(new_float, 1pixel, -1)
243 cfg_new_window(new_float, normal, 2)
244 cfg_new_window(new_float, none, -1)
245 EOT
246
247 is(parser_calls($config),
248    $expected,
249    'new_window ok');
250
251 ################################################################################
252 # hide_edge_borders
253 ################################################################################
254
255 $config = <<'EOT';
256 hide_edge_borders none
257 hide_edge_borders vertical
258 hide_edge_borders horizontal
259 hide_edge_borders both
260 EOT
261
262 $expected = <<'EOT';
263 cfg_hide_edge_borders(none)
264 cfg_hide_edge_borders(vertical)
265 cfg_hide_edge_borders(horizontal)
266 cfg_hide_edge_borders(both)
267 EOT
268
269 is(parser_calls($config),
270    $expected,
271    'hide_edge_borders ok');
272
273 ################################################################################
274 # focus_follows_mouse
275 ################################################################################
276
277 $config = <<'EOT';
278 focus_follows_mouse yes
279 focus_follows_mouse no
280 EOT
281
282 $expected = <<'EOT';
283 cfg_focus_follows_mouse(yes)
284 cfg_focus_follows_mouse(no)
285 EOT
286
287 is(parser_calls($config),
288    $expected,
289    'focus_follows_mouse ok');
290
291 ################################################################################
292 # force_display_urgency_hint
293 ################################################################################
294
295 is(parser_calls('force_display_urgency_hint 300'),
296    "cfg_force_display_urgency_hint(300)\n",
297    'force_display_urgency_hint ok');
298
299 is(parser_calls('force_display_urgency_hint 500 ms'),
300    "cfg_force_display_urgency_hint(500)\n",
301    'force_display_urgency_hint ok');
302
303 is(parser_calls('force_display_urgency_hint 700ms'),
304    "cfg_force_display_urgency_hint(700)\n",
305    'force_display_urgency_hint ok');
306
307 $config = <<'EOT';
308 force_display_urgency_hint 300
309 force_display_urgency_hint 500 ms
310 force_display_urgency_hint 700ms
311 force_display_urgency_hint 700
312 EOT
313
314 $expected = <<'EOT';
315 cfg_force_display_urgency_hint(300)
316 cfg_force_display_urgency_hint(500)
317 cfg_force_display_urgency_hint(700)
318 cfg_force_display_urgency_hint(700)
319 EOT
320
321 is(parser_calls($config),
322    $expected,
323    'force_display_urgency_hint ok');
324
325 ################################################################################
326 # workspace
327 ################################################################################
328
329 $config = <<'EOT';
330 workspace 3 output VGA-1
331 workspace "4: output" output VGA-2
332 workspace bleh output LVDS1/I_1
333 EOT
334
335 $expected = <<'EOT';
336 cfg_workspace(3, VGA-1)
337 cfg_workspace(4: output, VGA-2)
338 cfg_workspace(bleh, LVDS1/I_1)
339 EOT
340
341 is(parser_calls($config),
342    $expected,
343    'workspace ok');
344
345 ################################################################################
346 # ipc-socket
347 ################################################################################
348
349 $config = <<'EOT';
350 ipc-socket /tmp/i3.sock
351 ipc_socket ~/.i3/i3.sock
352 EOT
353
354 $expected = <<'EOT';
355 cfg_ipc_socket(/tmp/i3.sock)
356 cfg_ipc_socket(~/.i3/i3.sock)
357 EOT
358
359 is(parser_calls($config),
360    $expected,
361    'ipc-socket ok');
362
363 ################################################################################
364 # colors
365 ################################################################################
366
367 $config = <<'EOT';
368 client.focused          #4c7899 #285577 #ffffff #2e9ef4
369 client.focused_inactive #333333 #5f676a #ffffff #484e50
370 client.unfocused        #333333 #222222 #888888 #292d2e
371 client.urgent           #2f343a #900000 #ffffff #900000
372 EOT
373
374 $expected = <<'EOT';
375 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
376 cfg_color(client.focused_inactive, #333333, #5f676a, #ffffff, #484e50)
377 cfg_color(client.unfocused, #333333, #222222, #888888, #292d2e)
378 cfg_color(client.urgent, #2f343a, #900000, #ffffff, #900000)
379 EOT
380
381 is(parser_calls($config),
382    $expected,
383    'colors ok');
384
385 ################################################################################
386 # Verify that errors don’t harm subsequent valid statements
387 ################################################################################
388
389 $config = <<'EOT';
390 hide_edge_border both
391 client.focused          #4c7899 #285577 #ffffff #2e9ef4
392 EOT
393
394 $expected = <<'EOT';
395 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
396 ERROR: CONFIG: (in file <stdin>)
397 ERROR: CONFIG: Line   1: hide_edge_border both
398 ERROR: CONFIG:           ^^^^^^^^^^^^^^^^^^^^^
399 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
400 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
401 EOT
402
403 is(parser_calls($config),
404    $expected,
405    'errors dont harm subsequent statements');
406
407 $config = <<'EOT';
408 hide_edge_borders FOOBAR
409 client.focused          #4c7899 #285577 #ffffff #2e9ef4
410 EOT
411
412 $expected = <<'EOT';
413 ERROR: CONFIG: Expected one of these tokens: 'none', 'vertical', 'horizontal', 'both', '1', 'yes', 'true', 'on', 'enable', 'active'
414 ERROR: CONFIG: (in file <stdin>)
415 ERROR: CONFIG: Line   1: hide_edge_borders FOOBAR
416 ERROR: CONFIG:                             ^^^^^^
417 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
418 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
419 EOT
420
421 is(parser_calls($config),
422    $expected,
423    'errors dont harm subsequent statements');
424
425
426 ################################################################################
427 # Error message with 2+2 lines of context
428 ################################################################################
429
430 $config = <<'EOT';
431 # i3 config file (v4)
432
433 font foobar
434
435 unknown qux
436
437 # yay
438 # this should not show up
439 EOT
440
441 $expected = <<'EOT';
442 cfg_font(foobar)
443 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
444 ERROR: CONFIG: (in file <stdin>)
445 ERROR: CONFIG: Line   3: font foobar
446 ERROR: CONFIG: Line   4: 
447 ERROR: CONFIG: Line   5: unknown qux
448 ERROR: CONFIG:           ^^^^^^^^^^^
449 ERROR: CONFIG: Line   6: 
450 ERROR: CONFIG: Line   7: # yay
451 EOT
452
453 is(parser_calls($config),
454    $expected,
455    'error message (2+2 context) ok');
456
457 ################################################################################
458 # Error message with 0+0 lines of context
459 ################################################################################
460
461 $config = <<'EOT';
462 unknown qux
463 EOT
464
465 $expected = <<'EOT';
466 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
467 ERROR: CONFIG: (in file <stdin>)
468 ERROR: CONFIG: Line   1: unknown qux
469 ERROR: CONFIG:           ^^^^^^^^^^^
470 EOT
471
472 is(parser_calls($config),
473    $expected,
474    'error message (0+0 context) ok');
475
476 ################################################################################
477 # Error message with 1+0 lines of context
478 ################################################################################
479
480 $config = <<'EOT';
481 # context before
482 unknown qux
483 EOT
484
485 $expected = <<'EOT';
486 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
487 ERROR: CONFIG: (in file <stdin>)
488 ERROR: CONFIG: Line   1: # context before
489 ERROR: CONFIG: Line   2: unknown qux
490 ERROR: CONFIG:           ^^^^^^^^^^^
491 EOT
492
493 is(parser_calls($config),
494    $expected,
495    'error message (1+0 context) ok');
496
497 ################################################################################
498 # Error message with 0+1 lines of context
499 ################################################################################
500
501 $config = <<'EOT';
502 unknown qux
503 # context after
504 EOT
505
506 $expected = <<'EOT';
507 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
508 ERROR: CONFIG: (in file <stdin>)
509 ERROR: CONFIG: Line   1: unknown qux
510 ERROR: CONFIG:           ^^^^^^^^^^^
511 ERROR: CONFIG: Line   2: # context after
512 EOT
513
514 is(parser_calls($config),
515    $expected,
516    'error message (0+1 context) ok');
517
518 ################################################################################
519 # Error message with 0+2 lines of context
520 ################################################################################
521
522 $config = <<'EOT';
523 unknown qux
524 # context after
525 # context 2 after
526 EOT
527
528 $expected = <<'EOT';
529 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', 'bar', 'font', 'mode', 'floating_minimum_size', 'floating_maximum_size', 'floating_modifier', 'default_orientation', 'workspace_layout', 'new_window', 'new_float', 'hide_edge_borders', 'for_window', 'assign', 'focus_follows_mouse', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent'
530 ERROR: CONFIG: (in file <stdin>)
531 ERROR: CONFIG: Line   1: unknown qux
532 ERROR: CONFIG:           ^^^^^^^^^^^
533 ERROR: CONFIG: Line   2: # context after
534 ERROR: CONFIG: Line   3: # context 2 after
535 EOT
536
537 is(parser_calls($config),
538    $expected,
539    'error message (0+2 context) ok');
540
541 ################################################################################
542 # Error message within mode blocks
543 ################################################################################
544
545 $config = <<'EOT';
546 mode "yo" {
547     bindsym x resize shrink left
548     unknown qux
549 }
550 EOT
551
552 $expected = <<'EOT';
553 cfg_enter_mode(yo)
554 cfg_mode_binding(bindsym, (null), x, (null), resize shrink left)
555 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', '}'
556 ERROR: CONFIG: (in file <stdin>)
557 ERROR: CONFIG: Line   1: mode "yo" {
558 ERROR: CONFIG: Line   2:     bindsym x resize shrink left
559 ERROR: CONFIG: Line   3:     unknown qux
560 ERROR: CONFIG:               ^^^^^^^^^^^
561 ERROR: CONFIG: Line   4: }
562 EOT
563
564 is(parser_calls($config),
565    $expected,
566    'error message (mode block) ok');
567
568 ################################################################################
569 # Error message within bar blocks
570 ################################################################################
571
572 $config = <<'EOT';
573 bar {
574     output LVDS-1
575     unknown qux
576 }
577 EOT
578
579 $expected = <<'EOT';
580 cfg_bar_output(LVDS-1)
581 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'i3bar_command', 'status_command', 'socket_path', 'mode', 'modifier', 'position', 'output', 'tray_output', 'font', 'workspace_buttons', 'verbose', 'colors', '}'
582 ERROR: CONFIG: (in file <stdin>)
583 ERROR: CONFIG: Line   1: bar {
584 ERROR: CONFIG: Line   2:     output LVDS-1
585 ERROR: CONFIG: Line   3:     unknown qux
586 ERROR: CONFIG:               ^^^^^^^^^^^
587 ERROR: CONFIG: Line   4: }
588 cfg_bar_finish()
589 EOT
590
591 is(parser_calls($config),
592    $expected,
593    'error message (bar block) ok');
594
595 done_testing;