1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package it.niedermann.nextcloud.deck.util;
import android.content.Context;
import android.os.Build;
import androidx.test.core.app.ApplicationProvider;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import it.niedermann.nextcloud.deck.model.Card;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.P})
public class CardUtilTest {
@Test
public void testGetCardContentAsString() {
final Context appContext = ApplicationProvider.getApplicationContext();
final Card card = new Card();
final FullCard fullCard = new FullCard();
card.setTitle("Foo");
card.setDescription("Bar");
card.setDueDate(null);
fullCard.setCard(card);
fullCard.setLabels(Collections.emptyList());
assertEquals("Bar", CardUtil.getCardContentAsString(appContext, fullCard));
final Instant testDate = LocalDateTime.of(2011, 10, 6, 6, 30, 50, 100000).toInstant(ZoneOffset.UTC);
card.setDueDate(testDate);
assertEquals("Bar\n" +
"Due date: Oct 6, 2011 8:30:50 AM", CardUtil.getCardContentAsString(appContext, fullCard));
card.setDueDate(null);
final Label testLabel1 = new Label();
testLabel1.setTitle("Baz");
final Label testLabel2 = new Label();
testLabel2.setTitle("Pow");
fullCard.setLabels(Lists.newArrayList(testLabel1));
assertEquals("Bar\n" +
"Labels: Baz", CardUtil.getCardContentAsString(appContext, fullCard));
fullCard.setLabels(Lists.newArrayList(testLabel1, testLabel2));
assertEquals("Bar\n" +
"Labels: Baz, Pow", CardUtil.getCardContentAsString(appContext, fullCard));
card.setDueDate(testDate);
assertEquals("Bar\n" +
"Due date: Oct 6, 2011 8:30:50 AM\n" +
"Labels: Baz, Pow", CardUtil.getCardContentAsString(appContext, fullCard));
}
@Test
public void generateTitleFromDescription() {
final String content = "" +
"# Heading\n" +
"- Test \n" +
"\n" +
"\n" +
"This is **bold**";
assertEquals("Heading", CardUtil.generateTitleFromDescription(content));
assertEquals("Test", CardUtil.generateTitleFromDescription("Test"));
assertEquals("", CardUtil.generateTitleFromDescription(null));
}
@Test
public void getLineWithoutMarkDown() {
try {
final Method getLineWithoutMarkDown = CardUtil.class.getDeclaredMethod("getLineWithoutMarkDown", String.class, int.class);
getLineWithoutMarkDown.setAccessible(true);
final String content = "" +
"# Heading\n" +
"- Test \n" +
"\n" +
"\n" +
"This is **bold**";
assertEquals("Test", getLineWithoutMarkDown.invoke(null, "Test", 0));
assertEquals("Heading", getLineWithoutMarkDown.invoke(null, content, 0));
assertEquals("Test", getLineWithoutMarkDown.invoke(null, content, 1));
assertEquals("This is bold", getLineWithoutMarkDown.invoke(null, content, 2));
assertEquals("This is bold", getLineWithoutMarkDown.invoke(null, content, 3));
assertEquals("This is bold", getLineWithoutMarkDown.invoke(null, content, 4));
assertEquals("", getLineWithoutMarkDown.invoke(null, content, 5));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
@Test
@SuppressWarnings("ConstantConditions")
public void isEmptyLine() {
try {
final Method isEmptyLine = CardUtil.class.getDeclaredMethod("isEmptyLine", String.class);
isEmptyLine.setAccessible(true);
assertTrue((Boolean) isEmptyLine.invoke(null, ""));
assertTrue((Boolean) isEmptyLine.invoke(null, "# "));
assertTrue((Boolean) isEmptyLine.invoke(null, "\n"));
assertTrue((Boolean) isEmptyLine.invoke(null, "- "));
assertTrue((Boolean) isEmptyLine.invoke(null, "****"));
assertFalse((Boolean) isEmptyLine.invoke(null, "*italic*"));
assertFalse((Boolean) isEmptyLine.invoke(null, "__bold__"));
assertFalse((Boolean) isEmptyLine.invoke(null, "_italic_"));
assertFalse((Boolean) isEmptyLine.invoke(null, "# Headline\n\n**bold** \n_italic_"));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
@Test
public void removeMarkDown() {
try {
final Method removeMarkDown = CardUtil.class.getDeclaredMethod("removeMarkDown", String.class);
removeMarkDown.setAccessible(true);
assertEquals("", removeMarkDown.invoke(null, ""));
assertEquals("Headline", removeMarkDown.invoke(null, "# Headline"));
assertEquals("Item", removeMarkDown.invoke(null, "- Item"));
assertEquals("bold", removeMarkDown.invoke(null, "**bold**"));
assertEquals("italic", removeMarkDown.invoke(null, "*italic*"));
assertEquals("bold", removeMarkDown.invoke(null, "__bold__"));
assertEquals("italic", removeMarkDown.invoke(null, "_italic_"));
assertEquals("Headline\nbold\nitalic", removeMarkDown.invoke(null, "# Headline\n\n**bold** \n_italic_"));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
|