This commit is contained in:
Kacper Koza 2019-04-03 09:47:58 +02:00
parent b84b7117a6
commit 6fe5f008a7
5 changed files with 39 additions and 38 deletions

View File

@ -1,9 +0,0 @@
@file:JvmName("Lists")
package com.baeldung.kotlin
fun <T> MutableList<T>.swap(firstIndex: Int, secondIndex: Int): MutableList<T> {
val tmp = this[firstIndex]
this[firstIndex] = this[secondIndex]
this[secondIndex] = tmp
return this
}

View File

@ -0,0 +1,9 @@
@file:JvmName("Strings")
package com.baeldung.kotlin
fun String.escapeForXml() : String {
return this
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
}

View File

@ -1,22 +0,0 @@
package com.baeldung.kotlin;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static com.baeldung.kotlin.Lists.*;
public class ListUtilTest {
@Test
public void shouldSwapTwoElementsInList() {
List<Integer> list = Arrays.asList(0, 1, 2);
List<Integer> swappedElements = swap(list, 1, 2);
Assert.assertEquals(swappedElements, Arrays.asList(0, 2, 1));
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.kotlin;
import kotlin.text.StringsKt;
import org.junit.Assert;
import org.junit.Test;
import static com.baeldung.kotlin.Strings.*;
public class StringUtilTest {
@Test
public void shouldSwapTwoElementsInList() {
String xml = "<a>hi</a>";
String escapedXml = escapeForXml(xml);
Assert.assertEquals("&lt;a&gt;hi&lt;/a&gt;", escapedXml);
}
@Test
public void callingBuiltInKotlinExtensionMethod() {
String name = "john";
String capitalizedName = StringsKt.capitalize(name);
Assert.assertEquals("John", capitalizedName);
}
}

View File

@ -6,13 +6,6 @@ import org.junit.Test
class ExtensionMethods {
@Test
fun simpleExtensionMethod() {
fun String.escapeForXml() : String {
return this
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
}
Assert.assertEquals("Nothing", "Nothing".escapeForXml())
Assert.assertEquals("&lt;Tag&gt;", "<Tag>".escapeForXml())
Assert.assertEquals("a&amp;b", "a&b".escapeForXml())