# -------------------------------------------------------
# File: "PersonsCmd.test"
#                        Created: 2004-09-07 15:43:09
#              Last modification: 2009-03-04 09:15:51
# Author: Bernard Desgraupes
# e-mail: <bdesgraupes@users.sourceforge.net.fr>
# www: <http://tcladdressbook.sourceforge.net>
# (c) Copyright: Bernard Desgraupes, 2004-2009
# All rights reserved.
# This software is free software with BSD licence.
# Versions history: see the Changes.Log file.
# 
# Test suite for the Tcl [addressbook persons] command defined 
# in the Tcladdressbook extension.
# 
# -------------------------------------------------------

source [file join [file dir [info script]] TclABCommonTests.tcl]

namespace eval ::addressbook::test {
	namespace import ::tcltest::*

	test AddressbookPersons-1-1 {List of persons} -setup {
	} -body {
		set PersonList [addressbook persons]
		set result 1
		foreach pers $PersonList {
			set len [llength $pers]
			if {$len != 1 && $len != 2} {
				set result 0
				break
			} 
		} 
		return $result
	} -cleanup {
	} -result "1"
		
	test AddressbookPersons-1-2 {List of person IDs} -setup {
	} -body {
		set PersonList [addressbook persons -ids]
		set result 1
		foreach pers $PersonList {
			if {[llength $pers] != 1 || ![string match *ABPerson $pers]} {
				set result 0
				break
			} 
		} 
		return $result
	} -cleanup {
	} -result "1"
		
	test AddressbookPersons-1-3 {Person ID is in list} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set PersonList [addressbook persons]
		set result 0
		foreach pers $PersonList {
			if {[lindex $pers 0] == $PersonID} {
				set result 1
				break
			} 
		} 
		return $result
	} -cleanup {
	} -result "1"
		
	test AddressbookPersons-1-4 {Person name is in list} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set PersonList [addressbook persons]
		set result 0
		foreach pers $PersonList {
			if {[lindex $pers 1] eq $PersonName} {
				set result 1
				break
			} 
		} 
		return $result
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-5 {Person ID is in person IDs list} -setup {
	} -body {
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		set PersonList [addressbook persons -ids]
		set result 0
		foreach pers $PersonList {
			if {$pers eq $PersonID} {
				set result 1
				break
			} 
		} 
		return $result
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-6 {Length of persons list in subgroup} -setup {
	} -body {
		set GroupName "TclABPersonsTestGroup[incr GroupNum]"
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		set PersonList [addressbook persons -ingroup $GroupID]
		return [llength $PersonList]
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-7 {Person is in list of persons in subgroup} -setup {
	} -body {
		set GroupName "TclABPersonsTestGroup[incr GroupNum]"
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		set PersonList [addressbook persons -ingroup $GroupID]
		set PersonItem [lindex $PersonList 0]
		set result 1
		return [expr {[lindex $PersonItem 0] eq $PersonID && [lindex $PersonItem 1] eq $PersonName}]
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-8 {Person ID is in list of person IDs in subgroup} -setup {
	} -body {
		set GroupName "TclABPersonsTestGroup[incr GroupNum]"
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		set PersonList [addressbook persons -ids -ingroup $GroupID]
		set PersonItem [lindex $PersonList 0]
		return [expr {[lindex $PersonItem 0] eq $PersonID}]
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-9 {Subgroup ID is in all group IDs list} -setup {
	} -body {
		set GroupName "TclABPersonsTestGroup[incr GroupNum]"
		set PersonName "TclABPersonsTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		set PersonList [addressbook persons -ids]
		return [expr {$PersonID in $PersonList}]
	} -cleanup {
	} -result "1"

	test AddressbookPersons-1-10 {} -setup {
	} -body {
	} -cleanup {
	} -result ""

	# Usage string
	# ============
	test AddressbookPersons-2-1 {Usage string for wrong num args} -setup {
	} -body {
		addressbook persons extraArg extraArg extraArg extraArg
	} -cleanup {
	} -returnCodes error \
	  -result "wrong # args: should be \"addressbook persons ?-ids? ?-ingroup groupID?\""

	# Cleanup
	::tcltest::cleanupTests
}

namespace delete ::addressbook::test

