// Script with unit tests.

block load
{
	// These are macros used 
	alias ts_init "log on;es_xsetinfo ts_count 0;es_xsetinfo ts_passes 0"
	alias ts_alldone "es_log Tests Passed: server_var(ts_passes) of server_var(ts_count);if (server_var(ts_passes) = server_var(ts_count)) then es_log GREEN GREEN GREEN GREEN GREEN GREEN GREEN;else es_log RED RED RED RED RED RED RED RED RED !!!! RED RED RED"

	alias ts_begin "es_xsetinfo ts_passed 0;es_xmath ts_count + 1"
	alias ts_pass "es_xmath ts_passed + 1;es_xmath ts_passes + 1;es_log Test passed - server_var(ts_count) - server_var(ts_name)"
	alias ts_fail "es_log Test FAILED - server_var(ts_count) - server_var(ts_name)"
	alias ts_end "es_log ."

	// signify that we've loaded
	es_setinfo ts_load 1
	es_doblock unittest/tests
}

block tests
{
	es_doblock unittest/testelse
	es_doblock unittest/testsoon
	es_doblock unittest/testif
	es_doblock unittest/testnewmath
	es_doblock unittest/testregcmd
	es_doblock unittest/testexists
	es_doblock unittest/test_fek
	es_doblock unittest/test_fev

	//es_doblock unittest/test_profile
	es_doblock unittest/test_kgf
}

block test_profile
{
	es_load corelib
	es_setinfo loop_1 0
	while "server_var(loop_1) < 10" "es_xmath loop_1 + 1;es_xdoblock unittest/profile_loop1"
	es_setinfo loop_2 0
	while "server_var(loop_2) < 10" "es_xmath loop_2 + 1;es_xdoblock unittest/profile_loop2"
	es_setinfo loop_3 0
	while "server_var(loop_3) < 10" "es_xmath loop_3 + 1;es_xdoblock unittest/profile_loop3"
	es_setinfo loop_4 0
	while "server_var(loop_4) < 10" "es_xmath loop_4 + 1;es_xdoblock unittest/profile_loop4"

	es_setinfo loop_5 0
	while "server_var(loop_5) < 10" "es_xmath loop_5 + 1;es_xdoblock unittest/profile_ifdo"

	es_setinfo loop_6 0
	while "server_var(loop_6) < 10" "es_xmath loop_6 + 1;es_xdoblock unittest/profile_ifthen"

}

block profile_ifdo
{
	profile begin ifdo
	if (server_var(loop_5) > 5) do
	{
		es_setinfo hello server_var(loop_5)
	}
	profile end ifdo
}

block profile_ifthen
{
	profile begin ifthen
	if (server_var(loop_6) > 5) then es_xsetinfo hello server_var(loop_6)
	profile end ifthen
}


block profile_loop1
{
	profile begin es_setinfo1
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	es_setinfo hello server_var(loop_1)
	profile end es_setinfo1

}

block profile_loop2
{
	profile begin eses_setinfo1
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	es es_xsetinfo hello2 server_var(loop_2)
	profile end eses_setinfo1
}

block profile_loop3
{
	profile begin esxcopy1
	es_xsetinfo hello3 0
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	es_xcopy hello3 loop_3
	profile end esxcopy1
}

block profile_loop4
{
	profile begin esxsetinfo1
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	es_xsetinfo hello2 8
	profile end esxsetinfo1
}

block testsoon
{
	es_soon es_xsetinfo ts_name "Script Addon: soon test"
	es_soon ts_begin
	es_soon es_setinfo ts_tfail 0
	// this will execute immediately
	es_setinfo ts_tfail 1
	es_xsoon if (server_var(ts_tfail) notequalto 0) then ts_fail
	es_xsoon if (server_var(ts_tfail) equalto 0) then ts_pass
	es_soon ts_end
	
}

block testelse
{
	es_xsetinfo ts_name "Script Addon: test else with nested if failure"
	ts_begin
	es_setinfo ts_tfail 1
	if (0 == 0) do
	{
		es_setinfo ts_tfail 0
		if (0 == 1) do
		{
			es_setinfo ts_tfail 1
			
		}
	}
	else do
	{
		es_setinfo ts_tfail 2
	}

	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end


	es_xsetinfo ts_name "Script Addon: test else with nested if success"
	ts_begin
	es_setinfo ts_tfail 1
	if (0 == 0) do
	{
		es_setinfo ts_tfail 1
		if (0 == 0) do
		{
			es_setinfo ts_tfail 0
			
		}
	}
	else do
	{
		es_setinfo ts_tfail 2
	}

	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end


	es_xsetinfo ts_name "Script Addon: test else with initial failure"
	ts_begin
	es_setinfo ts_tfail 1
	if (0 == 1) do
	{
		es_setinfo ts_tfail 2
		if (0 == 0) do
		{
			es_setinfo ts_tfail 3
			
		}
	}
	else do
	{
		es_setinfo ts_tfail 0
	}

	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end

}

block testregcmd
{
	es_regcmd test_command unittest/cmdtest "This is a test command."
	es_setinfo cmdtested 0
	test_command param
	es_xsetinfo ts_name "Script Addon: es_regcmd test"
	ts_begin
	es_setinfo ts_tfail server_var(cmdtested)
	if (server_var(ts_tfail) equalto 0) then ts_fail
	if (server_var(ts_tfail) notequalto 0) then ts_pass
	ts_end

	test_command more than2
	test_command more than 4 commands
	
}

block cmdtest
{
	es_xsetinfo ts_name "Script Addon: es_getargc test1: basic"
	ts_begin
	es_setinfo ts_tfail 0
	es_getargc ts_tfail
	if (server_var(ts_tfail) equalto 0) then ts_fail
	if (server_var(ts_tfail) notequalto 0) then ts_pass
	ts_end
	es_msg es_getargc: server_var(ts_tfail)

	if (server_var(ts_tfail) equalto 3) do
	{
		es_xsetinfo ts_name "Script Addon: es_getargc test2: inside block"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargc ts_tfail
		if (server_var(ts_tfail) notequalto 3) then ts_fail
		if (server_var(ts_tfail) equalto 3) then ts_pass
		ts_end

		test_command more than 3

		es_xsetinfo ts_name "Script Addon: es_getargc test4: validate original argcount"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargc ts_tfail
		if (server_var(ts_tfail) notequalto 3) then ts_fail
		if (server_var(ts_tfail) equalto 3) then ts_pass
		ts_end
		es_msg es_getargc: server_var(ts_tfail)

	}

	if (server_var(ts_tfail) equalto 4) do
	{
		es_xsetinfo ts_name "Script Addon: es_getargc test3: reentrant"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargc ts_tfail
		if (server_var(ts_tfail) notequalto 4) then ts_fail
		if (server_var(ts_tfail) equalto 4) then ts_pass
		ts_end
	}


	if (server_var(ts_tfail) equalto 5) do
	{
		es_regcmd test_command2 unittest/cmdtest2 "This is a test command2."
		es_setinfo testloop 0
		test_command2 param
		es_xsetinfo ts_name "Script Addon: es_regcmd test5: regcmd within regcmd"
		ts_begin
		es_setinfo ts_tfail server_var(testloop)
		if (server_var(ts_tfail) notequalto 6) then ts_fail
		if (server_var(ts_tfail) equalto 6) then ts_pass
		ts_end

		es_xsetinfo ts_name "Script Addon: es_getargs test1"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargs ts_tfail
		es_msg "---" server_var(ts_tfail) "---"
		if (server_var(ts_tfail) notequalto "more than 4 commands") then ts_fail
		if (server_var(ts_tfail) equalto "more than 4 commands") then ts_pass
		ts_end

		es_xsetinfo ts_name "Script Addon: es_getargv test1: argv(0)"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargv ts_tfail 0
		es_msg "---" server_var(ts_tfail) "---"
		if (server_var(ts_tfail) notequalto "test_command") then ts_fail
		if (server_var(ts_tfail) equalto "test_command") then ts_pass
		ts_end

		es_xsetinfo ts_name "Script Addon: es_getargv test2: argv(1)"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargv ts_tfail 1
		es_msg "---" server_var(ts_tfail) "---"
		if (server_var(ts_tfail) notequalto "more") then ts_fail
		if (server_var(ts_tfail) equalto "more") then ts_pass
		ts_end

	}
	
	es_setinfo cmdtested 1
}

block cmdtest2
{
	es_xmath testloop + 1
	if (server_var(testloop) < 6) do
	{
		es test_command2 server_var(testloop)
	}
	else do
	{
		es_xsetinfo ts_name "Script Addon: es_getargv test0: two functions deep"
		ts_begin
		es_setinfo ts_tfail 0
		es_getargv ts_tfail 0
		es_msg "---" server_var(ts_tfail) "---"
		if (server_var(ts_tfail) equalto "test_command") then ts_fail
		if (server_var(ts_tfail) notequalto "test_command") then ts_pass
		ts_end
	}
}

block testnewmath
{
	es_xsetinfo ts_name "Script Addon: es_math atan test"
	ts_begin
	es_setinfo ts_tfail 3.00
	es_math ts_tfail atan
	es_msg atan: server_var(ts_tfail)
	if (server_var(ts_tfail) notequalto 1.249046) then ts_fail
	if (server_var(ts_tfail) equalto 1.249046) then ts_pass
	ts_end	

	es_xsetinfo ts_name "Script Addon: es_math acos test"
	ts_begin
	es_setinfo ts_tfail 0.5
	es_math ts_tfail acos
	es_msg acos: server_var(ts_tfail)
	if (server_var(ts_tfail) notequalto 1.047198) then ts_fail
	if (server_var(ts_tfail) equalto 1.047198) then ts_pass
	ts_end	

	es_xsetinfo ts_name "Script Addon: es_math asin test"
	ts_begin
	es_setinfo ts_tfail 0.5
	es_math ts_tfail asin
	es_msg asin: server_var(ts_tfail)
	if (server_var(ts_tfail) notequalto 0.523599) then ts_fail
	if (server_var(ts_tfail) equalto 0.523599) then ts_pass
	ts_end	

	es_xsetinfo ts_name "Script Addon: es_math int test"
	ts_begin
	es_setinfo ts_tfail 1.5
	es_math ts_tfail int
	es_msg int: server_var(ts_tfail)
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end	

}


block testexists
{
	es_xsetinfo ts_name "Script Addon: es_exists test: variable"
	ts_begin
	es_setinfo ts_tfail 0
	es_setinfo this_variable_exists 0
	es_exists ts_tfail variable this_variable_exists
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end	

	es_xsetinfo ts_name "Script Addon: es_exists test2: variable"
	ts_begin
	es_setinfo ts_tfail -1
	es_exists ts_tfail variable this_variable_does_not_exist
	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end	

	es_xsetinfo ts_name "Script Addon: es_exists test: cmd"
	ts_begin
	es_setinfo ts_tfail 0
	es_regcmd test_exists unittest/testexists "This command exists."
	es_exists ts_tfail command test_exists
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test2: cmd"
	ts_begin
	es_setinfo ts_tfail -1
	es_exists ts_tfail command test_no_exists
	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test: keygroup"
	ts_begin
	es_setinfo ts_tfail 0
	es_keygroupcreate KEYGROUP_EXISTS
	es_exists ts_tfail keygroup KEYGROUP_EXISTS
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test2: keygroup"
	ts_begin
	es_setinfo ts_tfail -1
	es_keygroupdelete KEYGROUP_EXISTS
	es_exists ts_tfail keygroup KEYGROUP_EXISTS
	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test: key"
	ts_begin
	es_setinfo ts_tfail 0
	es_keygroupcreate test_keygroup
	es_keycreate test_keygroup key_exists1
	es_exists ts_tfail key test_keygroup key_exists1
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test2: key"
	ts_begin
	es_setinfo ts_tfail -1
	es_keydelete test_keygroup key_exists1
	es_exists ts_tfail key test_keygroup key_exists1
	if (server_var(ts_tfail) notequalto 0) then ts_fail
	if (server_var(ts_tfail) equalto 0) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_exists test3: key with no keygroup"
	ts_begin
	es_setinfo ts_tfail 0
	es_keycreate key_exists
	es_exists ts_tfail key key_exists
	if (server_var(ts_tfail) notequalto 1) then ts_fail
	if (server_var(ts_tfail) equalto 1) then ts_pass
	ts_end

}

block test_fek
{
	// run the es_foreachkey tests
	es_xsetinfo ts_name "Script Addon: es_foreachkey nest tests"
	ts_begin
	es_setinfo fekcount 0
	es_keygroupcreate fek
	es_keycreate fek test
	es_keysetvalue fek test value 1
	es_keycreate fek test2
	es_keysetvalue fek test2 value 2
	es_keycreate fek test3
	es_keysetvalue fek test3 value 3
	es_keycreate fek test4
	es_keysetvalue fek test4 value 4
	es_keygroupcreate fek2
	es_keycreate fek2 test
	es_keysetvalue fek2 test value 1
	es_keycreate fek2 test2
	es_keysetvalue fek2 test2 value 2
	es_keycreate fek2 test3
	es_keysetvalue fek2 test3 value 3
	es_keycreate fek2 test4
	es_keysetvalue fek2 test4 value 4
	es_keycreate fek2 test5
	es_keysetvalue fek2 test5 value 5
	es_foreachkey myvar in fek "es_xdoblock unittest/test_fek2"
	// 4 on top and 100 internally
	if (server_var(fekcount) == 104) then ts_pass
	if (server_var(fekcount) != 104) then ts_fail
	ts_end

}

block test_fek2
{
	es_xmath fekcount + 1
	es_foreachkey myvar2 in fek2 "es_xdoblock unittest/test_fek3"
}

block test_fek3
{
	es_xmath fekcount + 1
	es_foreachkey myvar3 in fek "es_xmath fekcount + 1"
}

block test_fev
{
	// run the es_foreachval tests
	es_xsetinfo ts_name "Script Addon: es_foreachval nest tests"
	ts_begin
	es_setinfo fevcount 0
	es_keygroupcreate fev
	es_keycreate fev test
	es_keysetvalue fev test value 1
	es_keysetvalue fev test value1 2
	es_keysetvalue fev test value2 3
	es_keysetvalue fev test value3 4
	es_keycreate fev test2
	es_keysetvalue fev test2 valueA A
	es_keysetvalue fev test2 valueB B
	es_keysetvalue fev test2 valueC C
	es_keysetvalue fev test2 valueD D
	es_keysetvalue fev test2 valueE E
	es_keycreate fev test3
	es_keysetvalue fev test3 valueQ Q
	es_keysetvalue fev test3 valueR R
	es_keysetvalue fev test3 valueS S
	es_foreachval myvar in fev test "es_xdoblock unittest/test_fev2"
	// 4 on top and 80 internally
	es_log server_var(fevcount)
	if (server_var(fevcount) == 84) then ts_pass
	if (server_var(fevcount) != 84) then ts_fail
	ts_end
}

block test_fev2
{
	es_xmath fevcount + 1
	es_log server_var(myvar)
	es_foreachval myvar2 in fev test2 "es_xdoblock unittest/test_fev3"
}

block test_fev3
{
	es_xmath fevcount + 1
	es_log server_var(myvar2)
	es_foreachval myvar3 in fev test3 "es_xmath fevcount + 1"
}

block test_kgf
{
	es_xsetinfo ts_name "Script Addon: es_keygroupfilter test: only"
	ts_begin
	es_setinfo ts_tfail 0
	es_setinfo ts_texists 0
	es_keygroupcreate KEYGROUP_FILTER
	es_keycreate KEYGROUP_FILTER test
	es_keysetvalue KEYGROUP_FILTER test valueA A
	es_keysetvalue KEYGROUP_FILTER test valueB A
	es_keysetvalue KEYGROUP_FILTER test valueC C
	es_keycreate KEYGROUP_FILTER test2
	es_keysetvalue KEYGROUP_FILTER test2 valueB A
	es_keysetvalue KEYGROUP_FILTER test2 valueC C
	es_keycreate KEYGROUP_FILTER test3
	es_keysetvalue KEYGROUP_FILTER test3 valueB R
	es_keysetvalue KEYGROUP_FILTER test3 valueC C
	es_keycreate KEYGROUP_FILTER test4
	es_keycreate KEYGROUP_FILTER test5
	profile begin filter1
	es_keygroupfilter KEYGROUP_FILTER only valueB A
	profile end filter1
	es_exists ts_texists key KEYGROUP_FILTER test
	if (server_var(ts_texists) equalto 0) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test2
	if (server_var(ts_texists) equalto 0) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test3
	if (server_var(ts_texists) equalto 1) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test4
	if (server_var(ts_texists) equalto 1) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test5
	if (server_var(ts_texists) equalto 1) then es_xsetinfo ts_tfail 1
	es_keygroupdelete KEYGROUP_FILTER

	if (server_var(ts_tfail) equalto 1) then ts_fail
	if (server_var(ts_tfail) notequalto 1) then ts_pass
	ts_end

	es_xsetinfo ts_name "Script Addon: es_keygroupfilter test: not"
	ts_begin
	es_setinfo ts_tfail 0
	es_setinfo ts_texists 0
	es_keygroupcreate KEYGROUP_FILTER
	es_keycreate KEYGROUP_FILTER test
	es_keysetvalue KEYGROUP_FILTER test valueA A
	es_keysetvalue KEYGROUP_FILTER test valueB A
	es_keysetvalue KEYGROUP_FILTER test valueC C
	es_keycreate KEYGROUP_FILTER test2
	es_keysetvalue KEYGROUP_FILTER test2 valueB A
	es_keysetvalue KEYGROUP_FILTER test2 valueC C
	es_keycreate KEYGROUP_FILTER test3
	es_keysetvalue KEYGROUP_FILTER test3 valueB R
	es_keysetvalue KEYGROUP_FILTER test3 valueC C
	es_keycreate KEYGROUP_FILTER test4
	es_keycreate KEYGROUP_FILTER test5
	profile begin filter2
	es_keygroupfilter KEYGROUP_FILTER not valueB A
	profile end filter2

	es_exists ts_texists key KEYGROUP_FILTER test
	if (server_var(ts_texists) equalto 1) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test2
	if (server_var(ts_texists) equalto 1) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test3
	if (server_var(ts_texists) equalto 0) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test4
	if (server_var(ts_texists) equalto 0) then es_xsetinfo ts_tfail 1
	es_exists ts_texists key KEYGROUP_FILTER test5
	if (server_var(ts_texists) equalto 0) then es_xsetinfo ts_tfail 1
	es_keygroupdelete KEYGROUP_FILTER

	if (server_var(ts_tfail) equalto 1) then ts_fail
	if (server_var(ts_tfail) notequalto 1) then ts_pass
	ts_end

}