]> git.sur5r.net Git - i3/i3/blob - testcases/t/201-config-parser.t
Split list of command / config directives in tests. (#2345)
[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((null), 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 hide_edge_borders smart
290 EOT
291
292 $expected = <<'EOT';
293 cfg_hide_edge_borders(none)
294 cfg_hide_edge_borders(vertical)
295 cfg_hide_edge_borders(horizontal)
296 cfg_hide_edge_borders(both)
297 cfg_hide_edge_borders(smart)
298 EOT
299
300 is(parser_calls($config),
301    $expected,
302    'hide_edge_borders ok');
303
304 ################################################################################
305 # focus_follows_mouse
306 ################################################################################
307
308 $config = <<'EOT';
309 focus_follows_mouse yes
310 focus_follows_mouse no
311 EOT
312
313 $expected = <<'EOT';
314 cfg_focus_follows_mouse(yes)
315 cfg_focus_follows_mouse(no)
316 EOT
317
318 is(parser_calls($config),
319    $expected,
320    'focus_follows_mouse ok');
321
322 ################################################################################
323 # mouse_warping
324 ################################################################################
325
326 $config = <<'EOT';
327 mouse_warping output
328 mouse_warping none
329 EOT
330
331 $expected = <<'EOT';
332 cfg_mouse_warping(output)
333 cfg_mouse_warping(none)
334 EOT
335
336 is(parser_calls($config),
337    $expected,
338    'mouse_warping ok');
339
340 ################################################################################
341 # force_display_urgency_hint
342 ################################################################################
343
344 is(parser_calls('force_display_urgency_hint 300'),
345    "cfg_force_display_urgency_hint(300)\n",
346    'force_display_urgency_hint ok');
347
348 is(parser_calls('force_display_urgency_hint 500 ms'),
349    "cfg_force_display_urgency_hint(500)\n",
350    'force_display_urgency_hint ok');
351
352 is(parser_calls('force_display_urgency_hint 700ms'),
353    "cfg_force_display_urgency_hint(700)\n",
354    'force_display_urgency_hint ok');
355
356 $config = <<'EOT';
357 force_display_urgency_hint 300
358 force_display_urgency_hint 500 ms
359 force_display_urgency_hint 700ms
360 force_display_urgency_hint 700
361 EOT
362
363 $expected = <<'EOT';
364 cfg_force_display_urgency_hint(300)
365 cfg_force_display_urgency_hint(500)
366 cfg_force_display_urgency_hint(700)
367 cfg_force_display_urgency_hint(700)
368 EOT
369
370 is(parser_calls($config),
371    $expected,
372    'force_display_urgency_hint ok');
373
374 ################################################################################
375 # workspace
376 ################################################################################
377
378 $config = <<'EOT';
379 workspace 3 output VGA-1
380 workspace "4: output" output VGA-2
381 workspace bleh output LVDS1/I_1
382 EOT
383
384 $expected = <<'EOT';
385 cfg_workspace(3, VGA-1)
386 cfg_workspace(4: output, VGA-2)
387 cfg_workspace(bleh, LVDS1/I_1)
388 EOT
389
390 is(parser_calls($config),
391    $expected,
392    'workspace ok');
393
394 ################################################################################
395 # ipc-socket
396 ################################################################################
397
398 $config = <<'EOT';
399 ipc-socket /tmp/i3.sock
400 ipc_socket ~/.i3/i3.sock
401 EOT
402
403 $expected = <<'EOT';
404 cfg_ipc_socket(/tmp/i3.sock)
405 cfg_ipc_socket(~/.i3/i3.sock)
406 EOT
407
408 is(parser_calls($config),
409    $expected,
410    'ipc-socket ok');
411
412 ################################################################################
413 # colors
414 ################################################################################
415
416 $config = <<'EOT';
417 client.focused          #4c7899 #285577 #ffffff #2e9ef4 #b34d4c
418 client.focused_inactive #333333 #5f676a #ffffff #484e50
419 client.unfocused        #333333 #222222 #888888 #292d2e
420 client.urgent           #2f343a #900000 #ffffff #900000 #c00000
421 client.placeholder      #000000 #0c0c0c #ffffff #000000
422 EOT
423
424 $expected = <<'EOT';
425 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4, #b34d4c)
426 cfg_color(client.focused_inactive, #333333, #5f676a, #ffffff, #484e50, NULL)
427 cfg_color(client.unfocused, #333333, #222222, #888888, #292d2e, NULL)
428 cfg_color(client.urgent, #2f343a, #900000, #ffffff, #900000, #c00000)
429 cfg_color(client.placeholder, #000000, #0c0c0c, #ffffff, #000000, NULL)
430 EOT
431
432 is(parser_calls($config),
433    $expected,
434    'colors ok');
435
436 ################################################################################
437 # Verify that errors don’t harm subsequent valid statements
438 ################################################################################
439
440 $config = <<'EOT';
441 hide_edge_border both
442 client.focused          #4c7899 #285577 #ffffff #2e9ef4
443 EOT
444
445 my $expected_all_tokens = "ERROR: CONFIG: Expected one of these tokens: <end>, '#', '" . join("', '", qw(
446         set
447         set_from_resource
448         bindsym
449         bindcode
450         bind
451         bar
452         font
453         mode
454         floating_minimum_size
455         floating_maximum_size
456         floating_modifier
457         default_orientation
458         workspace_layout
459         new_window
460         new_float
461         hide_edge_borders
462         for_window
463         assign
464         no_focus
465         focus_follows_mouse
466         mouse_warping
467         force_focus_wrapping
468         force_xinerama
469         force-xinerama
470         workspace_auto_back_and_forth
471         fake_outputs
472         fake-outputs
473         force_display_urgency_hint
474         focus_on_window_activation
475         show_marks
476         workspace
477         ipc_socket
478         ipc-socket
479         restart_state
480         popup_during_fullscreen
481         exec_always
482         exec
483         client.background
484         client.focused_inactive
485         client.focused
486         client.unfocused
487         client.urgent
488         client.placeholder
489     )) . "'\n";
490
491 my $expected_end = <<'EOT';
492 ERROR: CONFIG: (in file <stdin>)
493 ERROR: CONFIG: Line   1: hide_edge_border both
494 ERROR: CONFIG:           ^^^^^^^^^^^^^^^^^^^^^
495 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
496 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4, NULL)
497 EOT
498
499 $expected = $expected_all_tokens . $expected_end;
500
501 is(parser_calls($config),
502    $expected,
503    'errors dont harm subsequent statements');
504
505 $config = <<'EOT';
506 hide_edge_borders FOOBAR
507 client.focused          #4c7899 #285577 #ffffff #2e9ef4
508 EOT
509
510 $expected = <<'EOT';
511 ERROR: CONFIG: Expected one of these tokens: 'none', 'vertical', 'horizontal', 'both', 'smart', '1', 'yes', 'true', 'on', 'enable', 'active'
512 ERROR: CONFIG: (in file <stdin>)
513 ERROR: CONFIG: Line   1: hide_edge_borders FOOBAR
514 ERROR: CONFIG:                             ^^^^^^
515 ERROR: CONFIG: Line   2: client.focused          #4c7899 #285577 #ffffff #2e9ef4
516 cfg_color(client.focused, #4c7899, #285577, #ffffff, #2e9ef4, NULL)
517 EOT
518
519 is(parser_calls($config),
520    $expected,
521    'errors dont harm subsequent statements');
522
523 ################################################################################
524 # Regression: semicolons end comments, but shouldn’t
525 ################################################################################
526
527 $config = <<'EOT';
528 # "foo" client.focused          #4c7899 #285577 #ffffff #2e9ef4
529 EOT
530
531 $expected = <<'EOT';
532
533 EOT
534
535 is(parser_calls($config),
536    $expected,
537    'semicolon does not end a comment line');
538
539 ################################################################################
540 # Error message with 2+2 lines of context
541 ################################################################################
542
543 $config = <<'EOT';
544 # i3 config file (v4)
545
546 font foobar
547
548 unknown qux
549
550 # yay
551 # this should not show up
552 EOT
553
554 my $expected_head = <<'EOT';
555 cfg_font(foobar)
556 EOT
557
558 my $expected_tail = <<'EOT';
559 ERROR: CONFIG: (in file <stdin>)
560 ERROR: CONFIG: Line   3: font foobar
561 ERROR: CONFIG: Line   4: 
562 ERROR: CONFIG: Line   5: unknown qux
563 ERROR: CONFIG:           ^^^^^^^^^^^
564 ERROR: CONFIG: Line   6: 
565 ERROR: CONFIG: Line   7: # yay
566 EOT
567
568 $expected = $expected_head . $expected_all_tokens . $expected_tail;
569
570 is(parser_calls($config),
571    $expected,
572    'error message (2+2 context) ok');
573
574 ################################################################################
575 # Error message with 0+0 lines of context
576 ################################################################################
577
578 $config = <<'EOT';
579 unknown qux
580 EOT
581
582 $expected_tail = <<'EOT';
583 ERROR: CONFIG: (in file <stdin>)
584 ERROR: CONFIG: Line   1: unknown qux
585 ERROR: CONFIG:           ^^^^^^^^^^^
586 EOT
587
588 $expected = $expected_all_tokens . $expected_tail;
589
590 is(parser_calls($config),
591    $expected,
592    'error message (0+0 context) ok');
593
594 ################################################################################
595 # Error message with 1+0 lines of context
596 ################################################################################
597
598 $config = <<'EOT';
599 # context before
600 unknown qux
601 EOT
602
603 $expected_tail = <<'EOT';
604 ERROR: CONFIG: (in file <stdin>)
605 ERROR: CONFIG: Line   1: # context before
606 ERROR: CONFIG: Line   2: unknown qux
607 ERROR: CONFIG:           ^^^^^^^^^^^
608 EOT
609
610 $expected = $expected_all_tokens . $expected_tail;
611
612 is(parser_calls($config),
613    $expected,
614    'error message (1+0 context) ok');
615
616 ################################################################################
617 # Error message with 0+1 lines of context
618 ################################################################################
619
620 $config = <<'EOT';
621 unknown qux
622 # context after
623 EOT
624
625 $expected_tail = <<'EOT';
626 ERROR: CONFIG: (in file <stdin>)
627 ERROR: CONFIG: Line   1: unknown qux
628 ERROR: CONFIG:           ^^^^^^^^^^^
629 ERROR: CONFIG: Line   2: # context after
630 EOT
631
632 $expected = $expected_all_tokens . $expected_tail;
633
634 is(parser_calls($config),
635    $expected,
636    'error message (0+1 context) ok');
637
638 ################################################################################
639 # Error message with 0+2 lines of context
640 ################################################################################
641
642 $config = <<'EOT';
643 unknown qux
644 # context after
645 # context 2 after
646 EOT
647
648 $expected_tail = <<'EOT';
649 ERROR: CONFIG: (in file <stdin>)
650 ERROR: CONFIG: Line   1: unknown qux
651 ERROR: CONFIG:           ^^^^^^^^^^^
652 ERROR: CONFIG: Line   2: # context after
653 ERROR: CONFIG: Line   3: # context 2 after
654 EOT
655
656 $expected = $expected_all_tokens . $expected_tail;
657
658 is(parser_calls($config),
659    $expected,
660    'error message (0+2 context) ok');
661
662 ################################################################################
663 # Error message within mode blocks
664 ################################################################################
665
666 $config = <<'EOT';
667 mode "yo" {
668     bindsym x resize shrink left
669     unknown qux
670 }
671 EOT
672
673 $expected = <<'EOT';
674 cfg_enter_mode((null), yo)
675 cfg_mode_binding(bindsym, (null), x, (null), (null), (null), resize shrink left)
676 ERROR: CONFIG: Expected one of these tokens: <end>, '#', 'set', 'bindsym', 'bindcode', 'bind', '}'
677 ERROR: CONFIG: (in file <stdin>)
678 ERROR: CONFIG: Line   1: mode "yo" {
679 ERROR: CONFIG: Line   2:     bindsym x resize shrink left
680 ERROR: CONFIG: Line   3:     unknown qux
681 ERROR: CONFIG:               ^^^^^^^^^^^
682 ERROR: CONFIG: Line   4: }
683 EOT
684
685 is(parser_calls($config),
686    $expected,
687    'error message (mode block) ok');
688
689 ################################################################################
690 # Error message within bar blocks
691 ################################################################################
692
693 $config = <<'EOT';
694 bar {
695     output LVDS-1
696     unknown qux
697 }
698 EOT
699
700 $expected = <<'EOT';
701 cfg_bar_start()
702 cfg_bar_output(LVDS-1)
703 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', 'bindsym', 'position', 'output', 'tray_output', 'tray_padding', 'font', 'separator_symbol', 'binding_mode_indicator', 'workspace_buttons', 'strip_workspace_numbers', 'verbose', 'colors', '}'
704 ERROR: CONFIG: (in file <stdin>)
705 ERROR: CONFIG: Line   1: bar {
706 ERROR: CONFIG: Line   2:     output LVDS-1
707 ERROR: CONFIG: Line   3:     unknown qux
708 ERROR: CONFIG:               ^^^^^^^^^^^
709 ERROR: CONFIG: Line   4: }
710 cfg_bar_finish()
711 EOT
712
713 is(parser_calls($config),
714    $expected,
715    'error message (bar block) ok');
716
717 done_testing;