From 427bb4c2ab0ce5cfb988789657c2de3761fec392 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 15 Jul 2024 00:46:18 +0200 Subject: Add Utils for parsing path parts to a directory --- .../java/org/the_jk/cleversync/StringUtilsTest.kt | 40 +++++++++++++++++ .../java/org/the_jk/cleversync/io/UtilsTest.kt | 51 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt create mode 100644 app/src/test/java/org/the_jk/cleversync/io/UtilsTest.kt (limited to 'app/src/test/java/org') diff --git a/app/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt b/app/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt new file mode 100644 index 0000000..6a36156 --- /dev/null +++ b/app/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt @@ -0,0 +1,40 @@ +package org.the_jk.cleversync + +import com.google.common.truth.Truth.assertThat +import org.junit.Test + +class StringUtilsTest { + @Test + fun splitEmpty() { + assertThat(StringUtils.split("", '.', keepEmpty = true)).containsExactly("") + assertThat(StringUtils.split("", '.', keepEmpty = false)).isEmpty() + } + + @Test + fun splitSanity() { + assertThat(StringUtils.split("a.bb.a", '.')).containsExactly("a", "bb", "a").inOrder() + assertThat(StringUtils.split(".a.bb.a", '.', keepEmpty = true)).containsExactly("", "a", "bb", "a").inOrder() + assertThat(StringUtils.split(".a.bb.a", '.', keepEmpty = false)).containsExactly("a", "bb", "a").inOrder() + assertThat(StringUtils.split(".a.bb.a.", '.', keepEmpty = true)) + .containsExactly("", "a", "bb", "a", "").inOrder() + assertThat(StringUtils.split(".a.bb.a.", '.', keepEmpty = false)).containsExactly("a", "bb", "a").inOrder() + } + + @Test + fun splitDouble() { + assertThat(StringUtils.split("foo..bar", '.', keepEmpty = true)).containsExactly("foo", "", "bar").inOrder() + assertThat(StringUtils.split("foo..bar", '.', keepEmpty = false)).containsExactly("foo", "bar").inOrder() + } + + @Test + fun splitLimit() { + assertThat(StringUtils.split("a.bb.a", '.', limit = 1)).containsExactly("a.bb.a") + assertThat(StringUtils.split("a.bb.a", '.', limit = 2)).containsExactly("a", "bb.a").inOrder() + assertThat(StringUtils.split("a.bb.a", '.', limit = 3)).containsExactly("a", "bb", "a").inOrder() + assertThat(StringUtils.split("a.bb.a.", '.', limit = 3, keepEmpty = true)) + .containsExactly("a", "bb", "a.").inOrder() + assertThat(StringUtils.split("a.bb.a.", '.', limit = 3, keepEmpty = false)) + .containsExactly("a", "bb", "a").inOrder() + assertThat(StringUtils.split("a.bb.a", '.', limit = 1000)).containsExactly("a", "bb", "a").inOrder() + } +} diff --git a/app/src/test/java/org/the_jk/cleversync/io/UtilsTest.kt b/app/src/test/java/org/the_jk/cleversync/io/UtilsTest.kt new file mode 100644 index 0000000..56c4051 --- /dev/null +++ b/app/src/test/java/org/the_jk/cleversync/io/UtilsTest.kt @@ -0,0 +1,51 @@ +package org.the_jk.cleversync.io + +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@Config(manifest= Config.NONE) +@RunWith(RobolectricTestRunner::class) +class UtilsTest { + @get:Rule + val folder = TemporaryFolder() + + private lateinit var tree: ModifiableTree + + @Before + fun setUp() { + tree = TreeFactory.localModifiableTree(folder.root.toPath()) + } + + @Test + fun makeDirectories() { + assertThat(Utils.makeDirectories(tree)).isEqualTo(tree) + val foo = Utils.makeDirectories(tree, "foo") + val foobar = Utils.makeDirectories(tree, "foo", "bar") + val foofum = Utils.makeDirectories(tree, "foo/fum") + assertThat(foo.name).isEqualTo("foo") + assertThat(tree.list().directories).containsExactly(foo) + assertThat(foobar.name).isEqualTo("bar") + assertThat(foofum.name).isEqualTo("fum") + assertThat(foo.list().directories).containsExactly(foobar, foofum) + } + + @Test + fun openDirectories() { + assertThat(Utils.openDirectory(tree)).isEqualTo(tree) + assertThat(Utils.openDirectory(tree, "foo")).isNull() + assertThat(Utils.openDirectory(tree, "foo", "bar")).isNull() + assertThat(Utils.openDirectory(tree, "foo/fum")).isNull() + val foo = tree.createDirectory("foo") + val foobar = foo.createDirectory("bar") + val foofum = foo.createDirectory("fum") + assertThat(Utils.openDirectory(tree, "foo")).isEqualTo(foo) + assertThat(Utils.openDirectory(tree, "foo", "bar")).isEqualTo(foobar) + assertThat(Utils.openDirectory(tree, "foo/fum")).isEqualTo(foofum) + } +} -- cgit v1.2.3-70-g09d2