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
use crate::core_types::typed_array::TypedArray;
use crate::core_types::GodotString;

/// A reference-counted vector of `GodotString` that uses Godot's pool allocator.
pub type StringArray = TypedArray<GodotString>;

godot_test!(
    test_string_array_access {
        use crate::NewRef as _;

        let arr = StringArray::from_vec(vec![
            GodotString::from("foo"),
            GodotString::from("bar"),
            GodotString::from("baz"),
        ]);

        let original_read = {
            let read = arr.read();
            assert_eq!(&[
                GodotString::from("foo"),
                GodotString::from("bar"),
                GodotString::from("baz"),
            ], read.as_slice());
            read.clone()
        };

        let mut cow_arr = arr.new_ref();

        {
            let mut write = cow_arr.write();
            assert_eq!(3, write.len());
            for s in write.as_mut_slice() {
                *s = s.to_uppercase();
            }
        }

        assert_eq!(GodotString::from("FOO"), cow_arr.get(0));
        assert_eq!(GodotString::from("BAR"), cow_arr.get(1));
        assert_eq!(GodotString::from("BAZ"), cow_arr.get(2));

        // the write shouldn't have affected the original array
        assert_eq!(&[
            GodotString::from("foo"),
            GodotString::from("bar"),
            GodotString::from("baz"),
        ], original_read.as_slice());
    }
);

godot_test!(
    test_string_array_debug {
        let arr = StringArray::from_vec(vec![
            GodotString::from("foo"),
            GodotString::from("bar"),
            GodotString::from("baz"),
        ]);

        assert_eq!(format!("{:?}", arr), "[\"foo\", \"bar\", \"baz\"]");
    }
);