Skip to content

Commit

Permalink
Fixes for initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
macmoonshine committed Nov 12, 2023
1 parent bb065a3 commit 5a362fa
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 5.7
/*
MIT License

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
```

Expand Down
6 changes: 1 addition & 5 deletions Sources/sqids/Sqids.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import XCTest
@testable import sqids

final class SqidsBlocklistTests: XCTestCase {
final class BlocklistTests: XCTestCase {
func testDefaultBlocklist() throws {
let sqids = Sqids()

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -107,7 +107,7 @@ final class SqidsBlocklistTests: XCTestCase {
_ = try sqids.encode([0])
XCTFail()
}
catch(error: Sqids.Error.maximumAttemptsReached) {
catch Sqids.Error.maximumAttemptsReached {

}
}
Expand Down
79 changes: 79 additions & 0 deletions Tests/sqidsTests/EncodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion Tests/sqidsTests/MinLengthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5a362fa

Please sign in to comment.