summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-fixtures/todolist/tests/bindings/test_todolist.kts
blob: bb2b292224d0aa4738427fa764168817054af4d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import uniffi.todolist.*

val todo = TodoList()

// This throws an exception:
try {
    todo.getLast()
    throw RuntimeException("Should have thrown a TodoError!")
} catch (e: TodoException.EmptyTodoList) {
    // It's okay, we don't have any items yet!
}

try {
    createEntryWith("")
    throw RuntimeException("Should have thrown a TodoError!")
} catch (e: TodoException) {
    // It's okay, the string was empty!
    assert(e is TodoException.EmptyString)
    assert(e !is TodoException.EmptyTodoList)
}

todo.addItem("Write strings support")

assert(todo.getLast() == "Write strings support")

todo.addItem("Write tests for strings support")

assert(todo.getLast() == "Write tests for strings support")

val entry = createEntryWith("Write bindings for strings as record members")

todo.addEntry(entry)
assert(todo.getLast() == "Write bindings for strings as record members")
assert(todo.getLastEntry().text == "Write bindings for strings as record members")

todo.addItem("Test Ünicode hàndling without an entry can't believe I didn't test this at first 🤣")
assert(todo.getLast() == "Test Ünicode hàndling without an entry can't believe I didn't test this at first 🤣")

val entry2 = TodoEntry("Test Ünicode hàndling in an entry can't believe I didn't test this at first 🤣")
todo.addEntry(entry2)
assert(todo.getLastEntry().text == "Test Ünicode hàndling in an entry can't believe I didn't test this at first 🤣")

assert(todo.getEntries().size == 5)

todo.addEntries(listOf(TodoEntry("foo"), TodoEntry("bar")))
assert(todo.getEntries().size == 7)
assert(todo.getLastEntry().text == "bar")

todo.addItems(listOf("bobo", "fofo"))
assert(todo.getItems().size == 9)
assert(todo.getItems()[7] == "bobo")

assert(getDefaultList() == null)

// Note that each individual object instance needs to be explicitly destroyed,
// either by using the `.use` helper or explicitly calling its `.destroy` method.
// Failure to do so will leak the underlying Rust object.
TodoList().use { todo2 ->
    setDefaultList(todo)
    getDefaultList()!!.use { default ->
        assert(todo.getEntries() == default.getEntries())
        assert(todo2.getEntries() != default.getEntries())
    }

    todo2.makeDefault()
    getDefaultList()!!.use { default ->
        assert(todo.getEntries() != default.getEntries())
        assert(todo2.getEntries() == default.getEntries())
    }

    todo.addItem("Test liveness after being demoted from default")
    assert(todo.getLast() == "Test liveness after being demoted from default")

    todo2.addItem("Test shared state through local vs default reference")
    getDefaultList()!!.use { default ->
        assert(default.getLast() == "Test shared state through local vs default reference")
    }
}

// Ensure the kotlin version of deinit doesn't crash, and is idempotent.
todo.destroy()
todo.destroy()