Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active December 10, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanlou/e33bcc1c974dbda258b74b35087b33c8 to your computer and use it in GitHub Desktop.
Save khanlou/e33bcc1c974dbda258b74b35087b33c8 to your computer and use it in GitHub Desktop.
struct BundleVersion: Comparable {
let tuple: (Int, Int, Int)
init(string: String) {
let numbers = string.split(separator: ".").compactMap({ Int($0) })
let major = numbers.count >= 1 ? numbers[0] : 0
let minor = numbers.count >= 2 ? numbers[1] : 0
let patch = numbers.count >= 3 ? numbers[2] : 0
self.tuple = (major, minor, patch)
}
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.tuple == rhs.tuple
}
static func < (lhs: Self, rhs: Self) -> Bool {
return lhs.tuple < rhs.tuple
}
}
class BundleVersionTests: XCTestCase {
func testBasic() {
XCTAssert(BundleVersion(string: "1").tuple == (1, 0, 0))
XCTAssert(BundleVersion(string: "2.5.3").tuple == (2, 5, 3))
XCTAssert(BundleVersion(string: "3.6").tuple == (3, 6, 0))
}
func testComparable() {
XCTAssertLessThan(BundleVersion(string: "1"), BundleVersion(string: "2.5.3"))
XCTAssertLessThan(BundleVersion(string: "2.5.3"), BundleVersion(string: "3.6"))
XCTAssertGreaterThan(BundleVersion(string: "4"), BundleVersion(string: "2.5.3"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment