X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Ftest%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FMobileLedgerProfileTest.java;fp=app%2Fsrc%2Ftest%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FMobileLedgerProfileTest.java;h=8416c1015097a6225e4456f0e0903b3f46b830c1;hp=0000000000000000000000000000000000000000;hb=0a73337c99e2074aa7e7228204289896342ec636;hpb=48e81a77a6f218ccaaa4b77fa1a84084422c7a86 diff --git a/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java b/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java new file mode 100644 index 00000000..8416c101 --- /dev/null +++ b/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java @@ -0,0 +1,90 @@ +/* + * Copyright © 2020 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.model; + +import org.junit.Test; +import org.junit.internal.ArrayComparisonFailure; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; + +public class MobileLedgerProfileTest { + private List listFromArray(LedgerAccount[] array) { + ArrayList result = new ArrayList<>(); + Collections.addAll(result, array); + + return result; + } + private void aTest(LedgerAccount[] oldList, LedgerAccount[] newList, + LedgerAccount[] expectedResult) { + List result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList), + listFromArray(newList)); + assertArrayEquals(expectedResult, result.toArray()); + } + private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList, + LedgerAccount[] expectedResult) { + List result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList), + listFromArray(newList)); + assertThrows(ArrayComparisonFailure.class, + () -> assertArrayEquals(expectedResult, result.toArray())); + } + private LedgerAccount[] emptyArray() { + return new LedgerAccount[]{}; + } + @Test + public void mergeEmptyLists() { + aTest(emptyArray(), emptyArray(), emptyArray()); + } + @Test + public void mergeIntoEmptyLists() { + LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null); + aTest(emptyArray(), new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}); + } + @Test + public void mergeEmptyList() { + LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null); + aTest(new LedgerAccount[]{acc1}, emptyArray(), emptyArray()); + } + @Test + public void mergeEqualLists() { + LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null); + aTest(new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}); + } + @Test + public void mergeFlags() { + LedgerAccount acc1a = new LedgerAccount(null, "Acc1", null); + LedgerAccount acc1b = new LedgerAccount(null, "Acc1", null); + acc1b.setExpanded(true); + acc1b.setAmountsExpanded(true); + List merged = + MobileLedgerProfile.mergeAccountLists(listFromArray(new LedgerAccount[]{acc1a}), + listFromArray(new LedgerAccount[]{acc1b})); + assertArrayEquals(new LedgerAccount[]{acc1b}, merged.toArray()); + assertSame(merged.get(0), acc1a); + // restore original values, modified by the merge + acc1a.setExpanded(false); + acc1a.setAmountsExpanded(false); + negTest(new LedgerAccount[]{acc1a}, new LedgerAccount[]{acc1b}, + new LedgerAccount[]{new LedgerAccount(null, "Acc1", null)}); + } +} \ No newline at end of file