summaryrefslogtreecommitdiff
path: root/tests/errors/loops.vala
blob: 552865224657a08a1b0acdb5e84f216f2de4d370 (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
errordomain FooError {
	FAIL
}

string[] get_array () throws Error {
	throw new FooError.FAIL ("foo");
}

bool get_bool () throws Error {
	throw new FooError.FAIL ("foo");
}

int get_int () throws Error {
	throw new FooError.FAIL ("foo");
}

void error_in_for () {
	try {
		for (var i = get_int (); i < 2; i++) {
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}

	try {
		for (var i = 0; get_bool (); i++) {
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}

	try {
		bool reached = false;
		for (var i = 0; i < 2; i += get_int ()) {
			if (reached) {
				assert_not_reached ();
			} else {
				reached = true;
			}
		}
		assert_not_reached ();
	} catch {
	}

	try {
		for (var i = 0; i < 2; i++) {
			throw new FooError.FAIL ("foo");
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}
}

void error_in_foreach () {
	try {
		foreach (var s in get_array ()) {
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}

	try {
		string[] array = { "bar" };
		foreach (var s in array) {
			throw new FooError.FAIL ("foo");
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}
}

void error_in_do () {
	try {
		do {
		} while (get_bool ());
		assert_not_reached ();
	} catch {
	}

	try {
		do {
			throw new FooError.FAIL ("foo");
			assert_not_reached ();
		} while (true);
		assert_not_reached ();
	} catch {
	}
}

void error_in_while () {
	try {
		while (get_bool ()) {
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}

	try {
		while (true) {
			throw new FooError.FAIL ("foo");
			assert_not_reached ();
		}
		assert_not_reached ();
	} catch {
	}
}

void main () {
	error_in_for ();
	error_in_foreach ();
	error_in_do ();
	error_in_while ();
}