~eda-qa/dhlib/main

1 by edA-qa mort-ora-y
first
1
/* <license>
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
3
 * For full copyright and license information please refer to doc/license.txt.
4
 * </license> 
5
 */
6
/**
7
 * To make up for a lack in haxe, this is a standard assertion
8
 * class.
9
 * It's behaviour is to trace that an assertion occured and then
10
 * to through an exception -- hopefully something can display
11
 * the exception (like Flash Debug player)
12
 *
13
 * TODO: somehow merge/unify with TestCaseX, since basically all the same
14
 * tests might appear...
15
 */
16
class Assert
17
{
18
	var msg : String;
19
	public function new( msg : String )
20
	{
21
		this.msg = msg;
22
	}
23
	public function toString() : String
24
	{
25
		return msg;
26
	}
27
	
28
	static public function fail( reason : String, ?msg : String, ?pos:haxe.PosInfos )
29
	{
30
		if( msg == null )
31
			msg = "";
32
		else
33
			msg = ":" + msg;
34
			
35
		trace( "Assertion in " 
36
			+ pos.className 
37
			+ "::" 
38
			+pos.methodName 
39
			+ ": " 
40
			+ "<"
41
			+ pos.fileName 
42
			+ ":"
43
			+ pos.lineNumber
44
			+ ">"
45
			+ reason
46
			+ msg, pos );
47
		throw new Assert(msg);	//something that nobody is likely catching...
48
	}
49
	
50
	inline static public function isTrue( cond : Bool, ?msg : String, ?pos:haxe.PosInfos )
51
	{
52
		if( !cond )
53
			fail( "isTrue", msg, pos );
54
	}
55
	
56
	inline static public function isFalse( cond : Bool,?msg : String,  ?pos:haxe.PosInfos )
57
	{
58
		if( cond )
59
			fail( "isFalse", msg, pos );
60
	}
61
	
62
	inline static public function unreachable(  ?pos:haxe.PosInfos) : Void
63
	{
64
		fail( "unreachable", pos);
65
	}
66
	
67
	inline static public function invalidParam( ?text : String, ?pos: haxe.PosInfos ) : Void
68
	{
69
		fail( "invalidParam", text, pos );
70
	}
71
	
72
	inline static public function is( obj : Dynamic, type : Dynamic, ?msg : String, ?pos:haxe.PosInfos )
73
	{
74
		if( !Std.is( obj, type ) )
75
			fail( "is(" + obj + "," + type + ")", msg, pos );
76
	}
77
	
78
	inline static public function notNull( obj : Dynamic, ?msg : String, ?pos:haxe.PosInfos )
79
	{
80
		if( obj == null )
81
			fail( "notNull", msg, pos );
82
	}
83
	
84
	inline static public function isNull( obj : Dynamic, ?msg : String, ?pos:haxe.PosInfos )
85
	{
86
		if( obj != null )
87
			fail( "isNull", msg, pos );
88
	}
89
	
90
	/**
91
	 * Used to indicate the funciton is never meant to be called, it is rather
92
	 * a placeholder for a function to be defined in a derived class.
93
	 */
94
	inline static public function pureVirtual( ?pos:haxe.PosInfos )
95
	{
96
		fail( "pure virtual called", pos );
97
	}
98
	
99
	/**
100
	 * Used to indicate a function was called, typically as part of an interface,
101
	 * which has no definition, nor should it have a definition as it should
102
	 * not be called.
103
	 */
104
	inline static public function noVirtual( ?pos:haxe.PosInfos )
105
	{
106
		fail( "noVirtual", pos );
107
	}
108
	
109
	/**
110
	 * Used to indicate that something is unimplemented
111
	 *
112
	 * @param	cond	[in] if specified, states what is unimplemented, note, this
113
	 *		is logically equivalent to isFalse (you specify which condition is unimplemented)
114
	 */
115
	inline static public function unimplemented( ?cond: Null<Bool>, ?msg : String, ?pos : haxe.PosInfos )
116
	{
117
		if( cond == null || cond )
118
			fail( "unimplemented", msg, pos );
119
	}
120
}