##########################################################################
# TEPAM - Tcl's Enhanced Procedure and Argument Manager
##########################################################################
#
# proc_subproc.test: 
# This file is part of the enhanced procedure and argument manager's regression
# test. It validates the declaration and call of the procedure sub commands.
#
# Copyright (C) 2009, 2010 Andreas Drollinger
# 
# Id: proc_subproc.test
##########################################################################
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
##########################################################################

source [file join \
   [file dirname [file dirname [file join [pwd] [info script]]]] \
   devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

catch {namespace delete ::tepam}
testing {
   useLocal tepam.tcl tepam
}

######## Main procedure and sub procedures, top-down declaration ########

   # Declaration
      test tepam-sp.dcl_td.dcl.1 "tepam, sub procedure top-down declaration - main procedure declaration" \
               -body {tepam::procedure {MainProc1} {} {return Proc1}} \
               -result "" -output ""

      test tepam-sp.dcl_td.dcl.2 "tepam, sub procedure top-down declaration - first sub procedure declaration" \
               -body {tepam::procedure {MainProc1 SubProc1} {} {return Proc11}} \
               -result "" -output ""

      test tepam-sp.dcl_td.dcl.3 "tepam, sub procedure top-down declaration - second sub procedure declaration" \
               -body {tepam::procedure {MainProc1 SubProc2} {} {return Proc12}} \
               -result "" -output ""

      test tepam-sp.dcl_td.dcl.4 "tepam, sub procedure top-down declaration - sub sub procedure declaration" \
               -body {tepam::procedure {MainProc1 SubProc2 SubProc22} {} {return Proc122}} \
               -result "" -output ""

   # Help
      test tepam-sp.dcl_td.hlp.1 "tepam, sub procedure top-down declaration - main procedure help" \
               -body {MainProc1 -help} \
               -result "" -output {SYNOPSIS\s+MainProc1\s+MainProc1 SubProc1\s+MainProc1 SubProc2\s+MainProc1 SubProc2 SubProc22\s*$} -match regexp 

      test tepam-sp.dcl_td.hlp.2 "tepam, sub procedure top-down declaration - first sub procedure help" \
               -body {MainProc1 SubProc1 -help} \
               -result "" -output {SYNOPSIS\s+MainProc1 SubProc1\s+$} -match regexp

      test tepam-sp.dcl_td.hlp.3 "tepam, sub procedure top-down declaration - second sub procedure help" \
               -body {MainProc1 SubProc2 -help} \
               -result "" -output {SYNOPSIS\s+MainProc1 SubProc2\s+MainProc1 SubProc2 SubProc22\s*$} -match regexp

      test tepam-sp.dcl_td.hlp.4 "tepam, sub procedure top-down declaration - sub sub procedure help" \
               -body {MainProc1 SubProc2 SubProc22 -help} \
               -result "" -output {SYNOPSIS\s+MainProc1 SubProc2 SubProc22\s+$} -match regexp

   # Execution
      test tepam-sp.dcl_td.exe.1 "tepam, sub procedure top-down declaration - main procedure execution" \
               -body {MainProc1} \
               -result "Proc1" -output "" -match exact

      test tepam-sp.dcl_td.exe.2 "tepam, sub procedure top-down declaration - first sub procedure execution" \
               -body {MainProc1 SubProc1} \
               -result "Proc11" -output "" -match exact

      test tepam-sp.dcl_td.exe.3 "tepam, sub procedure top-down declaration - second sub procedure execution" \
               -body {MainProc1 SubProc2} \
               -result "Proc12" -output "" -match exact

      test tepam-sp.dcl_td.exe.4 "tepam, sub procedure top-down declaration - sub sub procedure execution" \
               -body {MainProc1 SubProc2 SubProc22} \
               -result "Proc122" -output "" -match exact

######## Main procedure and sub procedures, buttom-up declaration ########

   # Declaration
      test tepam-sp.dcl_bu.dcl.1 "tepam, sub procedure buttom-up declaration - sub sub procedure declaration" \
               -body {tepam::procedure {MainProc2 SubProc2 SubProc22} {} {return Proc222}} \
               -result "" -output ""

      test tepam-sp.dcl_bu.dcl.2 "tepam, sub procedure buttom-up declaration - second sub procedure declaration" \
               -body {tepam::procedure {MainProc2 SubProc2} {} {return Proc22}} \
               -result "" -output ""

      test tepam-sp.dcl_bu.dcl.3 "tepam, sub procedure buttom-up declaration - first sub procedure declaration" \
               -body {tepam::procedure {MainProc2 SubProc1} {} {return Proc21}} \
               -result "" -output ""

      test tepam-sp.dcl_bu.dcl.4 "tepam, sub procedure buttom-up declaration - main procedure declaration" \
               -body {tepam::procedure {MainProc2} {} {return Proc2}} \
               -result "" -output ""

   # Help
      test tepam-sp.dcl_bu.hlp.1 "tepam, sub procedure buttom-down declaration - main procedure help" \
               -body {MainProc2 -help} \
               -result "" -output {SYNOPSIS\s+MainProc2\s+MainProc2 SubProc1\s+MainProc2 SubProc2\s+MainProc2 SubProc2 SubProc22\s*$} -match regexp 

      test tepam-sp.dcl_bu.hlp.2 "tepam, sub procedure buttom-down declaration - first sub procedure help" \
               -body {MainProc2 SubProc1 -help} \
               -result "" -output {SYNOPSIS\s+MainProc2 SubProc1\s+$} -match regexp

      test tepam-sp.dcl_bu.hlp.3 "tepam, sub procedure buttom-down declaration - second sub procedure help" \
               -body {MainProc2 SubProc2 -help} \
               -result "" -output {SYNOPSIS\s+MainProc2 SubProc2\s+MainProc2 SubProc2 SubProc22\s*$} -match regexp

      test tepam-sp.dcl_bu.hlp.4 "tepam, sub procedure buttom-down declaration - sub sub procedure help" \
               -body {MainProc2 SubProc2 SubProc22 -help} \
               -result "" -output {SYNOPSIS\s+MainProc2 SubProc2 SubProc22\s+$} -match regexp

   # Execution
      test tepam-sp.dcl_bu.exe.1 "tepam, sub procedure buttom-up declaration - main procedure execution" \
               -body {MainProc2} \
               -result "Proc2" -output "" -match exact

      test tepam-sp.dcl_bu.exe.2 "tepam, sub procedure buttom-up declaration - first sub procedure execution" \
               -body {MainProc2 SubProc1} \
               -result "Proc21" -output "" -match exact

      test tepam-sp.dcl_bu.exe.3 "tepam, sub procedure buttom-up declaration - second sub procedure execution" \
               -body {MainProc2 SubProc2} \
               -result "Proc22" -output "" -match exact

      test tepam-sp.dcl_bu.exe.4 "tepam, sub procedure buttom-up declaration - sub sub procedure execution" \
               -body {MainProc2 SubProc2 SubProc22} \
               -result "Proc222" -output "" -match exact

######## Only sub procedures (no main procedure) ########

   # Declaration
      test tepam-sp.dcl_sc.dcl.1 "tepam, sub procedure without main procedure - first sub procedure declaration" \
               -body {tepam::procedure {MainProc3 SubProc1} {} {return Proc31}} \
               -result "" -output ""

      test tepam-sp.dcl_sc.dcl.2 "tepam, sub procedure without main procedure - second sub procedure declaration" \
               -body {tepam::procedure {MainProc3 SubProc2} {} {return Proc32}} \
               -result "" -output ""

      test tepam-sp.dcl_sc.dcl.3 "tepam, sub procedure without main procedure - sub sub procedure declaration" \
               -body {tepam::procedure {MainProc3 SubProc2 SubProc22} {} {return Proc322}} \
               -result "" -output ""

   # Help
      test tepam-sp.dcl_sc.hlp.1 "tepam, sub procedure without main procedure - main procedure help" \
               -body {MainProc3 -help} \
               -result "" -output {SYNOPSIS\s+MainProc3 SubProc1\s+MainProc3 SubProc2\s+MainProc3 SubProc2 SubProc22\s*$} -match regexp 

      test tepam-sp.dcl_sc.hlp.2 "tepam, sub procedure without main procedure - first sub procedure help" \
               -body {MainProc3 SubProc1 -help} \
               -result "" -output {SYNOPSIS\s+MainProc3 SubProc1\s+$} -match regexp

      test tepam-sp.dcl_sc.hlp.3 "tepam, sub procedure without main procedure - second sub procedure help" \
               -body {MainProc3 SubProc2 -help} \
               -result "" -output {SYNOPSIS\s+MainProc3 SubProc2\s+MainProc3 SubProc2 SubProc22\s*$} -match regexp

      test tepam-sp.dcl_sc.hlp.4 "tepam, sub procedure without main procedure - sub sub procedure help" \
               -body {MainProc3 SubProc2 SubProc22 -help} \
               -result "" -output {SYNOPSIS\s+MainProc3 SubProc2 SubProc22\s+$} -match regexp

   # Execution
      test tepam-sp.dcl_sc.exe.1 "tepam, sub procedure without main procedure - main procedure execution" \
               -body {MainProc3} \
               -returnCodes error -result {'::MainProc3' requires a subcommand} -output "" -match exact

      test tepam-sp.dcl_sc.exe.2 "tepam, sub procedure without main procedure - first sub procedure execution" \
               -body {MainProc3 SubProc1} \
               -result "Proc31" -output "" -match exact

      test tepam-sp.dcl_sc.exe.3 "tepam, sub procedure without main procedure - second sub procedure execution" \
               -body {MainProc3 SubProc2} \
               -result "Proc32" -output "" -match exact

      test tepam-sp.dcl_sc.exe.4 "tepam, sub procedure without main procedure - sub sub procedure execution" \
               -body {MainProc3 SubProc2 SubProc22} \
               -result "Proc322" -output "" -match exact

######## Specific tests about the help text ########

   namespace eval ::TestNS {
      tepam::procedure {MainProc4} {
         -description "DescrMain"
         -args { {a0 -description "MainProc4 arg"} }
         -example {TestNS::MainProc4 "Hello0"}
      } {}

      tepam::procedure {MainProc4 SubProc1} {
         -description "DescrSub1"
         -args { {a1 -description "DescrSub1 arg"} }
         -example {TestNS::MainProc4 DescrSub1 "Hello1"}
      } {}
      
      tepam::procedure {MainProc4 SubProc2} {
         -description "DescrSub2"
         -args { {a2 -description "DescrSub2 arg"} }
         -example {TestNS::MainProc4 DescrSub2 "Hello2"}
      } {}
      
      tepam::procedure {MainProc4 SubProc3} {
         -description "DescrSub3"
         -args { {a3 -description "DescrSub3 arg"} }
         -example {TestNS::MainProc4 DescrSub3 "Hello3"}
      } {}
      
      tepam::procedure {MainProc4 SubProc3 SubSubProc31} {
         -description "SubSubProc31"
         -args { {a31 -description "DescrSubSub31 arg"} }
         -example {TestNS::MainProc4 DescrSub3 SubSubProc31 "Hello31"}
      } {}
   }

   test tepam-sp.help_main "tepam, main, sub and sub-sub procedures, help main" \
      -body {::TestNS::MainProc4 -help} \
      -result "" -output [join {
         {NAME}
         {      TestNS::MainProc4}
         {SYNOPSIS}
         {      TestNS::MainProc4}
         {            <a0>}
         {               MainProc4 arg}
         {      TestNS::MainProc4 SubProc1}
         {            <a1>}
         {               DescrSub1 arg}
         {      TestNS::MainProc4 SubProc2}
         {            <a2>}
         {               DescrSub2 arg}
         {      TestNS::MainProc4 SubProc3}
         {            <a3>}
         {               DescrSub3 arg}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {            <a31>}
         {               DescrSubSub31 arg}
         {DESCRIPTION}
         {      TestNS::MainProc4}
         {         DescrMain}
         {      TestNS::MainProc4 SubProc1}
         {         DescrSub1}
         {      TestNS::MainProc4 SubProc2}
         {         DescrSub2}
         {      TestNS::MainProc4 SubProc3}
         {         DescrSub3}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {         SubSubProc31}
         {EXAMPLE}
         {      TestNS::MainProc4}
         {         TestNS::MainProc4 "Hello0"}
         {      TestNS::MainProc4 SubProc1}
         {         TestNS::MainProc4 DescrSub1 "Hello1"}
         {      TestNS::MainProc4 SubProc2}
         {         TestNS::MainProc4 DescrSub2 "Hello2"}
         {      TestNS::MainProc4 SubProc3}
         {         TestNS::MainProc4 DescrSub3 "Hello3"}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {         TestNS::MainProc4 DescrSub3 SubSubProc31 "Hello31"}
         {} {}
      } "\n"]

  test tepam-sp.help_sp1 "tepam, main, sub and sub-sub procedures, help sub-proc 1" \
      -body {::TestNS::MainProc4 SubProc1 -help} \
      -result "" -output [join {
         {NAME}
         {      TestNS::MainProc4 SubProc1}
         {SYNOPSIS}
         {      TestNS::MainProc4 SubProc1}
         {            <a1>}
         {               DescrSub1 arg}
         {DESCRIPTION}
         {      DescrSub1}
         {EXAMPLE}
         {      TestNS::MainProc4 DescrSub1 "Hello1"}
         {} {}
      } "\n"]

  test tepam-sp.help_sp3 "tepam, main, sub and sub-sub procedures, help sub-proc 3" \
      -body {::TestNS::MainProc4 SubProc3 -help} \
      -result "" -output [join {
         {NAME}
         {      TestNS::MainProc4 SubProc3}
         {SYNOPSIS}
         {      TestNS::MainProc4 SubProc3}
         {            <a3>}
         {               DescrSub3 arg}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {            <a31>}
         {               DescrSubSub31 arg}
         {DESCRIPTION}
         {      TestNS::MainProc4 SubProc3}
         {         DescrSub3}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {         SubSubProc31}
         {EXAMPLE}
         {      TestNS::MainProc4 SubProc3}
         {         TestNS::MainProc4 DescrSub3 "Hello3"}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {         TestNS::MainProc4 DescrSub3 SubSubProc31 "Hello31"}
         {} {}
      } "\n"]

  test tepam-sp.help_sp31 "tepam, main, sub and sub-sub procedures, help sub-sub-proc 3" \
      -body {::TestNS::MainProc4 SubProc3 SubSubProc31 -help} \
      -result "" -output [join {
         {NAME}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {SYNOPSIS}
         {      TestNS::MainProc4 SubProc3 SubSubProc31}
         {            <a31>}
         {               DescrSubSub31 arg}
         {DESCRIPTION}
         {      SubSubProc31}
         {EXAMPLE}
         {      TestNS::MainProc4 DescrSub3 SubSubProc31 "Hello31"}
         {} {}
      } "\n"]

######## With one unnamed argument ########

######## That's all ########

::tcltest::cleanupTests
return

##########################################################################
# Id: proc_subproc.test
# Modifications:
#
# Revision 2015/03/05 21:50:55  droll
# * Added a test to detect failures in generated help text for procedures that contain sub-commands.
#
# Revision 2010/02/11 21:50:55  droll
# * TEPAM module checkin
##########################################################################
