]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java
rework account list management to be fully asynchronous
[mobile-ledger.git] / app / src / test / java / net / ktnx / mobileledger / model / MobileLedgerProfileTest.java
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 (file)
index 0000000..8416c10
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+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<LedgerAccount> listFromArray(LedgerAccount[] array) {
+        ArrayList<LedgerAccount> result = new ArrayList<>();
+        Collections.addAll(result, array);
+
+        return result;
+    }
+    private void aTest(LedgerAccount[] oldList, LedgerAccount[] newList,
+                       LedgerAccount[] expectedResult) {
+        List<LedgerAccount> result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList),
+                listFromArray(newList));
+        assertArrayEquals(expectedResult, result.toArray());
+    }
+    private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList,
+                         LedgerAccount[] expectedResult) {
+        List<LedgerAccount> 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<LedgerAccount> 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