Skip to content

Commit

Permalink
[Oztechan/CCC#3490] Update ScopeMob Higher-order functions and lambda…
Browse files Browse the repository at this point in the history
…s to use lambdas
  • Loading branch information
mustafaozhan committed May 30, 2024
1 parent c288ed1 commit 1113a43
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.github.submob.scopemob

inline fun <reified T> T.either(
vararg method: T.(condition: T) -> Boolean
vararg method: (condition: T) -> Boolean
): T? = if (this != null) {
if (method.any { it(this) }) {
this
Expand All @@ -16,7 +16,7 @@ inline fun <reified T> T.either(
}

inline fun <reified T> T.eitherNot(
vararg method: T.(condition: T) -> Boolean
vararg method: (condition: T) -> Boolean
): T? = if (this != null) {
if (!method.any { it(this) }) {
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package com.github.submob.scopemob

inline fun <reified T> T.sameAs(
method: T.(condition: T) -> T
method: (condition: T) -> T
): T? = if (this == method(this)) {
this
} else {
null
}

inline fun <reified T> T.notSameAs(
method: T.(condition: T) -> T
method: (condition: T) -> T
): T? = if (this != method(this)) {
this
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.github.submob.scopemob

inline fun <reified T, reified R> T.mapTo(
transform: T.(map: T) -> R
transform: (map: T) -> R
): R = transform(this)

inline fun <reified T> Any.castTo(): T? = this as? T
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package com.github.submob.scopemob

inline fun <reified T> T.whether(
method: T.(condition: T) -> Boolean
method: (condition: T) -> Boolean
): T? = if (this != null && method(this)) {
this
} else {
null
}

inline fun <reified T> T.whether(
vararg method: T.(condition: T) -> Boolean
vararg method: (condition: T) -> Boolean
): T? = if (this != null) {
if (method.all { it(this) }) {
this
Expand All @@ -24,15 +24,15 @@ inline fun <reified T> T.whether(
}

inline fun <reified T> T.whetherNot(
method: T.(condition: T) -> Boolean
method: (condition: T) -> Boolean
): T? = if (this != null && !method(this)) {
this
} else {
null
}

inline fun <reified T> T.whetherNot(
vararg method: T.(condition: T) -> Boolean
vararg method: (condition: T) -> Boolean
): T? = if (this != null) {
if (!method.all { it(this) }) {
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package com.github.submob.scopemob

inline fun <reified T> T.inCase(
condition: Boolean?,
method: T.(T) -> Unit
method: (T) -> Unit
): T {
if (condition == true) {
method(this)
Expand All @@ -17,7 +17,7 @@ inline fun <reified T> T.inCase(

inline fun <reified T> T.inCaseNot(
condition: Boolean?,
method: T.(T) -> Unit
method: (T) -> Unit
): T {
if (condition == false) {
method(this)
Expand All @@ -26,8 +26,8 @@ inline fun <reified T> T.inCaseNot(
}

inline fun <reified T> T.inCase(
vararg condition: T.(T) -> Boolean?,
method: T.(T) -> Unit
vararg condition: (T) -> Boolean?,
method: (T) -> Unit
): T {
if (condition.all { it(this) == true }) {
method(this)
Expand All @@ -36,8 +36,8 @@ inline fun <reified T> T.inCase(
}

inline fun <reified T> T.inCaseNot(
vararg condition: T.(T) -> Boolean?,
method: T.(T) -> Unit
vararg condition: (T) -> Boolean?,
method: (T) -> Unit
): T {
if (condition.all { it(this) == false }) {
method(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherMixMix() {
subjectFunction
?.either({ it.trueCondition }, { it.falseCondition })
?.either({ trueCondition }, { falseCondition })
?.either({ it.trueCondition }, { it.falseCondition })
?.let { passTest() }
?: run { failTest() }

subjectFunction
?.either({ it.falseCondition }, { it.trueCondition })
?.either({ falseCondition }, { trueCondition })
?.either({ it.falseCondition }, { it.trueCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand All @@ -29,13 +29,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherMixTrue() {
subjectFunction
?.either({ it.trueCondition }, { it.falseCondition })
?.either({ trueCondition }, { trueCondition })
?.either({ it.trueCondition }, { it.trueCondition })
?.let { passTest() }
?: run { failTest() }

subjectFunction
?.either({ it.falseCondition }, { it.trueCondition })
?.either({ trueCondition }, { trueCondition })
?.either({ it.trueCondition }, { it.trueCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand All @@ -44,13 +44,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherMixFalse() {
subjectFunction
?.either({ it.trueCondition }, { it.falseCondition })
?.either({ falseCondition }, { falseCondition })
?.either({ it.falseCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }

subjectFunction
?.either({ it.falseCondition }, { it.trueCondition })
?.either({ falseCondition }, { falseCondition })
?.either({ it.falseCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -59,13 +59,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherNotMix() {
subjectFunction
?.eitherNot({ it.trueCondition }, { it.falseCondition })
?.eitherNot({ trueCondition }, { falseCondition })
?.eitherNot({ it.trueCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }

subjectFunction
?.eitherNot({ it.falseCondition }, { it.trueCondition })
?.eitherNot({ falseCondition }, { trueCondition })
?.eitherNot({ it.falseCondition }, { it.trueCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -74,13 +74,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherNotMixTrue() {
subjectFunction
?.eitherNot({ it.trueCondition }, { it.falseCondition })
?.eitherNot({ trueCondition }, { trueCondition })
?.eitherNot({ it.trueCondition }, { it.trueCondition })
?.let { failTest() }
?: run { passTest() }

subjectFunction
?.eitherNot({ it.falseCondition }, { it.trueCondition })
?.eitherNot({ trueCondition }, { trueCondition })
?.eitherNot({ it.trueCondition }, { it.trueCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -89,13 +89,13 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherNotMixFalse() {
subjectFunction
?.eitherNot({ it.trueCondition }, { it.falseCondition })
?.eitherNot({ falseCondition }, { falseCondition })
?.eitherNot({ it.falseCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }

subjectFunction
?.eitherNot({ it.falseCondition }, { it.trueCondition })
?.eitherNot({ falseCondition }, { falseCondition })
?.eitherNot({ it.falseCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -104,7 +104,7 @@ internal class EitherScopeTest : MainScopeTest() {
fun eitherNotFalseFalse() {
subjectFunction
?.eitherNot({ it.falseCondition }, { it.falseCondition })
?.eitherNot({ falseCondition }, { falseCondition })
?.eitherNot({ it.falseCondition }, { it.falseCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class InCaseScopeTest : MainScopeTest() {
}

subjectFunction
?.whether { trueCondition }
?.whether { it.trueCondition }
?.inCase(true) { passTest() }
?.inCase(false) { failTest() }
?.inCase(true) { passTest() }
Expand All @@ -34,7 +34,7 @@ internal class InCaseScopeTest : MainScopeTest() {
}

subjectFunction
?.whether { trueCondition }
?.whether { it.trueCondition }
?.inCaseNot(false) { passTest() }
?.inCaseNot(true) { failTest() }
?.inCaseNot(false) { passTest() }
Expand All @@ -45,31 +45,31 @@ internal class InCaseScopeTest : MainScopeTest() {
@Test
fun inCaseScope() {
subjectFunction
?.inCase({ falseCondition }) {
?.inCase({ it.falseCondition }) {
failTest()
}

subjectFunction
?.whether { trueCondition }
?.inCase({ trueCondition }) { passTest() }
?.inCase({ falseCondition }) { failTest() }
?.inCase({ trueCondition }) { passTest() }
?.whether { it.trueCondition }
?.inCase({ it.trueCondition }) { passTest() }
?.inCase({ it.falseCondition }) { failTest() }
?.inCase({ it.trueCondition }) { passTest() }
?.let { passTest() }
?: run { failTest() }
}

@Test
fun inCaseNotScope() {
subjectFunction
?.inCaseNot({ trueCondition }) {
?.inCaseNot({ it.trueCondition }) {
failTest()
}

subjectFunction
?.whether { trueCondition }
?.inCaseNot({ falseCondition }) { passTest() }
?.inCaseNot({ trueCondition }) { failTest() }
?.inCaseNot({ falseCondition }) { passTest() }
?.whether { it.trueCondition }
?.inCaseNot({ it.falseCondition }) { passTest() }
?.inCaseNot({ it.trueCondition }) { failTest() }
?.inCaseNot({ it.falseCondition }) { passTest() }
?.let { passTest() }
?: run { failTest() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ internal class TransformScope : MainScopeTest() {
?.let { failTest() }
?: run { passTest() }
subjectFunction
?.mapTo { trueCondition }
?.mapTo { it.trueCondition }
?.whether { it }
?.let { passTest() }
?: run { failTest() }

subjectFunction
?.mapTo { falseCondition }
?.mapTo { it.falseCondition }
?.whether { it }
?.let { failTest() }
?: run { passTest() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherTrue() {
subjectFunction
?.whether { it.trueCondition }
?.whether { trueCondition }
?.whether { it.trueCondition }
?.let { passTest() }
?: run { failTest() }

// vararg
subjectFunction
?.whether({ it.trueCondition }, { trueCondition })
?.whether({ it.trueCondition }, { it.trueCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand All @@ -29,13 +29,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherFalse() {
subjectFunction
?.whether { it.falseCondition }
?.whether { falseCondition }
?.whether { it.falseCondition }
?.let { failTest() }
?: run { passTest() }

// vararg
subjectFunction
?.whether({ it.falseCondition }, { falseCondition })
?.whether({ it.falseCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -44,13 +44,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherMix() {
subjectFunction
?.whether { it.trueCondition }
?.whether { falseCondition }
?.whether { it.falseCondition }
?.let { failTest() }
?: run { passTest() }

// vararg
subjectFunction
?.whether({ it.trueCondition }, { falseCondition })
?.whether({ it.trueCondition }, { it.falseCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -59,13 +59,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherNotFalse() {
subjectFunction
?.whetherNot { it.falseCondition }
?.whetherNot { falseCondition }
?.whetherNot { it.falseCondition }
?.let { passTest() }
?: run { failTest() }

// vararg
subjectFunction
?.whetherNot({ it.falseCondition }, { falseCondition })
?.whetherNot({ it.falseCondition }, { it.falseCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand All @@ -74,13 +74,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherNotTrue() {
subjectFunction
?.whetherNot { it.trueCondition }
?.whetherNot { trueCondition }
?.whetherNot { it.trueCondition }
?.let { failTest() }
?: run { passTest() }

// vararg
subjectFunction
?.whetherNot({ it.trueCondition }, { trueCondition })
?.whetherNot({ it.trueCondition }, { it.trueCondition })
?.let { failTest() }
?: run { passTest() }
}
Expand All @@ -89,13 +89,13 @@ internal class WhetherScopeTest : MainScopeTest() {
fun whetherNotMix() {
subjectFunction
?.whetherNot { it.falseCondition }
?.whetherNot { trueCondition }
?.whetherNot { it.trueCondition }
?.let { failTest() }
?: run { passTest() }

// vararg
subjectFunction
?.whetherNot({ it.falseCondition }, { trueCondition })
?.whetherNot({ it.falseCondition }, { it.trueCondition })
?.let { passTest() }
?: run { failTest() }
}
Expand Down
Loading

0 comments on commit 1113a43

Please sign in to comment.