From 941e5e606034b9bbb847fb5afe2ad3bad75bf139 Mon Sep 17 00:00:00 2001 From: Clemens Wagner Date: Sun, 12 Nov 2023 11:55:18 +0100 Subject: [PATCH] Fixes for version 0.1.0 --- Package.swift | 2 +- README.md | 2 +- Sources/sqids/Sqids.swift | 6 +- .../{SqidsBlocklist.swift => Blocklist.swift} | 6 +- Tests/sqidsTests/EncodeTests.swift | 79 +++++++++++++++++++ Tests/sqidsTests/MinLengthTests.swift | 2 +- 6 files changed, 86 insertions(+), 11 deletions(-) rename Tests/sqidsTests/{SqidsBlocklist.swift => Blocklist.swift} (96%) diff --git a/Package.swift b/Package.swift index 7f9f250..16535e8 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 5.7 /* MIT License diff --git a/README.md b/README.md index 9f2be04..ae4103f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Add the following dependency to your Swift `Package.swift`: ```swift dependencies.append( - .package(url: "https://github.com/squids/squitss-swift.git", from: "1.0.0") + .package(url: "https://github.com/squids/squids-swift.git", from: "0.1.0") ) ``` diff --git a/Sources/sqids/Sqids.swift b/Sources/sqids/Sqids.swift index 4e6f9ee..0064970 100644 --- a/Sources/sqids/Sqids.swift +++ b/Sources/sqids/Sqids.swift @@ -32,7 +32,6 @@ public struct Sqids { case invalidMinLength(Int) case valueError(Id) case maximumAttemptsReached - case invalidId } public static let defaultAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" public static let minAlphabetLength = 3 @@ -147,10 +146,7 @@ public struct Sqids { if !id.isEmpty { let characterSet = CharacterSet(alphabet.flatMap({ $0.unicodeScalars })) - if !id.unicodeScalars.reduce(true, { $0 && characterSet.contains($1) }) { - throw Error.invalidId - } - else { + if id.unicodeScalars.reduce(true, { $0 && characterSet.contains($1) }) { let offset = alphabet.firstIndex(of: id.first!)! var alphabet = splitReverse(offset: offset) var value = String(Array(id).suffix(from: 1)) diff --git a/Tests/sqidsTests/SqidsBlocklist.swift b/Tests/sqidsTests/Blocklist.swift similarity index 96% rename from Tests/sqidsTests/SqidsBlocklist.swift rename to Tests/sqidsTests/Blocklist.swift index 9ce7e3f..60000df 100644 --- a/Tests/sqidsTests/SqidsBlocklist.swift +++ b/Tests/sqidsTests/Blocklist.swift @@ -25,7 +25,7 @@ import XCTest @testable import sqids -final class SqidsBlocklistTests: XCTestCase { +final class BlocklistTests: XCTestCase { func testDefaultBlocklist() throws { let sqids = Sqids() @@ -76,7 +76,7 @@ final class SqidsBlocklistTests: XCTestCase { XCTAssertEqual(try sqids.decode("5sQRZO"), [1, 2, 3]) } - func test_match_against_short_blocklist_word() throws { + func testMatchAgainstShortBlocklistWord() throws { let sqids = Sqids(blocklist: ["pnd"]) XCTAssertEqual(try sqids.decode(try sqids.encode([1000])), [1000]) @@ -107,7 +107,7 @@ final class SqidsBlocklistTests: XCTestCase { _ = try sqids.encode([0]) XCTFail() } - catch(error: Sqids.Error.maximumAttemptsReached) { + catch Sqids.Error.maximumAttemptsReached { } } diff --git a/Tests/sqidsTests/EncodeTests.swift b/Tests/sqidsTests/EncodeTests.swift index cc0c6e1..e78993e 100644 --- a/Tests/sqidsTests/EncodeTests.swift +++ b/Tests/sqidsTests/EncodeTests.swift @@ -66,4 +66,83 @@ final class EncodeTests: XCTestCase { XCTAssertEqual(try sqids.decode(id), numbers) } } + + func testIncrementalNumbersSameIndex0() throws { + let sqids = Sqids() + let ids: [String: Sqids.Ids] = [ + "SvIz": [0, 0], + "n3qa": [0, 1], + "tryF": [0, 2], + "eg6q": [0, 3], + "rSCF": [0, 4], + "sR8x": [0, 5], + "uY2M": [0, 6], + "74dI": [0, 7], + "30WX": [0, 8], + "moxr": [0, 9], + ] + for (id, numbers) in ids { + XCTAssertEqual(try sqids.encode(numbers), id) + XCTAssertEqual(try sqids.decode(id), numbers) + } + } + + + func testIncrementalNumbersSameIndex1() throws { + let sqids = Sqids() + let ids: [String: Sqids.Ids] = [ + "SvIz": [0, 0], + "nWqP": [1, 0], + "tSyw": [2, 0], + "eX68": [3, 0], + "rxCY": [4, 0], + "sV8a": [5, 0], + "uf2K": [6, 0], + "7Cdk": [7, 0], + "3aWP": [8, 0], + "m2xn": [9, 0], + ] + for (id, numbers) in ids { + XCTAssertEqual(try sqids.encode(numbers), id) + XCTAssertEqual(try sqids.decode(id), numbers) + } + } + + func testMultiInput() throws { + let sqids = Sqids() + let numbers: Sqids.Ids = Array(1..<100) + let output = try sqids.decode(try sqids.encode(numbers)) + + XCTAssertEqual(numbers, output) + } + + func testEncodingNoNumbers() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.encode([]), "") + } + + func testDecodingEmptyString() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.decode(""), []) + } + + func testDecodingInvalidCharacter() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.decode("*"), []) + } + + func testEncodeOutOfRangeNumbers() throws { + let sqids = Sqids() + + do { + _ = try sqids.encode([-1]) + XCTFail() + } + catch Sqids.Error.valueError(let id) { + XCTAssertEqual(-1, id) + } + } } diff --git a/Tests/sqidsTests/MinLengthTests.swift b/Tests/sqidsTests/MinLengthTests.swift index b6377e7..56d4107 100644 --- a/Tests/sqidsTests/MinLengthTests.swift +++ b/Tests/sqidsTests/MinLengthTests.swift @@ -26,7 +26,7 @@ import XCTest @testable import sqids final class MinLengthTests: XCTestCase { - func test_simple() throws { + func testSimple() throws { let sqids = Sqids(minLength: Sqids.defaultAlphabet.count) let numbers: Sqids.Ids = [1, 2, 3] let id = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM"