1
0
Fork 0

For #4556: Removed redundant code in few tests. (#6367)

* Moved some values as member variables as having them declared separately in methods seemed redundant.

* Added @Before method in logtest to avoid code duplication Fixes #4556

* Added @Before method to FragmentTest class Fixes #4556

* variable renamed and added private access specifier; removed set from member variable as it is used only once and we can convert the list to set when needed

* Fixes linter errors
master
Shangeeth Sivan 2019-11-08 02:50:51 +05:30 committed by Sawyer Blatz
parent 284f3cf0f5
commit 218763f9be
3 changed files with 27 additions and 48 deletions

View File

@ -17,36 +17,31 @@ import mozilla.components.concept.storage.BookmarkNodeType
@Config(application = TestApplication::class)
class BookmarkNodeTest {
private val bookmarkChild1 = newBookmarkNode("Child 1", 1, null)
private val bookmarkChild2 = newBookmarkNode("Child 2", 2, null)
private val bookmarkChild3 = newBookmarkNode("Child 3", 3, null)
private val allChildren = listOf(bookmarkChild1, bookmarkChild2)
@Test
fun `GIVEN a bookmark node with children WHEN minusing a sub set of children THEN the children subset is removed and rest remains`() {
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val allChildren = listOf(bookMarkChild1, bookMarkChild2)
val bookmarkNode = newBookmarkNode("Parent 1", 0, allChildren)
val subsetToSubtract = setOf(bookMarkChild1)
val expectedRemainingSubset = listOf(bookMarkChild2)
val subsetToSubtract = setOf(bookmarkChild1)
val expectedRemainingSubset = listOf(bookmarkChild2)
val bookmarkNodeSubsetRemoved = bookmarkNode.minus(subsetToSubtract)
assertEquals(expectedRemainingSubset, bookmarkNodeSubsetRemoved.children)
}
@Test
fun `GIVEN a bookmark node with children WHEN minusing a set of all children THEN all children are removed and empty list remains`() {
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val allChildren = listOf(bookMarkChild1, bookMarkChild2)
val bookmarkNode = newBookmarkNode("Parent 1", 0, allChildren)
val setofAllChildren = setOf(bookMarkChild1, bookMarkChild2)
val setofAllChildren = setOf(bookmarkChild1, bookmarkChild2)
val bookmarkNodeAllChildrenRemoved = bookmarkNode.minus(setofAllChildren)
assertEquals(emptyList<BookmarkNode>(), bookmarkNodeAllChildrenRemoved.children)
}
@Test
fun `GIVEN a bookmark node with children WHEN minusing a set of non-children THEN no children are removed`() {
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val allChildren = listOf(bookMarkChild1, bookMarkChild2)
val bookMarkChild3 = newBookmarkNode("Child 3", 3, null)
val setofNonChildren = setOf(bookMarkChild3)
val setofNonChildren = setOf(bookmarkChild3)
val bookmarkNode = newBookmarkNode("Parent 1", 0, allChildren)
val bookmarkNodeNonChildrenRemoved = bookmarkNode.minus(setofNonChildren)
assertEquals(allChildren, bookmarkNodeNonChildrenRemoved.children)
@ -54,9 +49,6 @@ class BookmarkNodeTest {
@Test
fun `GIVEN a bookmark node with children WHEN minusing an empty set THEN no children are removed`() {
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val allChildren = listOf(bookMarkChild1, bookMarkChild2)
val bookmarkNode = newBookmarkNode("Parent 1", 0, allChildren)
val bookmarkNodeEmptySetRemoved = bookmarkNode.minus(emptySet())
assertEquals(allChildren, bookmarkNodeEmptySetRemoved.children)
@ -65,9 +57,7 @@ class BookmarkNodeTest {
@Test
fun `GIVEN a bookmark node with an empty list as children WHEN minusing a set of non-children from an empty parent THEN an empty list remains`() {
val parentWithEmptyList = newBookmarkNode("Parent 1", 0, emptyList<BookmarkNode>())
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val setofAllChildren = setOf(bookMarkChild1, bookMarkChild2)
val setofAllChildren = setOf(bookmarkChild1, bookmarkChild2)
val parentWithEmptyListNonChildRemoved = parentWithEmptyList.minus(setofAllChildren)
assertEquals(emptyList<BookmarkNode>(), parentWithEmptyListNonChildRemoved.children)
}
@ -75,21 +65,15 @@ class BookmarkNodeTest {
@Test
fun `GIVEN a bookmark node with null as children WHEN minusing a set of non-children from a parent with null children THEN null remains`() {
val parentWithNullList = newBookmarkNode("Parent 1", 0, null)
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val setofAllChildren = setOf(bookMarkChild1, bookMarkChild2)
val parentWithNullListNonChildRemoved = parentWithNullList.minus(setofAllChildren)
val parentWithNullListNonChildRemoved = parentWithNullList.minus(allChildren.toSet())
assertEquals(null, parentWithNullListNonChildRemoved.children)
}
@Test
fun `GIVEN a bookmark node with children WHEN minusing a sub-set of children THEN the rest of the parents object should remain the same`() {
val bookMarkChild1 = newBookmarkNode("Child 1", 1, null)
val bookMarkChild2 = newBookmarkNode("Child 2", 2, null)
val allChildren = listOf(bookMarkChild1, bookMarkChild2)
val bookmarkNode = newBookmarkNode("Parent 1", 0, allChildren)
val subsetToSubtract = setOf(bookMarkChild1)
val expectedRemainingSubset = listOf(bookMarkChild2)
val subsetToSubtract = setOf(bookmarkChild1)
val expectedRemainingSubset = listOf(bookmarkChild2)
val resultBookmarkNode = bookmarkNode.minus(subsetToSubtract)
// We're pinning children to the same value so we can compare the rest.

View File

@ -22,6 +22,7 @@ import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.NavDestination
import androidx.navigation.NavController
import androidx.navigation.Navigator.Extras
import org.junit.Before
@RunWith(RobolectricTestRunner::class)
@Config(application = TestApplication::class)
@ -36,15 +37,19 @@ class FragmentTest {
val mockFragment: Fragment = mockk(relaxed = true)
val mockOptions: NavOptions = mockk(relaxed = true)
@Test
fun `Test nav fun with ID and directions`() {
@Before
fun setup() {
mockkStatic(NavHostFragment::class)
every { (NavHostFragment.findNavController(mockFragment)) } returns navController
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections)) } just Runs
every { (mockDestination.getId()) } returns mockId
every { (navController.getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) }
}
@Test
fun `Test nav fun with ID and directions`() {
every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections)) } just Runs
mockFragment.nav(mockId, navDirections)
verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) }
@ -54,13 +59,7 @@ class FragmentTest {
@Test
fun `Test nav fun with ID, directions, and extras`() {
mockkStatic(NavHostFragment::class)
every { (NavHostFragment.findNavController(mockFragment)) } returns navController
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockExtras)) } just Runs
every { (mockDestination.getId()) } returns mockId
every { (navController.getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) }
mockFragment.nav(mockId, navDirections, mockExtras)
verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) }
@ -70,13 +69,7 @@ class FragmentTest {
@Test
fun `Test nav fun with ID, directions, and options`() {
mockkStatic(NavHostFragment::class)
every { (NavHostFragment.findNavController(mockFragment)) } returns navController
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).navigate(navDirections, mockOptions)) } just Runs
every { (mockDestination.getId()) } returns mockId
every { (navController.getCurrentDestination()) } returns mockDestination
every { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()?.getId()) } answers { (mockDestination.getId()) }
mockFragment.nav(mockId, navDirections, mockOptions)
verify { (NavHostFragment.findNavController(mockFragment).getCurrentDestination()) }

View File

@ -9,6 +9,7 @@ import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.verify
import android.util.Log
import org.junit.Before
import org.mozilla.fenix.BuildConfig
@RunWith(RobolectricTestRunner::class)
@ -18,23 +19,25 @@ class LogTest {
val numCalls = if (BuildConfig.DEBUG) 1 else 0
@Before
fun setup() {
mockkStatic(Log::class)
}
@Test
fun `Test log debug function`() {
mockkStatic(Log::class)
logDebug("hi", "hi")
verify(exactly = numCalls) { (Log.d("hi", "hi")) }
}
@Test
fun `Test log warn function with tag and message args`() {
mockkStatic(Log::class)
logWarn("hi", "hi")
verify(exactly = numCalls) { (Log.w("hi", "hi")) }
}
@Test
fun `Test log warn function with tag, message, and exception args`() {
mockkStatic(Log::class)
val mockThrowable: Throwable = mockk(relaxed = true)
logWarn("hi", "hi", mockThrowable)
verify(exactly = numCalls) { (Log.w("hi", "hi", mockThrowable)) }
@ -42,7 +45,6 @@ class LogTest {
@Test
fun `Test log error function with tag, message, and exception args`() {
mockkStatic(Log::class)
val mockThrowable: Throwable = mockk(relaxed = true)
logErr("hi", "hi", mockThrowable)
verify(exactly = numCalls) { (Log.e("hi", "hi", mockThrowable)) }