Skip to content

Commit

Permalink
add comparator test
Browse files Browse the repository at this point in the history
  • Loading branch information
dayaffe committed Jan 9, 2025
1 parent 9b756c9 commit 7181815
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ let package = Package(
],
resources: [ .process("Resources") ]
),
.testTarget(
name: "SmithyCBORTests",
dependencies: ["SmithyCBOR", "ClientRuntime", "SmithyTestUtil"]
),
.testTarget(
name: "SmithyHTTPClientTests",
dependencies: [
Expand Down
72 changes: 72 additions & 0 deletions Tests/SmithyCBORTests/ReaderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import XCTest
@testable @_spi(SmithyReadWrite) import SmithyCBOR
import SmithyTestUtil

class ReaderTests: XCTestCase {

func test_readsNull() async throws {
let cborData = Data()
let reader = try SmithyCBOR.Reader.from(data: cborData)
XCTAssertEqual(reader.cborValue, .null)
}

func test_compare_nulls() async throws {
let cborData1 = Data()
let cborData2 = Data()
XCTAssertTrue(try CBORComparator.cborData(cborData1, isEqualTo: cborData2))
}

func test_compare_complex() async throws {
// cborData1 is semantically different from cborData2, but logically equivalent (reordered keys in the map)
let cborData1 = Data([
0xBF, // Start of outer indefinite-length map

// Key: "defaults"
0x68, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74, 0x73,
0xBF, // Start of "defaults" map

// Key: "customString"
0x6C, 0x63, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67,
// Indefinite-length string for "hello"
0x7F, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0xFF,

// Key: "defaultString"
0x6D, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74,
0x53, 0x74, 0x72, 0x69, 0x6E, 0x67,
// Indefinite-length string for "hi"
0x7F, 0x62, 0x68, 0x69, 0xFF,

0xFF, // End of the "defaults" map
0xFF // End of indefinite-length map
])
let cborData2 = Data([
0xBF, // Start of outer indefinite-length map

// Key: "defaults"
0x68, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74, 0x73,
0xBF, // Start of "defaults" map

// Key: "defaultString"
0x6D, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74,
0x53, 0x74, 0x72, 0x69, 0x6E, 0x67,
// Indefinite-length string for "hi"
0x7F, 0x62, 0x68, 0x69, 0xFF,

// Key: "customString"
0x6C, 0x63, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67,
// Indefinite-length string for "hello"
0x7F, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0xFF,

0xFF, // End of the "defaults" map
0xFF // End of indefinite-length map
])
XCTAssertTrue(try CBORComparator.cborData(cborData1, isEqualTo: cborData2))
}
}

0 comments on commit 7181815

Please sign in to comment.