# Commands covered:  treectrl's widget command notify
#
# This file contains a collection of tests for the notify widget command of
# the tktreectrl extension.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2000 by Scriptics Corporation.
# Copyright (c) 2002 by Christian Krone.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import ::tcltest::*
}

package require Tk
package require treectrl

test notify-0.1 {some needed preparations} -body {
    treectrl .t
} -result {.t}

test notify-1.1 {notify: missing args} -body {
    .t notify
} -returnCodes error -result {wrong # args: should be ".t notify command ?arg arg ...?"}

test notify-1.2 {notify: unknown command} -body {
    .t notify foo
} -returnCodes error -result {bad command "foo": must be *} -match glob

test notify-2.1 {notify eventnames: too much args} -body {
    .t notify eventnames Open
} -returnCodes error -result {wrong # args: should be ".t notify eventnames"}

test notify-2.2 {notify eventnames: nothing own installed yet} -body {
    lsort [.t notify eventnames]
} -result {ActiveItem Collapse Expand ItemDelete ItemVisibility Scroll Selection}

test notify-2.3 {notify install: missing args} -body {
    .t notify install
} -returnCodes error -result {wrong # args: should be ".t notify install pattern ?percentsCommand?"}

test notify-2.4 {notify install: bad pattern} -body {
    .t notify install foo
} -returnCodes error -result {missing "<" in event pattern "foo"}

test notify-2.5 {notify install event: old-style missing args} -body {
    .t notify install event
} -returnCodes error -result {wrong # args: should be ".t notify install event name ?percentsCommand?"}

test notify-2.6 {notify install event: old-style too much args} -body {
    .t notify install event foo bar baz
} -returnCodes error -result {wrong # args: should be ".t notify install event name ?percentsCommand?"}

test notify-2.7 {notify install event} -body {
    .t notify install event Greetings
    .t notify install event GoodBye
} -result {}

test notify-2.8 {notify eventnames: list Greetings} -body {
    lsort [.t notify eventnames]
} -result {ActiveItem Collapse Expand GoodBye Greetings ItemDelete ItemVisibility Scroll Selection}

test notify-2.9 {notify detailnames: missing args} -body {
    .t notify detailnames
} -returnCodes error -result {wrong # args: should be ".t notify detailnames eventName"}

test notify-2.10 {notify detailnames: too many args} -body {
    .t notify detailnames foo bar
} -returnCodes error -result {wrong # args: should be ".t notify detailnames eventName"}

test notify-2.11 {notify detailnames: unknown event} -body {
    .t notify detailnames Hello
} -returnCodes error -result {unknown event "Hello"}

test notify-2.12 {notify detailnames: no details yet} -body {
    .t notify detailnames Greetings
} -result {}

test notify-2.13 {notify install detail: old-style missing args} -body {
    .t notify install detail
} -returnCodes error -result {wrong # args: should be ".t notify install detail event detail ?percentsCommand?"}

test notify-2.14 {notify install detail: old-style unknown event} -body {
    .t notify install detail Hello GoodBye
} -returnCodes error -result {unknown event "Hello"}

test notify-2.15 {notify install detail} -body {
    .t notify install detail Greetings Wrote
    .t notify install detail Greetings Sent
} -result {}

test notify-2.16 {notify detailnames} -body {
    lsort [.t notify detailnames Greetings]
} -result {Sent Wrote}

test notify-3.1 {notify linkage: missing args} -body {
    .t notify linkage
} -returnCodes error -result {wrong # args: should be ".t notify linkage pattern"}

test notify-3.2 {notify linkage: unknown event} -body {
    .t notify linkage foo
} -returnCodes error -result {unknown event "foo"}

test notify-3.3 {notify linkage: standard event} -body {
    .t notify linkage Scroll
} -result {static}

test notify-3.4 {notify linkage: self made event} -body {
    .t notify linkage Greetings
} -result {dynamic}

test notify-3.5 {notify linkage: unknown detail} -body {
    .t notify linkage Greetings foo
} -returnCodes error -result {unknown detail "foo" for event "Greetings"}

test notify-3.6 {notify linkage: standard event} -body {
    .t notify linkage Scroll x
} -result {static}

test notify-3.7 {notify linkage: self made event} -body {
    .t notify linkage Greetings Sent
} -result {dynamic}

test notify-4.1 {notify bind: too much args} -body {
    .t notify bind z y z z y
} -returnCodes error -result {wrong # args: should be ".t notify bind ?object? ?pattern? ?script?"}

test notify-4.2 {notify bind: nothing bound yet} -body {
    .t notify bind .t
} -result {}

test notify-4.3 {notify bind: invalid pattern} -body {
    .t notify bind .t Greetings
} -returnCodes error -result {missing "<" in event pattern "Greetings"}

test notify-4.4 {notify bind: unknown event} -body {
    .t notify bind .t <Hello>
} -returnCodes error -result {unknown event "Hello"}

test notify-4.5 {notify bind: unknown detail} -body {
    .t notify bind .t <Greetings-Prepare>
} -returnCodes error -result {unknown detail "Prepare" for event "Greetings"}

test notify-4.6 {notify bind: nothing yet for simple event} -body {
    .t notify bind .t <Greetings>
} -result {}

test notify-4.7 {notify bind: simple event} -body {
    .t notify bind .t <GoodBye> {puts -nonewline "Bye bye"}
} -result {}

test notify-4.8 {notify bind: simple event, script added} -body {
    .t notify bind .t <GoodBye> {+puts ""}
} -result {}

test notify-4.9 {notify bind: simple event defined} -body {
    .t notify bind .t <GoodBye>
} -result {puts -nonewline "Bye bye"
puts ""}

test notify-4.10 {notify bind: nothing yet for event with detail} -body {
    .t notify bind .t <Greetings-Sent>
} -result {}

test notify-4.11 {notify bind: event with detail} -body {
    .t notify bind .t <Greetings-Wrote> {puts -nonewline "Hello World"}
    .t notify bind .t <Greetings-Sent>  {puts ""}
} -result {}

test notify-4.12 {notify bind: event with detail defined} -body {
    .t notify bind .t <Greetings-Sent>
} -result {puts ""}

test notify-4.13 {notify bind without pattern} -body {
    lsort [.t notify bind .t]
} -result {<GoodBye> <Greetings-Sent> <Greetings-Wrote>}

test notify-5.1 {notify configure: missing args} -body {
    .t notify configure
} -returnCodes error -result {wrong # args: should be ".t notify configure object pattern ?option? ?value? ?option value ...?"}

test notify-5.2 {notify configure: unknown event} -body {
    .t notify configure .t <Hello>
} -returnCodes error -result {unknown event "Hello"}

test notify-5.3 {notify configure: unknown event with detail} -body {
    .t notify configure .t <Hello-World>
} -returnCodes error -result {unknown event "Hello"}

test notify-5.4 {notify configure: unbound event} -body {
    .t notify configure .t <Scroll>
} -result {}

test notify-5.5 {notify configure: unbound event with details} -body {
    .t notify configure .t <Scroll-x>
} -result {}

test notify-5.6 {notify configure: dynamic event} -body {
    .t notify configure .t <Greetings-Sent>
} -result {-active 1}

test notify-5.7 {notify configure: dynamic event} -body {
    .t notify configure .t <Greetings-Sent> -active 0
    .t notify configure .t <Greetings-Sent>
} -result {-active 0}

test notify-6.1 {notify generate: missing args} -body {
    .t notify generate
} -returnCodes error -result {wrong # args: should be ".t notify generate pattern ?charMap? ?percentsCommand?"}

test notify-6.2 {notify generate: invalid event} -body {
    .t notify generate Greetings
} -returnCodes error -result {missing "<" in event pattern "Greetings"}

test notify-6.3 {notify generate: virtual event} -body {
    .t notify generate <<Greetings>>
} -returnCodes error -result {unknown event "<Greetings>"}

test notify-6.4 {notify generate: unknown event} -body {
    .t notify generate <Hello>
} -returnCodes error -result {unknown event "Hello"}

test notify-6.5 {notify generate: unknown detail} -body {
    .t notify generate <Greetings-Prepare>
} -returnCodes error -result {unknown detail "Prepare" for event "Greetings"}

test notify-6.6 {notify generate: missing detail} -body {
    .t notify generate <Greetings>
} -returnCodes error -result {cannot generate "<Greetings>": missing detail}

test notify-6.7 {notify generate: NOW!} -body {
    .t notify generate <Greetings-Wrote>
} -output {Hello World}

test notify-6.8 {notify generate: not active} -body {
    .t notify generate <Greetings-Sent>
} -result {}

test notify-6.9 {notify generate: and AGAIN!} -body {
    .t notify configure .t <Greetings-Sent> -active 1
    .t notify generate <Greetings-Sent>
} -output {
}

test notify-6.10 {notify generate: invalid percent char} -body {
    .t notify generate <Greetings-Sent> {foo bar}
} -returnCodes error -result {invalid percent char "foo"}

test notify-6.11 {notify generate: odd number of field args} -body {
    .t notify generate <Greetings-Sent> f
} -returnCodes error -result {char map must have even number of elements}

test notify-6.12 {notify generate: huge number of field args} -body {
    for {set x 1} {$x < 2048} {incr x} {
	lappend map f $x
    }
    .t notify generate <Greetings-Sent> $map
} -output {
}

test notify-6.13 {notify generate: not so much field args} -body {
    .t notify generate <Greetings-Sent> \
	{0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j X x}
} -output {
}

test notify-6.14 {notify install/bind/generate: do some replacements} -body {
    .t notify install event  Percent
    .t notify install detail Percent Test
    .t notify bind .t <Percent-Test> \
	{puts -nonewline "%%W: %2 %b %| %~ %2 %b%%"}
    .t notify generate <Percent-Test> {2 to b be ~ not | or}
} -output {%W: to be or not to be%}

proc doMyOwnSubst {field window eventName detail args} {
    return [string map {2 TO b BE ~ NOT | OR} $field]
}

test notify-6.15 {notify install/bind/generate: my own replacements} -body {
    .t notify uninstall detail Percent Test
    .t notify install detail Percent Test doMyOwnSubst
    .t notify bind .t <Percent-Test> \
	{puts -nonewline "%%W: %2 %b %| %~ %2 %b%%"}
    .t notify generate <Percent-Test>
} -output {%W: TO BE OR NOT TO BE%}

test notify-6.16 {notify install/bind/generate: standard replacements} -body {
    .t notify generate <Percent-Test> {2 to b be ~ not | or}
} -output {%W: TO BE OR NOT TO BE%}

test notify-6.17 {notify install/bind/generate: my own replacements} -body {
    .t notify uninstall detail Percent Test
    .t notify uninstall event Percent
    .t notify install event Percent doMyOwnSubst
    .t notify bind .t <Percent> \
	{puts -nonewline "%%W: %2 %b %| %~ %2 %b%%"}
    .t notify generate <Percent>
} -output {%W: TO BE OR NOT TO BE%}

test notify-6.18 {notify install/bind/generate: standard replacements} -body {
    .t notify generate <Percent> {2 to b be ~ not | or}
} -output {%W: TO BE OR NOT TO BE%}

test notify-7.1 {notify uninstall: missing args} -body {
    .t notify uninstall
} -returnCodes error -result {wrong # args: should be ".t notify uninstall pattern"}

test notify-7.2 {notify uninstall: unknown command} -body {
    .t notify uninstall foo
} -returnCodes error -result {missing "<" in event pattern "foo"}

test notify-7.3 {notify uninstall detail: missing args} -body {
    .t notify uninstall detail
} -returnCodes error -result {wrong # args: should be ".t notify uninstall detail event detail"}

test notify-7.4 {notify uninstall detail: too much args} -body {
    .t notify uninstall detail foo bar baz
} -returnCodes error -result {wrong # args: should be ".t notify uninstall detail event detail"}

test notify-7.5 {notify uninstall detail: unknown event} -body {
    .t notify uninstall detail foo bar
} -returnCodes error -result {unknown event "foo"}

test notify-7.6 {notify uninstall detail: unknown detail} -body {
    .t notify uninstall detail Greetings GoodBye
} -returnCodes error -result {unknown detail "GoodBye" for event "Greetings"}

test notify-7.7 {notify uninstall detail} -body {
    .t notify uninstall detail Greetings Sent
} -result {}

test notify-7.8 {notify uninstall detail: double check} -body {
    lsearch -exact [.t notify detailnames Greetings] Sent
} -result {-1}

test notify-7.9 {notify uninstall detail: delete a static detail} -body {
    .t notify uninstall detail Scroll x
} -returnCodes error -result {can't uninstall static detail "x"}

test notify-7.10 {notify uninstall event: missing args} -body {
    .t notify uninstall event
} -returnCodes error -result {wrong # args: should be ".t notify uninstall event name"}

test notify-7.11 {notify uninstall event: too much args} -body {
    .t notify uninstall event foo bar
} -returnCodes error -result {wrong # args: should be ".t notify uninstall event name"}

test notify-7.12 {notify uninstall event: unknown event} -body {
    .t notify uninstall event foo
} -returnCodes error -result {unknown event "foo"}

test notify-7.13 {notify uninstall event} -body {
    .t notify uninstall event Greetings
} -result {}

test notify-7.14 {notify uninstall event: double check} -body {
    lsearch -exact [.t notify eventnames] Greetings
} -result {-1}

test notify-7.15 {notify uninstall event: delete a static event} -body {
    .t notify uninstall event Scroll
} -returnCodes error -result {can't uninstall static event "Scroll"}

test notify-99.1 {some needed cleanup} -body {
    destroy .t
} -result {}

# cleanup
::tcltest::cleanupTests
return
