Skip to content

Commit

Permalink
Cleanup parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Mar 17, 2016
1 parent b3aee00 commit c2e0b1c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static ParserResult parse(Reader in) throws IOException {
* @param bibtexString
* @return Returns returns an empty collection if no entries where found or if an error occurred.
*/
public static Collection<BibEntry> fromString(String bibtexString) {
public static List<BibEntry> fromString(String bibtexString) {
StringReader reader = new StringReader(bibtexString);
BibtexParser parser = new BibtexParser(reader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public static void makeLabel(MetaData metaData, BibDatabase dBase, BibEntry entr
if (!alwaysAddLetter && (occurrences == 0)) {
// No dupes found, so we can just go ahead.
if (!key.equals(oldKey)) {
if (! database.containsEntryWithId(entry.getId())) {
if (!database.containsEntryWithId(entry.getId())) {
// entry does not (yet) exist in the database, just update the entry
entry.setField(BibEntry.KEY_FIELD, key);
} else {
Expand Down
142 changes: 65 additions & 77 deletions src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,64 +49,64 @@ public void initalizationWithNullThrowsNullPointerException() {

@Test
public void fromStringRecognizesEntry() throws Exception {
Collection<BibEntry> c = BibtexParser.fromString("@article{test,author={Ed von Test}}");
assertEquals(1, c.size());
List<BibEntry> parsed = BibtexParser.fromString("@article{test,author={Ed von Test}}");

BibEntry e = c.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
assertEquals(2, e.getFieldNames().size());
assertEquals("Ed von Test", e.getField("author"));
BibEntry expected = new BibEntry();
expected.setType("article");
expected.setCiteKey("test");
expected.setField("author", "Ed von Test");
BibtexEntryAssert.assertEquals(Arrays.asList(expected), parsed);
}

@Test
public void fromStringReturnsEmptyListFromEmptyString() {
Collection<BibEntry> c = BibtexParser.fromString("");
Assert.assertNotNull(c);
assertEquals(0, c.size());
Collection<BibEntry> parsed = BibtexParser.fromString("");
Assert.assertNotNull(parsed);
assertEquals(Collections.emptyList(), parsed);
}

@Test
public void fromStringReturnsEmptyListIfNoEntryRecognized() {
Collection<BibEntry> c = BibtexParser.fromString("@@article@@{{{{{{}");
Assert.assertNotNull(c);
assertEquals(0, c.size());
Collection<BibEntry> parsed = BibtexParser.fromString("@@article@@{{{{{{}");
Assert.assertNotNull(parsed);
assertEquals(Collections.emptyList(), parsed);
}

@Test
public void singleFromStringRecognizesEntry() {
BibEntry e = BibtexParser.singleFromString(
BibEntry parsed = BibtexParser.singleFromString(
"@article{canh05," + " author = {Crowston, K. and Annabi, H.},\n" + " title = {Title A}}\n");

assertEquals("article", e.getType());
assertEquals("canh05", e.getCiteKey());
assertEquals("Crowston, K. and Annabi, H.", e.getField("author"));
assertEquals("Title A", e.getField("title"));
BibEntry expected = new BibEntry();
expected.setType("article");
expected.setCiteKey("canh05");
expected.setField("author", "Crowston, K. and Annabi, H.");
expected.setField("title", "Title A");
BibtexEntryAssert.assertEquals(expected, parsed);
}

@Test
public void singleFromStringRecognizesEntryInMultiple() {
BibEntry e = BibtexParser.singleFromString("@article{canh05," + " author = {Crowston, K. and Annabi, H.},\n"
BibEntry parsed = BibtexParser.singleFromString("@article{canh05," + " author = {Crowston, K. and Annabi, H.},\n"
+ " title = {Title A}}\n" + "@inProceedings{foo," + " author={Norton Bar}}");

Assert.assertTrue(e.getCiteKey().equals("canh05") || e.getCiteKey().equals("foo"));
Assert.assertTrue(parsed.getCiteKey().equals("canh05") || parsed.getCiteKey().equals("foo"));
}

@Test
public void singleFromStringReturnsNullFromEmptyString() {
BibEntry e = BibtexParser.singleFromString("");
Assert.assertNull(e);
BibEntry parsed = BibtexParser.singleFromString("");
Assert.assertNull(parsed);
}

@Test
public void singleFromStringReturnsNullIfNoEntryRecognized() {
BibEntry e = BibtexParser.singleFromString("@@article@@{{{{{{}");
Assert.assertNull(e);
BibEntry parsed = BibtexParser.singleFromString("@@article@@{{{{{{}");
Assert.assertNull(parsed);
}

@Test
public void parseTwoTimesReturnsSameResult() throws IOException {

BibtexParser parser = new BibtexParser(new StringReader("@article{test,author={Ed von Test}}"));
ParserResult result = parser.parse();

Expand All @@ -115,14 +115,13 @@ public void parseTwoTimesReturnsSameResult() throws IOException {

@Test
public void parseRecognizesEntry() throws IOException {

ParserResult result = BibtexParser.parse(new StringReader(
"@article{test,author={Ed von Test}}"));

Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(1, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();
assertEquals(1, parsed.size());

BibEntry e = c.iterator().next();
BibEntry e = parsed.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
assertEquals(2, e.getFieldNames().size());
Expand All @@ -135,10 +134,10 @@ public void parseQuotedEntries() throws IOException {
ParserResult result = BibtexParser.parse(new StringReader(
"@article{test,author=\"Ed von Test\"}"));

Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(1, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();
assertEquals(1, parsed.size());

BibEntry e = c.iterator().next();
BibEntry e = parsed.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
assertEquals(2, e.getFieldNames().size());
Expand All @@ -152,10 +151,10 @@ public void parseRecognizesEntryOnlyWithKey() throws IOException {
ParserResult result = BibtexParser.parse(new StringReader(
"@article{test}"));

Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(1, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();
assertEquals(1, parsed.size());

BibEntry e = c.iterator().next();
BibEntry e = parsed.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
}
Expand All @@ -165,10 +164,10 @@ public void parseRecognizesEntryWithWhitespaceAtBegining() throws IOException {

ParserResult result = BibtexParser.parse(new StringReader(" @article{test,author={Ed von Test}}"));

Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(1, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();
assertEquals(1, parsed.size());

BibEntry e = c.iterator().next();
BibEntry e = parsed.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
assertEquals(2, e.getFieldNames().size());
Expand All @@ -180,10 +179,10 @@ public void parseRecognizesEntryWithWhitespace() throws IOException {

ParserResult result = BibtexParser.parse(new StringReader("@article { test,author={Ed von Test}}"));

Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(1, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();
assertEquals(1, parsed.size());

BibEntry e = c.iterator().next();
BibEntry e = parsed.iterator().next();
assertEquals("article", e.getType());
assertEquals("test", e.getCiteKey());
assertEquals(2, e.getFieldNames().size());
Expand Down Expand Up @@ -307,28 +306,23 @@ public void parseRecognizesMultipleEntries() throws IOException {
ParserResult result = BibtexParser
.parse(new StringReader("@article{canh05," + " author = {Crowston, K. and Annabi, H.},\n"
+ " title = {Title A}}\n" + "@inProceedings{foo," + " author={Norton Bar}}"));
Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(2, c.size());

Iterator<BibEntry> i = c.iterator();
BibEntry a = i.next();
BibEntry b = i.next();
List<BibEntry> parsed = result.getDatabase().getEntries();

// Sort them because we can't be sure about the order
if (a.getCiteKey().equals("foo")) {
BibEntry tmp = a;
a = b;
b = tmp;
}
List<BibEntry> expected = new ArrayList<>();
BibEntry firstEntry = new BibEntry();
firstEntry.setType("article");
firstEntry.setCiteKey("canh05");
firstEntry.setField("author", "Crowston, K. and Annabi, H.");
firstEntry.setField("title", "Title A");
expected.add(firstEntry);

assertEquals("article", a.getType());
assertEquals("canh05", a.getCiteKey());
assertEquals("Crowston, K. and Annabi, H.", a.getField("author"));
assertEquals("Title A", a.getField("title"));
BibEntry secondEntry = new BibEntry();
secondEntry.setType("inproceedings");
secondEntry.setCiteKey("foo");
secondEntry.setField("author", "Norton Bar");
expected.add(secondEntry);

assertEquals("inproceedings", b.getType());
assertEquals("foo", b.getCiteKey());
assertEquals("Norton Bar", b.getField("author"));
BibtexEntryAssert.assertEquals(expected, parsed);
}

@Test
Expand All @@ -352,25 +346,20 @@ public void parseSetsParsedSerialization() throws IOException {
public void parseRecognizesMultipleEntriesOnSameLine() throws IOException {

ParserResult result = BibtexParser.parse(new StringReader("@article{canh05}" + "@inProceedings{foo}"));
Collection<BibEntry> c = result.getDatabase().getEntries();
assertEquals(2, c.size());
List<BibEntry> parsed = result.getDatabase().getEntries();

Iterator<BibEntry> i = c.iterator();
BibEntry a = i.next();
BibEntry b = i.next();

// Sort them because we can't be sure about the order
if (a.getCiteKey().equals("foo")) {
BibEntry tmp = a;
a = b;
b = tmp;
}
List<BibEntry> expected = new ArrayList<>();
BibEntry firstEntry = new BibEntry();
firstEntry.setType("article");
firstEntry.setCiteKey("canh05");
expected.add(firstEntry);

assertEquals("article", a.getType());
assertEquals("canh05", a.getCiteKey());
BibEntry secondEntry = new BibEntry();
secondEntry.setType("inproceedings");
secondEntry.setCiteKey("foo");
expected.add(secondEntry);

assertEquals("inproceedings", b.getType());
assertEquals("foo", b.getCiteKey());
BibtexEntryAssert.assertEquals(expected, parsed);
}

@Test
Expand Down Expand Up @@ -1462,6 +1451,5 @@ public void parseReturnsEntriesInSameOrder() throws IOException {
expected.add(c);

BibtexEntryAssert.assertEquals(expected, result.getDatabase().getEntries());

}
}

0 comments on commit c2e0b1c

Please sign in to comment.