root/trunk/src/semitwist/apps/tests/deferAssertTest/main.d

Revision 146, 2.8 kB (checked in by Abscissa, 1 week ago)

(NB) deferAssertTest: Works now.

  • Property svn:eol-style set to native
Line 
1 // SemiTwist D Tools
2 // Tests: Defer Assert Test
3 // Written in the D programming language.
4
5 /++
6 Author:
7 $(WEB www.semitwist.com, Nick Sabalausky)
8
9 This has been tested to work with DMD 2.048
10 +/
11
12 module semitwist.apps.tests.deferAssertTest.main;
13
14 import semitwist.util.all;
15 import semitwist.util.compat.all;
16
17 void main()
18 {
19     flushAsserts();
20     // Main program code here
21 }
22
23 unittest
24 {
25     int foo = 2;
26     string bar = "hello";
27
28     bool throwsException()
29     {
30         throw new Exception("Some exception");
31     }
32    
33     // Improvement to mixin syntax would be nice.
34     // Also, my editor doesn't know that backticks indicate a string,
35     // so it's still properly highlighted as code :)
36     mixin(deferAssert!(`foo == 3 || foo > 5`, "foo is bad"));
37     mixin(deferAssert!(`2 + 2 == 4`, "Basic arithmetic"));
38     mixin(deferAssert!(`false`));
39     mixin(deferAssert!(`throwsException()`, "Exceptions are handled"));
40    
41     mixin(deferEnsure!(`foo`, `_ == 3 || _ > 5`, "ensure foo failed"));
42     mixin(deferEnsure!(`foo`, `_ > 0`));
43     mixin(deferEnsure!(`bar`, `_ == "hola"`));
44     mixin(deferEnsure!(`2+2`, `_ == 4`));
45     mixin(deferEnsure!(`throwsException()`, `!_`, "Exceptions are handled"));
46     mixin(deferEnsure!(`false`, `_ == throwsException()`, "Exceptions are handled"));
47    
48     mixin(deferEnsureThrows!(`throw new Exception("Hello");`, Exception));
49     mixin(deferEnsureThrows!(`throw new Object();`, Exception, "Wrong type thrown!"));
50     mixin(deferEnsureThrows!(`throw new Exception("Hello");`, Object, "Wrong type thrown!"));
51 }
52
53 /++
54 Program output:
55
56 src\semitwist\apps\tests\deferAssertTest\main.d(36): Assert Failed (foo == 3 || foo > 5): foo is bad
57 src\semitwist\apps\tests\deferAssertTest\main.d(38): Assert Failed (false)
58 src\semitwist\apps\tests\deferAssertTest\main.d(39): Assert Threw (throwsException()): Exceptions are handled:
59 Threw: object.Exception: Some exception
60 src\semitwist\apps\tests\deferAssertTest\main.d(41): Ensure Failed: ensure foo failed
61 Expression 'foo':
62 Expected: _ == 3 || _ > 5
63 Actual: 2
64 src\semitwist\apps\tests\deferAssertTest\main.d(43): Ensure Failed
65 Expression 'bar':
66 Expected: _ == "hola"
67 Actual: hello
68 src\semitwist\apps\tests\deferAssertTest\main.d(45): Ensure Threw: Exceptions are handled:
69 Expression 'throwsException()':
70 Expected: !_
71 Threw: object.Exception: Some exception
72 src\semitwist\apps\tests\deferAssertTest\main.d(46): Ensure Threw: Exceptions are handled:
73 Expression 'false':
74 Expected: _ == throwsException()
75 Threw: object.Exception: Some exception
76 src\semitwist\apps\tests\deferAssertTest\main.d(49): Ensure Throw Failed: Wrong type thrown!
77 Statement 'throw new Object();':
78 Expected: object.Exception
79 Actual:   object.Object: object.Object
80 src\semitwist\apps\tests\deferAssertTest\main.d(50): Ensure Throw Failed: Wrong type thrown!
81 Statement 'throw new Exception("Hello");':
82 Expected: object.Object
83 Actual:   object.Exception: Hello
84 core.exception.AssertError@src\semitwist\util\deferAssert.d(171): 9 Assert Failures
85
86 +/
Note: See TracBrowser for help on using the browser.