|
| 1 | +import org.assertj.core.api.Assertions.assertThat |
| 2 | +import org.junit.jupiter.api.Test |
1 | 3 | import kotlin.properties.Delegates
|
2 | 4 |
|
3 | 5 | interface TextChangedListener {
|
4 |
| - fun onTextChanged(newText: String) |
| 6 | + |
| 7 | + fun onTextChanged(oldText: String, newText: String) |
5 | 8 | }
|
6 | 9 |
|
7 | 10 | class PrintingTextChangedListener : TextChangedListener {
|
8 |
| - override fun onTextChanged(newText: String) = println("Text is changed to: $newText") |
| 11 | + |
| 12 | + override fun onTextChanged(oldText: String, newText: String) = |
| 13 | + println("Text is changed $oldText -> $newText") |
9 | 14 | }
|
10 | 15 |
|
11 | 16 | class TextView {
|
12 | 17 |
|
13 | 18 | var listener: TextChangedListener? = null
|
14 | 19 |
|
15 |
| - var text: String by Delegates.observable("") { prop, old, new -> |
16 |
| - listener?.onTextChanged(new) |
| 20 | + var text: String by Delegates.observable("<empty>") { _, old, new -> |
| 21 | + listener?.onTextChanged(old, new) |
17 | 22 | }
|
18 | 23 | }
|
19 | 24 |
|
20 |
| -fun main(args: Array<String>) { |
21 |
| - val textView = TextView() |
22 |
| - textView.listener = PrintingTextChangedListener() |
23 |
| - textView.text = "Lorem ipsum" |
24 |
| - textView.text = "dolor sit amet" |
| 25 | +class ListenerTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `Listener`() { |
| 29 | + val textView = TextView().apply { |
| 30 | + listener = PrintingTextChangedListener() |
| 31 | + } |
| 32 | + |
| 33 | + with(textView) { |
| 34 | + text = "Lorem ipsum" |
| 35 | + text = "dolor sit amet" |
| 36 | + } |
| 37 | + |
| 38 | + assertThat(textView.listener).isNotNull() |
| 39 | + } |
25 | 40 | }
|
26 | 41 |
|
0 commit comments