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

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

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

	# property names
	# ==============
	test AddressbookProperty-1-1 {Group properties} -setup {
	} -body {
		 set GroupProps [addressbook property names -groups]
		 return [expr {[llength $GroupProps] > 0}]
	} -cleanup {
	} -result "1"
		
	test AddressbookProperty-1-2 {Group property Creation} -setup {
	} -body {
		 set GroupProps [addressbook property names -groups]
		 set idx [lsearch $GroupProps Creation]
		 return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"
		
	test AddressbookProperty-1-3 {Group property GroupName} -setup {
	} -body {
		 set GroupProps [addressbook property names -groups]
		 set idx [lsearch $GroupProps GroupName]
		 return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"
		
	test AddressbookProperty-1-4 {Person properties} -setup {
	} -body {
		 set PersonProps [addressbook property names -persons]
		 return [expr {[llength $PersonProps] > 0}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-1-5 {Person property Creation} -setup {
	} -body {
		 set PersonProps [addressbook property names -persons]
		 set idx [lsearch $PersonProps Creation]
		 return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-1-6 {Person property Title} -setup {
	} -body {
		 set PersonProps [addressbook property names -persons]
		 set idx [lsearch $PersonProps Title]
		 return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-1-7 {Person property First} -setup {
	} -body {
		 set PersonProps [addressbook property names -persons]
		 set idx [lsearch $PersonProps First]
		 return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-1-8 {} -setup {
	} -body {
	} -cleanup {
	} -result ""

	test AddressbookProperty-1-9 {} -setup {
	} -body {
	} -cleanup {
	} -result ""

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

	
	# property type
	# =============

	test AddressbookProperty-2-1 {Persons property type Date} -setup {
	} -body {
		addressbook property type -persons Creation
	} -cleanup {
	} -result "Date"

	test AddressbookProperty-2-2 {Persons property type Integer} -setup {
	} -body {
		addressbook property type -persons ABPersonFlags
	} -cleanup {
	} -result "Integer"

	test AddressbookProperty-2-3 {Persons property type MultiDate} -setup {
	} -body {
		addressbook property type -persons ABDate
	} -cleanup {
	} -result "MultiDate"

	test AddressbookProperty-2-4 {Persons property type String} -setup {
	} -body {
		addressbook property type -persons First
	} -cleanup {
	} -result "String"

	test AddressbookProperty-2-5 {Persons property type MultiString} -setup {
	} -body {
		addressbook property type -persons Phone
	} -cleanup {
	} -result "MultiString"

	test AddressbookProperty-2-6 {Persons property type MultiDictionary} -setup {
	} -body {
		addressbook property type -persons Address
	} -cleanup {
	} -result "MultiDictionary"

	test AddressbookProperty-2-7 {Groups property type Date} -setup {
	} -body {
		addressbook property type -groups Creation
	} -cleanup {
	} -result "Date"

	test AddressbookProperty-2-8 {Groups property type String} -setup {
	} -body {
		addressbook property type -groups GroupName
	} -cleanup {
	} -result "String"

	test AddressbookProperty-2-9 {Groups property type MultiString} -setup {
	} -body {
		addressbook property type -groups RemoteLocation
	} -cleanup {
	} -result "MultiString"

	test AddressbookProperty-2-10 {Persons non-existent property type} -setup {
	} -body {
		addressbook property type -persons _x_y_z
	} -cleanup {
	} -returnCodes error -result "unknown property '_x_y_z' for Person record type"

	test AddressbookProperty-2-11 {Groups non-existent property type} -setup {
	} -body {
		addressbook property type -groups _x_y_z
	} -cleanup {
	} -returnCodes error -result "unknown property '_x_y_z' for Group record type"


	# property add
	# ============

	test AddressbookProperty-3-1 {Add person MultiString property} -setup {
	} -body { 
		catch {addressbook property add -persons TclAB_Instruments MultiString}
		set PersonProps [addressbook property names -persons]
		set idx [lsearch $PersonProps TclAB_Instruments]
		return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-3-2 {Add group MultiDate property} -setup {
	} -body {
		catch {addressbook property add -groups TclAB_Meetings MultiDate}
		set GroupProps [addressbook property names -groups]
		set idx [lsearch $GroupProps TclAB_Meetings]
		return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-3-3 {Add unknown type person property} -setup {
	} -body {
		addressbook property add -persons TclAB_BarMitzvah _u_v_w
	} -cleanup {
	} -returnCodes error \
	  -result "bad type \"_u_v_w\": must be Array, Data, Date, Dictionary, Integer, Real, String, MultiArray, MultiData, MultiDate, MultiDictionary, MultiInteger, MultiReal, or MultiString"

	test AddressbookProperty-3-4 {Add unknown type group property} -setup {
	} -body {
		addressbook property add -groups TclAB_Meetings _u_v_w
	} -cleanup {
	} -returnCodes error \
	  -result "bad type \"_u_v_w\": must be Array, Data, Date, Dictionary, Integer, Real, String, MultiArray, MultiData, MultiDate, MultiDictionary, MultiInteger, MultiReal, or MultiString"

	test AddressbookProperty-3-5 {Add already existing person property} -setup {
	} -body {
		addressbook property add -persons First String
	} -cleanup {
	} -returnCodes error \
	  -result "property 'First' already exists"

	test AddressbookProperty-3-6 {Add already existing group property} -setup {
	} -body {
		addressbook property add -groups GroupName String
	} -cleanup {
	} -returnCodes error \
	  -result "property 'GroupName' already exists"

	test AddressbookProperty-3-7 {Add person Array property} -setup {
	} -body { 
		catch {addressbook property add -persons TclAB_Fruits Array}
		set PersonProps [addressbook property names -persons]
		set idx [lsearch $PersonProps TclAB_Fruits]
		return [expr {$idx != -1}]
	} -cleanup {
	} -result "1"

	
	# property remove
	# ===============
	
	#  The [property remove] command does not work because the feature is
	#  not yet implemented in the AddressBook framework.
	
# 	test AddressbookProperty-4-1 {Removing a person property} -setup {
# 	} -body {
# 		set result [addressbook property remove -persons TclAB_Instruments]
# 	} -cleanup {
# 	} -result "1"
# 
# 	test AddressbookProperty-4-2 {Removing a group property} -setup {
# 	} -body {
# 		set result [addressbook property remove -groups TclAB_Meetings]
# 	} -cleanup {
# 	} -result "1"

	test AddressbookProperty-4-3 {Remove a property in a particular person record} -setup {
	} -body {
		set PersonName "TclABPropertyTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		return [addressbook property remove $PersonID Last]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-4-4 {Check removed property in a particular person record} -setup {
	} -body {
		set PersonName "TclABPropertyTestPerson[incr PersonNum]"
		set PersonID [addressbook create person $PersonName]
		addressbook property remove $PersonID Last
		set rec [addressbook record $PersonID]
		keylget rec Last
	} -cleanup {
	} -returnCodes error -result "key \"Last\" not found in keyed list"

	test AddressbookProperty-4-5 {Remove a property in a particular group record} -setup {
	} -body {
		set GroupName "TclABPropertyTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		return [addressbook property remove $GroupID GroupName]
	} -cleanup {
	} -result "1"

	test AddressbookProperty-4-6 {Check removed property in a particular group record} -setup {
	} -body {
		set GroupName "TclABPropertyTestGroup[incr GroupNum]"
		set GroupID [addressbook create group $GroupName]
		addressbook property remove $GroupID GroupName
		set rec [addressbook record $GroupID]
		keylget rec GroupName
	} -cleanup {
	} -returnCodes error -result "key \"GroupName\" not found in keyed list"


	# Usage string
	# ============
	test AddressbookProperty-5-1 {Usage string for wrong num args} -setup {
	} -body {
		addressbook property
	} -cleanup {
	} -returnCodes error \
	  -result "wrong # args: should be \"addressbook property (add|names|remove|type) (-groups|-persons) ?args?\""

	# Cleanup
	::tcltest::cleanupTests
}

namespace delete ::addressbook::test

