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
|
//
// 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)
}
}
|