summaryrefslogtreecommitdiff
path: root/tests/objects/singleton.vala
blob: 6a54b5c222b265fc835dc62a4dff7ef28db38216 (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
[SingleInstance]
public class Foo : Object {
	public int bar = 42;
	construct {
	}
}

[SingleInstance]
public class Bar : Object {
	public int foo = 42;
}

void lifetime_1 () {
	Foo a = new Foo ();
	Foo b = (Foo) Object.new (typeof (Foo));

	assert (a == b);
	assert (a.bar == 23);
}

void lifetime_2 () {
	Foo a = new Foo ();
	Foo b = (Foo) Object.new (typeof (Foo));

	assert (a == b);
	assert (a.bar == 42);
}

void lifetime_3 () {
	Bar a = new Bar ();
	Bar b = (Bar) Object.new (typeof (Bar));

	assert (a == b);
	assert (a.foo == 23);
}

void main () {
	{
		// create singleton instance here
		// which lives as long until it runs out of scope
		Foo singleton = new Foo ();
		singleton.bar = 23;
		lifetime_1 ();
	}

	{
		// create new singleton instance here
		Foo singleton = new Foo ();
		assert (singleton.bar == 42);
		lifetime_2 ();
	}

	{
		// create singleton instance here
		Bar singleton = new Bar ();
		singleton.foo = 23;
		lifetime_3 ();
	}
}