Release 0.41.92
[vala-gnome.git] / tests / objects / properties.vala
blob3d86de106fb4a44ae31754869020b365d38c899a
1 using GLib;
3 [CCode (has_target = false)]
4 public delegate void Delegate ();
6 public struct RealStruct {
7 public int field;
10 public class NonPrivAccess : Object {
11 [NoAccessorMethod]
12 public RealStruct real_struct { get; set; }
15 public class Sample : Object {
16 private string automatic { get; set; }
18 public Delegate deleg { get; owned set; }
20 private string _name;
21 public string name {
22 get { return _name; }
23 set { _name = value; }
26 private string _read_only;
27 public string read_only {
28 get { return _read_only; }
31 public Sample (string name) {
32 this.name = name;
35 construct {
36 _automatic = "InitialAutomatic";
37 _read_only = "InitialReadOnly";
40 public void run() {
41 notify += (s, p) => {
42 stdout.printf("property `%s' has changed!\n",
43 p.name);
47 automatic = "TheNewAutomatic";
48 name = "TheNewName";
50 // The following statement would be rejected
51 // read_only = "TheNewReadOnly";
53 stdout.printf("automatic: %s\n", automatic);
54 stdout.printf("name: %s\n", name);
55 stdout.printf("read_only: %s\n", read_only);
56 stdout.printf("automatic: %s\n", automatic);
58 this.deleg = null;
61 public static int main () {
62 var test = new Sample("InitialName");
64 test.run();
66 Maman.Bar.run ();
68 stdout.printf ("Interface Properties Test: 1");
70 Maman.Ibaz ibaz = new Maman.Baz ();
71 ibaz.simple_method ();
73 stdout.printf (" 3\n");
75 var nonpriv = new NonPrivAccess ();
76 nonpriv.real_struct = { 10 };
77 assert (nonpriv.real_struct.field == 10);
79 return 0;
83 abstract class Maman.Foo : Object {
84 private int _public_base_property = 2;
85 public int public_base_property {
86 get {
87 return _public_base_property;
89 set {
90 _public_base_property = value;
93 public abstract int abstract_base_property { get; set; }
96 enum FooEnum {
97 FOO
100 abstract class Maman.EnumDefault {
101 public abstract FooEnum bar { get; default = FooEnum.FOO; }
104 class Maman.Bar : Foo {
105 public int public_property { get; set; default = 3; }
106 public override int abstract_base_property { get; set; }
108 void do_action () {
109 stdout.printf (" %d %d", public_base_property, public_property);
110 public_base_property = 4;
111 public_property = 5;
112 stdout.printf (" %d %d", public_base_property, public_property);
115 public static void run () {
116 stdout.printf ("Property Test: 1");
118 var bar = new Bar ();
119 bar.do_action ();
121 Foo foo = bar;
122 foo.abstract_base_property = 6;
123 stdout.printf (" %d", foo.abstract_base_property);
125 stdout.printf (" 7\n");
129 interface Maman.Ibaz : Object {
130 public abstract int number { get; }
132 public void simple_method () {
133 int n = number;
134 stdout.printf (" %d", n);
138 class Maman.Baz : Object, Ibaz {
139 public int number {
140 get { return 2; }
144 interface Maman.IBiz : Object {
145 public abstract int number { get; construct; }
148 abstract class Maman.ABiz : Object, IBiz {
149 public int number { get; construct; }
150 public abstract int number2 { get; construct; }
153 class Maman.Biz : ABiz {
154 public override int number2 { get; construct; }
157 void main () {
158 Sample.main ();