Functions/Assertions/Be.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Set-StrictMode -Version Latest

InModuleScope Pester {
    Describe "PesterBe" {
        It "returns true if the 2 arguments are equal" {
            1 | Should Be 1
            1 | Should -Be 1
            1 | Should -EQ 1
        }
        It "returns true if the 2 arguments are equal and have different case" {
            'A' | Should Be 'a'
            'A' | Should -Be 'a'
            'A' | Should -EQ 'a'
        }

        It "returns false if the 2 arguments are not equal" {
            1 | Should Not Be 2
            1 | Should -Not -Be 2
            1 | Should -Not -EQ 2
        }

        It 'Compares Arrays properly' {
            $array = @(1,2,3,4,'I am a string', (New-Object psobject -Property @{ IAm = 'An Object' }))
            $array | Should Be $array
            $array | Should -Be $array
            $array | Should -EQ $array
        }

        It 'Compares arrays with correct case-insensitive behavior' {
            $string = 'I am a string'
            $array = @(1,2,3,4,$string)
            $arrayWithCaps = @(1,2,3,4,$string.ToUpper())

            $array | Should Be $arrayWithCaps
            $array | Should -Be $arrayWithCaps
            $array | Should -EQ $arrayWithCaps
        }

        It 'Handles reference types properly' {
            $object1 = New-Object psobject -Property @{ Value = 'Test' }
            $object2 = New-Object psobject -Property @{ Value = 'Test' }

            $object1 | Should Be $object1
            $object1 | Should Not Be $object2
            $object1 | Should -Be $object1
            $object1 | Should -Not -Be $object2
            $object1 | Should -EQ $object1
            $object1 | Should -Not -EQ $object2
        }

        It 'Handles arrays with nested arrays' {
            $array1 = @(
                @(1,2,3,4,5),
                @(6,7,8,9,0)
            )

            $array2 = @(
                @(1,2,3,4,5),
                @(6,7,8,9,0)
            )

            $array1 | Should Be $array2
            $array1 | Should -Be $array2
            $array1 | Should -EQ $array2

            $array3 = @(
                @(1,2,3,4,5),
                @(6,7,8,9,0, 'Oops!')
            )

            $array1 | Should Not Be $array3
            $array1 | Should -Not -Be $array3
            $array1 | Should -Not -EQ $array3
        }

        It "returns true if the actual value can be cast to the expected value and they are the same value" {
            {abc} | Should Be "aBc"
            {abc} | Should -Be "aBc"
            {abc} | Should -EQ "aBc"
        }

        It "returns true if the actual value can be cast to the expected value and they are the same value (case sensitive)" {
            {abc} | Should BeExactly "abc"
            {abc} | Should -BeExactly "abc"
            {abc} | Should -CEQ "abc"
        }
    }

    Describe "PesterBeFailureMessage" {
        #the correctness of difference index value and the arrow pointing to the correct place
        #are not tested here thoroughly, but the behaviour was visually checked and is
        #implicitly tested by using the whole output in the following tests


        It "Returns nothing for two identical strings" {
            #this situation should actually never happen, as the code is called
            #only when the objects are not equal

            $string = "string"
            PesterBeFailureMessage $string $string | Should BeNullOrEmpty
            PesterBeFailureMessage $string $string | Should -BeNullOrEmpty
        }

        It "Outputs less verbose message for two different objects that are not strings" {
            PesterBeFailureMessage 2 1 | Should Be "Expected: {1}`nBut was: {2}"
            PesterBeFailureMessage 2 1 | Should -Be "Expected: {1}`nBut was: {2}"
        }

        It "Outputs verbose message for two strings of different length" {
            PesterBeFailureMessage "actual" "expected" | Should Be "Expected string length 8 but was 6. Strings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
            PesterBeFailureMessage "actual" "expected" | Should -Be "Expected string length 8 but was 6. Strings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
        }

        It "Outputs verbose message for two different strings of the same length" {
            PesterBeFailureMessage "x" "y" | Should Be "String lengths are both 1. Strings differ at index 0.`nExpected: {y}`nBut was: {x}`n-----------^"
            PesterBeFailureMessage "x" "y" | Should -Be "String lengths are both 1. Strings differ at index 0.`nExpected: {y}`nBut was: {x}`n-----------^"
        }

        It "Replaces non-printable characters correctly" {
            PesterBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Should Be "String lengths are both 6. Strings differ at index 5.`nExpected: {\n\r\b\0\ty}`nBut was: {\n\r\b\0\tx}`n---------------------^"
            PesterBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Should -Be "String lengths are both 6. Strings differ at index 5.`nExpected: {\n\r\b\0\ty}`nBut was: {\n\r\b\0\tx}`n---------------------^"
        }

        It "The arrow points to the correct position when non-printable characters are replaced before the difference" {
            PesterBeFailureMessage "123`n456" "123`n789" | Should Be "String lengths are both 7. Strings differ at index 4.`nExpected: {123\n789}`nBut was: {123\n456}`n----------------^"
            PesterBeFailureMessage "123`n456" "123`n789" | Should -Be "String lengths are both 7. Strings differ at index 4.`nExpected: {123\n789}`nBut was: {123\n456}`n----------------^"
        }

        It "The arrow points to the correct position when non-printable characters are replaced after the difference" {
            PesterBeFailureMessage "abcd`n123" "abc!`n123" | Should Be "String lengths are both 8. Strings differ at index 3.`nExpected: {abc!\n123}`nBut was: {abcd\n123}`n--------------^"
            PesterBeFailureMessage "abcd`n123" "abc!`n123" | Should -Be "String lengths are both 8. Strings differ at index 3.`nExpected: {abc!\n123}`nBut was: {abcd\n123}`n--------------^"
        }
    }
}

InModuleScope Pester {
    Describe "BeExactly" {
        It "passes if letter case matches" {
            'a' | Should BeExactly 'a'
            'a' | Should -BeExactly 'a'
        }

        It "fails if letter case doesn't match" {
            'A' | Should Not BeExactly 'a'
            'A' | Should -Not -BeExactly 'a'
        }

        It "passes for numbers" {
            1 | Should BeExactly 1
            2.15 | Should BeExactly 2.15
            1 | Should -BeExactly 1
            2.15 | Should -BeExactly 2.15
        }

        It 'Compares Arrays properly' {
            $array = @(1,2,3,4,'I am a string', (New-Object psobject -Property @{ IAm = 'An Object' }))
            $array | Should BeExactly $array
            $array | Should -BeExactly $array
        }

        It 'Compares arrays with correct case-sensitive behavior' {
            $string = 'I am a string'
            $array = @(1,2,3,4,$string)
            $arrayWithCaps = @(1,2,3,4,$string.ToUpper())

            $array | Should Not BeExactly $arrayWithCaps
            $array | Should -Not -BeExactly $arrayWithCaps
        }
    }

    Describe "PesterBeExactlyFailureMessage" {
        It "Writes verbose message for strings that differ by case" {
            PesterBeExactlyFailureMessage "a" "A" | Should Be "String lengths are both 1. Strings differ at index 0.`nExpected: {A}`nBut was: {a}`n-----------^"
            PesterBeExactlyFailureMessage "a" "A" | Should -Be "String lengths are both 1. Strings differ at index 0.`nExpected: {A}`nBut was: {a}`n-----------^"
        }
    }
}