|
|
#include "../../unity/unity.h" |
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char prefix_buf[64]; |
|
|
|
|
|
|
|
|
static void reset_state(void) |
|
|
{ |
|
|
|
|
|
crown = false; |
|
|
tagged = false; |
|
|
split = false; |
|
|
uniform = false; |
|
|
|
|
|
|
|
|
strcpy(prefix_buf, ""); |
|
|
set_prefix(prefix_buf); |
|
|
|
|
|
|
|
|
|
|
|
max_width = 75; |
|
|
goal_width = (max_width * (100 - 7)) / 100; |
|
|
|
|
|
|
|
|
in_column = 0; |
|
|
out_column = 0; |
|
|
tabs = false; |
|
|
|
|
|
prefix_indent = 0; |
|
|
first_indent = 0; |
|
|
other_indent = 0; |
|
|
|
|
|
next_char = 0; |
|
|
next_prefix_indent = 0; |
|
|
last_line_length = 0; |
|
|
|
|
|
|
|
|
wptr = parabuf; |
|
|
word_limit = word; |
|
|
} |
|
|
|
|
|
|
|
|
static FILE *make_file(const char *s) |
|
|
{ |
|
|
FILE *f = tmpfile(); |
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(f, "tmpfile() failed"); |
|
|
if (fputs(s, f) == EOF) |
|
|
{ |
|
|
fclose(f); |
|
|
TEST_FAIL_MESSAGE("fputs() to tmpfile failed"); |
|
|
} |
|
|
rewind(f); |
|
|
return f; |
|
|
} |
|
|
|
|
|
|
|
|
static int count_words(void) |
|
|
{ |
|
|
return (int)(word_limit - word); |
|
|
} |
|
|
|
|
|
void setUp(void) |
|
|
{ |
|
|
|
|
|
reset_state(); |
|
|
} |
|
|
|
|
|
void tearDown(void) |
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static void test_get_paragraph_basic_single_line(void) |
|
|
{ |
|
|
const char *input = "hello world\n\n"; |
|
|
FILE *f = make_file(input); |
|
|
|
|
|
|
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(get_paragraph(f), "get_paragraph should find a paragraph"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, count_words()); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, prefix_indent); |
|
|
TEST_ASSERT_EQUAL_INT(0, first_indent); |
|
|
TEST_ASSERT_EQUAL_INT(0, other_indent); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(5, word[0].length); |
|
|
TEST_ASSERT_TRUE(!word[0].final); |
|
|
TEST_ASSERT_TRUE(word_limit[-1].final); |
|
|
TEST_ASSERT_TRUE(word_limit[-1].period); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT('\n', next_char); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_get_paragraph_crown_mode(void) |
|
|
{ |
|
|
crown = true; |
|
|
const char *input = |
|
|
"one\n" |
|
|
" two words\n" |
|
|
" three\n" |
|
|
" four\n"; |
|
|
FILE *f = make_file(input); |
|
|
|
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(get_paragraph(f), "crown: should read a paragraph"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(4, count_words()); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, first_indent); |
|
|
TEST_ASSERT_EQUAL_INT(2, other_indent); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT('f', next_char); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(word_limit[-1].final); |
|
|
TEST_ASSERT_TRUE(word_limit[-1].period); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_get_paragraph_tagged_mode_requires_diff_indent(void) |
|
|
{ |
|
|
tagged = true; |
|
|
const char *input = |
|
|
"first\n" |
|
|
"second\n"; |
|
|
|
|
|
FILE *f = make_file(input); |
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(get_paragraph(f), "tagged: should read a paragraph"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, count_words()); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, first_indent); |
|
|
TEST_ASSERT_EQUAL_INT(DEF_INDENT, other_indent); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT('s', next_char); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_get_paragraph_split_only_single_line(void) |
|
|
{ |
|
|
split = true; |
|
|
const char *input = |
|
|
"aaa bbb\n" |
|
|
"ccc ddd\n"; |
|
|
|
|
|
FILE *f = make_file(input); |
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(get_paragraph(f), "split: should read a single-line paragraph"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, count_words()); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(first_indent, other_indent); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT('c', next_char); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_get_paragraph_with_prefix_included_lines(void) |
|
|
{ |
|
|
|
|
|
strcpy(prefix_buf, ">>"); |
|
|
set_prefix(prefix_buf); |
|
|
|
|
|
const char *input = |
|
|
">> a b\n" |
|
|
">> c\n"; |
|
|
FILE *f = make_file(input); |
|
|
|
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(get_paragraph(f), "prefix: should read a paragraph with prefixed lines"); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(3, count_words()); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, word[0].length); |
|
|
TEST_ASSERT_EQUAL_CHAR('a', word[0].text[0]); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, prefix_indent); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(EOF, next_char); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(word_limit[-1].final); |
|
|
TEST_ASSERT_TRUE(word_limit[-1].period); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_get_paragraph_empty_file_returns_false(void) |
|
|
{ |
|
|
const char *input = ""; |
|
|
FILE *f = make_file(input); |
|
|
|
|
|
next_char = get_prefix(f); |
|
|
|
|
|
TEST_ASSERT_FALSE_MESSAGE(get_paragraph(f), "Empty file: should return false"); |
|
|
|
|
|
fclose(f); |
|
|
} |
|
|
|
|
|
int main(void) |
|
|
{ |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_get_paragraph_basic_single_line); |
|
|
RUN_TEST(test_get_paragraph_crown_mode); |
|
|
RUN_TEST(test_get_paragraph_tagged_mode_requires_diff_indent); |
|
|
RUN_TEST(test_get_paragraph_split_only_single_line); |
|
|
RUN_TEST(test_get_paragraph_with_prefix_included_lines); |
|
|
RUN_TEST(test_get_paragraph_empty_file_returns_false); |
|
|
return UNITY_END(); |
|
|
} |