bc40b37c7453be2ee0b1f0aafcdce5a859ee0783
[miniexpect.git] / test-multi-match.c
1 /* miniexpect test suite
2  * Copyright (C) 2014 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <assert.h>
25
26 #include "miniexpect.h"
27 #include "tests.h"
28
29 int
30 main (int argc, char *argv[])
31 {
32   mexp_h *h;
33   int status;
34   int r;
35   int rv[5];
36   size_t i;
37   pcre2_code *multi_re = test_compile_re ("multi");
38   pcre2_code *match_re = test_compile_re ("match");
39   pcre2_code *ing_re = test_compile_re ("ing");
40   pcre2_code *str_re = test_compile_re ("str");
41   pcre2_code *s_re = test_compile_re ("s");
42   pcre2_match_data *match_data = pcre2_match_data_create (4, NULL);
43
44   /* If the subprocess prints multiple things, we should be able to
45    * repeatedly call mexp_expect to match on each one.  This didn't
46    * work correctly in earlier versions of the library.
47    */
48   h = mexp_spawnl ("echo", "echo", "multimatchingstrs", NULL);
49   assert (h != NULL);
50
51   for (i = 0; i < 5; ++i) {
52     r = mexp_expect (h,
53                      (mexp_regexp[]) {
54                        { 100, multi_re, 0 },
55                        { 101, match_re, 0 },
56                        { 102, ing_re, 0 },
57                        { 103, str_re, 0 },
58                        { 104, s_re, 0 },
59                        { 0 },
60                      }, match_data);
61     switch (r) {
62     case 100: case 101: case 102: case 103: case 104:
63       printf ("iteration %zu: matched %d\n", i, r);
64       rv[i] = r;
65       break;
66     case MEXP_EOF:
67       fprintf (stderr, "error: unexpected EOF in iteration %zu\n", i);
68       exit (EXIT_FAILURE);
69     case MEXP_TIMEOUT:
70       fprintf (stderr, "error: unexpected timeout in iteration %zu\n", i);
71       exit (EXIT_FAILURE);
72     case MEXP_ERROR:
73         perror ("mexp_expect");
74       exit (EXIT_FAILURE);
75     case MEXP_PCRE_ERROR:
76       fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h));
77       exit (EXIT_FAILURE);
78     }
79   }
80
81   assert (rv[0] == 100); /* multi */
82   assert (rv[1] == 101); /* match */
83   assert (rv[2] == 102); /* ing */
84   assert (rv[3] == 103); /* str */
85   assert (rv[4] == 104); /* s */
86
87   status = mexp_close (h);
88   if (status != 0 && !test_is_sighup (status)) {
89     fprintf (stderr, "%s: non-zero exit status from subcommand: ", argv[0]);
90     test_diagnose (status);
91     fprintf (stderr, "\n");
92     exit (EXIT_FAILURE);
93   }
94
95   pcre2_code_free (multi_re);
96   pcre2_code_free (match_re);
97   pcre2_code_free (ing_re);
98   pcre2_code_free (str_re);
99   pcre2_code_free (s_re);
100   pcre2_match_data_free (match_data);
101
102   exit (EXIT_SUCCESS);
103 }