mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-04 23:28:19 -05:00
Add some tests to the OsisParser
Testing state machines can be difficult if it's not black box...
This commit is contained in:
parent
6a45d872f8
commit
c57babd68a
@ -0,0 +1,51 @@
|
||||
package org.bspeice.minimalbible.test.format.osisparser;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import org.bspeice.minimalbible.MBTestCase;
|
||||
import org.bspeice.minimalbible.service.format.osisparser.OsisParser;
|
||||
import org.crosswire.jsword.book.OSISUtil;
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* State machine testing, oh boy!
|
||||
*/
|
||||
public class OsisParserTest extends MBTestCase {
|
||||
|
||||
OsisParser parser;
|
||||
|
||||
public void setUp() {
|
||||
parser = new OsisParser(null);
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public void testDoWriteEnabledVerse() {
|
||||
Attributes attributes = mock(Attributes.class);
|
||||
parser.startElement("", OSISUtil.OSIS_ELEMENT_VERSE, "", attributes);
|
||||
assertTrue(parser.getDoWrite().peek());
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private void parserAssert(OsisParser parser, String element) {
|
||||
Attributes attributes = mock(Attributes.class);
|
||||
parser.startElement("", element, "", attributes);
|
||||
assertFalse(parser.getDoWrite().isEmpty());
|
||||
parser.getDoWrite().pop();
|
||||
}
|
||||
|
||||
public void testDoWriteAlwaysAdded() {
|
||||
parserAssert(parser, OSISUtil.OSIS_ELEMENT_VERSE);
|
||||
parserAssert(parser, "");
|
||||
parserAssert(parser, "random string");
|
||||
parserAssert(parser, OSISUtil.OSIS_ELEMENT_CELL);
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public void testEndElementPopsQueue() {
|
||||
parser.getDoWrite().add(true);
|
||||
parser.endElement("", "", "");
|
||||
assertTrue(parser.getDoWrite().isEmpty());
|
||||
}
|
||||
}
|
@ -10,30 +10,29 @@ import org.crosswire.jsword.book.OSISUtil
|
||||
* Created by bspeice on 9/10/14.
|
||||
*/
|
||||
|
||||
class OsisParser(v: Verse) : DefaultHandler() {
|
||||
class OsisParser(v: Verse?) : DefaultHandler() {
|
||||
|
||||
val verseContent = VerseContent(v)
|
||||
val verseContent: VerseContent? = when (v) {
|
||||
is Verse -> VerseContent(v) // not null
|
||||
else -> null
|
||||
}
|
||||
// TODO: Implement a stack to keep min API 8
|
||||
val doWrite = ArrayDeque<Boolean>()
|
||||
|
||||
// Android Studio complains about compilation, but the method
|
||||
// has @NotNull annotations, so we program for those.
|
||||
override fun startElement(uri: String, localName: String,
|
||||
qName: String, attributes: Attributes) {
|
||||
when (localName) {
|
||||
OSISUtil.OSIS_ELEMENT_VERSE -> doWrite.push(true)
|
||||
else -> {
|
||||
doWrite.push(false)
|
||||
}
|
||||
else -> doWrite.push(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Android Studio complains about compilation, but the method
|
||||
// has @NotNull annotations, so we program for those.
|
||||
override fun endElement(uri: String?, localName: String, qName: String) {
|
||||
override fun endElement(uri: String, localName: String, qName: String) {
|
||||
doWrite.pop()
|
||||
}
|
||||
override fun characters(ch: CharArray?, start: Int, length: Int) {
|
||||
|
||||
override fun characters(ch: CharArray, start: Int, length: Int) {
|
||||
if (doWrite.peek())
|
||||
verseContent.appendContent(String(ch as CharArray))
|
||||
verseContent?.appendContent(String(ch))
|
||||
}
|
||||
}
|
@ -6,7 +6,4 @@ import org.crosswire.jsword.passage.Verse
|
||||
* Created by bspeice on 9/9/14.
|
||||
*/
|
||||
|
||||
class VerseReference(verse: Verse, index: Int) {
|
||||
val verse: Verse = verse
|
||||
val index: Int = index
|
||||
}
|
||||
class VerseReference(val verse: Verse, val index: Int) {}
|
||||
|
Loading…
Reference in New Issue
Block a user