1
16
17 import org.as2lib.test.unit.AbstractAssertInfo;
18 import org.as2lib.app.exec.Executable;
19 import org.as2lib.util.StringUtil;
20 import org.as2lib.env.reflect.ClassInfo;
21
22
27 class org.as2lib.test.unit.info.AssertThrowsInfo extends AbstractAssertInfo {
28
29
30 private var type;
31
32
33 private var toCall:Executable;
34
35
36 private var args:Array;
37
38
39 private var exception;
40
41
42 private var exceptionThrown:Boolean = false;
43
44
52 public function AssertThrowsInfo(message:String, type, toCall:Executable, args:Array) {
53 super(message);
54 this.type = type;
55 this.toCall = toCall;
56 this.args = args;
57 }
58
59
64 public function execute(Void):Boolean {
65 try {
66
67 toCall.execute.apply(Object(toCall), args);
68 } catch(e) {
69 exception = e;
70 exceptionThrown = true;
71 if(type != null) {
72 return(!(e instanceof type));
73 } else {
74 return false;
75 }
76 }
77 return true;
78 }
79
80
85 private function getFailureMessage(Void):String {
86 var result:String = "assertThrows failed";
87 if(hasMessage()) {
88 result += " with message: "+message;
89 }
90 if(type == null) {
91 result += "!\n No exception thrown - Any exception expected";
92 } else {
93 result += "!\n - Expected exception:\n ";
94 if(typeof type == "function") {
95 result += ClassInfo.forClass(type).getFullName();
96 } else {
97 result += type;
98 }
99 if(exceptionThrown) {
100 result += "\n - Thrown exception:\n"+StringUtil.addSpaceIndent(exception.toString(), 6);
101 } else {
102 result += "\n - No exception thrown.";
103 }
104 }
105 return result;
106 }
107
108
113 private function getSuccessMessage(Void):String {
114 var result:String = "assertThrows executed. ";
115
116 if(typeof type == "function") {
117 result += ClassInfo.forClass(type).getFullName();
118 } else {
119 result += type;
120 }
121
122 result += "was thrown by calling "+toCall.toString()+".";
123
124 return result;
125 }
126 }