Working with tests

This commit is contained in:
Dimitry Ivanov 2018-08-25 12:21:18 +03:00
parent a0e208c370
commit 9d80637035
12 changed files with 342 additions and 12 deletions

View File

@ -8,8 +8,12 @@ import android.text.TextUtils;
@SuppressWarnings({"unused", "WeakerAccess"})
public class UrlProcessorAndroidAssets implements UrlProcessor {
static final String MOCK = "https://android.asset/";
static final String BASE = "file:///android_asset/";
private final UrlProcessorRelativeToAbsolute assetsProcessor
= new UrlProcessorRelativeToAbsolute("file:///android_asset/");
= new UrlProcessorRelativeToAbsolute(MOCK);
private final UrlProcessor processor;
@ -27,7 +31,7 @@ public class UrlProcessorAndroidAssets implements UrlProcessor {
final String out;
final Uri uri = Uri.parse(destination);
if (TextUtils.isEmpty(uri.getScheme())) {
out = assetsProcessor.process(destination);
out = assetsProcessor.process(destination).replace(MOCK, BASE);
} else {
if (processor != null) {
out = processor.process(destination);

View File

@ -78,7 +78,7 @@ public class ImageSizeResolverDef extends ImageSizeResolver {
if (UNIT_EM.equals(dimension.unit)) {
out = (int) (dimension.value * textSize + .5F);
} else {
out = original;
out = (int) (dimension.value + .5F);
}
return out;
}

View File

@ -239,7 +239,7 @@ public class SpannableMarkdownVisitor extends AbstractVisitor {
newLine();
final int length = builder.length();
builder.append(' '); // without space it won't render
builder.append('\u00a0'); // without space it won't render
setSpan(length, factory.thematicBreak(theme));

View File

@ -0,0 +1,49 @@
package ru.noties.markwon;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertEquals;
import static ru.noties.markwon.UrlProcessorAndroidAssets.BASE;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class UrlProcessorAndroidAssetsTest {
private UrlProcessorAndroidAssets processor;
@Before
public void before() {
processor = new UrlProcessorAndroidAssets();
}
@Test
public void access_root() {
final String path = "/whoam.i";
assertEquals(
BASE.substring(0, BASE.length() - 1) + path,
processor.process(path)
);
}
@Test
public void access_folder_without_modifier() {
final String path = "first/second/thi.rd";
assertEquals(
BASE + path,
processor.process(path)
);
}
@Test
public void change_directory_inside_path() {
final String path = "first/../second/./thi.rd";
assertEquals(
BASE + "second/thi.rd",
processor.process(path)
);
}
}

View File

@ -0,0 +1,61 @@
package ru.noties.markwon;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.*;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class UrlProcessorRelativeToAbsoluteTest {
@Test
public void malformed_base_do_not_process() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("!@#$%^&*(");
final String destination = "../hey.there.html";
assertEquals(destination, processor.process(destination));
}
@Test
public void access_root() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("https://ro.ot/hello/");
final String url = "/index.html";
assertEquals("https://ro.ot/index.html", processor.process(url));
}
@Test
public void access_same_directory() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("https://ro.ot/hello/");
final String url = "./.htaccess";
assertEquals("https://ro.ot/hello/.htaccess", processor.process(url));
}
@Test
public void asset_directory_up() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("http://ro.ot/first/second/");
final String url = "../cat.JPG";
assertEquals("http://ro.ot/first/cat.JPG", processor.process(url));
}
@Test
public void change_directory_inside_destination() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("http://ro.ot/first/");
final String url = "../first/../second/./thi.rd";
assertEquals(
"http://ro.ot/second/thi.rd",
processor.process(url)
);
}
@Test
public void with_query_arguments() {
final UrlProcessorRelativeToAbsolute processor = new UrlProcessorRelativeToAbsolute("http://ro.ot/first/");
final String url = "../index.php?ROOT=1";
assertEquals(
"http://ro.ot/index.php?ROOT=1",
processor.process(url)
);
}
}

View File

@ -0,0 +1,132 @@
package ru.noties.markwon.renderer;
import android.graphics.Rect;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import ru.noties.markwon.renderer.ImageSize.Dimension;
import static org.junit.Assert.*;
import static ru.noties.markwon.renderer.ImageSizeResolverDef.UNIT_EM;
import static ru.noties.markwon.renderer.ImageSizeResolverDef.UNIT_PERCENT;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class ImageSizeResolverDefTest {
private ImageSizeResolverDef def;
@Before
public void before() {
def = new ImageSizeResolverDef();
}
@Test
public void no_image_size() {
// no image size returns image original bounds
final Rect rect = new Rect(0, 0, 15, 43);
assertEquals(rect, def.resolveImageSize(null, rect, -1, Float.NaN));
}
@Test
public void height_percent_not_used() {
final Rect rect = new Rect(1, 2, 3, 4);
assertEquals(
rect,
def.resolveImageSize(
new ImageSize(null, new Dimension(100.F, UNIT_PERCENT)),
rect,
-1,
Float.NaN
)
);
}
@Test
public void width_percent_scales_keeps_ratio() {
final Rect rect = new Rect(0, 0, 10, 20);
assertEquals(
new Rect(0, 0, 50, 100),
def.resolveImageSize(
new ImageSize(new Dimension(50.F, UNIT_PERCENT), null),
rect,
100,
Float.NaN
)
);
}
@Test
public void unknown_dimension_considered_absolute() {
final Rect rect = new Rect(0, 0, 22, 33);
assertEquals(
new Rect(0,0,7,9),
def.resolveImageSize(
new ImageSize(new Dimension(7, "width"), new Dimension(9, "height")),
rect,
-1,
Float.NaN
)
);
}
@Test
public void width_height_text_size_relative() {
final Rect rect = new Rect(0, 0, 100, 200);
assertEquals(
new Rect(0, 0, 20, 40),
def.resolveImageSize(
new ImageSize(new Dimension(2.f, UNIT_EM), new Dimension(4.F, UNIT_EM)),
rect,
-1,
10.F
)
);
}
@Test
public void width_text_size_relative_height_keeps_ratio() {
final Rect rect = new Rect(0, 0, 15, 30);
assertEquals(
new Rect(0, 0, 10, 20),
def.resolveImageSize(
new ImageSize(new Dimension(1.F, UNIT_EM), null),
rect,
-1,
10.F
)
);
}
@Test
public void absolute_height_keeps_width_ratio() {
final Rect rect = new Rect(0, 0, 50, 25);
assertEquals(
new Rect(0, 0, 100, 50),
def.resolveImageSize(
new ImageSize(null, new Dimension(50, "px")),
rect,
-1,
Float.NaN
)
);
}
@Test
public void relative_text_size_height_keeps_width_ratio() {
final Rect rect = new Rect(0, 0, 4, 12);
assertEquals(
new Rect(0, 0, 10, 30),
def.resolveImageSize(
new ImageSize(null, new Dimension(3.F, UNIT_EM)),
rect,
-1,
10.F
)
);
}
}

View File

@ -60,7 +60,7 @@ public class SpannableMarkdownVisitorTest {
final SpannableStringBuilder stringBuilder = builder.spannableStringBuilder();
System.out.printf("%s: %s%n", file, Arrays.toString(stringBuilder.getSpans(0, stringBuilder.length(), Object.class)));
// System.out.printf("%s: %s%n", file, Arrays.toString(stringBuilder.getSpans(0, stringBuilder.length(), Object.class)));
int index = 0;
@ -113,7 +113,7 @@ public class SpannableMarkdownVisitorTest {
final String info = node.toString();
System.out.printf("%s: %s%n", file, builder.subSequence(index, out));
// System.out.printf("%s: %s%n", file, builder.subSequence(index, out));
// we can possibly have parent spans here, should filter them
final Object[] spans = builder.getSpans(index, out, Object.class);
@ -148,6 +148,8 @@ public class SpannableMarkdownVisitorTest {
assertEquals(info, index, builder.getSpanStart(testSpan));
assertEquals(info, out, builder.getSpanEnd(testSpan));
System.out.printf("%s: expected: %s, actual: %s%n", file, span.attributes(), testSpan.attributes());
assertMapEquals(info, span.attributes(), testSpan.attributes());
return out;
@ -175,8 +177,8 @@ public class SpannableMarkdownVisitorTest {
@NonNull String message,
@NonNull Map<String, String> expected,
@NonNull Map<String, String> actual) {
boolean result = true;
if (expected.size() == actual.size()) {
boolean result = expected.size() == actual.size();
if (result) {
for (Map.Entry<String, String> entry : expected.entrySet()) {
if (!actual.containsKey(entry.getKey())
|| !equals(entry.getValue(), actual.get(entry.getKey()))) {

View File

@ -30,6 +30,7 @@ import java.util.Set;
import ix.Ix;
import ix.IxFunction;
import ix.IxPredicate;
import ru.noties.markwon.spans.TableRowSpan;
import static ru.noties.markwon.renderer.visitor.TestSpan.BLOCK_QUOTE;
import static ru.noties.markwon.renderer.visitor.TestSpan.BULLET_LIST;
@ -102,6 +103,7 @@ abstract class TestDataReader {
static class Reader {
private static final String TEXT = "text";
private static final String CELLS = "cells";
private static final Set<String> TAGS;
@ -240,7 +242,23 @@ abstract class TestDataReader {
if (valueElement.isJsonNull()) {
value = null;
} else {
value = valueElement.getAsString();
// another special case: table cell
// this is not so good
if (CELLS.equals(key)) {
final JsonArray cells = valueElement.getAsJsonArray();
final int length = cells.size();
final List<TableRowSpan.Cell> list = new ArrayList<>(length);
for (int k = 0; k < length; k++) {
final JsonObject cell = cells.get(k).getAsJsonObject();
list.add(new TableRowSpan.Cell(
cell.get("alignment").getAsInt(),
cell.get("text").getAsString()
));
}
value = list.toString();
} else {
value = valueElement.getAsString();
}
}
attributes.put(key, value);
}

View File

@ -71,8 +71,10 @@ output:
- h2: "unordered-list"
- text: "\n"
- ul: "ul1"
level: 0
- text: "\n"
- ul: "ul2"
level: 0
- text: "\n"
- h2: "ordered-list"
- text: "\n"
@ -86,6 +88,8 @@ output:
- text: "\n"
- img: "img"
src: "img://src"
imageSize: null
replacementTextIsLink: false
- text: "\n"
- h2: "blockquote"
- text: "\n"

View File

@ -4,4 +4,4 @@
input: "---"
output:
- hr: " "
- hr: "\u00a0"

View File

@ -1,4 +1,13 @@
input: "table-table-table"
input: "col1|col2|col3\n---|---|---|"
output:
- text: "should fail as we are to decide how to pass info here"
- tr: "\u00a0"
header: true
odd: false
cells:
- alignment: 0
text: "col1"
- alignment: 0
text: "col2"
- alignment: 0
text: "col3"

View File

@ -0,0 +1,51 @@
input: |-
head1|head2|head3
---|:---:|---:
row1-col1|row1-col2|row1-col3
row2-col1|row2-col2|row2-col3
row3-col1|row3-col2|row3-col3
output:
- tr: "\u00a0"
header: true
odd: false
cells:
- alignment: 0
text: "head1"
- alignment: 1
text: "head2"
- alignment: 2
text: "head3"
- text: "\n"
- tr: "\u00a0"
header: false
odd: false
cells:
- alignment: 0
text: "row1-col1"
- alignment: 1
text: "row1-col2"
- alignment: 2
text: "row1-col3"
- text: "\n"
- tr: "\u00a0"
header: false
odd: true
cells:
- alignment: 0
text: "row2-col1"
- alignment: 1
text: "row2-col2"
- alignment: 2
text: "row2-col3"
- text: "\n"
- tr: "\u00a0"
header: false
odd: false
cells:
- alignment: 0
text: "row3-col1"
- alignment: 1
text: "row3-col2"
- alignment: 2
text: "row3-col3"