# -*- tcl -*-
# Tests for the logger facility.
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 2002 by David N. Welton <davidw@dedasys.com>.
#
# $Id: logger.test 376460 2002-12-16 23:35:28Z jigen $

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

set auto_path "[file dirname [info script]] $auto_path"
package require logger

test logger-1.0 {init basic} {
    set log [logger::init global]
    ${log}::delete
    set log
} {::logger::tree::global}

test logger-1.1 {init sub-system} {
    set log [logger::init global::subsystem]
    ${log}::delete
    set log
} {::logger::tree::global::subsystem}

test logger-1.2 {instantiate main logger and child} {
    set log1 [logger::init global]
    set log2 [logger::init global::subsystem]
    ${log2}::delete
    ${log1}::delete
    list $log1 $log2
} {::logger::tree::global ::logger::tree::global::subsystem}

test logger-2.0 {delete} {
    set log [logger::init global]
    ${log}::delete
    catch {set ${log}::enabled} err
    set err
} {can't read "::logger::tree::global::enabled": no such variable}

test logger-3.0 {log} {
    set log [logger::init global]
    ${log}::error "Danger Will Robinson!"
    ${log}::delete
} {}

test logger-3.1 {log} {
    set log [logger::init global]
    ${log}::warn "Danger Will Robinson!"
    ${log}::delete
} {}

test logger-3.2 {log} {
    set log [logger::init global]
    ${log}::logproc info txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log}::info "Alert"
    ${log}::delete
    set ::INFO
} {Danger Will Robinson!}


test logger-3.4 {log} {
    set log1 [logger::init global]
    ${log1}::logproc info txt {
	set ::INFO "LOGGED: $txt"
    }
    set log2 [logger::init global::subsystem]
    ${log1}::info boo
    lappend retval [set ::INFO]
    ${log2}::info BOO
    lappend retval [set ::INFO]
    ${log2}::delete
    ${log1}::delete
    set retval
} {{LOGGED: boo} {LOGGED: BOO}}

test logger-4.0 {disable} {
    set ::INFO {no change}
    set log [logger::init global]
    ${log}::logproc info txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log}::disable warn
    ${log}::info "Alert"
    ${log}::delete
    set ::INFO
} {no change}

test logger-4.1 {disable + enable} {
    set ::INFO {no change}
    set log [logger::init global]
    ${log}::logproc info txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log}::disable warn
    ${log}::enable info
    ${log}::info "Alert"
    ${log}::delete
    set ::INFO
} {Danger Will Robinson!}

test logger-4.2 {disable all} {
    set ::INFO {no change}
    set log [logger::init global]
    ${log}::logproc critical txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log}::disable critical
    ${log}::critical "Alert"
    ${log}::delete
    set ::INFO
} {no change}

test logger-4.3 {enable all} {
    set ::INFO {no change}
    set log [logger::init global]
    ${log}::logproc debug txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log}::enable debug
    ${log}::debug "Alert"
    ${log}::delete
    set ::INFO
} {Danger Will Robinson!}

test logger-4.4 {enable bad args} {
    set log [logger::init global]
    catch { ${log}::enable badargs } err
    ${log}::delete
    set err
} {Invalid level 'badargs' - levels are debug info notice warn error critical}

test logger-4.5 {test method inheritance} {
    set log1 [logger::init global]
    set log2 [logger::init global::child]
    ${log1}::logproc notice txt {
	set ::INFO "Danger Will Robinson!"
    }
    ${log2}::notice "alert"
    ${log2}::delete
    ${log1}::delete
    set ::INFO
} {Danger Will Robinson!}


::tcltest::cleanupTests
return
