0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

NF: add new_text_token, new_and_legacy_text_token and tests

This commit is contained in:
Arthur Milchior 2021-05-28 14:52:12 +02:00 committed by Arthur Milchior
parent 71b715c058
commit d8c40f0c13
2 changed files with 52 additions and 9 deletions

View File

@ -183,15 +183,28 @@ public class Tokenizer implements Iterator<Tokenizer.Token> {
}
}
/**
* @param template The part of the template that must still be lexed
* @return The longest prefix without {{, or null if it's empty.
* @param legacy whether <% is accepted as a handlebar
* @return The longest prefix without handlebar, or null if it's empty.
*/
@VisibleForTesting
protected static @Nullable IResult text_token(@NonNull String template) {
int first_handlebar = template.indexOf("{{");
int text_size = (first_handlebar == -1) ? template.length() : first_handlebar;
protected static @Nullable IResult text_token(@NonNull String template, boolean legacy) {
int first_legacy_handlebar = (legacy) ? template.indexOf("<%") : -1;
int first_new_handlebar = template.indexOf("{{");
int text_size;
if (first_new_handlebar == -1) {
if (first_legacy_handlebar == -1) {
text_size = template.length();
} else {
text_size = first_legacy_handlebar;
}
} else {
if (first_legacy_handlebar == -1 || first_new_handlebar < first_legacy_handlebar) {
text_size = first_new_handlebar;
} else {
text_size = first_legacy_handlebar;
}
}
if (text_size == 0) {
return null;
}
@ -284,7 +297,7 @@ public class Tokenizer implements Iterator<Tokenizer.Token> {
if (t != null) {
return t;
}
return text_token(template);
return text_token(template, false);
}

View File

@ -31,11 +31,21 @@ import static org.junit.Assert.fail;
@RunWith(AndroidJUnit4.class)
public class TokenizerTest extends RobolectricTest {
private void test_text_token_is_null(@NonNull String template) {
assertThat(text_token(template), nullValue());
assertThat(text_token(template, false), is(nullValue()));
assertThat(text_token(template, true), is(nullValue()));
String legacy_template = new_to_legacy(template);
assertThat(text_token(legacy_template, true), is(nullValue()));
// No test for legacy_template without legacy interpretation.
}
private void test_text_token(@NonNull String template, @NonNull IResult expected) {
assertThat(text_token(template), is(expected));
assertThat(text_token(template, false), is(expected));
assertThat(text_token(template, true), is(expected));
String legacy_template = new_to_legacy(template);
IResult legacy_expected = expected.new_to_legacy();
assertThat(text_token(legacy_template, true), is(legacy_expected));
// No test for legacy_template without legacy interpretation.
}
@Test
@ -51,6 +61,26 @@ public class TokenizerTest extends RobolectricTest {
""));
}
@Test
public void legacy_in_test_new_and_legacytext_token() {
assertThat(text_token("foo<%bar%>{{plop}}", true),
is(new Tokenizer.IResult(
new Tokenizer.Token(Tokenizer.TokenKind.TEXT, "foo"),
"<%bar%>{{plop}}")));
assertThat(text_token("foo{{bar}}<%plop%>", true),
is(new Tokenizer.IResult(
new Tokenizer.Token(Tokenizer.TokenKind.TEXT, "foo"),
"{{bar}}<%plop%>")));
assertThat(text_token("foo<%bar%>{{plop}}", false),
is(new Tokenizer.IResult(
new Tokenizer.Token(Tokenizer.TokenKind.TEXT, "foo<%bar%>"),
"{{plop}}")));
assertThat(text_token("foo{{bar}}<%plop%>", false),
is(new Tokenizer.IResult(
new Tokenizer.Token(Tokenizer.TokenKind.TEXT, "foo"),
"{{bar}}<%plop%>")));
}
private void test_classify_handle(@NonNull String template, @NonNull Tokenizer.TokenKind token, @NonNull String remaining) {
assertThat(classify_handle(template), is (new Tokenizer.Token(token, remaining)));
}