📄 testtcl.tcl
字号:
# Testing data type Mapputs "Testing data type: Map"set dtMap [STAF::datatype createMap]if {[STAF::datatype getType $dtMap] != "$STAF::MapType"} { puts "Wrong type for a Map data type" puts "Expected: $STAF::MapType" puts "Found: [STAF::datatype getType $dtMap]" exit 1}if {[STAF::datatype getValue $dtMap] != ""} { puts "Wrong value for data type" puts "Expected: <Empty string>" puts "Found: [STAF::datatype getValue $dtMap]" exit 1}set mapTest(key1) value1set mapTest(key2) value2set mapTestValue [array get mapTest]STAF::datatype setValue dtMap $mapTestValueif {[STAF::datatype getValue $dtMap] != $mapTestValue} { puts "Wrong value for data type" puts "Expected: $mapTestValue" puts "Found: [STAF::datatype getValue $dtMap]" exit 1}set mapInit(key3) value3set mapInit(key4) value4set mapInitValue [array get mapInit]set dtMap [STAF::datatype createMap $mapInitValue]if {[STAF::datatype getValue $dtMap] != $mapInitValue} { puts "Wrong value for data type" puts "Expected: $mapInitValue" puts "Found: [STAF::datatype getValue $dtMap]" exit 1}STAF::datatype setValue dtMap $mapTestValueif {[STAF::datatype getValue $dtMap] != $mapTestValue} { puts "Wrong value for data type" puts "Expected: $mapTestValue" puts "Found: [STAF::datatype getValue $dtMap]" exit 1}set mapAdditionalValue(key5) value5append mapTestValue " " [array get mapAdditionalValue]append dtMap " " [array get mapAdditionalValue]if {[STAF::datatype getValue $dtMap] != $mapTestValue} { puts "Wrong value for data type" puts "Expected: $mapTestValue" puts "Found: [STAF::datatype getValue $dtMap]" exit 1}# Using data type Noneputs "\nUsing data type: None"set dtNone [STAF::datatype createNone]puts "dtNone value: [STAF::datatype getValue $dtNone]"# Check if an object is a None data typeif {[STAF::datatype getType $dtNone] == $STAF::NoneType} { puts "Data Type: [STAF::datatype getType $dtNone]"}# Using data type Contextputs "\nUsing data type: Context"# Create a Context data type with None as the root objectset dtContext [STAF::datatype createContext]puts "dtContext value: [STAF::datatype getValue $dtContext]"# Create a Context data type with a List data t ype as the root objectset dtList [STAF::datatype createList [list "List" "Test" "Value"]]set dtContext [STAF::mcontext create $dtList]puts "dtContext value: [STAF::datatype getValue $dtContext]"# Check if an object is a Context data typeif {[STAF::datatype getType $dtContext] == $STAF::ContextType} { puts "Data Type: [STAF::datatype getType $dtContext]"}# Using data type Scalarputs "\nUsing data type: Scalar"# Create a Scalar data type with an empty string valueset dtScalar [STAF::datatype createScalar]puts "dtScalar value: [STAF::datatype getValue $dtScalar]"# Set the value for a Scalar data type to a stringset myString "Testing 123..."STAF::datatype setValue dtScalar $myStringputs "dtScalar value: [STAF::datatype getValue $dtScalar]"# Set the value for a Scalar data type to a numberset myRC 99set dtScalar [STAF::datatype createScalar $myRC]puts "dtScalar value: [STAF::datatype getValue $dtScalar]"# Check if an object is a Scalar data typeif {[STAF::datatype getType $dtScalar] == "$STAF::ScalarType"} { puts "Data Type: [STAF::datatype getType $dtScalar]"}# Using data type Listputs "\nUsing data type: List"# Create an empty List data typeset dtList [STAF::datatype createList]puts "dtList value: [STAF::datatype getValue $dtList]"# Set the value for a List data typeset listTestValue [list "List" "Test" "Value"]STAF::datatype setValue dtList $listTestValueputs "dtList value: [STAF::datatype getValue $dtList]"# Create a List data type assigning an initial valueset listInitValue [list "List" "Init" "Value"]set dtList [STAF::datatype createList $listInitValue]puts "dtList value: [STAF::datatype getValue $dtList]"# Change the value for a List data typelappend listTestValue "And"lappend listTestValue "More"STAF::datatype setValue dtList $listTestValueputs "dtList value: [STAF::datatype getValue $dtList]"# Append entries to a List data typelappend dtList "And"lappend dtList "More"puts "dtList value: [STAF::datatype getValue $dtList]"# Check if an object is a List data typeif {[STAF::datatype getType $dtList] == "$STAF::ListType"} { puts "Data Type: [STAF::datatype getType $dtList]"}# Using data type Mapputs "\nUsing data type: Map"# Create an empty Map data typeset dtMap [STAF::datatype createMap]puts "dtMap value: [STAF::datatype getValue $dtMap]"# Set the value for a Map data typeset mapTest(key1) value1set mapTest(key2) value2set mapTestValue [array get mapTest]STAF::datatype setValue dtMap $mapTestValueputs "dtMap value: [STAF::datatype getValue $dtMap]"# Create a Map data type assigning an initial valueset mapInit(key3) value3set mapInit(key4) value4set mapInitValue [array get mapInit]set dtMap [STAF::datatype createMap $mapInitValue]puts "dtMap value: [STAF::datatype getValue $dtMap]"# Change the value for a Map data typeSTAF::datatype setValue dtMap $mapTestValueputs "dtMap value: [STAF::datatype getValue $dtMap]"# Add an additional key/value to a Map data typeset mapAdditionalValue(key5) value5append mapTestValue " " [array get mapAdditionalValue]append dtMap " " [array get mapAdditionalValue]puts "dtMap value: [STAF::datatype getValue $dtMap]"# Check if an object is a Map data type if {[STAF::datatype getType $dtMap] == "$STAF::MapType"} { puts "Data Type: [STAF::datatype getType $dtMap]"}# Using data types List and Map to create a list of mapsputs "\nUsing data types List and Map to create a list of maps"set map1(key1) value1set map1(key2) value2set map1Value [array get map1]set dtMap1 [STAF::datatype createMap $map1Value]set map2(test1) C:/tests/test1.cmdset map2(test2) C:/tests/test2.shset map2Value [array get map2]set dtMap2 [STAF::datatype createMap $map2Value]set listValue [list $dtMap1 $dtMap2]set dtList [STAF::datatype createList $listValue]puts "dtList: $dtList"puts "dtList value: [STAF::datatype getValue $dtList]"############################################# Now, let's test STAF::mapclassdefinition #############################################puts "\nTesting map class definition functions\n"# Create a map class definition# createset mcdName "foo"set mcd [STAF::mapclassdef create $mcdName]if {[STAF::mapclassdef getName $mcd] != $mcdName} { puts "Wrong name for map class def" puts "Expected: $mcdName" puts "Found: [STAF::mapclassdef getName $mcd]" exit 1}# addKey# setKeyProperty# getKeysset mcdKey1Name "key1"set mcdKey1DisplayName "Name 1"set mcdKey2Name "key2"set mcdKey2DisplayName "Name 2"set mcdKey2PName "display-short-name"set mcdKey2PValue "N2"STAF::mapclassdef addKey mcd $mcdKey1Name $mcdKey1DisplayNameSTAF::mapclassdef addKey mcd $mcdKey2Name $mcdKey2DisplayNameSTAF::mapclassdef setKeyProperty mcd $mcdKey2Name $mcdKey2PName $mcdKey2PValueset mcdKeysObj [STAF::mapclassdef getKeys $mcd]if {[STAF::datatype getType $mcdKeysObj] != "$STAF::ListType"} { puts "Wrong type for map class def keys" puts "Expected: $STAF::ListType" puts "Found: [STAF::datatype getType $mcdKeysObj]" exit 1}foreach mcdKeyObj [STAF::datatype getValue $mcdKeysObj] { if {[STAF::datatype getType $mcdKeyObj] != "$STAF::MapType"} { puts "Wrong type for a map class def key" puts "Expected: $STAF::MapType" puts "Found: [STAF::datatype getType $mcdKeysObj]" exit 1 } array set mcdKey [STAF::datatype getValue $mcdKeyObj] if {$mcdKey(key) == $mcdKey1Name} { if {$mcdKey(display-name) != $mcdKey1DisplayName} { puts "Error: Invalid key display-name in map class definition" puts "Expected: $mcdKey1DisplayName" puts "Found: $mcdKey(display-name)" exit 1 } } elseif {$mcdKey(key) == $mcdKey2Name} { if {$mcdKey(display-name) != $mcdKey2DisplayName} { puts "Error: Invalid key display-name in map class definition" puts "Expected: $mcdKey2DisplayName" puts "Found: $mcdKey(display-name)" exit 1 } if {$mcdKey($mcdKey2PName) != $mcdKey2PValue} { puts "Error: Invalid key property value in map class definition" puts "Expected: $mcdKey2PValue" puts "Found: $mcdKey($mcdKey2PName)" exit 1 } } else { puts "Error: Invalid key in map class definition" puts "Expected: <No key>" puts "Found: $mcdKey(key)" exit 1 }}# createInstanceset mcdInstanceObj [STAF::mapclassdef createInstance $mcd]if {[STAF::datatype getType $mcdInstanceObj] != "$STAF::MapType"} { puts "Wrong type for map class def keys" puts "Expected: $STAF::MapType" puts "Found: [STAF::datatype getType $mcdInstanceObj]" exit 1}array set mcdInstance [STAF::datatype getValue $mcdInstanceObj]if {$mcdInstance(staf-map-class-name) != $mcdName} { puts "Wrong map class definition type for map class definition instance" puts "Expected: $mcdName" puts "Found: $mcdInstance(staf-map-class-name)" exit 1}# getMapClassDefinitionObjectif {[STAF::mapclassdef getMapClassDefinitionObject $mcd] != $mcd} { puts "Wrong map class definition object" puts "Expected: $mcd" puts "Found: [STAF::mapclassdef getMapClassDefinitionObject $mcd]" exit 1}# create(with map class definition)set mcd2Name "bar"set mcd2 [STAF::mapclassdef create $mcd2Name [STAF::mapclassdef getMapClassDefinitionObject $mcd]]if {[STAF::mapclassdef getName $mcd2] != $mcdName} { puts "Wrong name for map class def" puts "Expected: $mcdName" puts "Found: [STAF::mapclassdef getName $mcd2]" exit 1}if {[STAF::mapclassdef getMapClassDefinitionObject $mcd2] != $mcd} { puts "Wrong map class definition object" puts "Expected: $mcd" puts "Found: [STAF::mapclassdef getMapClassDefinitionObject $mcd2]" exit 1}# Run mapclassdef example in STAF Tcl User's Guideset myMapClassDef [STAF::mapclassdef create "Test/MyMap"]STAF::mapclassdef addKey myMapClassDef "name" "Name"STAF::mapclassdef addKey myMapClassDef "exec" "Executable"STAF::mapclassdef addKey myMapClassDef "testType" "Test Type"STAF::mapclassdef setKeyProperty myMapClassDef "testType" "display-short-name" "Test"STAF::mapclassdef addKey myMapClassDef "outputList" "Outputs"set mapClassDefName [STAF::mapclassdef getName $myMapClassDef]puts "The keys for map class definition '$mapClassDefName' are:"puts "[STAF::formatObject [STAF::mapclassdef getKeys $myMapClassDef]]\n"################################### Now, let's test STAF::mcontext ###################################puts "Testing marshalling context functions"# createset context [STAF::mcontext create]if {[STAF::datatype getType $context] != "$STAF::ContextType"} { puts "Wrong type for marshalling context" puts "Expected: $STAF::ContextType" puts "Found: [STAF::datatype getType $context]" exit 1}if {[STAF::mcontext getRootObject $context] != [STAF::datatype createNone]} { puts "Error: New marshalling context should have a root object of None" puts "Found: [STAF::mcontext getRootObject $context]" exit 1}# XXX: Need an actual test of some type here# setMapClassDefinition# hasMapClassDefinition# getMapClassDefinitionSTAF::mcontext setMapClassDefinition context $mcdif {! [STAF::mcontext hasMapClassDefinition $context $mcdName]} { puts "Error: marshalling context does not contain map class definition" puts "Expected to find map class definition $mcdName" puts "Did not find it" exit 1}set testMCD [STAF::mcontext getMapClassDefinition $context $mcdName]if {$testMCD != $mcd} { puts "Error: map class definitions do not match" puts "Map class 1: $testMCD" puts "Map class 2: $mcd" exit 1}# getMapClassMapset mapClassMap [STAF::mcontext getMapClassMap $context]# getMapClassDefinitionNamesset mcd3Name "baz"set mcd3 [STAF::mapclassdef create $mcd3Name]STAF::mapclassdef addKey mcd3 $mcdKey1Name $mcdKey1DisplayNameSTAF::mapclassdef addKey mcd3 $mcdKey2Name $mcdKey2DisplayNameSTAF::mcontext setMapClassDefinition context $mcd3set mcdNames [STAF::mcontext getMapClassDefinitionNames $context]if {[llength $mcdNames] != 2} { puts "Error: Incorrect number of map class definition in marshalling context" puts "Expected: 2" puts "Found: [llength $mcdNames]" exit 1}if {( [lsearch -exact $mcdNames $mcdName] == -1 ) || ( [lsearch -exact $mcdNames $mcd3Name] == -1 )} { puts "Error: Invalid map class definition names in marshalling context" puts "Expected: $ncdName and $mcd3Name" puts "Found: $mcdNames" exit 1}# setRootObject# getRootObjectset testArray(a) "Hello"set testArray(b) "World"set testArrayString [array get testArray]set arrayObject [STAF::datatype createMap $testArrayString]STAF::mcontext setRootObject context $arrayObjectset testArrayObject [STAF::mcontext getRootObject $context]if {$testArrayObject != $arrayObject} { puts "Error: Incorrect root object" puts "Expected: $arrayObject" puts "Found: $testArrayObject" exit 1}# getPrimaryObjectif {[STAF::mcontext getPrimaryObject $context] != $context} { puts "Error: Wrong primary object for marshalling context"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -