File size: 3,963 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "../../unity/unity.h"
#include <string.h>
#include <stddef.h>

/* Unity hooks */
void setUp(void) {
  /* No global setup needed; each test fully initializes the globals it uses. */
}
void tearDown(void) {
  /* No teardown needed. */
}

/* Helper to configure default delimiters: section_del = "\:", and
   header/body/footer delimiters as "\:h", "\:b", "\:f". */
static void configure_default_delims(void)
{
  /* Use static storage so pointers remain valid during the test function. */
  static char h[] = "\\:h";
  static char b[] = "\\:b";
  static char f[] = "\\:f";

  /* section_del defaults to "\\:" defined in the program; use that object
     rather than a literal to avoid constness issues. */
  section_del = DEFAULT_SECTION_DELIMITERS;

  header_del = h;
  header_del_len = strlen(h);

  body_del = b;
  body_del_len = strlen(b);

  footer_del = f;
  footer_del_len = strlen(f);
}

/* Helper to set the global line buffer to a given C string s whose
   contents include a trailing '\n'. The length is set to strlen(s),
   consistent with the program's usage (length includes the newline). */
static void set_line_buf_str(const char *s)
{
  line_buf.buffer = (char *) s; /* We will not modify s. */
  line_buf.length = strlen(s);
}

/* Tests */

static void test_check_section_returns_Text_when_line_too_short(void)
{
  configure_default_delims();
  /* Only one character before newline => len = 1, should be Text. */
  const char *line = "A\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Text, sec);
}

static void test_check_section_returns_Text_when_prefix_mismatch(void)
{
  configure_default_delims();
  /* Does not start with the section_del ("\\:"), so must be Text. */
  const char *line = "xyh\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Text, sec);
}

static void test_check_section_detects_Header(void)
{
  configure_default_delims();
  /* Matches "\\:h" exactly (before newline). */
  const char *line = "\\:h\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Header, sec);
}

static void test_check_section_detects_Body(void)
{
  configure_default_delims();
  /* Matches "\\:b" exactly (before newline). */
  const char *line = "\\:b\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Body, sec);
}

static void test_check_section_detects_Footer(void)
{
  configure_default_delims();
  /* Matches "\\:f" exactly (before newline). */
  const char *line = "\\:f\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Footer, sec);
}

static void test_check_section_only_prefix_without_type_is_Text(void)
{
  configure_default_delims();
  /* Line equals just the two-character prefix before newline; should be Text
     since len (2) != any of the *_del_len (3). */
  const char *line = "\\:\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Text, sec);
}

static void test_check_section_disabled_by_empty_delimiter_returns_Text(void)
{
  configure_default_delims();
  /* Disable section matching by setting footer_del_len < 2, as per code path. */
  footer_del_len = 0;

  /* Even though the line would otherwise be a Header marker, it must be Text. */
  const char *line = "\\:h\n";
  set_line_buf_str(line);

  enum section sec = check_section();
  TEST_ASSERT_EQUAL_INT(Text, sec);
}

/* Unity main */
int main(void)
{
  UNITY_BEGIN();

  RUN_TEST(test_check_section_returns_Text_when_line_too_short);
  RUN_TEST(test_check_section_returns_Text_when_prefix_mismatch);
  RUN_TEST(test_check_section_detects_Header);
  RUN_TEST(test_check_section_detects_Body);
  RUN_TEST(test_check_section_detects_Footer);
  RUN_TEST(test_check_section_only_prefix_without_type_is_Text);
  RUN_TEST(test_check_section_disabled_by_empty_delimiter_returns_Text);

  return UNITY_END();
}