# How to use this expect-lite file, Lines that begin with:
#	'>' send to remote host, implies "wait for prompt"
#	'<' _MUST_ be received from the remote host, or this config script will fail
#	# are comment lines, and have no effect
#	; are printable (in stdout) comments, and have no other effect
#	@ change the expect timeout value
#	! Embedded Expect commands
#	? If statement, use format ?cond?action::else_action
# For more info see: expect-lite.html

#
#	test of Conditionals implemented in expect-lite
#	and self tested (where possible)


# Turn on warnings
!set ::WARN 1

; ==== Set Timeout ====
@4

; ==== Static Varible Tests ====
$name=craig
$telephone=723-9161
$var1=var1
$var2=var2
$age=25
$blank=

>
; === Conditional Equals	? $name == craig ?>echo "hello123"
? $name == craig ?>echo "hello123"
<hello123
>
>echo $name $telephone

; === Conditional Not-Equals	? $name!=$telephone?; === NE; Print var name:$name
>
? $name!=$telephone?; === NE; Print var name:$name

%dont_echo

#>echo "here" 

; === Conditional new var assignment	? $name != $telephone ?  $a=$telephone
? $name != $telephone ?  $a=$telephone

>echo "Print var a:$a "
<Print var a:723-9161


>
>
; === Conditional var re-assignment	?$name!=$telephone?$var1=$name
>
?$name!=$telephone?$var1=$name
>
>echo "Print var var1:$var1 "
<Print var var1:craig
>
; === Compare numbers larger or equal	?$age <= 25 ? >echo "new age"
?$age <= 25 ? >echo "new age"
<new age

; === Compare numbers	?$age < 30 ? >echo "old age"
?$age<30 ? >echo "old age"
<old age

; === Bad operator numbers	?$age % 30 ? >echo "Warning"
?$age % 30 ? >echo "bad stuff"
>
>

; === Compare numbers with else (and spaces)	?$age > 30 ? >echo "old age" :: > echo "new age"
? $age > 30 ? >echo "old age" :: > echo "new age"
<new age

; === Compare numbers with else	?30>$age? >echo "old age" :: > echo "new age"
?30>$age?>echo "old age"::> echo "new age"
#<old age
>

$check_ext=true
; === Capture with expect	? $check_ext == true ?+$module=^([a-z]+).+ext3
>
>lsmod
?$check_ext==true?+$module=^([a-z]+).+ext3
>
; === show capture $module
>

; === Capture with expect and colons
$check_ext=true
>cat /etc/passwd | grep nobody
? $check_ext == true ?+$module1=:[0-9]+:[0-9]+:([A-Za-z]+):       :: ; ==== or not
>echo $module1
<[Nn]obody

; === Reassign vars
$age=35
; === new age is $age
>
>echo "55"
+$age=\n([0-9]+)

?$age>=55?>echo "Freedom at $age"::>echo "Keep on working"
<Freedom at $age

; === Test conditional jump
?$age==55?%SKIP2_AGE55
>echo " Skipper"
>echo "2"
>echo "3"
%SKIP2_AGE55

; === Test conditional jump with label with spaces, and space at end of label
?$age==55?%move along, nothing to see
>echo " Skipper"
>echo "2"
>echo "3"
*FAIL
%move along, nothing to see  


; === Test conditional jump with indentation
?$age!=55?%move along, nothing to see
	>
	>echo " Skipper"
	>echo "2"
	>echo "3"
	<3
	>
%move along, nothing to see

; === Test conditional include
?$age==55?~test_include.txt

>
>echo "Pau"
<Pau



$empty=0
>
? $empty == 0?; Empty is |$empty |

$a=a
$b=a
?if $a!=$b ?>echo "$a $b"

$name=Joe Blow
?if$name==Joe Blow?>echo "Found $name"
<Found $name

; === Testing that var1 is equal to var2
?IF $var1 == craig ? %CONT ::! _el_fail_test
%CONT

# reproduce bug, where eval is false, but -s fouls up expect-lite
$PID=23682
; === Test using a flag in cond action statement
?IF no == craig ? >kill -s INT $PID
>

; === Test making assignments in action and else_action
?IF no == craig ? $var1=$name :: $var2=$age
> echo $var1 $var2
<craig 55
$var2=Zero
?IF $name != no ?$var1=TRUE::$var2=FALSE
>echo $var1 $var2
<TRUE Zero


; === Test making assignments in only else_action
?IF no == craig ? >echo "not here" :: $var2=$telephone
-<here
> echo $var1 $var2
<TRUE 723-9161

; === Test nesting of ifs
?IF $name == craig ? >echo "not here" :: ? $var1 == TRUE ? >echo "nested ifs"
<nested ifs
>

; === issue ^C from if statement
$one=1
>time sleep 3
?IF $one != 0 ? >^C
-<command not found
>

; === issue simple commmand as action
$one=1
?IF$one!=0?>pwd
-<command not found
>

; === Compare numbers with else	?30>$age? >echo "old age" :: > echo "new age"
?IF30>$age?>echo "old age"::>echo "new age"
<new age
>



; === Test skip to non-existant label
?IF $name == Joe Blow ? %SKIP_TO_NOWHERE
>echo "skip this line"
#self check
<skip


$age=5
$loop2=10

; ======== Decrementing ZZ Loop with embedded expect ============

# do a loop!
# hint label has tab at end, ensure white space is trimmed at end of labels
%REPEAT_LOOP	

	! incr user_namespace(age) -1
	>
	; == age=$age
	> echo ZZ$age
	# self check, ZZ should never go to -1
	-<ZZ-1

?if $age > 0 ?%REPEAT_LOOP

# check that above loop worked
?if $age > 0? *FAIL
>


>echo "Pau"
>
; ======== Decrementing YY Loop =============
# do another loop!
# Keep loop labels UNIQUE!
%REPEAT_LOOP1

	# decrement variable
	-$loop2
	>
	; == loop2=$loop2
	# stop the run away loop
	> echo $loop2
	# self check, loop2 should never go to -1
	-<-1

?if $loop2 > 0 ?%REPEAT_LOOP1


; ======== Incrementing Loop ========
$max=10
$count=3
%REPEAT_LOOP2

	# increment variable
	+$count
	# stop the run away loop
	>echo $count
	# self check, loop2 should never go to 12
	-<12

?if $count <= $max ?%REPEAT_LOOP2

>
; ======= Test incrementing string ======
$junk=test
+$junk
; === junk is $junk
# self check
?if $junk != test ? >echo FAIL :: >echo Good
<\nGood
>

; === Test conditional undefined
?$undef_var==$blank?>echo Good :: >echo FAIL
<\nGood
>
?$undef_var!=$blank?>echo FAIL :: >echo Good
<\nGood
>
?$undef_var==?>echo Good :: >echo FAIL
<\nGood
>

#*INTERACT

; ==== All Pau!
>
#Pau!


