<?xml version="1.0"?>
<mx:Application name="lightspark_String_test"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	applicationComplete="appComplete();"
	backgroundColor="white">

<mx:Script>
	<![CDATA[
	import Tests;
	private var callbackArgs:Array;
	private function appComplete():void
	{
		//Match tests
		var str:String = new String("matchablemat");
		var ret:Array = str.match("mat");
		Tests.assertArrayEquals(["mat"], ret, "match(): normal string match", true);
		
		var re:RegExp = new RegExp("m a* t | a[a-z]*e","x")
		ret = str.match(re);
		Tests.assertArrayEquals(["mat"], ret, "match(): non-global extended regexp match", true);
		
		re = new RegExp("MA*T|a[a-z]*e","i")
		ret = str.match(re);
		Tests.assertArrayEquals(["mat"], ret, "match(): non-global case-insensitive regexp match", true);
		
		re = new RegExp("ma*t|a[a-z]*e","")
		ret = str.match(re);
		Tests.assertArrayEquals(["mat"], ret, "match(): non-global regexp match", true);
		
		re = new RegExp("ma*t|a[a-z]*e","g")
		ret = str.match(re);
		Tests.assertArrayEquals(["mat", "able", "mat"], ret, "match(): global regexp match", true);

		re = new RegExp("nomatch", "g");
		ret = str.match(re);
		Tests.assertEquals(0, ret.length, "match(): no match");

		//Replace tests
		str = new String("replaceable");
		var ret2:String = str.replace("l","ll");
		Tests.assertEquals("repllaceable", ret2, "replace(): normal string replace with string", true);
		
		re = new RegExp("(p | b)l", "x")
		ret2 = str.replace(re,"ll");
		Tests.assertEquals("rellaceable", ret2, "replace(): non-global extended regexp replace with string", true);
		
		re = new RegExp("[PB]l", "i")
		ret2 = str.replace(re,"ll");
		Tests.assertEquals("rellaceable", ret2, "replace(): non-global case-insensetive regexp replace with string", true);
		
		re = new RegExp("[pb]l")
		ret2 = str.replace(re,"ll");
		Tests.assertEquals("rellaceable", ret2, "replace(): non-global regexp replace with string", true);

		re = new RegExp("[pb]l","g")
		callbackArgs = new Array();
		ret2 = str.replace(re,func1);
		Tests.assertEquals("refuncaceafunce", ret2, "replace(): non-global regexp replace with function", true);

		Tests.assertEquals(2, callbackArgs.length);
		Tests.assertArrayEquals(["pl", 2, "replaceable"], callbackArgs[0], 
			"replace(): non-global regexp replace with function callback arguments", true);
		Tests.assertArrayEquals(["bl", 8, "replaceable"], callbackArgs[1],
			"replace(): non-global regexp replace with function callback arguments", true);

		re = new RegExp("(([pb])l)","g");
		callbackArgs = new Array();
		ret2 = str.replace(re,func1);
		Tests.assertEquals(2, callbackArgs.length);
		Tests.assertEquals("refuncaceafunce", ret2, "replace(): non-global regexp replace with function", true);
		Tests.assertArrayEquals(["pl","pl","p",2,"replaceable"], callbackArgs[0],
			"replace(): global regexp replace with capturing groups with function callback arguments", false);
		Tests.assertArrayEquals(["bl","bl","b",8,"replaceable"], callbackArgs[1],
			"replace(): global regexp replace with capturing groups with function callback arguments", false);

		ret2 = str.replace(/\s*/g, "-");
		Tests.assertEquals("-r-e-p-l-a-c-e-a-b-l-e-", ret2, "replace(): whitespace", true);

		//Split tests
		str = new String("splittable");
		var ret3:Array = str.split("l");
		Tests.assertArrayEquals(["sp", "ittab", "e"], ret3, "split(): normal string split", true);
		
		re = new RegExp("pl | bl","x")
		ret3 = str.split(re);
		Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing extended regexp split", true);
		
		re = new RegExp("[PB]l","i")
		ret3 = str.split(re);
		Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing case-insensitive regexp split", true);
		
		re = new RegExp("[pb]l","")
		ret3 = str.split(re);
		Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing regexp split", true);
		
		re = new RegExp("[pb](l)","")
		ret3 = str.split(re);
		Tests.assertArrayEquals(["s", "l", "itta", "l", "e"], ret3, "split(): capturing regexp split", true);

		re = new RegExp("^$","")
		ret3 = str.split(re);
		Tests.assertArrayEquals(["splittable"], ret3, "split(): null matching regexp split", true);

		ret3 = str.split("");
		Tests.assertArrayEquals(["s", "p", "l", "i", "t", "t", "a", "b", "l", "e"], ret3, "split(): empty string split", true);

		//Search tests
		str = new String("searchable");
		var ret4:int = str.search("e");
		Tests.assertEquals(1, ret4, "search(): normal string search");

		re = new RegExp("e | a", "x")
		ret4 = str.search(re);
		Tests.assertEquals(1, ret4, "search(): extended regexp search");

		re = new RegExp("[EA]", "i")
		ret4 = str.search(re);
		Tests.assertEquals(1, ret4, "search(): case-insensitive regexp search");

		re = new RegExp("[ea]")
		ret4 = str.search(re);
		Tests.assertEquals(1, ret4, "search(): regexp search");

		re = new RegExp("[ea]", "g")
		ret4 = str.search(re);
		Tests.assertEquals(1, ret4, "search(): global regexp search");

		re = new RegExp("([ea])")
		ret4 = str.search(re);
		Tests.assertEquals(1, ret4, "search(): capturing regexp search");

		//Substring tests
		str = new String("sliceable");
		var ret5:String = str.substr(5);
		Tests.assertEquals("able", ret5, "substr(): start", true);

		ret5 = str.substr(5, 2);
		Tests.assertEquals("ab", ret5, "substr(): start and len", true);

		ret5 = str.substring(5);
		Tests.assertEquals("able", ret5, "substring(): start", true);

		ret5 = str.substring(5, 7);
		Tests.assertEquals("ab", ret5, "substring(): start and end", true);

		ret5 = str.slice(5, 7);
		Tests.assertEquals("ab", ret5, "slice(): positive parameters", true);

		ret5 = str.slice(-4, -2);
		Tests.assertEquals("ab", ret5, "slice(): negative parameters", true);

		//indexOf tests
		str = "abc";
		Tests.assertEquals(str.indexOf("b", -10), 1, "indexOf() negative offset");
		Tests.assertEquals(str.indexOf("b", 3), -1, "indexOf() offset too big");

		//lastIndexOf tests
		var e:String="abcdb";
		Tests.assertEquals(e.lastIndexOf("b"), 4, "lastIndexOf() no offset");
		Tests.assertEquals(e.lastIndexOf("b", 1), 1, "lastIndexOf() positive offset");
		Tests.assertEquals(e.lastIndexOf("b", 0), -1, "lastIndexOf() zero offset");
		Tests.assertEquals(e.lastIndexOf("ab", -100), -1, "lastIndexOf() negative offset (1)");
		Tests.assertEquals(e.lastIndexOf("bc", -100), -1, "lastIndexOf() negative offset (2)");
		Tests.assertEquals(e.lastIndexOf("b", NaN), 4, "lastIndexOf() NaN offset");
		Tests.assertEquals(e.lastIndexOf("b", undefined), 4, "lastIndexOf() undefined offset");
		Tests.assertEquals(e.lastIndexOf("b", 20), 4, "lastIndexOf() too big");

		//charCodeAt tests
		e=String.fromCharCode(128);
		Tests.assertEquals(0x80,e.charCodeAt(0),"charCodeAt");

		//Type conversions
		var mc:MovieClip = new MovieClip();
		Tests.assertEquals(String(NaN), "NaN", "String(NaN)", true);
		Tests.assertEquals(String(3.2), "3.2", "String(3.2)", true);
		Tests.assertEquals(String(3.8), "3.8", "String(3.8)", true);
		Tests.assertEquals(String(true), "true", "String(true)", true);
		Tests.assertEquals(String(false), "false", "String(false)", true);
		Tests.assertEquals(String(-4), "-4", "String(-4)", true);
		Tests.assertEquals(String(null),"null","String(null)",true);
		Tests.assertEquals(String(undefined),"undefined","String(undefined)",true);
		Tests.assertEquals(String(mc),"[object MovieClip]","String(MovieClip)",true);
		var array:Array = ["primary", "secondary", "tertiary"]; 
		Tests.assertEquals(String(array),"primary,secondary,tertiary","String(Array)",true);

		var str1:String = "";
		var str2:String = str1.replace("", "ins");
		Tests.assertEquals("ins", str2, "replace on empty string");

		Tests.report(visual, this.name);
	}
	private function func1():String
	{
		callbackArgs.push(arguments);
		return "func";
	}
	]]>
</mx:Script>

<mx:UIComponent id="visual" />

</mx:Application>
