File: Expression.ValueTests.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (215 lines) | stat: -rw-r--r-- 6,295 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@testable @_spi(ForToolsIntegrationOnly) import Testing

@Suite("Expression.Value Tests")
struct Expression_ValueTests {

  @Test("Value reflecting a simple struct with one property")
  func simpleStruct() throws {
    struct Foo {
      var x: Int = 123
    }

    let foo = Foo()

    let value = Expression.Value(reflecting: foo)
    let children = try #require(value.children)
    try #require(children.count == 1)

    let child = try #require(children.first)
    #expect(child.label == "x")
    #expect(child.typeInfo == TypeInfo(describing: Int.self))
    #expect(String(describing: child) == "123")
    #expect(child.children == nil)
  }

  @Test("Value reflecting an object with multiple non-cyclic references")
  func multipleNonCyclicReferences() throws {
    class C: CustomStringConvertible {
      var one: C?
      var two: C?
      let description: String

      init(description: String) {
        self.description = description
      }
    }

    let x = C(description: "x")
    let y = C(description: "y")
    x.one = y
    x.two = y

    let value = Expression.Value(reflecting: x)
    let children = try #require(value.children)
    try #require(children.count == 3)

    let one = try #require(value.children?[0].children?.first)
    #expect(String(describing: one) == "y")

    let two = try #require(value.children?[1].children?.first)
    #expect(String(describing: two) == "y")
  }

  @Test("Value reflecting an object with multiple cyclic references")
  func multipleCyclicReferences() throws {
    class C: CustomStringConvertible {
      var one: C?
      weak var two: C?
      let description: String

      init(description: String) {
        self.description = description
      }
    }

    let x = C(description: "x")
    let y = C(description: "y")
    x.one = y
    x.two = y
    y.two = x

    let value = Expression.Value(reflecting: x)
    let children = try #require(value.children)
    try #require(children.count == 3)

    do {
      let one = try #require(value.children?[0].children?.first)
      #expect(String(describing: one) == "y")

      let oneChildren = try #require(one.children)
      try #require(oneChildren.count == 3)
      try #require(oneChildren[1].label == "two")

      let childlessX = try #require(oneChildren[1].children?.first)
      #expect(String(describing: childlessX) == "x")
    }
    do {
      let two = try #require(value.children?[1].children?.first)
      #expect(String(describing: two) == "y")

      let twoChildren = try #require(two.children)
      try #require(twoChildren.count == 3)
      try #require(twoChildren[1].label == "two")

      let childlessX = try #require(twoChildren[1].children?.first)
      #expect(String(describing: childlessX) == "x")
    }
  }

  @Test("Value reflecting an object with a cyclic reference to itself")
  func recursiveObjectReference() throws {
    class RecursiveItem {
      weak var anotherItem: RecursiveItem?
      let boolValue = false
    }

    let recursiveItem = RecursiveItem()
    recursiveItem.anotherItem = recursiveItem

    let value = Expression.Value(reflecting: recursiveItem)
    let children = try #require(value.children)
    try #require(children.count == 2)

    let firstChild = try #require(children.first)
    #expect(firstChild.label == "anotherItem")

    let lastChild = try #require(children.last)
    #expect(lastChild.label == "boolValue")
    #expect(String(describing: lastChild) == "false")
  }

  @Test("Value reflecting an object with a reference to another object which has a cyclic back-reference the first")
  func cyclicBackReference() throws {
    class One {
      var two: Two?
    }
    class Two {
      weak var one: One?
    }

    let one = One()
    let two = Two()
    one.two = two
    two.one = one

    let value = Expression.Value(reflecting: one)
    let children = try #require(value.children)
    try #require(children.count == 1)

    let twoChild = try #require(children.first)
    #expect(twoChild.label == "two")
    #expect(twoChild.typeInfo == TypeInfo(describing: Two?.self))
    let twoChildChildren = try #require(twoChild.children)
    try #require(twoChildChildren.count == 1)
    let twoChildChildrenOptionalChild = try #require(twoChildChildren.first)
    #expect(twoChildChildrenOptionalChild.label == "some")
    let twoChildChildrenOptionalChildren = try #require(twoChildChildrenOptionalChild.children)
    try #require(twoChildChildrenOptionalChildren.count == 1)

    let oneChild = try #require(twoChildChildrenOptionalChildren.first)
    #expect(oneChild.label == "one")
    #expect(oneChild.typeInfo == TypeInfo(describing: One?.self))
    let oneChildChildren = try #require(oneChild.children)
    try #require(oneChildChildren.count == 1)
    let oneChildChildrenOptionalChild = try #require(oneChildChildren.first)
    #expect(oneChildChildrenOptionalChild.label == "some")
    #expect(oneChildChildrenOptionalChild.children == nil)
  }

  @Test("Value reflecting an object with two back-references to itself",
        .bug("https://github.com/swiftlang/swift-testing/issues/785#issuecomment-2440222995"))
  func multipleSelfReferences() {
    class A {
      weak var one: A?
      weak var two: A?
    }

    let a = A()
    a.one = a
    a.two = a

    let value = Expression.Value(reflecting: a)
    #expect(value.children?.count == 2)
  }

  @Test("Value reflecting an object in a complex graph which includes back-references",
        .bug("https://github.com/swiftlang/swift-testing/issues/785"))
  func complexObjectGraphWithCyclicReferences() throws {
    class A {
      var c1: C!
      var c2: C!
      var b: B!
    }
    class B {
      weak var a: A!
      var c: C!
    }
    class C {
      weak var a: A!
    }

    let a = A()
    let b = B()
    let c = C()
    a.c1 = c
    a.c2 = c
    a.b = b
    b.a = a
    b.c = c
    c.a = a

    let value = Expression.Value(reflecting: a)
    #expect(value.children?.count == 3)
  }

}