1// Boolean_as.hx: ActionScript 3 "Boolean" class, for Gnash.
2//
3//   Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software
4//   Foundation, Inc
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19//
20
21// Test case for Boolean ActionScript class
22// Migrated to haXe by Jonathan Crider
23
24#if flash9
25import flash.display.MovieClip;
26#else
27import flash.MovieClip;
28#end
29
30import flash.Lib;
31import Type;
32import Reflect;
33import Std;
34
35// importing our testing api
36import DejaGnu;
37
38class Boolean_as {
39	static function main() {
40
41	//NOTE: These tests do not compile in swf version 9. This is due to the way
42	//     in which haxe implements a Bool. Compilation in swf9 gives the error
43	//     'Unbound variable Boolean' at each place the Boolean name is used.
44	//     So far I have been unable to find a workaround for this. For now we
45	//     will be unable to compile these test for flash9.
46	#if flash9
47	DejaGnu.note("These tests do not currently compile in flash version 9: see comments in Boolean_as.hx");
48
49	#else
50
51	//check_equals(typeof(Boolean), 'function');
52	if ( untyped __typeof__(Boolean) == 'function') {
53			DejaGnu.pass("Boolean class exists");
54    } else {
55			DejaGnu.fail("Boolean class does not exist");
56    }
57	//check_equals(typeof(Boolean()), 'undefined');
58	if ( untyped __typeof__(Boolean()) == 'undefined') {
59		DejaGnu.pass("Call to Boolean() returns null");
60	} else {
61		DejaGnu.fail("Call to Boolean() does not return null");
62	}
63	//check_equals(typeof(Boolean(true)), 'boolean');
64	if ( untyped __typeof__(Boolean(true)) == 'boolean') {
65		DejaGnu.pass("Boolean(true) correctly returns a boolean value");
66	} else {
67		DejaGnu.fail("Boolean(true) does not return a boolean value");
68	}
69	//check_equals(typeof(new Boolean()), 'object');
70	if (Reflect.isObject(untyped __new__(Boolean))) {
71		DejaGnu.pass("new Boolean(); correctly constructs an object");
72	} else {
73		DejaGnu.fail("new Boolean(); does not correctly construct an object");
74	}
75
76	//var boolObj = new Boolean;
77	var boolObj = untyped __new__(Boolean);
78
79	// test the Boolean constuctor
80	//check (boolObj);
81	if (boolObj	) {
82		DejaGnu.pass("Boolean object successfully constructed and assigned");
83	} else {
84		DejaGnu.fail("Boolean object not successfully constructed or assigned");
85	}
86
87	//check (boolObj.toString);
88	if (untyped boolObj.toString) {
89		DejaGnu.pass("boolObj.toString() inherited correctly");
90	} else {
91		DejaGnu.fail("boolObj.toString() was not inherited correctly");
92	}
93	//check (boolObj.valueOf);
94	if (untyped boolObj.valueOf) {
95		DejaGnu.pass("boolObj.valueOf() inherited correctly");
96	} else {
97		DejaGnu.fail("boolObj.valueOf() not inherited correctly");
98	}
99
100
101	#if flash6
102	// flash6 is not case sensitive
103	//check (boolObj.tostring)
104	if (untyped boolObj.tostring) {
105		DejaGnu.pass("boolObj.tostring property exists (Not Case Sensitive)");
106	} else {
107		DejaGnu.fail("boolObj.tostring property does not exist");
108	}
109	//check (boolObj.valueof)
110	if (untyped boolObj.valueof) {
111		DejaGnu.pass("boolObj.valueof property exists (Not Case Sensitive)");
112	} else {
113		DejaGnu.fail("boolObj.valueof property does not exist");
114	}
115	#else
116	// SWF7 and up is case-sensitive
117	//check_equals (boolObj.tostring, undefined);
118	if (Type.typeof(untyped boolObj.tostring) == ValueType.TNull) {
119		DejaGnu.pass("tostring property does not exist (Case Sensitive)");
120	} else {
121		DejaGnu.fail("tostring property exists when it should not");
122	}
123	//check_equals (boolObj.valueof, undefined);
124	if (Type.typeof(untyped boolObj.valueof) == ValueType.TNull) {
125		DejaGnu.pass("valueof property does not exist (Case Sensitive)");
126	} else {
127		DejaGnu.fail("valueof property exists when it should not");
128	}
129	#end
130
131	//var defaultBool = new Boolean();
132	var defaultBool = untyped __new__(Boolean);
133	//check_equals(defaultBool.toString(), "false");
134	if (untyped defaultBool.toString() == "false") {
135		DejaGnu.pass("Default constructor correctly sets value to false");
136	} else {
137		DejaGnu.fail("Default constructor does not set value to false");
138	}
139	//check_equals(defaultBool.valueOf(), false);
140	if (untyped defaultBool.valueOf() == false) {
141		DejaGnu.pass("Default valueOf() correctly returns false");
142	} else {
143		DejaGnu.fail("Default valueOf() does not return false");
144	}
145
146	//var trueBool = new Boolean(true);
147	var trueBool = untyped __new__(Boolean, true);
148	//check_equals(trueBool.toString(), "true");
149	if (untyped trueBool.toString() == "true") {
150		DejaGnu.pass("Correctly constructed Boolean with value 'true'");
151	} else {
152		DejaGnu.fail("Did not correctly construct Boolean with value 'true'");
153	}
154	//check_equals(trueBool.valueOf(), true);
155	if (untyped trueBool.valueOf() == true) {
156		DejaGnu.pass("trueBool.valueOf() correctly returned true");
157	} else {
158		DejaGnu.fail("trueBool.valueOf() did not correctly return true");
159	}
160
161	//var falseBool = new Boolean(false);
162	var falseBool = untyped __new__(Boolean, false);
163	//check_equals(falseBool.toString(), "false");
164	if (untyped falseBool.toString() == "false") {
165		DejaGnu.pass("Boolean correctly constructed with argument 'false'");
166	} else {
167		DejaGnu.fail("Boolean not correctly constructed with argument 'false'");
168	}
169	//check_equals(falseBool.valueOf(), false);
170	if (untyped falseBool.valueOf() == false) {
171		DejaGnu.pass("falseBool.valueOf() correctly returned false");
172	} else {
173		DejaGnu.fail("falseBool.valueOf() did not correctly return false");
174	}
175
176
177	//---------------------------------------------------
178	// Test convertion to boolean
179	//---------------------------------------------------
180	DejaGnu.note("*** Begin testing convertion to Boolean");
181
182	// boolean
183	//check( true );
184	if (true) {
185		DejaGnu.pass("keyword 'true' correctly evaluates to true");
186	} else {
187		DejaGnu.fail("keyword 'true' does not correctly evaluate to true");
188	}
189	//check( ! false );
190	if ( ! false ) {
191		DejaGnu.pass("expression '! false' correctly evaluates to true");
192	} else {
193		DejaGnu.fail("expression '! false' did not evaluate to true");
194	}
195
196	// number
197	//check( 1 );
198	if (untyped 1) {
199		DejaGnu.pass("expression '1' correctly evaluates to true");
200	} else {
201		DejaGnu.fail("expression '1' did not correctly evaluate to true");
202	}
203
204	if (untyped 0) {
205		DejaGnu.fail("expression '0' evaluated to true");
206	} else {
207		DejaGnu.pass("expression '0' evaluated to false");
208	}
209	//check( !0 );
210	if ( untyped !0 ) {
211		DejaGnu.pass("expression '!0' correctly evaluates to true");
212	} else {
213		DejaGnu.fail("expression '!0' did not evaluate to true");
214	}
215
216	// movieclip
217	//check( _root );
218	if (untyped flash.Lib.current) {
219		DejaGnu.pass("_root; (flash.lib.current in haxe) evaluated true");
220	} else {
221		DejaGnu.fail("_root; (flash.lib.current in haxe) evaluated false");
222	}
223
224	// string
225	//check( "1" );
226	if (untyped "1") {
227		DejaGnu.pass("String expression '1' evaluated true");
228	} else {
229		DejaGnu.fail("String expression '1' did not evaluate true");
230	}
231	//#if OUTPUT_VERSION < 7
232	#if flash6
233	//check( ! "0" );
234	if ( untyped !"0" ) {
235		DejaGnu.pass("string expression !'0' evaluated true");
236	} else {
237		DejaGnu.fail("string expression !'0' did not evaluate true");
238	}
239	//check( ! "true" );
240	if ( untyped !"true") {
241		DejaGnu.pass("string expression !'true' evaluated true");
242	} else {
243		DejaGnu.fail("string expression !'true' did not evaluate true");
244	}
245	//check( ! "false" );
246	if ( untyped !"false") {
247		DejaGnu.pass("string expression !'false' evaluated true");
248	} else {
249		DejaGnu.fail("string expression !'false' did not evaluate true");
250	}
251	#else
252	//check( "0" );
253	if ( untyped "0" ) {
254		DejaGnu.pass("string expression '0' evaluated true");
255	} else {
256		DejaGnu.fail("string expression '0' did not evaluate true");
257	}
258	//check( "true" );
259	if ( untyped "true" ) {
260		DejaGnu.pass("string expression 'true' evaluated true");
261	} else {
262		DejaGnu.fail("string expression 'true' did not evaluate true");
263	}
264	//check( "false" );
265	if ( untyped "false" ) {
266		DejaGnu.pass("string expression 'false' evaluated true");
267	} else {
268		DejaGnu.fail("string expression 'false' did not evaluate true");
269	}
270	//#endif
271	#end
272
273	// Null
274	//check_equals(typeOf(null), "null" );
275	if (untyped __typeof__(null) == "null") {
276		DejaGnu.pass("typeof null is null");
277	} else {
278		DejaGnu.fail("typeof null is not null");
279	}
280	//check( ! null );
281	if ( ! null ) {
282		DejaGnu.pass("expression '!null' evaluates to true");
283	} else {
284		DejaGnu.fail("expression '!null' did not evaluate to true");
285	}
286
287	// Undefined
288	//check( ! undefined );
289	if ( !(untyped undefined) ) {
290		DejaGnu.pass("expression '! undefined' evaluates to true");
291	} else {
292		DejaGnu.fail("expression '! undefined' did not evaluate to true");
293	}
294
295	// Function
296	//var playfunc = untyped __global__["play"];
297	//check( play );
298	if ( untyped play ) {
299		DejaGnu.pass("Global function play evaluates to true");
300	} else {
301		DejaGnu.fail("Global function play does not evaluate to true");
302	}
303
304	// Object - conversion might depend on object type
305	//emptyArray = new Array();
306	var emptyArray = untyped __new__(Array);
307	//check( emptyArray );
308	if ( emptyArray ) {
309		DejaGnu.pass("emptyArray object evaluates to true");
310	} else {
311		DejaGnu.fail("emptyArray object does not evaluate to true");
312	}
313
314	#end //end if !flash9
315
316
317	//NOTE: may need to retain Ming tests at the end of the file somehow
318
319	DejaGnu.done();
320	}
321}
322
323//NOTE: Haxe does not give acces to the Boolean class directly.
324//      In haXe Bool is an Enum value and does all the processing in the
325//      background.
326
327