Add closing tag error reporting

This commit is contained in:
Roberto Alsina 2023-07-13 21:57:47 -03:00
parent 34a16bd6bd
commit 62fd9b5029
4 changed files with 36 additions and 33 deletions

View File

@ -1,7 +1,7 @@
CC=gcc
all: test
shortcodes.c: shortcodes.rl
ragel -G2 -L shortcodes.rl -o shortcodes.c
ragel -G2 shortcodes.rl -o shortcodes.c
tests.so: shortcodes.c tests.c
$(CC) -fPIC -shared -g -o $@ $^ -lbg -lcgreen
clean:

View File

@ -5,15 +5,15 @@
// and goes on for len characters.
struct chunk
{
int start, len;
unsigned int start, len;
};
typedef struct chunk chunk;
// An error
struct sc_error
{
int position;
int code;
unsigned int position;
unsigned int code;
};
typedef struct sc_error sc_error;
@ -26,7 +26,7 @@ struct shortcode
char matching;
chunk argnames[100];
chunk argvals[100];
int argcount;
unsigned int argcount;
};
typedef struct shortcode shortcode;
@ -34,7 +34,7 @@ struct sc_result
{
shortcode sc[100];
sc_error errors[10];
int errcount;
unsigned int errcount;
};
typedef struct sc_result sc_result;

View File

@ -87,8 +87,6 @@
matched_shortcode = (shortcode any* closing_shortcode)
@ {
sc_list[c_sc-1].matching = 1;
sc_list[c_sc-1].whole.len = p-start-sc_list[c_sc-1].whole.start + 1;
if (
sc_list[c_sc-1].name.len != sc_list[c_sc].name.len ||
strncmp(
@ -96,11 +94,16 @@
start + sc_list[c_sc].name.start,
sc_list[c_sc-1].name.len) !=0)
{
result.errors[result.errcount].position = p-start;
result.errors[result.errcount].position =
sc_list[c_sc].whole.start;
result.errors[result.errcount].code = ERR_MISMATCHED_CLOSING_TAG;
result.errcount++;
} else {
// The previous shortcode is matching (mark it)
sc_list[c_sc-1].matching = 1;
sc_list[c_sc-1].whole.len = p-start-sc_list[c_sc-1].whole.start + 1;
}
// Reuse this shortcode entry for next one
// Reuse the closing shortcode tag entry for next one
sc_list[c_sc].name.start = 0;
sc_list[c_sc].name.len=0;
};

44
tests.c
View File

@ -20,6 +20,7 @@ Ensure(parse, empty_string)
result = parse(input);
// This means no shortcodes in it
assert_that(result.sc[0].name.len, is_equal_to(0));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, simple_shortcode)
@ -37,8 +38,22 @@ Ensure(parse, simple_shortcode)
// The whole shortcode is the whole thing
chunk_s(input, result.sc[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode %}}"));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, mismatched_tags)
{
char *input = "foobar {{% shortcode %}}blah{{% /foo %}}";
result = parse(input);
// No shortcodes
assert_that(result.sc[0].name.len, is_equal_to(0));
assert_that(result.errcount, is_equal_to(1));
assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_CLOSING_TAG));
str_copyb(&s, input + result.errors[0].position, 8);
assert_that(s.s, is_equal_to_string("{{% /foo"));
}
Ensure(parse, mismatched_brackets)
{
char *input = "foobar {{% shortcode >}}blah";
@ -61,6 +76,7 @@ Ensure(parse, mismatched_brackets_inside_data_are_ok)
assert_that(s.s, is_equal_to_string("{{% sc %}} >}}blah {{% /sc %}}"));
chunk_s(input, result.sc[0].data);
assert_that(s.s, is_equal_to_string(" >}}blah "));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, mismatched_brackets_in_qval_are_ok)
@ -73,6 +89,7 @@ Ensure(parse, mismatched_brackets_in_qval_are_ok)
assert_that(s.s, is_equal_to_string("{{% sc \">}}blah\" %}} {{% /sc %}}"));
chunk_s(input, result.sc[0].argvals[0]);
assert_that(s.s, is_equal_to_string(">}}blah"));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, inner_spaces_optional)
@ -90,6 +107,7 @@ Ensure(parse, inner_spaces_optional)
// The whole shortcode is the whole thing
chunk_s(input, result.sc[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode%}}"));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, name_can_be_path)
@ -104,6 +122,7 @@ Ensure(parse, name_can_be_path)
assert_that(s.s, is_equal_to_string("shortcode/foo/bar"));
assert_that(result.sc[0].matching, is_equal_to(0));
assert_that(result.sc[0].argcount, is_equal_to(0));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, multiple_shortcodes)
@ -129,6 +148,7 @@ Ensure(parse, multiple_shortcodes)
// The whole shortcode is the whole thing
chunk_s(input, result.sc[1].whole);
assert_that(s.s, is_equal_to_string("{{<sc2 >}}"));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, matching_shortcode)
@ -150,6 +170,7 @@ Ensure(parse, matching_shortcode)
// The whole shortcode is the whole thing
chunk_s(input, result.sc[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode %}}foo bar{{% /shortcode %}}"));
assert_that(result.errcount, is_equal_to(0));
}
Ensure(parse, shortcode_args)
@ -193,6 +214,7 @@ Ensure(parse, shortcode_args)
assert_that(s.s, is_equal_to_string("baz"));
chunk_s(input, result.sc[0].argvals[4]);
assert_that(s.s, is_equal_to_string("v2"));
assert_that(result.errcount, is_equal_to(0));
}
// BUG?
@ -203,25 +225,3 @@ Ensure(parse, shortcode_args)
// // No shortcodes
// assert_that(result.sc[0].name.len, is_equal_to(0));
// }
#ifdef MAIN
int main(int argc, char **argv)
{
str_init(&s);
TestSuite *suite = create_test_suite();
add_test_with_context(suite, parse, empty_string);
add_test_with_context(suite, parse, simple_shortcode);
add_test_with_context(suite, parse, mismatched_brackets);
add_test_with_context(suite, parse, mismatched_brackets_inside_data_are_ok);
add_test_with_context(suite, parse, mismatched_brackets_in_qval_are_ok);
add_test_with_context(suite, parse, name_can_be_path);
add_test_with_context(suite, parse, inner_spaces_optional);
add_test_with_context(suite, parse, multiple_shortcodes);
add_test_with_context(suite, parse, matching_shortcode);
add_test_with_context(suite, parse, shortcode_args);
// Bugs
// add_test_with_context(suite, parse, escaped_shortcode);
return run_test_suite(suite, create_text_reporter());
}
#endif