-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-xml.test.ps1
95 lines (72 loc) · 3.02 KB
/
import-xml.test.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using namespace System.Xml.Linq
BeforeAll {
function rehydrate([System.Xml.XmlElement]$object) {
if (($object -ne $null) -and ($null -ne $object.property)) {
$psobject = New-Object pscustomobject
foreach ($property in $object.property) {
if ($property.property.name -eq 'property') {
$psobject | Add-Member NoteProperty $property.name ($property.property | % { rehydrate $_ })
}
elseif ($property.Value) {
$a = @()
foreach ($value in $property.value) {
$a += $value
}
$psobject | Add-Member NoteProperty $property.name $a
}
elseif ($property.key) {
$hashtable = @{}
foreach ($key in $property) {
$hashtable[$key.name] = $key.innertext
}
$psobject | Add-Member NoteProperty $property.name $hashtable
}
elseif ($null -ne $property.'#text') {
$psobject | Add-Member NoteProperty $property.name $property.'#text'
}
else {
if ($null -ne $property.name -and $property.property) {
$psobject | Add-Member NoteProperty $property.name (rehydrate $property)
}
else {
$psobject | Add-Member NoteProperty $property.name $null
}
}
}
$psobject
}
}
# Create a PSCustomObject with properties for clipboard
$testObject = [PSCustomObject]@{
txt = "Sample clipboard text"
name = "SampleName"
}
# Create an instance of the clipboard class
$testInstance = [testinstance]::new()
# Initialize the instance with the PSCustomObject
$testInstance.DoInit($testObject)
del $PSScriptRoot\xml\test.xml -ErrorAction SilentlyContinue
}
Describe 'test module import-xml' {
It 'test save-object' {
save-object $testInstance
}
it 'test import-xml'{
$importinstance =import-xml $testInstance.path
$importinstance | Should -BeOfType testinstance
$importinstance.txt | Should -Be "Sample clipboard text"
$importinstance.name | Should -Be "SampleName"
}
it 'debug import-xml'{
[xml]$xml = cat $psscriptroot\xml\test.xml
foreach($object in $xml.objects.object){
$objecttype = $object.type
$objecttype | Should -Be "testinstance"
$instance = New-Object -TypeName $objecttype
$pscustomobject = rehydrate $object
$pscustomobject | Should -BeOfType pscustomobject
$instance.doinit($pscustomobject)
$instance | Should -BeOfType testinstance
}
}
}