]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
tests: Add dedicated "property ownership" tests
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 2 Sep 2019 08:21:05 +0000 (10:21 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 2 Sep 2019 08:27:27 +0000 (10:27 +0200)
tests/Makefile.am
tests/objects/property-ownership.vala [new file with mode: 0644]

index a5bf21ec8dfc5c593cbb10df300f14859bab95dd..093ee5863f760c5c9f910f4b65383df6886eb445 100644 (file)
@@ -319,6 +319,7 @@ TESTS = \
        objects/plugin-module-init.vala \
        objects/properties.vala \
        objects/property-notify.vala \
+       objects/property-ownership.vala \
        objects/property-read-only-auto.vala \
        objects/property-read-only-write.test \
        objects/property-construct-only-write.test \
diff --git a/tests/objects/property-ownership.vala b/tests/objects/property-ownership.vala
new file mode 100644 (file)
index 0000000..4fa1775
--- /dev/null
@@ -0,0 +1,89 @@
+class Foo : Object {
+}
+
+class Bar : Object {
+       public Foo foo { get; set; }
+       public Foo foo_owned { owned get; set; }
+       public weak Foo foo_weak { get; set; }
+       public weak Foo foo_weak_owned { owned get; set; }
+}
+
+class Manam {
+       public Foo foo { get; set; }
+       public Foo foo_owned { owned get; set; }
+       public weak Foo foo_weak { get; set; }
+       public weak Foo foo_weak_owned { owned get; set; }
+}
+
+void main () {
+       var foo = new Foo ();
+       assert (foo.ref_count == 1);
+
+       //GObject class
+       {
+               var bar = new Bar ();
+               bar.foo = foo;
+               assert (foo.ref_count == 2);
+               unowned Foo f = bar.foo;
+               assert (foo.ref_count == 2);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var bar = new Bar ();
+               bar.foo_owned = foo;
+               assert (foo.ref_count == 2);
+               Foo f = bar.foo_owned;
+               assert (foo.ref_count == 3);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var bar = new Bar ();
+               bar.foo_weak = foo;
+               assert (foo.ref_count == 1);
+               unowned Foo f_weak = bar.foo_weak;
+               assert (foo.ref_count == 1);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var bar = new Bar ();
+               bar.foo_weak_owned = foo;
+               assert (foo.ref_count == 1);
+               Foo f_weak_owned = bar.foo_weak_owned;
+               assert (foo.ref_count == 2);
+       }
+       assert (foo.ref_count == 1);
+
+       //GType class
+       {
+               var manam = new Manam ();
+               manam.foo = foo;
+               assert (foo.ref_count == 2);
+               unowned Foo f = manam.foo;
+               assert (foo.ref_count == 2);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var manam = new Manam ();
+               manam.foo_owned = foo;
+               assert (foo.ref_count == 2);
+               Foo f = manam.foo_owned;
+               assert (foo.ref_count == 3);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var manam = new Manam ();
+               manam.foo_weak = foo;
+               assert (foo.ref_count == 1);
+               unowned Foo f_weak = manam.foo_weak;
+               assert (foo.ref_count == 1);
+       }
+       assert (foo.ref_count == 1);
+       {
+               var manam = new Manam ();
+               manam.foo_weak_owned = foo;
+               assert (foo.ref_count == 1);
+               Foo f_weak_owned = manam.foo_weak_owned;
+               assert (foo.ref_count == 2);
+       }
+       assert (foo.ref_count == 1);
+}