From 1b454b7267f3d6c246e007cb39442e444f470aff Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Fri, 6 Apr 2018 12:08:33 +0100 Subject: [PATCH] Examples for BAEL-1169 (#3934) --- .../com/baeldung/kotlin/objects/Counter.kt | 15 ++++++++ .../baeldung/kotlin/objects/ObjectsTest.kt | 36 +++++++++++++++++++ .../com/baeldung/kotlin/objects/OuterClass.kt | 10 ++++++ .../kotlin/objects/ReverseStringComparator.kt | 5 +++ .../kotlin/objects/SimpleSingleton.kt | 7 ++++ .../baeldung/kotlin/objects/StaticClass.kt | 8 +++++ 6 files changed, 81 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/Counter.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ObjectsTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/OuterClass.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ReverseStringComparator.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/SimpleSingleton.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/StaticClass.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/Counter.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/Counter.kt new file mode 100644 index 0000000000..46ba42e1e5 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/Counter.kt @@ -0,0 +1,15 @@ +package com.baeldung.kotlin.objects + +object Counter { + private var count: Int = 0 + + fun currentCount() = count + + fun increment() { + ++count + } + + fun decrement() { + --count + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ObjectsTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ObjectsTest.kt new file mode 100644 index 0000000000..0bbb1c741d --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ObjectsTest.kt @@ -0,0 +1,36 @@ +package com.baeldung.kotlin.objects + +import org.junit.Assert +import org.junit.Test + +class ObjectsTest { + @Test + fun singleton() { + + Assert.assertEquals(42, SimpleSingleton.answer) + Assert.assertEquals("Hello, world!", SimpleSingleton.greet("world")) + } + + @Test + fun counter() { + Assert.assertEquals(0, Counter.currentCount()) + Counter.increment() + Assert.assertEquals(1, Counter.currentCount()) + Counter.decrement() + Assert.assertEquals(0, Counter.currentCount()) + } + + @Test + fun comparator() { + val strings = listOf("Hello", "World") + val sortedStrings = strings.sortedWith(ReverseStringComparator) + + Assert.assertEquals(listOf("World", "Hello"), sortedStrings) + } + + @Test + fun companion() { + Assert.assertEquals("You can see me", OuterClass.public) + // Assert.assertEquals("You can't see me", OuterClass.secret) // Cannot access 'secret' + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/OuterClass.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/OuterClass.kt new file mode 100644 index 0000000000..4abb7a668d --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/OuterClass.kt @@ -0,0 +1,10 @@ +package com.baeldung.kotlin.objects + +class OuterClass { + companion object { + private val secret = "You can't see me" + val public = "You can see me" + } + + fun getSecretValue() = secret +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ReverseStringComparator.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ReverseStringComparator.kt new file mode 100644 index 0000000000..20dc2d8c5b --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/ReverseStringComparator.kt @@ -0,0 +1,5 @@ +package com.baeldung.kotlin.objects + +object ReverseStringComparator : Comparator { + override fun compare(o1: String, o2: String) = o1.reversed().compareTo(o2.reversed()) +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/SimpleSingleton.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/SimpleSingleton.kt new file mode 100644 index 0000000000..bfafd8183f --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/SimpleSingleton.kt @@ -0,0 +1,7 @@ +package com.baeldung.kotlin.objects + +object SimpleSingleton { + val answer = 42; + + fun greet(name: String) = "Hello, $name!" +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/StaticClass.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/StaticClass.kt new file mode 100644 index 0000000000..36cd476110 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/objects/StaticClass.kt @@ -0,0 +1,8 @@ +package com.baeldung.kotlin.objects + +class StaticClass { + companion object { + @JvmStatic + val staticField = 42 + } +}