# -------------------------------------------------------
# File: "RemoveCmd.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 remove] command defined 
# in the Tcladdressbook extension.
# 
# -------------------------------------------------------

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

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

	test AddressbookRemove-1-1 {Remove person records from a group} -setup {
	} -body {
		set GroupName "TclABRemoveTestGroup[incr GroupNum]"
		set PersonName1 "TclABRemoveTestPerson[incr PersonNum]"
		set PersonName2 "TclABRemoveTestPerson[incr PersonNum]"
		set PersonName3 "TclABRemoveTestPerson[incr PersonNum]"
		set GroupID [addressbook create group $GroupName]
		set PersonID1 [addressbook create person $PersonName1]
		set PersonID2 [addressbook create person $PersonName2]
		set PersonID3 [addressbook create person $PersonName3]
		# Add three persons
		addressbook add $GroupID $PersonID1 $PersonID2 $PersonID3
		# Now remove two of them
		addressbook remove $GroupID $PersonID1 $PersonID3
		set PersonList [addressbook persons -ids -ingroup $GroupID]
		return [llength $PersonList]
	} -cleanup {
	} -result "1"
		
	test AddressbookRemove-1-2 {Remove group records from a group} -setup {
	} -body {
		set GroupName "TclABRemoveTestGroup[incr GroupNum]"
		set SubGroupName1 "TclABRemoveTestSubGroup[incr SubGroupNum]"
		set SubGroupName2 "TclABRemoveTestSubGroup[incr SubGroupNum]"
		set SubGroupName3 "TclABRemoveTestSubGroup[incr SubGroupNum]"
		set GroupID [addressbook create group $GroupName]
		set SubGroupID1 [addressbook create group $SubGroupName1]
		set SubGroupID2 [addressbook create group $SubGroupName2]
		set SubGroupID3 [addressbook create group $SubGroupName3]
		# Add three persons
		addressbook add $GroupID $SubGroupID1 $SubGroupID2 $SubGroupID3
		# Now remove two of them
		addressbook remove $GroupID $SubGroupID1 $SubGroupID3
		set SubGroupList [addressbook groups -ids -ingroup $GroupID]
		return [llength $SubGroupList]
	} -cleanup {
	} -result "1"
		
	test AddressbookRemove-1-3 {Remove a person and a group} -setup {
	} -body {
		set GroupName "TclABRemoveTestGroup[incr GroupNum]"
		set SubGroupName1 "TclABRemoveTestSubGroup[incr SubGroupNum]"
		set SubGroupName2 "TclABRemoveTestSubGroup[incr SubGroupNum]"
		set PersonName1 "TclABRemoveTestPerson[incr PersonNum]"
		set PersonName2 "TclABRemoveTestPerson[incr PersonNum]"
		# Create the person and group records
		set GroupID [addressbook create group $GroupName]
		set PersonID1 [addressbook create person $PersonName1]
		set PersonID2 [addressbook create person $PersonName2]
		set SubGroupID1 [addressbook create group $SubGroupName1]
		set SubGroupID2 [addressbook create group $SubGroupName2]
		# Add these records to the group
		addressbook add $GroupID $PersonID1 $PersonID2 $SubGroupID1 $SubGroupID2 
		# Now remove two of them
		addressbook remove $GroupID $PersonID1 $SubGroupID2
		set SubGroupCount [addressbook count -person -ingroup $GroupID]
		set PersonCount [addressbook count -group -ingroup $GroupID]
		return [list $PersonCount $SubGroupCount]
	} -cleanup {
	} -result "1 1"
		
	# Usage string
	# ============
	test AddressbookRemove-2-1 {Usage string for wrong num args} -setup {
	} -body {
		addressbook remove
	} -cleanup {
	} -returnCodes error \
	  -result "wrong # args: should be \"addressbook remove groupID recordID ?recordID ...?\""

	# Cleanup
	::tcltest::cleanupTests
}

namespace delete ::addressbook::test

