1 module ut.exception;
2 
3 
4 import ut;
5 import nogc.exception;
6 
7 
8 @("enforce")
9 @safe unittest {
10     const(char)[] msg, file;
11     size_t line, expectedLine;
12     () @safe @nogc {
13         try {
14             expectedLine = __LINE__ + 1;
15             enforce(false, "foo", 5, "bar");
16         } catch(NoGcException ex) {
17             msg = ex.msg;
18             file = ex.file;
19             line = ex.line;
20         }
21     }();
22 
23     msg.shouldEqual("foo5bar");
24     file.shouldEqual(__FILE__);
25     line.shouldEqual(expectedLine);
26 }
27 
28 
29 version(FIXME) {
30     @("TestAllocator")
31         @safe @nogc unittest {
32 
33         import test_allocator;
34         static TestAllocator allocator;
35 
36         alias MyException = NoGcExceptionImpl!(TestAllocator*);
37 
38         // just to make sure it compiles
39         void func() {
40             throw new MyException(&allocator, 42, " foobar ", 33.3);
41         }
42     }
43 }
44 
45 
46 @("malloc")
47 @safe @nogc unittest {
48 
49     try
50         throw new NoGcException(42, " foobar ", 33.3);
51     catch(NoGcException e) {
52         assert(e.msg == "42 foobar 33.300000", e.msg);
53         assert(e.file == __FILE__);
54         assert(e.line == __LINE__ - 4);
55     }
56 }
57 
58 
59 @("derived exception")
60 @safe @nogc unittest {
61 
62     static class MyException: NoGcException {
63 
64         static int numInstances;
65 
66         this(A...)(auto ref A args) {
67             import std.functional: forward;
68             super(forward!args);
69             ++numInstances;
70         }
71 
72         ~this() {
73             --numInstances;
74         }
75     }
76 
77     try {
78         assert(MyException.numInstances == 0);
79         throw new MyException(42, " foobar ", 33.3);
80     } catch(MyException e) {
81         assert(MyException.numInstances == 1);
82     }
83 
84     // FIXME: druntime issue
85     version(fixme)
86         assert(MyException.numInstances == 0);
87 }
88 
89 
90 @("throw.base")
91 @safe @nogc unittest {
92     try
93         NoGcException.throw_(File("foo.d"), Line(42),
94                              "this is the message ", 33, " or ", 77.7, " hah");
95     catch(Exception e) {
96         assert(e.line == 42);
97         debug {
98             e.file.should == "foo.d";
99             e.line.should == 42;
100             e.msg.should == "this is the message 33 or 77.700000 hah";
101         }
102         return;
103     }
104 
105     assert(false, "Didn't catch the exception");
106 }
107 
108 
109 @("throw.child")
110 @safe @nogc unittest {
111 
112     static class MyException: NoGcException {
113         mixin NoGcExceptionCtors;
114     }
115 
116     try
117         MyException.throw_(File("foo.d"), Line(42),
118                            "this is the message ", 33, " or ", 77.7, " hah");
119     catch(MyException e) {
120         assert(e.line == 42);
121         debug {
122             e.file.should == "foo.d";
123             e.line.should == 42;
124             e.msg.should == "this is the message 33 or 77.700000 hah";
125         }
126         return;
127     }
128 
129     assert(false, "Didn't catch the exception");
130 }