summaryrefslogtreecommitdiff
path: root/tests/objects/properties.vala
blob: 1ad97367eebd3ce72793a5916ea92c0ca8050e05 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using GLib;

[CCode (has_target = false)]
public delegate void Delegate ();

public struct RealStruct {
	public int field;
}

public class NonPrivAccess : Object {
	[NoAccessorMethod]
	public RealStruct real_struct { owned get; set; }
}

public class Sample : Object {
	private string automatic { get; set; }

	public Delegate deleg { get; owned set; }

	private string _name;
	public string name {
		get { return _name; }
		set { _name = value; }
	}

	private string _read_only;
	public string read_only {
		get { return _read_only; }
	}

	public int public_prop { get; set; }
	private int private_prop { get; set; }

	public Sample (string name) {
		this.name = name;
	}

	construct {
		_automatic = "InitialAutomatic";
		_read_only = "InitialReadOnly";
	}

	public void run() {
		notify.connect ((s, p) => {
			stdout.printf("property `%s' has changed!\n",
				      p.name);
		});


		automatic = "TheNewAutomatic";
		name = "TheNewName";

		// The following statement would be rejected
		// read_only = "TheNewReadOnly";

		stdout.printf("automatic: %s\n", automatic);
		stdout.printf("name: %s\n", name);
		stdout.printf("read_only: %s\n", read_only);
		stdout.printf("automatic: %s\n", automatic);

		this.deleg = null;

		lock (public_prop) {
			public_prop = 42;
		}
		lock (private_prop) {
			private_prop = 42;
		}
	}

	public static int main () {
		var test = new Sample("InitialName");

		test.run();

		Maman.Bar.run ();

		stdout.printf ("Interface Properties Test: 1");

		Maman.Ibaz ibaz = new Maman.Baz ();
		ibaz.simple_method ();

		stdout.printf (" 3\n");

		var nonpriv = new NonPrivAccess ();
		nonpriv.real_struct = { 10 };
		assert (nonpriv.real_struct.field == 10);

		return 0;
	}
}

abstract class Maman.Foo : Object {
	private int _public_base_property = 2;
	public int public_base_property {
		get {
			return _public_base_property;
		}
		set {
			_public_base_property = value;
		}
	}
	public abstract int abstract_base_property { get; set; }
}

enum FooEnum {
	FOO
}

abstract class Maman.EnumDefault {
	public abstract FooEnum bar { get; default = FooEnum.FOO; }
}

class Maman.Bar : Foo {
	public int public_property { get; set; default = 3; }
	public override int abstract_base_property { get; set; }

	void do_action () {
		stdout.printf (" %d %d", public_base_property, public_property);
		public_base_property = 4;
		public_property = 5;
		stdout.printf (" %d %d", public_base_property, public_property);
	}

	public static void run () {
		stdout.printf ("Property Test: 1");

		var bar = new Bar ();
		bar.do_action ();

		Foo foo = bar;
		foo.abstract_base_property = 6;
		stdout.printf (" %d", foo.abstract_base_property);

		stdout.printf (" 7\n");
	}
}

interface Maman.Ibaz : Object {
	public abstract int number { get; }

	public void simple_method () {
		int n = number;
		stdout.printf (" %d", n);
	}
}

class Maman.Baz : Object, Ibaz {
	public int number {
		get { return 2; }
	}
}

interface Maman.IBiz : Object {
	public abstract int number { get; construct; }
}

abstract class Maman.ABiz : Object, IBiz {
	public int number { get; construct; }
	public abstract int number2 { get; construct; }
}

class Maman.Biz : ABiz {
	public override int number2 { get; construct; }
}