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 Make sure to pass "-debug=deferAssertTest_unittest" to DMD.
10 
11 This has been tested to work with DMD 2.058 through 2.062
12 +/
13 
14 module semitwist.apps.tests.deferAssertTest.main;
15 
16 import semitwist.util.all;
17 
18 void main()
19 {
20 	flushAsserts();
21 	// Main program code here
22 }
23 
24 alias unittestSection!"deferAssertTest_unittest" unittestDeferAssertTest;
25 
26 mixin(unittestDeferAssertTest(q{
27 
28 	int foo = 2;
29 	string bar = "hello";
30 
31 	bool throwsException()
32 	{
33 		throw new Exception("Some exception");
34 	}
35 	
36     // Improvement to mixin syntax would be nice.
37 	// Also, my editor doesn't know that backticks indicate a string,
38 	// so it's still properly highlighted as code :)
39 	//mixin(deferAssert!(`foo == 3 || foo > 5`, "foo is bad"));
40 	assertPred!("a == 3 || a > 5", "foo is bad")(foo);
41 	
42 	//mixin(deferAssert!(`2 + 2 == 4`, "Basic arithmetic"));
43 	//assertPred!(q{ 2 + 2 == a }, "Basic arithmetic")(4);
44 	assertPred!"+"(2, 2, 4, "Basic arithmetic");
45 	assertPred!"+"(2, 2, 5, "Bad arithmetic");
46 	
47 	//mixin(deferAssert!(`false`));
48 	assertPred!"a"(false);
49 
50 	//mixin(deferAssert!(`throwsException()`, "Exceptions are handled"));
51 //	assertPred!q{ throwsException() }(null, "Exceptions are handled");
52 	//assertPred!(q{ a }, "Exceptions are handled")(throwsException());
53 	
54 	mixin(deferEnsure!(`foo`, `_ == 3 || _ > 5`, "ensure foo failed"));
55 	mixin(deferEnsure!(`foo`, `_ > 0`));
56 	mixin(deferEnsure!(`bar`, `_ == "hola"`));
57 	mixin(deferEnsure!(`2+2`, `_ == 4`));
58 	mixin(deferEnsure!(`throwsException()`, `!_`, "Exceptions are handled"));
59 	mixin(deferEnsure!(`false`, `_ == throwsException()`, "Exceptions are handled"));
60 	
61 	mixin(deferEnsureThrows!(`throw new Exception("Hello");`, Exception));
62 	//mixin(deferEnsureThrows!(`throw new Object();`, Exception, "Wrong type thrown!"));
63 	//mixin(deferEnsureThrows!(`throw new Exception("Hello");`, Object, "Wrong type thrown!"));
64 
65 }));
66 
67 /++
68 Program output:
69 
70 src\semitwist\apps\tests\deferAssertTest\main.d(36): Assert Failed (foo == 3 || foo > 5): foo is bad
71 src\semitwist\apps\tests\deferAssertTest\main.d(38): Assert Failed (false)
72 src\semitwist\apps\tests\deferAssertTest\main.d(39): Assert Threw (throwsException()): Exceptions are handled:
73 Threw: object.Exception: Some exception
74 src\semitwist\apps\tests\deferAssertTest\main.d(41): Ensure Failed: ensure foo failed
75 Expression 'foo':
76 Expected: _ == 3 || _ > 5
77 Actual: 2
78 src\semitwist\apps\tests\deferAssertTest\main.d(43): Ensure Failed
79 Expression 'bar':
80 Expected: _ == "hola"
81 Actual: hello
82 src\semitwist\apps\tests\deferAssertTest\main.d(45): Ensure Threw: Exceptions are handled:
83 Expression 'throwsException()':
84 Expected: !_
85 Threw: object.Exception: Some exception
86 src\semitwist\apps\tests\deferAssertTest\main.d(46): Ensure Threw: Exceptions are handled:
87 Expression 'false':
88 Expected: _ == throwsException()
89 Threw: object.Exception: Some exception
90 src\semitwist\apps\tests\deferAssertTest\main.d(49): Ensure Throw Failed: Wrong type thrown!
91 Statement 'throw new Object();':
92 Expected: object.Exception
93 Actual:   object.Object: object.Object
94 src\semitwist\apps\tests\deferAssertTest\main.d(50): Ensure Throw Failed: Wrong type thrown!
95 Statement 'throw new Exception("Hello");':
96 Expected: object.Object
97 Actual:   object.Exception: Hello
98 core.exception.AssertError@src\semitwist\util\deferAssert.d(171): 9 Assert Failures
99 
100 +/