Compare commits

...

3 Commits

Author SHA1 Message Date
Roberto Alsina cead5d075f more tests 2023-07-12 18:03:05 -03:00
Roberto Alsina 809bfd1e42 test args 2023-07-12 18:01:46 -03:00
Roberto Alsina 558230ea50 more tests 2023-07-12 17:37:03 -03:00
3 changed files with 106 additions and 2 deletions

View File

@ -5,7 +5,7 @@ run: shortcodes
shortcodes.c: shortcodes.rl
ragel -G2 shortcodes.rl -o shortcodes.c
shortcodes: shortcodes.c
$(CC) shortcodes.c -lbg -g -o shortcodes
$(CC) shortcodes.c -g -o shortcodes
tests: shortcodes.c shortcodes.h tests.c
$(CC) tests.c shortcodes.c -lbg -lcgreen -g -o tests
test: tests

View File

@ -2,9 +2,9 @@
## Things That May Get Done
* Add tests
* Produce decent error structures
* Detect mismatched start/end like {{% foo >}}
* Inform tag type (% or <) in result
* Handle other kinds of quotes
* Handle escaping quote characters
* Handle escaping the shortcode itself like \{{% foo %}} (check with hugo docs)

104
tests.c
View File

@ -38,6 +38,62 @@ Ensure(parse, simple_shortcode)
assert_that(s.s, is_equal_to_string("{{% shortcode %}}"));
}
Ensure(parse, inner_spaces_optional)
{
char *input = "foobar {{% shortcode%}}blah";
result = parse(input);
// Only 1 shortcode
assert_that(result[1].name.len, is_equal_to(0));
// It's a simple one called shortcode, no args
chunk_s(input, result[0].name);
assert_that(s.s, is_equal_to_string("shortcode"));
assert_that(result[0].matching, is_equal_to(0));
assert_that(result[0].argcount, is_equal_to(0));
// The whole shortcode is the whole thing
chunk_s(input, result[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode%}}"));
}
Ensure(parse, name_can_be_path)
{
char *input = "foobar {{% shortcode/foo/bar %}}blah";
result = parse(input);
// Only 1 shortcode
assert_that(result[1].name.len, is_equal_to(0));
// It's a simple one called shortcode, no args
chunk_s(input, result[0].name);
assert_that(s.s, is_equal_to_string("shortcode/foo/bar"));
assert_that(result[0].matching, is_equal_to(0));
assert_that(result[0].argcount, is_equal_to(0));
}
Ensure(parse, multiple_shortcodes)
{
char *input = "foobar {{% shortcode %}}blah {{<sc2 >}}blahblah";
result = parse(input);
// 2 shortcodes
assert_that(result[2].name.len, is_equal_to(0));
// It's a simple one called shortcode, no args
chunk_s(input, result[0].name);
assert_that(s.s, is_equal_to_string("shortcode"));
assert_that(result[0].matching, is_equal_to(0));
assert_that(result[0].argcount, is_equal_to(0));
// The whole shortcode is the whole thing
chunk_s(input, result[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode %}}"));
// It's a simple one called sc2, no args
chunk_s(input, result[1].name);
assert_that(s.s, is_equal_to_string("sc2"));
assert_that(result[1].matching, is_equal_to(0));
assert_that(result[1].argcount, is_equal_to(0));
// The whole shortcode is the whole thing
chunk_s(input, result[1].whole);
assert_that(s.s, is_equal_to_string("{{<sc2 >}}"));
}
Ensure(parse, matching_shortcode)
{
char *input = "blah {{% shortcode %}}foo bar{{% /shortcode %}} blah";
@ -59,12 +115,60 @@ Ensure(parse, matching_shortcode)
assert_that(s.s, is_equal_to_string("{{% shortcode %}}foo bar{{% /shortcode %}}"));
}
Ensure(parse, shortcode_args)
{
char *input = "foobar {{% shortcode foo \"bar\" 42 bat=v1 baz=\"v2\" %}}blah";
result = parse(input);
// Only 1 shortcode
assert_that(result[1].name.len, is_equal_to(0));
// The whole shortcode is the whole thing
chunk_s(input, result[0].whole);
assert_that(s.s, is_equal_to_string("{{% shortcode foo \"bar\" 42 bat=v1 baz=\"v2\" %}}"));
// Name is shortcode
chunk_s(input, result[0].name);
assert_that(s.s, is_equal_to_string("shortcode"));
assert_that(result[0].matching, is_equal_to(0));
// Has 5 args
assert_that(result[0].argcount, is_equal_to(5));
// Arg1 is foo, no name
assert_that(result[0].argnames[0].len, is_equal_to(0));
chunk_s(input, result[0].argvals[0]);
assert_that(s.s, is_equal_to_string("foo"));
// Arg2 is bar, no name
assert_that(result[0].argnames[1].len, is_equal_to(0));
chunk_s(input, result[0].argvals[1]);
assert_that(s.s, is_equal_to_string("bar"));
// Arg3 is 42, no name
assert_that(result[0].argnames[2].len, is_equal_to(0));
chunk_s(input, result[0].argvals[2]);
assert_that(s.s, is_equal_to_string("42"));
// Arg4 is bat=v1
chunk_s(input, result[0].argnames[3]);
assert_that(s.s, is_equal_to_string("bat"));
chunk_s(input, result[0].argvals[3]);
assert_that(s.s, is_equal_to_string("v1"));
// Arg5 is baz=v2
chunk_s(input, result[0].argnames[4]);
assert_that(s.s, is_equal_to_string("baz"));
chunk_s(input, result[0].argvals[4]);
assert_that(s.s, is_equal_to_string("v2"));
}
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, 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);
return run_test_suite(suite, create_text_reporter());
}