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

Migrate TemplateTest.java to Kotlin

This commit is contained in:
Prince kushwaha 2022-05-02 22:31:00 +05:30 committed by Mike Hardy
parent 9d690e922f
commit a582349586
2 changed files with 115 additions and 131 deletions

View File

@ -42,8 +42,8 @@ permission notice:
// Example of class name: "/com/ichi2/anki/UIUtils.kt"
// Ensure that it starts with '/' (slash)
def source = Source.TEST
def className = "/com/ichi2/libanki/template/TemplateTest.kt"
def source = Source.MAIN
def className = ""
enum Source {
MAIN("/src/main/java"),

View File

@ -13,167 +13,151 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.libanki.template
package com.ichi2.libanki.template;
import com.ichi2.anki.R;
import com.ichi2.anki.RobolectricTest;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import java.util.HashMap;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static com.ichi2.libanki.template.Tokenizer.ALT_HANDLEBAR_DIRECTIVE;
import static com.ichi2.libanki.template.Tokenizer.new_to_legacy;
import static com.ichi2.libanki.template.TokenizerTest.new_to_legacy_template;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
@RunWith(AndroidJUnit4.class)
public class TemplateTest extends RobolectricTest {
private String render(String template, Map<String, String> fields) {
return ParsedNode.parse_inner(template).render(fields, true, getTargetContext());
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.RobolectricTest
import com.ichi2.utils.KotlinCleanup
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.Matchers.*
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
@KotlinCleanup(" 'is'->equalTo")
@RunWith(AndroidJUnit4::class)
class TemplateTest : RobolectricTest() {
private fun render(template: String, fields: Map<String, String>): String {
return ParsedNode.parse_inner(template).render(fields, true, targetContext)
}
@Test
public void fieldStartingWithExclamation() {
fun fieldStartingWithExclamation() {
// Ankidroid used not to display fields whose name start with !
HashMap<String, String> context = new HashMap<>();
context.put("!Front", "Test");
assertThat(render("{{!Front}}", context), is("Test"));
}
@Test
@Config(qualifiers = "en")
public void missingExclamation() {
// Ankidroid used not to display fields whose name start with !
HashMap<String, String> context = new HashMap<>();
String rendered = render("{{!Front}}", context);
assertThat(rendered, is(notNullValue()));
assertThat(rendered, containsString("there is no field called '!Front'"));
}
@Test
public void typeInFieldRenders() {
HashMap<String, String> context = new HashMap<>();
context.put("Front", "AA{{type:Back}}");
assertThat(render("{{Front}}", context), is("AA{{type:Back}}"));
}
@Test
public void testNotFoundWillRender() {
String maybeBad = "{{#NotFound}}{{NotFound}}{{/NotFound}}";
HashMap<String, String> context = new HashMap<>();
assertThat(render(maybeBad, context), Matchers.emptyString());
val context = HashMap<String, String>()
context["!Front"] = "Test"
assertThat(render("{{!Front}}", context), `is`("Test"))
}
@Test
@Config(qualifiers = "en")
public void nestedTemplatesRenderWell() {
//#6123
String problematicTemplate = "{{#One}}\n" +
" {{#One}}\n" +
" {{One}}<br>\n" +
" {{/One}}\n" +
" {{#Two}}\n" +
" {{Two}}\n" +
" {{/Two}}\n" +
"{{/One}}";
fun missingExclamation() {
// Ankidroid used not to display fields whose name start with !
val context = HashMap<String, String>()
val rendered = render("{{!Front}}", context)
HashMap<String, String> context = new HashMap<>();
context.put("One", "Card1 - One");
context.put("Two", "Card1 - Two");
String result = render(problematicTemplate, context);
assertThat(rendered, `is`(notNullValue()))
assertThat(
rendered, containsString("there is no field called '!Front'")
)
}
//most important - that it does render
assertThat(result, not("{{Invalid template}}"));
//Actual value (may be subject to change).
assertThat(result, is("\n \n Card1 - One<br>\n \n \n Card1 - Two\n \n"));
@Test
fun typeInFieldRenders() {
val context = HashMap<String, String>()
context["Front"] = "AA{{type:Back}}"
assertThat(render("{{Front}}", context), `is`("AA{{type:Back}}"))
}
@Test
fun testNotFoundWillRender() {
val maybeBad = "{{#NotFound}}{{NotFound}}{{/NotFound}}"
val context = HashMap<String, String>()
assertThat(render(maybeBad, context), Matchers.emptyString())
}
@Test
@Config(qualifiers = "en")
fun nestedTemplatesRenderWell() {
// #6123
val problematicTemplate = """{{#One}}
{{#One}}
{{One}}<br>
{{/One}}
{{#Two}}
{{Two}}
{{/Two}}
{{/One}}"""
val context = HashMap<String, String>()
context["One"] = "Card1 - One"
context["Two"] = "Card1 - Two"
val result = render(problematicTemplate, context)
// most important - that it does render
assertThat(result, not("{{Invalid template}}"))
// Actual value (may be subject to change).
assertThat(result, `is`("\n \n Card1 - One<br>\n \n \n Card1 - Two\n \n"))
}
@Test
@Ignore("GitHub: 6284")
public void fieldNamesHaveTrailingSpacesIgnored() {
//#6284
String templateWithSpaces = "{{#IllustrationExample }}Illustration Example: {{IllustrationExample }}{{/IllustrationExample}}";
fun fieldNamesHaveTrailingSpacesIgnored() {
// #6284
val templateWithSpaces =
"{{#IllustrationExample }}Illustration Example: {{IllustrationExample }}{{/IllustrationExample}}"
HashMap<String, String> context = new HashMap<>();
context.put("IllustrationExample", "ilex");
test_render(templateWithSpaces, context, "Illustration Example: ilex");
val context = HashMap<String, String>()
context["IllustrationExample"] = "ilex"
test_render(templateWithSpaces, context, "Illustration Example: ilex")
}
private void test_render(@NonNull String template, @NonNull Map<String, String> m, @NonNull String expected) {
assertThat(render(template, m), is(expected));
String legacy_template = new_to_legacy_template(template);
assertThat(render(legacy_template, m), is(expected));
private fun test_render(template: String, m: Map<String, String>, expected: String) {
assertThat(render(template, m), `is`(expected))
val legacy_template = TokenizerTest.new_to_legacy_template(template)
assertThat(render(legacy_template, m), `is`(expected))
}
private void test_render_contains(@NonNull String template, @NonNull Map<String, String> m, @NonNull String contained) {
assertThat(render(template, m), containsString(contained));
String legacy_template = new_to_legacy_template(template);
assertThat(render(legacy_template, m), containsString(contained));
private fun test_render_contains(template: String, m: Map<String, String>, contained: String) {
assertThat(render(template, m), containsString(contained))
val legacy_template = TokenizerTest.new_to_legacy_template(template)
assertThat(render(legacy_template, m), containsString(contained))
}
@Test
@Config(qualifiers = "en")
public void test_render() {
Map<String, String> m = new HashMap<>();
m.put("Test", "Test");
m.put("Foo", "Foo");
test_render("", m, "");
test_render("Test", m, "Test");
test_render("{{Test}}", m, "Test");
test_render("{{Filter2:Filter1:Test}}", m, "Test");
test_render("{{type:Test}}", m, "[[type:Test]]");
test_render("{{Filter2:type:Test}}", m, "[[Filter2:type:Test]]");
test_render("Foo{{Test}}", m, "FooTest");
test_render_contains("Foo{{!Test}}", m, "there is no field called '!Test'");
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "Test");
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "");
m.put("Foo", "");
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "");
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "Test");
m.put("Foo", " \t");
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "");
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "Test");
fun test_render() {
val m: MutableMap<String, String> = HashMap()
m["Test"] = "Test"
m["Foo"] = "Foo"
test_render("", m, "")
test_render("Test", m, "Test")
test_render("{{Test}}", m, "Test")
test_render("{{Filter2:Filter1:Test}}", m, "Test")
test_render("{{type:Test}}", m, "[[type:Test]]")
test_render("{{Filter2:type:Test}}", m, "[[Filter2:type:Test]]")
test_render("Foo{{Test}}", m, "FooTest")
test_render_contains("Foo{{!Test}}", m, "there is no field called '!Test'")
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "Test")
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "")
m["Foo"] = ""
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "")
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "Test")
m["Foo"] = " \t"
test_render("{{#Foo}}{{Test}}{{/Foo}}", m, "")
test_render("{{^Foo}}{{Test}}{{/Foo}}", m, "Test")
}
@Test
@Config(qualifiers = "en")
public void empty_field_name() {
Map m = new HashMap();
fun empty_field_name() {
val m: MutableMap<String, String> = HashMap()
// Empty field is not usually a valid field name and should be corrected.
// However, if we have an empty field name in the collection, this test ensure
// that it works as expected.
// This is especially relevant because filter applied to no field is valid
m.put("Test", "Test");
m.put("Foo", "Foo");
test_render_contains("{{}}", m, "there is no field called ''");
test_render_contains("{{ }}", m, "there is no field called ''");
test_render("{{filterName:}}", m, "");
test_render("{{filterName: }}", m, "");
m["Test"] = "Test"
m["Foo"] = "Foo"
test_render_contains("{{}}", m, "there is no field called ''")
test_render_contains("{{ }}", m, "there is no field called ''")
test_render("{{filterName:}}", m, "")
test_render("{{filterName: }}", m, "")
m.put("", "Test");
test_render("{{}}", m, "Test");
m.clear();
m[""] = "Test"
test_render("{{}}", m, "Test")
m.clear()
}
}