Skip to content

Commit 35f2ad5

Browse files
committed
Add unit test for CID
1 parent 3cd7007 commit 35f2ad5

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// CIDSuite.swift
3+
// MultiformatsKit
4+
//
5+
// Created by Christopher Jr Riley on 2025-03-25.
6+
//
7+
8+
import Foundation
9+
import Testing
10+
@testable import MultiformatsKit
11+
12+
@Suite("CID Suite") struct CIDSuite {
13+
@Test("Generates, then round trips between encoding and decoding a CIDv1.") func cidRoundtrip() async throws {
14+
let text = "I'm feeling great today!"
15+
16+
let cid = try await CID(content: text)
17+
18+
let encodedCID = cid.encode()
19+
try #require(encodedCID == "bafybeifhhzii2au6jnkhjr3ng3r5s7pn3td7xzbos547rlqonpgc76fde4", "The encoded string should match the original text.")
20+
21+
let decoded = try await CID.decode(from: cid.encode())
22+
23+
#expect(decoded == cid, "The decoded CID should match the original CID.")
24+
}
25+
}

Tests/MultiformatsKitTests/MultibaseSuite.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import Testing
1212
@Suite("Multibase Suite") struct MultibaseSuite {
1313

1414
@Suite("BaseX Tests") struct BaseXTests {
15-
@Test("Roundtrips between decoding and encoding base58btc.")
15+
@Test("Roundtrips between decoding and encoding base32.")
1616
func base32RoundTrip() async throws {
17+
guard let text = "I'm feeling great today!".data(using: .utf8) else { return }
1718
let baseEncoder = BaseCodec.base32Lower
18-
let encodedResult = try baseEncoder.decode("I'm feeling great today!")
19+
let encodedResult = baseEncoder.encode(text)
1920

20-
try #require(encodedResult == Data([0x4a, 0x45, 0x54, 0x57, 0x32, 0x49, 0x44, 0x47, 0x4d, 0x56, 0x53, 0x57, 0x59, 0x32, 0x4c, 0x4f, 0x4d, 0x34, 0x51, 0x47, 0x4f, 0x34, 0x54, 0x46, 0x4d, 0x46, 0x32, 0x43, 0x41, 0x35, 0x44, 0x50, 0x4d, 0x52, 0x51, 0x58, 0x53, 0x49, 0x49]), "The result should be equal to the original data.")
21+
try #require(encodedResult == "jetw2idgmvswy2lom4qgo4tfmf2ca5dpmrqxsii", "The result should be equal to the original data.")
2122

22-
let decodedResult = baseEncoder.encode(encodedResult)
23-
#expect(decodedResult == "I'm feeling great today!", "The encoded string should be equal to the original decoded string.")
23+
let decodedResult = try baseEncoder.decode(encodedResult)
24+
guard let decodedString = String(data: decodedResult, encoding: .utf8) else {
25+
return
26+
}
27+
#expect(decodedString == "I'm feeling great today!", "The result should be equal to the original text.")
2428
}
2529
}
2630
}

0 commit comments

Comments
 (0)