From 0a309530f5e61642d0861ff3b6eeff12ba749a99 Mon Sep 17 00:00:00 2001 From: jinzequn Date: Tue, 24 Sep 2024 11:12:06 +0800 Subject: [PATCH] flatten module order error Fixes gh-1998 --- .../kotlin/org/koin/core/module/Module.kt | 2 +- .../commonTest/kotlin/org/koin/dsl/FlattenTest.kt | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt index 4bdcb3130..3143200c2 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt @@ -251,7 +251,7 @@ fun flatten(modules: List): Set { } // Add all the included modules to the stack if they haven't been visited yet. - for (module in current.includedModules) { + for (module in current.includedModules.asReversed()) { if (module !in flatten) { stack += module } diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/FlattenTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/FlattenTest.kt index ab419d870..26b42f5f0 100644 --- a/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/FlattenTest.kt +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/FlattenTest.kt @@ -2,6 +2,7 @@ package org.koin.dsl import org.koin.core.module.flatten import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class FlattenTest { @@ -12,17 +13,17 @@ class FlattenTest { val m2 = module { } val modules = m1 + m2 - assertTrue { flatten(modules) == setOf(m1, m2) } + assertSetEqualsInOrder(flatten(modules), setOf(m1, m2)) } @Test fun test_flatten_common() { val m1 = module { } val m2 = module { } - val m3 = module { includes(m1) } + val m3 = module { includes(m1, m2) } val modules = m3 + m2 - assertTrue { flatten(modules) == setOf(m3, m1, m2) } + assertSetEqualsInOrder(flatten(modules), setOf(m3, m1, m2)) } @Test @@ -32,7 +33,7 @@ class FlattenTest { val m3 = module { includes(m1) } val modules = m3 + m2 - assertTrue { flatten(modules) == setOf(m3, m1, m2) } + assertSetEqualsInOrder(flatten(modules), setOf(m3, m1, m2)) } @Test @@ -43,6 +44,10 @@ class FlattenTest { val m3 = module { includes(m1) } val modules = m3 + m2 - assertTrue { flatten(modules) == setOf(m3, m1, m4, m2) } + assertSetEqualsInOrder(flatten(modules), setOf(m3, m1, m4, m2)) + } + + private fun assertSetEqualsInOrder(actual: Set, expected: Set) { + assertEquals(expected.toList(), actual.toList()) } }