Skip to content

Commit

Permalink
Interop tests (#143)
Browse files Browse the repository at this point in the history
* adding interop tests using exemplar copied from automerge-core
  • Loading branch information
heckj authored Apr 4, 2024
1 parent e6fc13d commit a6133eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Binary file added Tests/AutomergeTests/Fixtures/exemplar
Binary file not shown.
53 changes: 52 additions & 1 deletion Tests/AutomergeTests/InteropTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
@testable import Automerge
import Automerge
import Foundation
import XCTest

extension Data {
/// Returns the data as a hex-encoded string.
/// - Parameter uppercase: A Boolean value that indicates whether the hex encoded string uses uppercase letters.
func hexEncodedString(uppercase: Bool = false) -> String {
let format = uppercase ? "%02hhX" : "%02hhx"
return map { String(format: format, $0) }.joined()
}
}

@available(macOS 12, iOS 16, *)
class InteropTests: XCTestCase {
var markdownData: Data? = nil
Expand Down Expand Up @@ -41,6 +50,48 @@ class InteropTests: XCTestCase {
func testFixtureFileLoad() throws {
XCTAssertNotNil(markdownData)
}

struct ExemplarStructure: Codable, Equatable {
var title: String
var notes: AutomergeText
var timestamp: Date
var location: URL
var counter: Counter
var int: Int
var uint: UInt
var fp: Double
var bytes: Data
var bool: Bool
}

func testExemplarAutomergeDocRepresentations() throws {
guard let data = try dataFrom(resource: "exemplar") else {
XCTFail("Unable to load exemplar fixture")
return
}
let doc = try Document(data)
let decoder = AutomergeDecoder(doc: doc)

let formatter = ISO8601DateFormatter()
formatter.formatOptions.insert(.withFractionalSeconds)
let expectedDate = formatter.date(from: "1941-04-26T08:17:01.000Z")

let magicValue: String = "856f4a83"
// hex values for the magic value of an Automerge document

let exemplar = try decoder.decode(ExemplarStructure.self)

XCTAssertEqual(exemplar.timestamp, expectedDate)
XCTAssertEqual(exemplar.title, "Hello πŸ‡¬πŸ‡§πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘¦πŸ˜€")
XCTAssertEqual(exemplar.notes.value, "πŸ‡¬πŸ‡§πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘¦πŸ˜€")
XCTAssertEqual(exemplar.location, URL(string: "https://automerge.org/")!)
XCTAssertEqual(exemplar.counter.value, 5)
XCTAssertEqual(exemplar.int, -4)
XCTAssertEqual(exemplar.uint, UInt(UInt64.max))
XCTAssertEqual(exemplar.fp, 3.14159267, accuracy: 0.0000001)
XCTAssertEqual(exemplar.bytes.hexEncodedString(), magicValue)
XCTAssertEqual(exemplar.bool, true)
}

func testAttributedStringParse() throws {
let data = try XCTUnwrap(markdownData)
Expand Down

0 comments on commit a6133eb

Please sign in to comment.