Skip to content

Commit 639d3bc

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 67c23cc + 6526704 commit 639d3bc

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class InfixFunctionsTest {
7+
@Test
8+
fun testColours() {
9+
val color = 0x123456
10+
val red = (color and 0xff0000) shr 16
11+
val green = (color and 0x00ff00) shr 8
12+
val blue = (color and 0x0000ff) shr 0
13+
14+
Assert.assertEquals(0x12, red)
15+
Assert.assertEquals(0x34, green)
16+
Assert.assertEquals(0x56, blue)
17+
}
18+
19+
@Test
20+
fun testNewAssertions() {
21+
class Assertion<T>(private val target: T) {
22+
infix fun isEqualTo(other: T) {
23+
Assert.assertEquals(other, target)
24+
}
25+
26+
infix fun isDifferentFrom(other: T) {
27+
Assert.assertNotEquals(other, target)
28+
}
29+
}
30+
31+
val result = Assertion(5)
32+
33+
result isEqualTo 5
34+
35+
// The following two lines are expected to fail
36+
// result isEqualTo 6
37+
// result isDifferentFrom 5
38+
}
39+
40+
@Test
41+
fun testNewStringMethod() {
42+
infix fun String.substringMatches(r: Regex) : List<String> {
43+
return r.findAll(this)
44+
.map { it.value }
45+
.toList()
46+
}
47+
48+
val matches = "a bc def" substringMatches ".*? ".toRegex()
49+
Assert.assertEquals(listOf("a ", "bc "), matches)
50+
}
51+
}

0 commit comments

Comments
 (0)