From d79927bd7e1c0ff1c131662bb154489bbc3666ff Mon Sep 17 00:00:00 2001 From: petershatunov Date: Thu, 2 Jan 2020 01:20:44 +0300 Subject: [PATCH] Kotlin Ternary Conditional Operator (#8427) * Kotlin Ternary Conditional Operator * moved to package Co-authored-by: Sam Millington --- core-kotlin-2/README.md | 6 +++++ .../baeldung/ternary/TernaryOperatorTest.kt | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/ternary/TernaryOperatorTest.kt diff --git a/core-kotlin-2/README.md b/core-kotlin-2/README.md index 544c0b57ec..5249262fa3 100644 --- a/core-kotlin-2/README.md +++ b/core-kotlin-2/README.md @@ -5,4 +5,10 @@ This module contains articles about core Kotlin. ### Relevant articles: - [Kotlin Scope Functions](https://www.baeldung.com/kotlin-scope-functions) +- [Kotlin Annotations](https://www.baeldung.com/kotlin-annotations) +- [Split a List into Parts in Kotlin](https://www.baeldung.com/kotlin-split-list-into-parts) +- [String Comparison in Kotlin](https://www.baeldung.com/kotlin-string-comparison) +- [Guide to JVM Platform Annotations in Kotlin](https://www.baeldung.com/kotlin-jvm-annotations) +- [Finding an Element in a List Using Kotlin](https://www.baeldung.com/kotlin-finding-element-in-list) +- [Kotlin Ternary Conditional Operator](https://www.baeldung.com/kotlin-ternary-conditional-operator) - More articles: [[<-- prev]](/core-kotlin) diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/ternary/TernaryOperatorTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/ternary/TernaryOperatorTest.kt new file mode 100644 index 0000000000..21dfdd2ae0 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/ternary/TernaryOperatorTest.kt @@ -0,0 +1,24 @@ +package com.baeldung.ternary + +import org.junit.Test +import kotlin.test.assertEquals + +class TernaryOperatorTest { + + @Test + fun `using If`() { + val a = true + val result = if (a) "yes" else "no" + assertEquals("yes", result) + } + + @Test + fun `using When`() { + val a = true + val result = when(a) { + true -> "yes" + false -> "no" + } + assertEquals("yes", result) + } +} \ No newline at end of file