Skip to content

Commit

Permalink
Updated Kotlin and dependencies. Polished tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
EranBoudjnah committed Nov 9, 2023
1 parent a276ea9 commit db68018
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 71 deletions.
3 changes: 3 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin'

ext {
Version = [
mockito: '4.10.0'
mockito: '5.2.0'
]
}

Expand All @@ -18,7 +18,7 @@ dependencies {
testImplementation "org.mockito:mockito-inline:${Version.mockito}"
testImplementation 'org.mockito.kotlin:mockito-kotlin:4.1.0'

testImplementation 'io.mockk:mockk:1.13.3'
testImplementation 'io.mockk:mockk:1.13.8'

testImplementation 'org.assertj:assertj-core:3.23.1'
testImplementation 'org.hamcrest:hamcrest:2.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,87 @@ class MockableTypeQualifierDataTypeTest(
@JvmStatic
@Parameters(name = "Given {0}")
fun data(): Collection<Array<*>> = listOf(
arrayOf(DataType.Specific("Boolean", false), "Boolean", "false", false),
arrayOf(DataType.Specific("Byte", true), "Byte", "0b0", false),
arrayOf(DataType.Specific("Class", false), "Class", "Any::class.java", false),
arrayOf(DataType.Specific("Double", true), "Double", "0.0", false),
arrayOf(DataType.Specific("Float", false), "Float", "0f", false),
arrayOf(DataType.Specific("Int", true), "Int", "0", false),
arrayOf(DataType.Specific("Integer", false), "Integer", "0 as Integer", false),
arrayOf(DataType.Specific("Long", true), "Long", "0L", false),
arrayOf(DataType.Specific("Short", false), "Short", "0.toShort()", false),
arrayOf(DataType.Specific("String", true), "String", "\"Test\"", false),
arrayOf(DataType.Specific("Array", false), "Array", "arrayOf<Any>()", false),
arrayOf(DataType.Specific("List", true), "List", "listOf<Any>()", false),
arrayOf(DataType.Specific("MutableList", false), "MutableList", "mutableListOf<Any>()", false),
arrayOf(DataType.Specific("Map", true), "Map", "mapOf<Any>()", false),
arrayOf(DataType.Specific("MutableMap", false), "MutableMap", "mutableMapOf<Any>()", false),
arrayOf(DataType.Specific("Set", true), "Set", "setOf<Any>()", false),
arrayOf(DataType.Specific("MutableSet", false), "MutableSet", "mutableSetOf<Any>()", false),
arrayOf(DataType.Specific("Unit", true), "Unit", "Unit", false),
arrayOf(DataType.Specific("CustomDataType", false), null, null, true),
arrayOf(DataType.Lambda("Lambda", true, dataType("a"), dataType("b")), "(?)->?", "{ a, b -> }", true),
specificTestCase(name = "Boolean", defaultValue = "false"),
specificTestCase(name = "Byte", defaultValue = "0b0", isNullable = true),
specificTestCase(name = "Class", defaultValue = "Any::class.java"),
specificTestCase(name = "Double", defaultValue = "0.0", isNullable = true),
specificTestCase(name = "Float", defaultValue = "0f"),
specificTestCase(name = "Int", defaultValue = "0", isNullable = true),
specificTestCase(name = "Integer", defaultValue = "0 as Integer"),
specificTestCase(name = "Long", defaultValue = "0L", isNullable = true),
specificTestCase(name = "Short", defaultValue = "0.toShort()"),
specificTestCase(name = "String", defaultValue = "\"Test\"", isNullable = true),
specificTestCase(name = "Array", defaultValue = "arrayOf<Any>()"),
specificTestCase(name = "List", defaultValue = "listOf<Any>()", isNullable = true),
specificTestCase(
name = "MutableList",
defaultValue = "mutableListOf<Any>()",
isNullable = false,
isMockable = false
),
specificTestCase(
name = "Map",
defaultValue = "mapOf<Any>()",
isNullable = true
),
specificTestCase(
name = "MutableMap",
defaultValue = "mutableMapOf<Any>()"
),
specificTestCase(
name = "Set",
defaultValue = "setOf<Any>()",
isNullable = true
),
specificTestCase(
name = "MutableSet",
defaultValue = "mutableSetOf<Any>()"
),
specificTestCase(
name = "Unit",
defaultValue = "Unit",
isNullable = true
),
specificTestCase(
name = "CustomDataType",
defaultValue = null,
isNullable = false,
isMockable = true,
expectedType = null
),
arrayOf(
DataType.Lambda("Lambda", true, dataType("a"), dataType("b")),
"(?)->?",
"{ a, b -> }",
true
),
arrayOf(DataType.Generic("Manager", false, dataType("Employee")), null, null, true)
)

private fun specificTestCase(
name: String,
defaultValue: String?,
isNullable: Boolean = false,
isMockable: Boolean = false,
expectedType: String? = name
) = arrayOf(DataType.Specific(name, isNullable), expectedType, defaultValue, isMockable)

private fun dataType(name: String) = DataType.Specific(name, false)
}

private lateinit var cut: MockableTypeQualifier
private lateinit var classUnderTest: MockableTypeQualifier

@Before
fun setUp() {
cut = MockableTypeQualifier()
classUnderTest = MockableTypeQualifier()
}

@Test
fun `When getNonMockableType then returns expected type, default value`() {
// Given

// When
val actualValue = cut.getNonMockableType(givenDataType)
val actualValue = classUnderTest.getNonMockableType(givenDataType)
val actualDataType = actualValue?.dataType
val actualDefaultValue = actualValue?.defaultValue?.invoke("Test", givenDataType)

Expand All @@ -71,7 +113,7 @@ class MockableTypeQualifierDataTypeTest(
// Given

// When
val actualValue = cut.isMockable(givenDataType)
val actualValue = classUnderTest.isMockable(givenDataType)

// Then
assertEquals(isMockableExpected2, actualValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,105 @@ import org.junit.runners.Parameterized.Parameters

@RunWith(Parameterized::class)
class MockableTypeQualifierTypedParameterTest(
private val isMockableTypedParameter: TypedParameter,
private val isMockableExpected: Boolean
private val givenTypedParameter: TypedParameter,
private val expected: Boolean
) {
companion object {
@JvmStatic
@Parameters(name = "Given {0}")
fun data(): Collection<Array<*>> = listOf(
arrayOf(TypedParameter("Name", DataType.Specific("Boolean", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Byte", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Class", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Double", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Float", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Int", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Integer", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Long", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Short", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("String", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Array", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("List", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("MutableList", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Map", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("MutableMap", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Set", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("MutableSet", false)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Unit", true)), false),
arrayOf(TypedParameter("Name", DataType.Specific("Unknown", false)), true),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Boolean"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Byte",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Class"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Double",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Float"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Int",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Integer"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Long",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Short"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "String",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "Array"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "List",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "MutableList"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Map",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "MutableMap"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Set",
isNullable = true
),
expectedSpecificTestCase(parameterName = "Name", dataTypeName = "MutableSet"),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Unit",
isNullable = true
),
expectedSpecificTestCase(
parameterName = "Name",
dataTypeName = "Unknown",
expected = true
),
arrayOf(TypedParameter("Name", DataType.Lambda("Lambda", true)), true),
arrayOf(TypedParameter("Name", DataType.Generic("Generic", false)), true)
)

private fun expectedSpecificTestCase(
parameterName: String,
dataTypeName: String,
isNullable: Boolean = false,
expected: Boolean = false
) = arrayOf(
TypedParameter(
name = parameterName,
type = DataType.Specific(dataTypeName, isNullable)
),
expected
)
}

private lateinit var cut: MockableTypeQualifier
private lateinit var classUnderTest: MockableTypeQualifier

@Before
fun setUp() {
cut = MockableTypeQualifier()
classUnderTest = MockableTypeQualifier()
}

@Test
fun `When isMockable(TypedParameter) then return expected mockable state`() {
// Given

// When
val actualValue = cut.isMockable(isMockableTypedParameter)
val actualValue = classUnderTest.isMockable(givenTypedParameter)

// Then
assertEquals(isMockableExpected, actualValue)
assertEquals(expected, actualValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.junit.Test
import org.mockito.kotlin.mock

class MockerCodeGeneratorProviderTest {
private lateinit var cut: MockerCodeGeneratorProvider
private lateinit var classUnderTest: MockerCodeGeneratorProvider

@Before
fun setUp() {
cut = MockerCodeGeneratorProvider(mock())
classUnderTest = MockerCodeGeneratorProvider(mock())
}

@Test
Expand All @@ -22,7 +22,7 @@ class MockerCodeGeneratorProviderTest {
val mockitoRuleVariableName = "mockitoRule"

// When
val actualValue = cut.getGenerator(mocker, mock(), mockitoRuleVariableName)
val actualValue = classUnderTest.getGenerator(mocker, mock(), mockitoRuleVariableName)

// Then
assertTrue(actualValue is MockitoCodeGenerator)
Expand All @@ -35,7 +35,7 @@ class MockerCodeGeneratorProviderTest {
val mockitoRuleVariableName = "canBeAnything"

// When
val actualValue = cut.getGenerator(mocker, mock(), mockitoRuleVariableName)
val actualValue = classUnderTest.getGenerator(mocker, mock(), mockitoRuleVariableName)

// Then
assertTrue(actualValue is MockKCodeGenerator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class MockerCodeGeneratorTest {
@Test
fun `Given non-mockable when DataType#isMockable then returns false`() {
// Given
val receiver = mock<DataType>()
val receiver = mock<DataType.Specific>()
given { mockableTypeQualifier.isMockable(receiver) }.willReturn(false)

// When
Expand All @@ -139,7 +139,7 @@ class MockerCodeGeneratorTest {
@Test
fun `Given mockable when DataType#isMockable then returns true`() {
// Given
val receiver = mock<DataType>()
val receiver = mock<DataType.Specific>()
given { mockableTypeQualifier.isMockable(receiver) }.willReturn(true)

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import org.junit.Before
import org.junit.Test

class DataTypeParserExceptionTest {
private lateinit var cut: DataTypeParser
private lateinit var classUnderTest: DataTypeParser

@Before
fun setUp() {
cut = DataTypeParser()
classUnderTest = DataTypeParser()
}

@Test(expected = IllegalArgumentException::class)
Expand All @@ -17,7 +17,7 @@ class DataTypeParserExceptionTest {
val dataType = "<A"

// When
cut.parse(dataType)
classUnderTest.parse(dataType)

// Then
// Exception is thrown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ class DataTypeParserTest(
)
}

private lateinit var cut: DataTypeParser
private lateinit var classUnderTest: DataTypeParser

@Before
fun setUp() {
cut = DataTypeParser()
classUnderTest = DataTypeParser()
}

@Test
fun `Given parse data and root null flag when parse then returns expected value`() {
// When
val actualValue = cut.parse(parseDataType)
val actualValue = classUnderTest.parse(parseDataType)

// Then
assertEquals(parseExpected, actualValue)
Expand Down
Loading

0 comments on commit db68018

Please sign in to comment.