Skip to content

Commit

Permalink
Add more unit test to support my change
Browse files Browse the repository at this point in the history
1. more unit test
2. rollback logic to follow old code which assumed that databaseContext is never null
  • Loading branch information
leaf-soba committed Sep 30, 2024
1 parent e94136f commit 639f9bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ public String doLayout(BibEntry bibtex, BibDatabase database) {
}
}

private String resolveFieldEntry(BibEntry bibtex, BibDatabase database) {
private String resolveFieldEntry(BibEntry bidEntry, BibDatabase database) {
// resolve field (recognized by leading backslash) or text
if (text.startsWith("\\")) {
return bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(text.substring(1)), database).orElse("");
return bidEntry.getResolvedFieldOrAlias(FieldFactory.parseField(text.substring(1)), database)
.orElse("");
}
if (database == null) {
return text;
Expand All @@ -249,10 +251,7 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
LOGGER.warn("'{}' is an obsolete name for the entry type. Please update your layout to use '{}' instead.", InternalField.OBSOLETE_TYPE_HEADER, InternalField.TYPE_HEADER);
fieldEntry = bibtex.getType().getDisplayName();
} else {
// changed section begin - arudert
// resolve field (recognized by leading backslash) or text
fieldEntry = resolveFieldEntry(bibtex, database);
// changed section end - arudert
}

if (option != null) {
Expand Down Expand Up @@ -368,8 +367,7 @@ public String doLayout(BibDatabaseContext databaseContext, Charset encoding) {
throw new UnsupportedOperationException("field and group ends not allowed in begin or end layout");

case LayoutHelper.IS_OPTION_FIELD:
String field = Optional.ofNullable(databaseContext)
.map(BibDatabaseContext::getDatabase)
String field = Optional.ofNullable(databaseContext.getDatabase())
.map(db -> db.resolveForStrings(text))
.orElse(text);
if (option != null) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/jabref/logic/layout/LayoutEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.metadata.MetaData;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -95,4 +101,34 @@ void parseMethodCalls() {
assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").getFirst()).get(1));
assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1)).get(1));
}

private void assertUnsupportedOperation(int type, List<StringInt> parsedEntries, BibDatabaseContext context) {
LayoutEntry layoutEntry = new LayoutEntry(parsedEntries, type, null, null, null);
assertThrows(UnsupportedOperationException.class, () -> layoutEntry.doLayout(context, StandardCharsets.UTF_8));
}

private void assertLayoutResult(int type, String expected, List<StringInt> parsedEntries, BibDatabaseContext context) {
LayoutEntry layoutEntry = new LayoutEntry(parsedEntries, type, null, null, null);
assertEquals(expected, layoutEntry.doLayout(context, StandardCharsets.UTF_8));
}

@Test
void doLayoutTest() {
List<StringInt> parsedEntries = List.of(new StringInt("place_holder", 0),
new StringInt("testString", 0));
BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData(), null);

List<Integer> unsupportedOperationTypes = List.of(2, 3, 4, 6, 7);
unsupportedOperationTypes.forEach(type -> assertUnsupportedOperation(type, parsedEntries, bibDatabaseContext));

// test IS_LAYOUT_TEXT
assertLayoutResult(1, "testString", parsedEntries, bibDatabaseContext);
// test IS_OPTION_FIELD with empty dataBase in BibDatabaseContext
assertLayoutResult(5, "testString", parsedEntries, bibDatabaseContext);
// test IS_ENCODING_NAME
assertLayoutResult(8, StandardCharsets.UTF_8.toString(), parsedEntries, bibDatabaseContext);
// test IS_FILENAME and IS_FILEPATH with empty path
assertLayoutResult(9, "", parsedEntries, bibDatabaseContext);
assertLayoutResult(10, "", parsedEntries, bibDatabaseContext);
}
}

0 comments on commit 639f9bb

Please sign in to comment.