#!/bin/bash # -*- shell-script -*- proc test_eval_subst { # Set up necessary vars typeset extracted typeset -a texts global texts := '( ''if (( x == 1 )); then' 'if (( y == 2 )) ; then' 'if (( y == 3 )) ;' 'if (( y == 4 ))' 'if (( z == 5 )) ' 'return something' 'elif [[ $x = test1 ]] && [ $? -eq 0 ] ; then' 'while [[ $x = test2 ]] && [ $? -eq 0 ] ; do' 'while [[ $x = test3 ]] && [ $? -eq 0 ]' ) global expected := '( ''(( x == 1 ))' '(( y == 2 )) ' '(( y == 3 )) ' '(( y == 4 ))' '(( z == 5 )) ' 'echo something' '[[ $x = test1 ]] && [ $? -eq 0 ] ' '[[ $x = test2 ]] && [ $? -eq 0 ] ' '[[ $x = test3 ]] && [ $? -eq 0 ]' ) typeset -i i for (( i=0 ; i<${#expected[@]}; i++ )) ; do _Dbg_eval_extract_condition "${texts[i]}" assertEquals "${expected[i]}" "$extracted" done global assign := ''foo=bar'' global pat := ''^[ \t]*[A-Za-z_][A-Za-z_0-9[]*[]-+]?=(.*$)'' if [[ $assign =~ $pat ]] { global texts := '( ''x=10' 'array[1]=20' '__fn__+=30' 'PROFILES="/etc/apparmor.d"' ) global expected := '( ''echo 10' 'echo 20' 'echo 30' 'echo "/etc/apparmor.d"' ) for (( i=0 ; i<${#expected[@]}; i++ )) ; do _Dbg_eval_extract_condition "${texts[i]}" assertEquals "${expected[i]}" "$extracted" done } } global abs_top_srcdir := '/src/external-vcs/sourceforge/bashdb' # Make sure $abs_top_srcrdir has a trailing slash global abs_top_srcdir := "$(abs_top_srcdir%%/)/" source ${abs_top_srcdir}test/unit/helper.sh source $abs_top_srcdir/lib/fns.sh set -- # reset $# so shunit2 doesn't get confused. [[ $0 == ${BASH_SOURCE} ]] && source ${shunit_file} (CommandList children: [ (FuncDef name: test_eval_subst body: (BraceGroup children: [ (C {(typeset)} {(extracted)}) (C {(typeset)} {(-a)} {(texts)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:texts) op: Equal rhs: { (ArrayLiteralPart words: [ {(SQ <"if (( x == 1 )); then">)} {(SQ <"if (( y == 2 )) ; then">)} {(SQ <"if (( y == 3 )) ;">)} {(SQ <"if (( y == 4 ))">)} {(SQ <"if (( z == 5 )) ">)} {(SQ <"return something">)} {(SQ <"elif [[ $x = test1 ]] && [ $? -eq 0 ] ; then">)} {(SQ <"while [[ $x = test2 ]] && [ $? -eq 0 ] ; do">)} {(SQ <"while [[ $x = test3 ]] && [ $? -eq 0 ]">)} ] ) } spids: [30] ) ] spids: [30] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:expected) op: Equal rhs: { (ArrayLiteralPart words: [ {(SQ <"(( x == 1 ))">)} {(SQ <"(( y == 2 )) ">)} {(SQ <"(( y == 3 )) ">)} {(SQ <"(( y == 4 ))">)} {(SQ <"(( z == 5 )) ">)} {(SQ <"echo something">)} {(SQ <"[[ $x = test1 ]] && [ $? -eq 0 ] ">)} {(SQ <"[[ $x = test2 ]] && [ $? -eq 0 ] ">)} {(SQ <"[[ $x = test3 ]] && [ $? -eq 0 ]">)} ] ) } spids: [84] ) ] spids: [84] ) (C {(typeset)} {(-i)} {(i)}) (ForExpr init: (BinaryAssign op_id: Arith_Equal left: (LhsName name:i) right: (ArithWord w:{(Lit_Digits 0)}) ) cond: (ArithBinary op_id: Arith_Less left: (ArithVarRef name:i) right: (ArithWord w: { (BracedVarSub token: prefix_op: VSub_Pound bracket_op: (WholeArray op_id:Lit_At) spids: [155 161] ) } ) ) update: (UnaryAssign op_id:Node_PostDPlus child:(LhsName name:i)) body: (DoGroup children: [ (C {(_Dbg_eval_extract_condition)} { (DQ (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithVarRef name:i)) spids: [178 183] ) ) } ) (C {(assertEquals)} { (DQ (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithVarRef name:i)) spids: [190 195] ) ) } {(DQ ($ VSub_Name "$extracted"))} ) ] spids: [172 203] ) ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:assign) op: Equal rhs: {(SQ <"foo=bar">)} spids: [206] ) ] spids: [206] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:pat) op: Equal rhs: {(SQ <"^[ \\t]*[A-Za-z_][A-Za-z_0-9[]*[]-+]?=(.*$)">)} spids: [212] ) ] spids: [212] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_EqualTilde left: {($ VSub_Name "$assign")} right: {($ VSub_Name "$pat")} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:texts) op: Equal rhs: { (ArrayLiteralPart words: [ {(SQ <"x=10">)} {(SQ <"array[1]=20">)} {(SQ <"__fn__+=30">)} {(SQ <"PROFILES=\"/etc/apparmor.d\"">)} ] ) } spids: [235] ) ] spids: [235] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:expected) op: Equal rhs: { (ArrayLiteralPart words: [ {(SQ <"echo 10">)} {(SQ <"echo 20">)} {(SQ <"echo 30">)} {(SQ <"echo \"/etc/apparmor.d\"">)} ] ) } spids: [263] ) ] spids: [263] ) (ForExpr init: (BinaryAssign op_id: Arith_Equal left: (LhsName name:i) right: (ArithWord w:{(Lit_Digits 0)}) ) cond: (ArithBinary op_id: Arith_Less left: (ArithVarRef name:i) right: (ArithWord w: { (BracedVarSub token: prefix_op: VSub_Pound bracket_op: (WholeArray op_id:Lit_At) spids: [302 308] ) } ) ) update: (UnaryAssign op_id:Node_PostDPlus child:(LhsName name:i)) body: (DoGroup children: [ (C {(_Dbg_eval_extract_condition)} { (DQ (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithVarRef name:i)) spids: [325 330] ) ) } ) (C {(assertEquals)} { (DQ (BracedVarSub token: bracket_op: (ArrayIndex expr:(ArithVarRef name:i)) spids: [337 342] ) ) } {(DQ ($ VSub_Name "$extracted"))} ) ] spids: [319 350] ) ) ] spids: [-1 232] ) ] spids: [-1 353] ) ] spids: [11] ) spids: [7 10] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:abs_top_srcdir) op: Equal rhs: {(/src/external-vcs/sourceforge/bashdb)} spids: [358] ) ] spids: [358] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:abs_top_srcdir) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_DPercent arg_word:{(Lit_Slash /)}) spids: [365 369] ) (/) } spids: [364] ) ] spids: [364] ) (C {(.)} {(${ VSub_Name abs_top_srcdir) (test/unit/helper.sh)}) (C {(.)} {($ VSub_Name "$abs_top_srcdir") (/lib/fns.sh)}) (C {(set)} {(--)}) (AndOr children: [ (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$0")} right: {(${ VSub_Name BASH_SOURCE)} ) ) (C {(.)} {(${ VSub_Name shunit_file)}) ] op_id: Op_DAmp ) ] )