-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds WorkflowIdentifierTypeNamer to differentiate identifier logic ba…
…sed on platform
- Loading branch information
Showing
11 changed files
with
223 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
workflow-core/src/iosMain/kotlin/com.squareup.workflow1/WorkflowIdentifierTypeNamer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.squareup.workflow1 | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal actual object WorkflowIdentifierTypeNamer { | ||
public actual fun uniqueName(kClass: KClass<*>): String { | ||
return kClass.qualifiedName ?: kClass.toString() | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
workflow-core/src/iosTest/kotlin/com/squareup/workflow1/NativeWorkflowIdentifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
workflow-core/src/jsMain/kotlin/com.squareup.workflow1/WorkflowIdentifierTypeNamer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.squareup.workflow1 | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal actual object WorkflowIdentifierTypeNamer { | ||
// Stores mappings between KClass instances and their assigned names. | ||
val mappings = mutableMapOf<KClass<*>, String>() | ||
|
||
// Note: This implementation does not differentiate between generic workflows. | ||
// (ie. SomeGenericWorkflow<String> and SomeGenericWorkflow<Int> would both return back the same | ||
// value.) | ||
// | ||
// Recommended workarounds: | ||
// - Always provide a key for generic workflows | ||
// - Create non-generic subclasses of generic workflows | ||
public actual fun uniqueName(kClass: KClass<*>): String { | ||
// Note: `kClass.qualifiedName` cannot be used here like other platforms as it's not supported | ||
// for JS. Therefore, we construct a unique name of each static KClass based on its simple name | ||
// and an index of when it was encountered. | ||
|
||
val mapping = mappings[kClass] | ||
if (mapping != null) { | ||
return mapping | ||
} | ||
|
||
// `simpleName` does not differentiate between generic workflows. | ||
val identifier = "${kClass.simpleName ?: kClass.hashCode()}(${mappings.size})" | ||
mappings[kClass] = identifier | ||
return identifier | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
workflow-core/src/jsTest/kotlin/com.squareup.workflow1/JsWorkflowIdentifierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.squareup.workflow1 | ||
|
||
import com.squareup.workflow1.WorkflowIdentifierTest.TestImpostor1 | ||
import com.squareup.workflow1.WorkflowIdentifierTest.TestWorkflow1 | ||
import com.squareup.workflow1.mocks.workflows1.JsMockWorkflow1 | ||
import com.squareup.workflow1.mocks.workflows1.JsMockWorkflow2 | ||
import kotlin.js.RegExp | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertTrue | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
internal class JsWorkflowIdentifierTest { | ||
@Test fun flat_identifier_toString() { | ||
val id = JsMockWorkflow1().identifier | ||
|
||
// Due to the dynamic naming of workflow identifiers (based on other identifiers that have been | ||
// created so far), we can only verify the composition of the name. | ||
// Expected value should be something like this: "WorkflowIdentifier(JsMockWorkflow1(7))" | ||
val idStructure = RegExp("WorkflowIdentifier\\(JsMockWorkflow1\\((\\d)+\\)\\)") | ||
assertTrue(idStructure.test(id.toString())) | ||
} | ||
|
||
@Test | ||
fun impostor_identifier_toString_uses_full_chain_when_describeRealIdentifier_returns_null() { | ||
class TestImpostor : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow { | ||
override val realIdentifier: WorkflowIdentifier = TestWorkflow1.identifier | ||
override fun describeRealIdentifier(): String? = null | ||
|
||
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> = | ||
throw NotImplementedError() | ||
} | ||
|
||
val id = TestImpostor().identifier | ||
|
||
// Expected value should be something like this: | ||
// "WorkflowIdentifier(TestImpostor(3), TestWorkflow1(1))" | ||
val idStructure = RegExp( | ||
"WorkflowIdentifier\\(TestImpostor\\((\\d)+\\), TestWorkflow1\\((\\d)+\\)\\)" | ||
) | ||
assertTrue(idStructure.test(id.toString())) | ||
} | ||
|
||
@Test fun impostor_identifier_description() { | ||
// Expected value should be something like this: "WorkflowIdentifier(TestWorkflow1(1))" | ||
val id = TestImpostor1(TestWorkflow1).identifier | ||
val idStructure = RegExp("TestImpostor1\\(TestWorkflow1\\((\\d)+\\)\\)") | ||
assertTrue(idStructure.test(id.toString())) | ||
} | ||
|
||
@Test fun same_workflow_returns_same_identifier() { | ||
val id1 = JsMockWorkflow1().identifier | ||
val id2 = JsMockWorkflow1().identifier | ||
assertEquals(id1.toString(), id2.toString()) | ||
} | ||
|
||
@Test fun different_workflows_same_namespace() { | ||
val id1 = JsMockWorkflow1().identifier | ||
val id2 = JsMockWorkflow2().identifier | ||
assertNotEquals(id1.toString(), id2.toString()) | ||
} | ||
|
||
@Test fun same_workflow_name_different_namespace() { | ||
val id1 = JsMockWorkflow1().identifier | ||
val id2 = com.squareup.workflow1.mocks.workflows2.JsMockWorkflow1().identifier | ||
assertNotEquals(id1.toString(), id2.toString()) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
workflow-core/src/jsTest/kotlin/com.squareup.workflow1/mocks.workflows1/JsMockWorkflows.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.squareup.workflow1.mocks.workflows1 | ||
|
||
import com.squareup.workflow1.StatefulWorkflow | ||
import com.squareup.workflow1.Workflow | ||
|
||
public class JsMockWorkflow1 : Workflow<Nothing, Nothing, Nothing> { | ||
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> = | ||
throw NotImplementedError() | ||
} | ||
|
||
public class JsMockWorkflow2 : Workflow<Nothing, Nothing, Nothing> { | ||
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> = | ||
throw NotImplementedError() | ||
} |
11 changes: 11 additions & 0 deletions
11
workflow-core/src/jsTest/kotlin/com.squareup.workflow1/mocks.workflows2/JsMockWorkflow1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.squareup.workflow1.mocks.workflows2 | ||
|
||
import com.squareup.workflow1.StatefulWorkflow | ||
import com.squareup.workflow1.Workflow | ||
|
||
// This is purposely duplicated to be like com.squareup.workflow1.mocks.workflows1.JsMockWorkflow1, | ||
// but with a slightly different Output. | ||
public class JsMockWorkflow1 : Workflow<Nothing, String, Nothing> { | ||
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, String, Nothing> = | ||
throw NotImplementedError() | ||
} |
9 changes: 9 additions & 0 deletions
9
workflow-core/src/jvmMain/kotlin/com/squareup/workflow1/WorkflowIdentifierTypeNamer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.squareup.workflow1 | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal actual object WorkflowIdentifierTypeNamer { | ||
public actual fun uniqueName(kClass: KClass<*>): String { | ||
return kClass.qualifiedName ?: kClass.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters