#
# Tests for the "ensemble" compound command facility
# ----------------------------------------------------------------------
#   AUTHOR:  Michael J. McLennan
#            Bell Labs Innovations for Lucent Technologies
#            mmclennan@lucent.com
#            http://www.tcltk.com/itcl
#
#      RCS:  $Id: ensemble.test,v 1.1.1.1 1999/03/31 20:39:47 damon Exp $
# ----------------------------------------------------------------------
#            Copyright (c) 1993-1998  Lucent Technologies, Inc.
# ======================================================================
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[string compare test [info procs test]] == 1} then {source defs}

test ensemble-1.1 {ensemble name must be specified} {
    list [catch {ensemble} msg] $msg
} {1 {wrong # args: should be "ensemble name ?command arg arg...?"}}

test ensemble-1.2 {creating a new ensemble} {
    ensemble test_numbers {
        part one {x} {
            return "one: $x"
        }
        part two {x y} {
            return "two: $x $y"
        }
    }
} ""
test ensemble-1.3 {adding to an existing ensemble} {
    ensemble test_numbers part three {x y z} {
        return "three: $x $y $z"
    }
} ""

test ensemble-1.4 {invoking ensemble parts} {
    list [test_numbers one 1] [test_numbers two 2 3] [test_numbers three 3 4 5]
} {{one: 1} {two: 2 3} {three: 3 4 5}}

test ensemble-1.5 {invoking parts with improper arguments} {
    list [catch "test_numbers three x" msg] $msg
} {1 {no value given for parameter "y" to "test_numbers three"}}

test ensemble-1.6 {errors trigger a usage summary} {
    list [catch "test_numbers foo x y" msg] $msg
} {1 {bad option "foo": should be one of...
  test_numbers one x
  test_numbers three x y z
  test_numbers two x y}}

test ensemble-1.7 {one part can't overwrite another} {
    set cmd {
        ensemble test_numbers part three {} {
            return "three: new version"
        }
    }
    list [catch $cmd msg] $msg
} {1 {part "three" already exists in ensemble}}

test ensemble-1.8 {an ensemble can't overwrite another part} {
    set cmd {
        ensemble test_numbers ensemble three part new {} {
            return "three: new version"
        }
    }
    list [catch $cmd msg] $msg
} {1 {part "three" is not an ensemble}}

test ensemble-1.9 {body errors are handled gracefully} {
    list [catch "ensemble test_numbers {foo bar baz}" msg] $msg $errorInfo
} {1 {invalid command name "foo"} {invalid command name "foo"
    while executing
"foo bar baz"
    ("ensemble" body line 1)
    invoked from within
"ensemble test_numbers {foo bar baz}"}}

test ensemble-1.10 {part errors are handled gracefully} {
    list [catch "ensemble test_numbers {part foo}" msg] $msg $errorInfo
} {1 {wrong # args: should be "part name args body"} {wrong # args: should be "part name args body"
    while executing
"part foo"
    ("ensemble" body line 1)
    invoked from within
"ensemble test_numbers {part foo}"}}

test ensemble-1.11 {part argument errors are handled gracefully} {
    list [catch "ensemble test_numbers {part foo {{}} {}}" msg] $msg $errorInfo
} {1 {procedure "foo" has argument with no name} {procedure "foo" has argument with no name
    while executing
"part foo {{}} {}"
    ("ensemble" body line 1)
    invoked from within
"ensemble test_numbers {part foo {{}} {}}"}}

test ensemble-2.0 {defining subensembles} {
    ensemble test_numbers {
        ensemble hex {
            part base {} {
                return 16
            }
            part digits {args} {
                foreach num $args {
                    lappend result "0x$num"
                }
                return $result
            }
        }
        ensemble octal {
            part base {} {
                return 8
            }
            part digits {{prefix 0} args} {
                foreach num $args {
                    lappend result "$prefix$num"
                }
                return $result
            }
        }
    }
    list [catch "test_numbers foo" msg] $msg
} {1 {bad option "foo": should be one of...
  test_numbers hex option ?arg arg ...?
  test_numbers octal option ?arg arg ...?
  test_numbers one x
  test_numbers three x y z
  test_numbers two x y}}

test ensemble-2.1 {invoking sub-ensemble parts} {
    list [catch "test_numbers hex base" msg] $msg
} {0 16}

test ensemble-2.2 {invoking sub-ensemble parts} {
    list [catch "test_numbers hex digits 3 a f" msg] $msg
} {0 {0x3 0xa 0xf}}

test ensemble-2.3 {errors from sub-ensembles} {
    list [catch "test_numbers hex" msg] $msg
} {1 {wrong # args: should be one of...
  test_numbers hex base
  test_numbers hex digits ?arg arg ...?}}

test ensemble-2.4 {invoking sub-ensemble parts} {
    list [catch "test_numbers octal base" msg] $msg
} {0 8}

test ensemble-2.5 {invoking sub-ensemble parts} {
    list [catch "test_numbers octal digits 0o 3 5 10" msg] $msg
} {0 {0o3 0o5 0o10}}

test ensemble-2.6 {errors from sub-ensembles} {
    list [catch "test_numbers octal" msg] $msg
} {1 {wrong # args: should be one of...
  test_numbers octal base
  test_numbers octal digits ?prefix? ?arg arg ...?}}

test ensemble-2.7 {sub-ensembles can't be accidentally redefined} {
    set cmd {
        ensemble test_numbers part octal {args} {
            return "octal: $args"
        }
    }
    list [catch $cmd msg] $msg
} {1 {part "octal" already exists in ensemble}}

test ensemble-3.0 {an error handler part can be used to handle errors} {
    ensemble test_numbers {
        part @error {args} {
            return "error: $args"
        }
    }
    list [catch {test_numbers foo 1 2 3} msg] $msg
} {0 {error: foo 1 2 3}}

test ensemble-3.1 {the error handler part shows up as generic "...and"} {
    list [catch {test_numbers} msg] $msg
} {1 {wrong # args: should be one of...
  test_numbers hex option ?arg arg ...?
  test_numbers octal option ?arg arg ...?
  test_numbers one x
  test_numbers three x y z
  test_numbers two x y
...and others described on the man page}}
