Compare commits
2 Commits
34a16bd6bd
...
7ead97a5e7
Author | SHA1 | Date |
---|---|---|
Roberto Alsina | 7ead97a5e7 | |
Roberto Alsina | 62fd9b5029 |
2
Makefile
2
Makefile
|
@ -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:
|
||||
|
|
17
shortcodes.h
17
shortcodes.h
|
@ -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,15 +26,16 @@ struct shortcode
|
|||
char matching;
|
||||
chunk argnames[100];
|
||||
chunk argvals[100];
|
||||
int argcount;
|
||||
unsigned int argcount;
|
||||
};
|
||||
typedef struct shortcode shortcode;
|
||||
|
||||
struct sc_result
|
||||
{
|
||||
shortcode sc[100];
|
||||
unsigned int sccount;
|
||||
sc_error errors[10];
|
||||
int errcount;
|
||||
unsigned int errcount;
|
||||
};
|
||||
typedef struct sc_result sc_result;
|
||||
|
||||
|
@ -46,6 +47,12 @@ Example:
|
|||
{{% foo %}} {{% /bar %}}
|
||||
*/
|
||||
#define ERR_MISMATCHED_CLOSING_TAG 1
|
||||
|
||||
/* You are using mismatched brackets.
|
||||
Example:
|
||||
|
||||
{{% foo >}}
|
||||
*/
|
||||
#define ERR_MISMATCHED_BRACKET 2
|
||||
|
||||
|
||||
|
|
|
@ -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,13 +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
|
||||
sc_list[c_sc].name.start = 0;
|
||||
sc_list[c_sc].name.len=0;
|
||||
// Do NOT increase c_sc
|
||||
};
|
||||
|
||||
main := (any* (shortcode | matched_shortcode | mismatched))*;
|
||||
|
@ -129,5 +130,6 @@ sc_result parse(char *input) {
|
|||
%% write init;
|
||||
%% write exec;
|
||||
|
||||
result.sccount = c_sc;
|
||||
return result;
|
||||
}
|
||||
|
|
69
tests.c
69
tests.c
|
@ -19,7 +19,8 @@ Ensure(parse, empty_string)
|
|||
char *input = "";
|
||||
result = parse(input);
|
||||
// This means no shortcodes in it
|
||||
assert_that(result.sc[0].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(0));
|
||||
assert_that(result.errcount, is_equal_to(0));
|
||||
}
|
||||
|
||||
Ensure(parse, simple_shortcode)
|
||||
|
@ -27,7 +28,7 @@ Ensure(parse, simple_shortcode)
|
|||
char *input = "foobar {{% shortcode %}}blah";
|
||||
result = parse(input);
|
||||
// Only 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
|
||||
// It's a simple one called shortcode, no args
|
||||
chunk_s(input, result.sc[0].name);
|
||||
|
@ -37,18 +38,31 @@ 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);
|
||||
// One shortcode, one error
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
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";
|
||||
result = parse(input);
|
||||
// No shortcodes
|
||||
assert_that(result.sc[0].name.len, is_equal_to(0));
|
||||
// No shortcodes, 1 error
|
||||
assert_that(result.sccount, is_equal_to(0));
|
||||
assert_that(result.errcount, is_equal_to(1));
|
||||
assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_BRACKET));
|
||||
str_copyb(&s, input + result.errors[0].position, 3);
|
||||
assert_that(s.s, is_equal_to_string(">}}"));
|
||||
assert_that(s.s, is_equal_to_string(">}}"));
|
||||
}
|
||||
|
||||
Ensure(parse, mismatched_brackets_inside_data_are_ok)
|
||||
|
@ -56,11 +70,12 @@ Ensure(parse, mismatched_brackets_inside_data_are_ok)
|
|||
char *input = "foobar {{% sc %}} >}}blah {{% /sc %}} ";
|
||||
result = parse(input);
|
||||
// 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
chunk_s(input, result.sc[0].whole);
|
||||
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)
|
||||
|
@ -68,11 +83,12 @@ Ensure(parse, mismatched_brackets_in_qval_are_ok)
|
|||
char *input = "foobar {{% sc \">}}blah\" %}} {{% /sc %}}";
|
||||
result = parse(input);
|
||||
// 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
chunk_s(input, result.sc[0].whole);
|
||||
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)
|
||||
|
@ -80,7 +96,7 @@ Ensure(parse, inner_spaces_optional)
|
|||
char *input = "foobar {{% shortcode%}}blah";
|
||||
result = parse(input);
|
||||
// Only 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
|
||||
// It's a simple one called shortcode, no args
|
||||
chunk_s(input, result.sc[0].name);
|
||||
|
@ -90,6 +106,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)
|
||||
|
@ -97,13 +114,14 @@ Ensure(parse, name_can_be_path)
|
|||
char *input = "foobar {{% shortcode/foo/bar %}}blah";
|
||||
result = parse(input);
|
||||
// Only 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
|
||||
// It's a simple one called shortcode, no args
|
||||
chunk_s(input, result.sc[0].name);
|
||||
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)
|
||||
|
@ -111,7 +129,7 @@ Ensure(parse, multiple_shortcodes)
|
|||
char *input = "foobar {{% shortcode %}}blah {{<sc2 >}}blahblah";
|
||||
result = parse(input);
|
||||
// 2 shortcodes
|
||||
assert_that(result.sc[2].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(2));
|
||||
|
||||
// It's a simple one called shortcode, no args
|
||||
chunk_s(input, result.sc[0].name);
|
||||
|
@ -129,6 +147,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)
|
||||
|
@ -137,7 +156,7 @@ Ensure(parse, matching_shortcode)
|
|||
result = parse(input);
|
||||
|
||||
// Only 1 shortcode
|
||||
assert_that(result.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
|
||||
// It's a matching one called shortcode, no args
|
||||
chunk_s(input, result.sc[0].name);
|
||||
|
@ -150,6 +169,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)
|
||||
|
@ -157,7 +177,7 @@ 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.sc[1].name.len, is_equal_to(0));
|
||||
assert_that(result.sccount, is_equal_to(1));
|
||||
|
||||
// The whole shortcode is the whole thing
|
||||
chunk_s(input, result.sc[0].whole);
|
||||
|
@ -193,6 +213,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?
|
||||
|
@ -202,26 +223,4 @@ Ensure(parse, shortcode_args)
|
|||
// result = parse(input);
|
||||
// // 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
|
||||
// }
|
Loading…
Reference in New Issue