forked from naeemkhedarun/psclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psclass.Tests.ps1
145 lines (118 loc) · 4.5 KB
/
psclass.Tests.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"
. "$here\psclass.ps1"
Describe "GivenAnObjectWithMethods_WhenDeserializing" {
$testClass = New-PSClass TestObject {
note -private myVariable 10
method getVariable {
return $private.myVariable
}
}
$toSerialize = $testClass.New();
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
It "ItShouldStillHaveMethods" {
$deserialized.getVariable().should.be(10)
}
}
Describe "GivenAnObjectWithPublicNotes_AndANonDefaultValue_WhenDeserializing" {
$testClass = New-PSClass TestObject {
note myVariable 10
}
$toSerialize = $testClass.New();
$toSerialize.myVariable = 8;
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
It "ItShouldStillHaveMethods" {
$deserialized.myVariable.should.be(8)
}
}
Describe "GivenAnObjectWithStaticNotes_AndANonDefaultValue_WhenDeserializing" {
$testClass = New-PSClass TestObject {
note -static myVariable 10
}
$toSerialize = $testClass.New();
$toSerialize.Class.myVariable = 8;
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
It "ItShouldStillHaveMethods" {
$deserialized.Class.myVariable.should.be(8)
}
}
Describe "GivenAnObjectWithAConstructor_WhenDeserializing" {
$testClass = New-PSClass TestObject {
note executedTimes 0
constructor {
$this.executedTimes++;
}
}
$toSerialize = $testClass.New();
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
It "ItShouldDeserializeWithoutExecuting" {
$deserialized.executedTimes.should.be(1)
}
}
Describe "GivenAnObjectWithADifferentNameToTestObject_AndPrivateNotes_WhenDeserializing" {
$testClass = New-PSClass AnotherTestObject {
note -private executedTimes 0
}
$toSerialize = $testClass.New();
Export-Clixml -InputObject $toSerialize -Path .\object.xml
It "ItShouldDeserializeWithoutErroring" {
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
}
}
Describe "GivenAnObjectWithAPropertyWhichIsAPSObject_AndTheBackingFieldIsUpdatedAfterDeserializing_WhenDeserializing" {
$referencedObject = New-PSClass ReferencedObject {
note -private myVariable 0
property MyVariable { $private.myVariable }
method SetVariable {
param($val)
$private.myVariable = $val
}
}
$testClass = New-PSClass TestObject {
constructor {
param($refObject)
$private.referencedObject = $refObject
}
note -private referencedObject
property ReferencedObject { $private.referencedObject }
}
$toSerialize = $testClass.New($referencedObject.New());
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
$deserialized.ReferencedObject.SetVariable(10)
It "ShouldReflectTheValue" {
$deserialized.ReferencedObject.MyVariable.should.be(10)
}
}
Describe "GivenAnObjectWithACollectionOfObjects_WhenDeserializing" {
$referencedObject = New-PSClass ReferencedObject {
note -private myVariable 0
property MyVariable { $private.myVariable }
method SetVariable {
param($val)
$private.myVariable = $val
}
}
$testClass = New-PSClass TestObject {
constructor {
param($refObjects)
$private.referencedObjects = $refObjects
}
note -private referencedObjects @()
property ReferencedObjects { $private.referencedObjects }
}
$toSerialize = $testClass.New(@($referencedObject.New(), $referencedObject.New()));
Export-Clixml -InputObject $toSerialize -Path .\object.xml
$deserialized = Deserialize-PSClass (Import-Clixml .\object.xml)
$deserialized.ReferencedObjects[0].SetVariable(10)
$deserialized.ReferencedObjects[1].SetVariable(20)
It "TheObjectsInTheCollectionShouldBeDeserialized" {
$deserialized.ReferencedObjects[0].MyVariable.should.be(10)
$deserialized.ReferencedObjects[1].MyVariable.should.be(20)
}
}