#
# Tests for creating and using classes.
#
# Copyright (c) 1997-1998 Mark Diekhans
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: class.test,v 1.10 1998/02/17 03:35:38 markd Exp $
#

if {[info procs test] != "test"} then {source testlib.tcl}

catch {namespace delete ::Sterno}
source ../library/object.tcl
source ../library/class.tcl
source ../library/instance.tcl

TestReg class-1.0 {creating classes} {
    set c1 [::Sterno::defClass {} {}]
} 0 {^::Sterno::Classes::class[0-9]+::___call$}

TestReg class-1.1 {creating classes} {
    set c2 [::Sterno::defClass TestClass {}]
} 0 {^::Sterno::Classes::class[0-9]+::___call$}


Test class-2.0 {class fields} {
    $c1 fset c1x C1XV
} 0 {C1XV}

Test class-2.1 {class fields} {
    $c1 fget c1x
} 0 {C1XV}

Test class-2.2 {class fields} {
    $c2 fset c2x C2XV
} 0 {C2XV}

Test class-2.3 {class fields} {
    TestClass fget c2x
} 0 {C2XV}

Test class-2.4 {class fields} {
    TestClass fget c2x
} 0 {C2XV}

Test class-2.5 {class fields} {
    TestClass fset c2a(X) XXX
    TestClass fset c2a(Y) XXX
    array names [TestClass fref c2a]
} 0 {X Y}

TestReg class-4.1 {class delete} {
    $c1 delete
    $c1 c1m1 
} 1 {^invalid command name "::Sterno::Classes::class[0-9]+::___call"}


Test class-4.2 {class delete} {
    $c2 delete
    TestClass fget c2x
} 1 {invalid command name "TestClass"}

TestReg class-5.0 {local classes} {
    set lc1 [namespace eval ::SternoTests {
        ::Sterno::defLocalClass {} {
            method foo {} {
                return "I am [namespace current]"
            }
        }
    }]
    set lc1
} 0 {^::SternoTests::Sterno::class[0-9]+::___call$}

TestReg class-5.1 {local classes} {
    set lo1 [$lc1 new]
} 0 {^::SternoTests::Sterno::class[0-9]+::obj[0-9]+::___call$}

TestReg class-5.2 {local classes} {
    $lo1 foo
} 0 {^I am ::SternoTests::Sterno::class[0-9]+::obj[0-9]+$}

TestReg class-5.3 {local classes} {
    $lc1 delete  ;# Should delete its objects as well
    $lo1 foo
} 1 {^invalid command name "::SternoTests::Sterno::class[0-9]+::obj[0-9]+::___call"$}

TestReg class-5.4 {local classes} {
    # local class in a global class's object
    set gc2 [::Sterno::defClass {} {
        method mkLocalClass {} {
            return [::Sterno::defLocalClass {} {
                method chk {} {
                    return "local chk: [namespace current]"
                }
            }]
        }
    }]
    set go2 [$gc2 new]
} 0 {^::Sterno::Classes::class[0-9]+::obj[0-9]+::___call$}

TestReg class-5.5 {local classes} {
    set lc3 [$go2 mkLocalClass]
    set lo3 [$lc3 new]
    $lo3 chk
} 0 {^local chk: ::Sterno::Classes::class[0-9]+::obj[0-9]+::Sterno::class[0-9]+::obj[0-9]+$}

TestReg class-5.6 {local classes} {
    $go2 delete  ;# Should also delete class.
    $lo3 chk
} 1 {^invalid command name "::Sterno::Classes::class[0-9]+::obj[0-9]+::Sterno::class[0-9]+::obj[0-9]+::___call"}


TestReg class-7.0 {class methods} {
    set c4 [::Sterno::defClass {} {
        classMethod c4m1 {{arg {}}} {return "c4m1: $arg"}
    }]
    $c4 classMethod c4m2 {{arg {}}} {return "c4m2: $arg"}
    set c4
} 0 {^::Sterno::Classes::class[0-9]+::___call$}

Test class-7.1 {class methods} {
    list [$c4 c4m1] [$c4 c4m2]
} 0 {{c4m1: } {c4m2: }}

Test class-7.2 {class methods} {
    list [$c4 c4m1 XXX] [$c4 c4m2 YYY]
} 0 {{c4m1: XXX} {c4m2: YYY}}

