Skip to content

Commit d79edd3

Browse files
committed
Test custom TraceParent Equatable conformance
1 parent 25dd8f0 commit d79edd3

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

Sources/W3CTraceContext/TraceParent.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ extension W3C {
1717
public let parentID: String
1818
public let traceFlags: String
1919

20+
public init(traceID: String, parentID: String, traceFlags: String) {
21+
self.traceID = traceID
22+
self.parentID = parentID
23+
self.traceFlags = traceFlags
24+
}
25+
2026
public var sampled: Bool {
2127
self.traceFlags == "01"
2228
}
2329

24-
static let version = "00"
30+
public static let headerName = "traceparent"
31+
32+
private static let version = "00"
2533
}
2634

2735
// TODO: Trace State
@@ -31,6 +39,7 @@ extension W3C.TraceParent: Equatable {
3139
public static func == (lhs: Self, rhs: Self) -> Bool {
3240
lhs.traceID == rhs.traceID
3341
&& lhs.parentID == rhs.parentID
42+
&& lhs.traceFlags == rhs.traceFlags
3443
}
3544
}
3645

Tests/W3CTraceContextTests/TraceParentRawRepresentableTests.swift renamed to Tests/W3CTraceContextTests/TraceParentTests.swift

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
@testable import W3CTraceContext
14+
import W3CTraceContext
1515
import XCTest
1616

1717
final class TraceParentRawRepresentableTests: XCTestCase {
18-
// MARK: - Trace Parent -
18+
// MARK: - Encoding
19+
20+
func testEncodesToValidRawValue() {
21+
let traceParent = W3C.TraceParent(
22+
traceID: "0af7651916cd43dd8448eb211c80319c",
23+
parentID: "b7ad6b7169203331",
24+
traceFlags: "01"
25+
)
26+
27+
XCTAssertEqual(traceParent.rawValue, "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
28+
}
29+
30+
// MARK: - Decoding
1931

2032
func testDecodeValidTraceParentStringWithSampledFlag() {
2133
let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
@@ -47,16 +59,6 @@ final class TraceParentRawRepresentableTests: XCTestCase {
4759
XCTAssertFalse(traceParent.sampled)
4860
}
4961

50-
func testEncodesToValidRawValue() {
51-
let traceParent = W3C.TraceParent(
52-
traceID: "0af7651916cd43dd8448eb211c80319c",
53-
parentID: "b7ad6b7169203331",
54-
traceFlags: "01"
55-
)
56-
57-
XCTAssertEqual(traceParent.rawValue, "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
58-
}
59-
6062
func testDecodeFailsWithTooLongRawValue() {
6163
let rawValue = String(repeating: "42", count: 1000)
6264
XCTAssertUninitializedTraceParent(rawValue)
@@ -117,7 +119,37 @@ final class TraceParentRawRepresentableTests: XCTestCase {
117119
XCTAssertUninitializedTraceParent(rawValue)
118120
}
119121

120-
// TODO: Trace State
122+
// MARK: - Equatable
123+
124+
func testNonEqualTraceID() {
125+
let parent1 = W3C.TraceParent(rawValue: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
126+
let parent2 = W3C.TraceParent(rawValue: "00-12345678912345678912345678912345-b7ad6b7169203331-01")
127+
128+
XCTAssertNotNil(parent1)
129+
XCTAssertNotNil(parent2)
130+
131+
XCTAssertNotEqual(parent1, parent2)
132+
}
133+
134+
func testNonEqualParentID() {
135+
let parent1 = W3C.TraceParent(rawValue: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
136+
let parent2 = W3C.TraceParent(rawValue: "00-0af7651916cd43dd8448eb211c80319c-1234567891234567-01")
137+
138+
XCTAssertNotNil(parent1)
139+
XCTAssertNotNil(parent2)
140+
141+
XCTAssertNotEqual(parent1, parent2)
142+
}
143+
144+
func testNonEqualTraceFlags() {
145+
let parent1 = W3C.TraceParent(rawValue: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00")
146+
let parent2 = W3C.TraceParent(rawValue: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
147+
148+
XCTAssertNotNil(parent1)
149+
XCTAssertNotNil(parent2)
150+
151+
XCTAssertNotEqual(parent1, parent2)
152+
}
121153
}
122154

123155
private func XCTAssertUninitializedTraceParent(_ rawValue: String, file: StaticString = #file, line: UInt = #line) {

0 commit comments

Comments
 (0)