Debian Codegraph @

gcc/testsuite/gdc.test/runnable/inclusive_incontracts.d

raw
1 // REQUIRED_ARGS: -preview=inclusiveincontracts
2 // PERMUTE_ARGS: -O -inline
3
4 import core.exception : AssertError;
5
6 void main()
7 {
8 basic_test;
9 multiple_incontracts_test;
10 }
11
12 void basic_test()
13 {
14 class A
15 {
16 void foo() @nogc
17 in (true, "foo")
18 {
19 }
20 }
21
22 class B : A
23 {
24 override void foo() @nogc
25 in (false, "nope not foo")
26 {
27 }
28 }
29
30 auto b = new B;
31
32 try
33 {
34 b.foo;
35 throw new Exception("Assert expected.");
36 }
37 catch (AssertError assertError)
38 {
39 assert(assertError.line == 25); // line of invalid in-contract
40 assert(assertError.msg == "Logic error: in-contract was tighter than parent in-contract");
41 }
42 }
43
44 void multiple_incontracts_test()
45 {
46 class A
47 {
48 void foo(int a, int b)
49 in (a > 0, "A::a")
50 in (b > 0, "B::b")
51 {
52 }
53 }
54
55 class B : A
56 {
57 override void foo(int a, int b)
58 in (a >= 0, "B::a")
59 in (b > 0, "B::b")
60 {
61 }
62 }
63
64 auto b = new B;
65
66 b.foo(0, 2);
67 try
68 {
69 b.foo(0, 0);
70 throw new Exception("Assert expected.");
71 }
72 catch (AssertError assertError) {
73 /**
74 * Having found that the looser contract in B is not fulfilled, we try
75 * the stricter contract in A. As that one is also violated, we error.
76 */
77 assert(assertError.line == 49);
78 assert(assertError.msg == "A::a");
79 }
80 }