]> git.sur5r.net Git - i3/i3/blob - testcases/t/201-config-parser.t
Merge pull request #1638 from hwangcc23/fix-1489
[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         '>/dev/null',
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     bindsym --whole-window button3 nop
49     bindsym --release --whole-window button3 nop
50     bindsym --border button3 nop
51     bindsym --release --border button3 nop
52 }
53 EOT
54
55 my $expected = <<'EOT';
56 cfg_enter_mode(meh)
57 cfg_mode_binding(bindsym, Mod1,Shift, x, (null), (null), (null), resize grow)
58 cfg_mode_binding(bindcode, Mod1, 44, (null), (null), (null), resize shrink)
59 cfg_mode_binding(bindsym, Mod1, x, --release, (null), (null), exec foo)
60 cfg_mode_binding(bindsym, (null), button3, (null), (null), --whole-window, nop)
61 cfg_mode_binding(bindsym, (null), button3, --release, (null), --whole-window, nop)
62 cfg_mode_binding(bindsym, (null), button3, (null), --border, (null), nop)
63 cfg_mode_binding(bindsym, (null), button3, --release, --border, (null), nop)
64 EOT
65
66 is(parser_calls($config),
67    $expected,
68    'mode bindings ok');
69
70 ################################################################################
71 # exec and exec_always
72 ################################################################################
73
74 $config = <<'EOT';
75 exec geeqie
76 exec --no-startup-id /tmp/foo.sh
77 exec_always firefox
78 exec_always --no-startup-id /tmp/bar.sh
79 EOT
80
81 $expected = <<'EOT';
82 cfg_exec(exec, (null), geeqie)
83 cfg_exec(exec, --no-startup-id, /tmp/foo.sh)
84 cfg_exec(exec_always, (null), firefox)
85 cfg_exec(exec_always, --no-startup-id, /tmp/bar.sh)
86 EOT
87
88 is(parser_calls($config),
89    $expected,
90    'exec okay');
91
92 ################################################################################
93 # for_window
94 ################################################################################
95
96 $config = <<'EOT';
97 for_window [class="^Chrome"] floating enable
98 EOT
99
100 $expected = <<'EOT';
101 cfg_criteria_add(class, ^Chrome)
102 cfg_for_window(floating enable)
103 EOT
104
105 is(parser_calls($config),
106    $expected,
107    'for_window okay');
108
109 ################################################################################
110 # assign
111 ################################################################################
112
113 $config = <<'EOT';
114 assign [class="^Chrome"] 4
115 assign [class="^Chrome"] named workspace
116 assign [class="^Chrome"] "quoted named workspace"
117 assign [class="^Chrome"] → "quoted named workspace"
118 EOT
119
120 $expected = <<'EOT';
121 cfg_criteria_add(class, ^Chrome)
122 cfg_assign(4)
123 cfg_criteria_add(class, ^Chrome)
124 cfg_assign(named workspace)
125 cfg_criteria_add(class, ^Chrome)
126 cfg_assign(quoted named workspace)
127 cfg_criteria_add(class, ^Chrome)
128 cfg_assign(quoted named workspace)
129 EOT
130
131 is(parser_calls($config),
132    $expected,
133    'for_window okay');
134
135 ################################################################################
136 # floating_minimum_size / floating_maximum_size
137 ################################################################################
138
139 $config = <<'EOT';
140 floating_minimum_size 80x55
141 floating_minimum_size 80    x  55  
142 floating_maximum_size 73 x 10
143 EOT
144
145 $expected = <<'EOT';
146 cfg_floating_minimum_size(80, 55)
147 cfg_floating_minimum_size(80, 55)
148 cfg_floating_maximum_size(73, 10)
149 EOT
150
151 is(parser_calls($config),
152    $expected,
153    'floating_minimum_size ok');
154
155 ################################################################################
156 # popup_during_fullscreen
157 ################################################################################
158
159 $config = <<'EOT';
160 popup_during_fullscreen ignore
161 popup_during_fullscreen leave_fullscreen
162 popup_during_fullscreen SMArt
163 EOT
164
165 $expected = <<'EOT';
166 cfg_popup_during_fullscreen(ignore)
167 cfg_popup_during_fullscreen(leave_fullscreen)
168 cfg_popup_during_fullscreen(smart)
169 EOT
170
171 is(parser_calls($config),
172    $expected,
173    'popup_during_fullscreen ok');
174
175
176 ################################################################################
177 # floating_modifier
178 ################################################################################
179
180 $config = <<'EOT';
181 floating_modifier Mod1
182 floating_modifier mOd1
183 EOT
184
185 $expected = <<'EOT';
186 cfg_floating_modifier(Mod1)
187 cfg_floating_modifier(Mod1)
188 EOT
189
190 is(parser_calls($config),
191    $expected,
192    'floating_modifier ok');
193
194 ################################################################################
195 # default_orientation
196 ################################################################################
197
198 $config = <<'EOT';
199 default_orientation horizontal
200 default_orientation vertical
201 default_orientation auto
202 EOT
203
204 $expected = <<'EOT';
205 cfg_default_orientation(horizontal)
206 cfg_default_orientation(vertical)
207 cfg_default_orientation(auto)
208 EOT
209
210 is(parser_calls($config),
211    $expected,
212    'default_orientation ok');
213
214 ################################################################################
215 # workspace_layout
216 ################################################################################
217
218 $config = <<'EOT';
219 workspace_layout default
220 workspace_layout stacked
221 workspace_layout stacking
222 workspace_layout tabbed
223 EOT
224
225 $expected = <<'EOT';
226 cfg_workspace_layout(default)
227 cfg_workspace_layout(stacked)
228 cfg_workspace_layout(stacking)
229 cfg_workspace_layout(tabbed)
230 EOT
231
232 is(parser_calls($config),
233    $expected,
234    'workspace_layout ok');
235
236 ################################################################################
237 # workspace assignments, with trailing whitespace (ticket #921)
238 ################################################################################
239
240 $config = <<'EOT';
241 workspace "3" output DP-1 
242 workspace "3" output            VGA-1   
243 EOT
244
245 $expected = <<'EOT';
246 cfg_workspace(3, DP-1)
247 cfg_workspace(3, VGA-1)
248 EOT
249
250 is(parser_calls($config),
251    $expected,
252    'workspace assignment ok');
253
254 ################################################################################
255 # new_window
256 ################################################################################
257
258 $config = <<'EOT';
259 new_window 1pixel
260 new_window normal
261 new_window none
262 new_float 1pixel
263 new_float normal
264 new_float none
265 EOT
266
267 $expected = <<'EOT';
268 cfg_new_window(new_window, 1pixel, -1)
269 cfg_new_window(new_window, normal, 2)
270 cfg_new_window(new_window, none, -1)
271 cfg_new_window(new_float, 1pixel, -1)
272 cfg_new_window(new_float, normal, 2)
273 cfg_new_window(new_float, none, -1)
274 EOT
275
276 is(parser_calls($config),
277    $expected,
278    'new_window ok');
279
280 ################################################################################
281 # hide_edge_borders
282 ################################################################################
283
284 $config = <<'EOT';
285 hide_edge_borders none
286 hide_edge_borders vertical
287 hide_edge_borders horizontal
288 hide_edge_borders both
289 EOT
290
291 $expected = <<'EOT';
292 cfg_hide_edge_borders(none)
293 cfg_hide_edge_borders(vertical)
294 cfg_hide_edge_borders(horizontal)
295 cfg_hide_edge_borders(both)
296 EOT
297
298 is(parser_calls($config),
299    $expected,
300    'hide_edge_borders ok');
301
302 ################################################################################
303 # focus_follows_mouse
304 ################################################################################
305
306 $config = <<'EOT';
307 focus_follows_mouse yes
308 focus_follows_mouse no
309 EOT
310
311 $expected = <<'EOT';
312 cfg_focus_follows_mouse(yes)
313 cfg_focus_follows_mouse(no)
314 EOT
315
316 is(parser_calls($config),
317    $expected,
318    'focus_follows_mouse ok');
319
320 ################################################################################
321 # mouse_warping
322 ################################################################################
323
324 $config = <<'EOT';
325 mouse_warping output
326 mouse_warping none
327 EOT
328
329 $expected = <<'EOT';
330 cfg_mouse_warping(output)
331 cfg_mouse_warping(none)
332 EOT
333
334 is(parser_calls($config),
335    $expected,
336    'mouse_warping ok');
337
338 ################################################################################
339 # force_display_urgency_hint
340 ################################################################################
341
342 is(parser_calls('force_display_urgency_hint 300'),
343    "cfg_force_display_urgency_hint(300)\n",
344    'force_display_urgency_hint ok');
345
346 is(parser_calls('force_display_urgency_hint 500 ms'),
347    "cfg_force_display_urgency_hint(500)\n",
348    'force_display_urgency_hint ok');
349
350 is(parser_calls('force_display_urgency_hint 700ms'),
351    "cfg_force_display_urgency_hint(700)\n",
352    'force_display_urgency_hint ok');
353
354 $config = <<'EOT';
355 force_display_urgency_hint 300
356 force_display_urgency_hint 500 ms
357 force_display_urgency_hint 700ms
358 force_display_urgency_hint 700
359 EOT
360
361 $expected = <<'EOT';
362 cfg_force_display_urgency_hint(300)
363 cfg_force_display_urgency_hint(500)
364 cfg_force_display_urgency_hint(700)
365 cfg_force_display_urgency_hint(700)
366 EOT
367
368 is(parser_calls($config),
369    $expected,
370    'force_display_urgency_hint ok');
371
372 ################################################################################
373 # delay_exit_on_zero_displays
374 ################################################################################
375
376 is(parser_calls('delay_exit_on_zero_displays 300'),
377    "cfg_delay_exit_on_zero_displays(300)\n",
378    'delay_exit_on_zero_displays ok');
379
380 is(parser_calls('delay_exit_on_zero_displays 500 ms'),
381    "cfg_delay_exit_on_zero_displays(500)\n",
382    'delay_exit_on_zero_displays ok');
383
384 is(parser_calls('delay_exit_on_zero_displays 700ms'),
385    "cfg_delay_exit_on_zero_displays(700)\n",
386    'delay_exit_on_zero_displays ok');
387
388 $config = <<'EOT';
389 delay_exit_on_zero_displays 300
390 delay_exit_on_zero_displays 500 ms
391 delay_exit_on_zero_displays 700ms
392 delay_exit_on_zero_displays 700
393 EOT
394
395 $expected = <<'EOT';
396 cfg_delay_exit_on_zero_displays(300)
397 cfg_delay_exit_on_zero_displays(500)
398 cfg_delay_exit_on_zero_displays(700)
399 cfg_delay_exit_on_zero_displays(700)
400 EOT
401
402 is(parser_calls($config),
403    $expected,
404    'delay_exit_on_zero_displays ok');
405
406 ################################################################################
407 # workspace
408 ################################################################################
409
410 $config = <<'EOT';
411 workspace 3 output VGA-1
412 workspace "4: output" output VGA-2
413 workspace bleh output LVDS1/I_1
414 EOT
415
416 $expected = <<'EOT';
417 cfg_workspace(3, VGA-1)
418 cfg_workspace(4: output, VGA-2)
419 cfg_workspace(bleh, LVDS1/I_1)
420 EOT
421
422 is(parser_calls($config),
423    $expected,
424    'workspace ok');
425
426 ################################################################################
427 # ipc-socket
428 ################################################################################
429
430 $config = <<'EOT';
431 ipc-socket /tmp/i3.sock
432 ipc_socket ~/.i3/i3.sock
433 EOT
434
435 $expected = <<'EOT';
436 cfg_ipc_socket(/tmp/i3.sock)
437 cfg_ipc_socket(~/.i3/i3.sock)
438 EOT
439
440 is(parser_calls($config),
441    $expected,
442    'ipc-socket ok');
443
444 ################################################################################
445 # colors
446 ################################################################################
447
448 $config = <<'EOT';
449 client.focused          #4c7899 #285577 #ffffff #2e9ef4
450 client.focused_inactive #333333 #5f676a #ffffff #484e50
451 client.unfocused        #333333 #222222 #888888 #292d2e
452 client.urgent           #2f343a #900000 #ffffff #900000
453 client.placeholder      #000000 #0c0c0c #ffffff #000000
454 EOT
455
456 $expected = <<'EOT';
457 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
458 cfg_color(client.focused_inactive, #333333, #5f676a, #ffffff, #484e50)
459 cfg_color(client.unfocused, #333333, #222222, #888888, #292d2e)
460 cfg_color(client.urgent, #2f343a, #900000, #ffffff, #900000)
461 cfg_color(client.placeholder, #000000, #0c0c0c, #ffffff, #000000)
462 EOT
463
464 is(parser_calls($config),
465    $expected,
466    'colors ok');
467
468 ################################################################################
469 # Verify that errors don’t harm subsequent valid statements
470 ################################################################################
471
472 $config = <<'EOT';
473 hide_edge_border both
474 client.focused          #4c7899 #285577 #ffffff #2e9ef4
475 EOT
476
477 my $expected_all_tokens = <<'EOT';
478 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', 'no_focus', 'focus_follows_mouse', 'mouse_warping', 'force_focus_wrapping', 'force_xinerama', 'force-xinerama', 'workspace_auto_back_and_forth', 'fake_outputs', 'fake-outputs', 'force_display_urgency_hint', 'delay_exit_on_zero_displays', 'focus_on_window_activation', 'show_marks', 'workspace', 'ipc_socket', 'ipc-socket', 'restart_state', 'popup_during_fullscreen', 'exec_always', 'exec', 'client.background', 'client.focused_inactive', 'client.focused', 'client.unfocused', 'client.urgent', 'client.placeholder'
479 EOT
480
481 my $expected_end = <<'EOT';
482 ERROR: CONFIG: (in file <stdin>)
483 ERROR: CONFIG: Line   1: hide_edge_border both
484 ERROR: CONFIG:           ^^^^^^^^^^^^^^^^^^^^^
485 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
486 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
487 EOT
488
489 $expected = $expected_all_tokens . $expected_end;
490
491 is(parser_calls($config),
492    $expected,
493    'errors dont harm subsequent statements');
494
495 $config = <<'EOT';
496 hide_edge_borders FOOBAR
497 client.focused          #4c7899 #285577 #ffffff #2e9ef4
498 EOT
499
500 $expected = <<'EOT';
501 ERROR: CONFIG: Expected one of these tokens: 'none', 'vertical', 'horizontal', 'both', '1', 'yes', 'true', 'on', 'enable', 'active'
502 ERROR: CONFIG: (in file <stdin>)
503 ERROR: CONFIG: Line   1: hide_edge_borders FOOBAR
504 ERROR: CONFIG:                             ^^^^^^
505 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
506 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4)
507 EOT
508
509 is(parser_calls($config),
510    $expected,
511    'errors dont harm subsequent statements');
512
513 ################################################################################
514 # Regression: semicolons end comments, but shouldn’t
515 ################################################################################
516
517 $config = <<'EOT';
518 # "foo" client.focused          #4c7899 #285577 #ffffff #2e9ef4
519 EOT
520
521 $expected = <<'EOT';
522
523 EOT
524
525 is(parser_calls($config),
526    $expected,
527    'semicolon does not end a comment line');
528
529 ################################################################################
530 # Error message with 2+2 lines of context
531 ################################################################################
532
533 $config = <<'EOT';
534 # i3 config file (v4)
535
536 font foobar
537
538 unknown qux
539
540 # yay
541 # this should not show up
542 EOT
543
544 my $expected_head = <<'EOT';
545 cfg_font(foobar)
546 EOT
547
548 my $expected_tail = <<'EOT';
549 ERROR: CONFIG: (in file <stdin>)
550 ERROR: CONFIG: Line   3: font foobar
551 ERROR: CONFIG: Line   4: 
552 ERROR: CONFIG: Line   5: unknown qux
553 ERROR: CONFIG:           ^^^^^^^^^^^
554 ERROR: CONFIG: Line   6: 
555 ERROR: CONFIG: Line   7: # yay
556 EOT
557
558 $expected = $expected_head . $expected_all_tokens . $expected_tail;
559
560 is(parser_calls($config),
561    $expected,
562    'error message (2+2 context) ok');
563
564 ################################################################################
565 # Error message with 0+0 lines of context
566 ################################################################################
567
568 $config = <<'EOT';
569 unknown qux
570 EOT
571
572 $expected_tail = <<'EOT';
573 ERROR: CONFIG: (in file <stdin>)
574 ERROR: CONFIG: Line   1: unknown qux
575 ERROR: CONFIG:           ^^^^^^^^^^^
576 EOT
577
578 $expected = $expected_all_tokens . $expected_tail;
579
580 is(parser_calls($config),
581    $expected,
582    'error message (0+0 context) ok');
583
584 ################################################################################
585 # Error message with 1+0 lines of context
586 ################################################################################
587
588 $config = <<'EOT';
589 # context before
590 unknown qux
591 EOT
592
593 $expected_tail = <<'EOT';
594 ERROR: CONFIG: (in file <stdin>)
595 ERROR: CONFIG: Line   1: # context before
596 ERROR: CONFIG: Line   2: unknown qux
597 ERROR: CONFIG:           ^^^^^^^^^^^
598 EOT
599
600 $expected = $expected_all_tokens . $expected_tail;
601
602 is(parser_calls($config),
603    $expected,
604    'error message (1+0 context) ok');
605
606 ################################################################################
607 # Error message with 0+1 lines of context
608 ################################################################################
609
610 $config = <<'EOT';
611 unknown qux
612 # context after
613 EOT
614
615 $expected_tail = <<'EOT';
616 ERROR: CONFIG: (in file <stdin>)
617 ERROR: CONFIG: Line   1: unknown qux
618 ERROR: CONFIG:           ^^^^^^^^^^^
619 ERROR: CONFIG: Line   2: # context after
620 EOT
621
622 $expected = $expected_all_tokens . $expected_tail;
623
624 is(parser_calls($config),
625    $expected,
626    'error message (0+1 context) ok');
627
628 ################################################################################
629 # Error message with 0+2 lines of context
630 ################################################################################
631
632 $config = <<'EOT';
633 unknown qux
634 # context after
635 # context 2 after
636 EOT
637
638 $expected_tail = <<'EOT';
639 ERROR: CONFIG: (in file <stdin>)
640 ERROR: CONFIG: Line   1: unknown qux
641 ERROR: CONFIG:           ^^^^^^^^^^^
642 ERROR: CONFIG: Line   2: # context after
643 ERROR: CONFIG: Line   3: # context 2 after
644 EOT
645
646 $expected = $expected_all_tokens . $expected_tail;
647
648 is(parser_calls($config),
649    $expected,
650    'error message (0+2 context) ok');
651
652 ################################################################################
653 # Error message within mode blocks
654 ################################################################################
655
656 $config = <<'EOT';
657 mode "yo" {
658     bindsym x resize shrink left
659     unknown qux
660 }
661 EOT
662
663 $expected = <<'EOT';
664 cfg_enter_mode(yo)
665 cfg_mode_binding(bindsym, (null), x, (null), (null), (null), resize shrink left)
666 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', '}'
667 ERROR: CONFIG: (in file <stdin>)
668 ERROR: CONFIG: Line   1: mode "yo" {
669 ERROR: CONFIG: Line   2:     bindsym x resize shrink left
670 ERROR: CONFIG: Line   3:     unknown qux
671 ERROR: CONFIG:               ^^^^^^^^^^^
672 ERROR: CONFIG: Line   4: }
673 EOT
674
675 is(parser_calls($config),
676    $expected,
677    'error message (mode block) ok');
678
679 ################################################################################
680 # Error message within bar blocks
681 ################################################################################
682
683 $config = <<'EOT';
684 bar {
685     output LVDS-1
686     unknown qux
687 }
688 EOT
689
690 $expected = <<'EOT';
691 cfg_bar_output(LVDS-1)
692 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'i3bar_command', 'status_command', 'socket_path', 'mode', 'hidden_state', 'id', 'modifier', 'wheel_up_cmd', 'wheel_down_cmd', 'position', 'output', 'tray_output', 'font', 'separator_symbol', 'binding_mode_indicator', 'workspace_buttons', 'strip_workspace_numbers', 'verbose', 'colors', '}'
693 ERROR: CONFIG: (in file <stdin>)
694 ERROR: CONFIG: Line   1: bar {
695 ERROR: CONFIG: Line   2:     output LVDS-1
696 ERROR: CONFIG: Line   3:     unknown qux
697 ERROR: CONFIG:               ^^^^^^^^^^^
698 ERROR: CONFIG: Line   4: }
699 cfg_bar_finish()
700 EOT
701
702 is(parser_calls($config),
703    $expected,
704    'error message (bar block) ok');
705
706 done_testing;