1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4TestCase := Rectangle {
5 pure callback test_func(int) -> int;
6 pure callback test_func2(string, int) -> string;
7 test_func2(str, val) => { str + "=" + (val + some_value) }
8
9 property <int> test_prop: 4 + test_func(2);
10 property <string> test_prop2: test_func2("hello", 42);
11 property <int> some_value: 8;
12
13 callback dummy() -> int;
14 dummy => { 4.4 }
15 callback returns_void;
16 returns_void => { test_func2("haha", 8) }
17
18 property <bool> test: test_prop == 4 && test_prop2 == "hello=50";
19}
20
21/*
22```rust
23let instance = TestCase::new().unwrap();
24instance.on_test_func({
25 let weak = instance.as_weak();
26 move |a| weak.upgrade().unwrap().get_some_value() * a
27});
28assert_eq!(instance.get_test_prop(), 4 + 16);
29assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=50"));
30instance.set_some_value(2);
31assert_eq!(instance.get_test_prop(), 4 + 4);
32assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=44"));
33
34assert_eq!(instance.invoke_test_func2("xxx".into(), 1), slint::SharedString::from("xxx=3"));
35```
36
37```cpp
38auto handle = TestCase::create();
39const TestCase &instance = *handle;
40instance.on_test_func([weak = slint::ComponentWeakHandle(handle)](int a) {
41 return (*weak.lock())->get_some_value() * a;
42});
43assert_eq(instance.get_test_prop(), 4 + 16);
44assert_eq(instance.get_test_prop2(), slint::SharedString("hello=50"));
45instance.set_some_value(2);
46assert_eq(instance.get_test_prop(), 4 + 4);
47assert_eq(instance.get_test_prop2(), slint::SharedString("hello=44"));
48
49assert_eq(instance.invoke_test_func2("xxx", 1), slint::SharedString("xxx=3"));
50```
51
52
53```js
54var instance = new slint.TestCase({
55 test_func: function(a) { return instance.some_value * a; }
56});
57assert.equal(instance.test_prop, 4 + 16);
58assert.equal(instance.test_prop2, "hello=50");
59instance.some_value = 2;
60assert.equal(instance.test_prop, 4 + 4);
61assert.equal(instance.test_prop2, "hello=44");
62
63assert.equal(instance.test_func2("xxx", 1), "xxx=3");
64```
65*/
66