# -------------------------------------------------------
# File: "IdentifierCmd.test"
#                        Created: 2004-09-07 15:43:09
#              Last modification: 2009-03-06 18:50:03
# 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 identifier] command defined 
# in the Tcladdressbook extension.
# 
# -------------------------------------------------------

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

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

	test AddressbookIdentifier-1-1 {Count multi-value list property items} -setup {
	} -body {
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		return [addressbook identifier count $PersonID Phone]
	} -cleanup {
	} -result "2"
		
	test AddressbookIdentifier-1-2 {Get identifier for element of multi-value list} -setup {
	} -body {
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		# The identifier has the form DCF83783-D107-4620-B937-9C7DAA47E904
		return [addressbook identifier get $PersonID Phone 1]
	} -cleanup {
	} -match regexp -result "\[-A-F0-9\]+"
		
	test AddressbookIdentifier-1-3 {Get the primary identifier} -setup {
	} -body {
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		return [addressbook identifier primary $PersonID Phone]
	} -cleanup {
	} -match regexp -result "\[-A-F0-9\]+"
		
	test AddressbookIdentifier-1-4 {Modify the primary identifier} -setup {
	} -body {
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		set Ident0 [addressbook identifier get $PersonID Phone 0]
		set Ident1 [addressbook identifier get $PersonID Phone 1]
		set prevPrimary [addressbook identifier primary $PersonID Phone]
		if {$prevPrimary eq $Ident0} {
			set otherIdent $Ident1
		} else {
			set otherIdent $Ident0
		} 
		addressbook identifier primary $PersonID Phone $otherIdent
		set nextPrimary [addressbook identifier primary $PersonID Phone]
		return [expr {$nextPrimary eq $otherIdent}]
	} -cleanup {
	} -result "1"

	test AddressbookIdentifier-1-5 {Find index of primary identifier} -setup {
	} -body {
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		set Ident1 [addressbook identifier get $PersonID Phone 1]
		addressbook identifier primary $PersonID Phone $Ident1
		return [addressbook identifier index $PersonID Phone $Ident1]
	} -cleanup {
	} -result "1"

	test AddressbookIdentifier-1-6 {Get the distribution identifier of a person in a group} -setup {
	} -body {
		set GroupName "TclABIdentifierTestGroup[incr GroupNum]"
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		return [addressbook identifier set $GroupID $PersonID Phone]
	} -cleanup {
	} -match regexp -result "\[-A-F0-9\]+"

	test AddressbookIdentifier-1-7 {Modify the distribution identifier of a person in a group} -setup {
	} -body {
		set GroupName "TclABIdentifierTestGroup[incr GroupNum]"
		set PersonName "TclABIdentifierTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID [addressbook create person $PersonName -ingroup $GroupID]
		keylset phonesList home 12345 work 67890
		addressbook set $PersonID Phone $phonesList
		set Ident0 [addressbook identifier get $PersonID Phone 0]
		set Ident1 [addressbook identifier get $PersonID Phone 1]
		set prevDist [addressbook identifier set $GroupID $PersonID Phone]
		if {$prevDist eq $Ident0} {
			set otherIdent $Ident1
		} else {
			set otherIdent $Ident0
		} 
		# Set the other value as distribution identifier
		addressbook identifier set $GroupID $PersonID Phone $otherIdent
		set nextDist [addressbook identifier set $GroupID $PersonID Phone ]
		return [expr {$nextDist eq $otherIdent}]
	} -cleanup {
	} -result "1"


	# Usage string
	# ============
	test AddressbookIdentifier-6-1 {Usage string for wrong num args} -setup {
	} -body {
		addressbook identifier
	} -cleanup {
	} -returnCodes error \
	  -result "wrong # args: should be \"addressbook identifier (count|get|index|primary|set) ?args?\""

	# Cleanup
	::tcltest::cleanupTests
}

namespace delete ::addressbook::test

