Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * @brief Initialize Config while capturing and checking its complete diagnostic output
5 : : *
6 : : * Direct configuration tests use TESTING=true and expect one initialization
7 : : * message on stdout and no stderr. The initialized settings remain available
8 : : * to the caller
9 : : *
10 : : * @return SUCCESS when initialization output matches the golden template and stderr is empty
11 : : */
12 : 94 : Return init_config_and_check_output(void)
13 : : {
14 : : /* Status returned by this function through deliver()
15 : : Default value assumes successful completion */
16 : 94 : Return status = SUCCESS;
17 : :
18 : 94 : m_create(char,captured_stdout,MEMORY_STRING);
19 : 94 : m_create(char,captured_stderr,MEMORY_STRING);
20 : 94 : m_create(char,pattern,MEMORY_STRING);
21 : :
22 : 94 : run(function_capture(init_config,captured_stdout,captured_stderr));
23 : 94 : run(get_file_content("templates/0045_033.txt",pattern));
24 : 94 : run(match_pattern(captured_stdout,pattern,"templates/0045_033.txt"));
25 : 94 : ASSERT(captured_stderr->length == 0U);
26 : :
27 : 94 : call(m_del(pattern));
28 : 94 : call(m_del(captured_stderr));
29 : 94 : call(m_del(captured_stdout));
30 : :
31 : 94 : deliver(status);
32 : : }
33 : :
34 : : /**
35 : : * @brief Run application scenarios in order and check their exit codes and selected output streams
36 : : *
37 : : * The cases array must end with a zero-initialized record whose arguments is
38 : : * NULL. An empty arguments string is a valid scenario that runs the application
39 : : * without arguments. Execution stops at the terminator or the first failure
40 : : *
41 : : * Each stdout_template or stderr_template accepts a file path, the corresponding
42 : : * STDOUT_MUST_BE_EMPTY or STDERR_MUST_BE_EMPTY macro, or NULL. A file supplies
43 : : * the expected PCRE2 pattern. The macros supply the pattern directly and
44 : : * require the corresponding stream to contain no bytes. NULL skips
45 : : * comparison of that stream. Optional substitution applies to all scenarios
46 : : * and changes each pattern in memory, leaving the template files unchanged.
47 : : * Environment and fixture setup belong to the caller
48 : : *
49 : : * @param[in] cases Scenario array terminated by a record with NULL arguments
50 : : * @param[in] template Text to replace in the patterns, or NULL to skip substitution
51 : : * @param[in] replacement Replacement text, required when template is not NULL
52 : : * @return SUCCESS when all scenarios match their expected exit codes and selected output streams
53 : : */
54 : 297 : Return run_and_match_templates(
55 : : const struct argument_test_case *cases,
56 : : const char *template,
57 : : const char *replacement)
58 : : {
59 : : /* Status returned by this function through deliver()
60 : : Default value assumes successful completion */
61 : 297 : Return status = SUCCESS;
62 : :
63 [ + + + - ]: 1078 : for(size_t i = 0; cases[i].arguments != NULL && SUCCESS == status; i++)
64 : : {
65 : 781 : const struct argument_test_case *test_case = &cases[i];
66 : :
67 : 781 : m_create(char,stdout_result,MEMORY_STRING);
68 : 781 : m_create(char,stderr_result,MEMORY_STRING);
69 : 781 : m_create(char,pattern,MEMORY_STRING);
70 : :
71 : 781 : run(runit(test_case->arguments,stdout_result,stderr_result,
72 : : test_case->expected_return_code,STDERR_ALLOW));
73 : :
74 [ + + ]: 781 : if(test_case->stdout_template != NULL)
75 : : {
76 : 777 : const char *filename = test_case->stdout_template;
77 [ + + ]: 777 : if(strcmp(filename,STDOUT_MUST_BE_EMPTY) == 0)
78 : : {
79 : 2 : run(m_copy_string(pattern,STDOUT_MUST_BE_EMPTY));
80 : 2 : filename = NULL;
81 : : } else {
82 : 775 : run(get_file_content(filename,pattern));
83 : : }
84 : :
85 [ + + ]: 777 : if(template != NULL)
86 : : {
87 : 60 : run(replace_placeholder(pattern,template,replacement));
88 : : }
89 : 777 : run(match_pattern(stdout_result,pattern,filename));
90 : : }
91 : :
92 [ + - ]: 781 : if(test_case->stderr_template != NULL)
93 : : {
94 : 781 : const char *filename = test_case->stderr_template;
95 [ + + ]: 781 : if(strcmp(filename,STDERR_MUST_BE_EMPTY) == 0)
96 : : {
97 : 494 : run(m_copy_string(pattern,STDERR_MUST_BE_EMPTY));
98 : 494 : filename = NULL;
99 : : } else {
100 : 287 : run(get_file_content(filename,pattern));
101 : : }
102 : :
103 [ + + ]: 781 : if(template != NULL)
104 : : {
105 : 60 : run(replace_placeholder(pattern,template,replacement));
106 : : }
107 : 781 : run(match_pattern(stderr_result,pattern,filename));
108 : : }
109 : :
110 : 781 : call(m_del(pattern));
111 : 781 : call(m_del(stderr_result));
112 : 781 : call(m_del(stdout_result));
113 : : }
114 : :
115 : 297 : deliver(status);
116 : : }
|