{"id":19760249,"url":"https://github.com/jmp/assertthat","last_synced_at":"2025-06-29T17:33:14.424Z","repository":{"id":65675097,"uuid":"323060061","full_name":"jmp/AssertThat","owner":"jmp","description":"More readable assertions in Swift.","archived":false,"fork":false,"pushed_at":"2023-02-09T19:33:25.000Z","size":84,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-27T23:42:28.408Z","etag":null,"topics":["swift","swift-assertions","swift-generics","testing"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-20T11:52:19.000Z","updated_at":"2023-02-05T14:45:21.000Z","dependencies_parsed_at":"2023-02-03T16:46:47.590Z","dependency_job_id":null,"html_url":"https://github.com/jmp/AssertThat","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jmp/AssertThat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmp%2FAssertThat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmp%2FAssertThat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmp%2FAssertThat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmp%2FAssertThat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmp","download_url":"https://codeload.github.com/jmp/AssertThat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmp%2FAssertThat/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262637630,"owners_count":23341164,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["swift","swift-assertions","swift-generics","testing"],"created_at":"2024-11-12T03:36:17.524Z","updated_at":"2025-06-29T17:33:14.398Z","avatar_url":"https://github.com/jmp.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AssertThat\n![Lint status](https://github.com/jmp/assert-that/workflows/lint/badge.svg)\n![Tests status](https://github.com/jmp/assert-that/workflows/tests/badge.svg)\n[![codecov](https://codecov.io/gh/jmp/assert-that/branch/master/graph/badge.svg?token=P6HAB7OM3L)](https://codecov.io/gh/jmp/assert-that)\n\nThis library tries to make [AssertJ](https://assertj.github.io/doc/)-style readable assertions usable in Swift.\n\nIt is currently experimental and **super work in progress**. 🚧\n\n## Motivation\n\nThe idea is that instead of having to write things like `XCTAssertEqual(someVar, \"foo\")` we can write\nsomething like this instead:\n\n```swift\nassertThat(someVar).isEqualTo(\"foo\")\n```\n\nThis also allows more complicated assertion chains, for example:\n\n```swift\nassertThat(\"The quick brown fox\")\n    .startsWith(\"The\")\n    .contains(\"brown\")\n    .endsWith(\"fox\")\n```\n\nIt should also allow the use of extensions to support custom assertions.\n\n## Existing solutions\n\nAs far as I know, there is no port of AssertJ or similar library available for Swift. The closest match is\nprobably [Swift Hamcrest](https://github.com/nschum/SwiftHamcrest), which is quite mature and a very\nnice alternative for Swift. The main difference is the syntax and how the assertions read. Another\nnoteworthy project is [Nimble](https://github.com/Quick/Nimble), which also has slightly different way\nof expressing the assertions. \n\n## How does it work\n\nIt makes use of Swift generics and extensions. The generic function `assertThat` returns an\n`Assertion` struct holding the subject of the assertion. Literally everything else is implemented as\nan extension of `Assertion`.\n\nFor example, `startsWith` is implemented as an extension of `Assertion` whose subject conforms to\n`StringProtocol`. If the subject conforms to the `Comparable` protocol, then things like `isLessThan`\nand `isBetween` can be used.\n\nWe can even have assertions for subjects that conform to multiple protocols at the same time.\nFor example, let's say we want to assert that a value is positive (strictly greater than zero). Creating\nan assertion for a `Numeric` subject would allow us to assert something on `Numeric` types. However,\nnot all `Numeric` types are `Comparable`. To solve this, we can restrict the subject to comparable numeric\ntypes by using `Numeric \u0026 Comparable`.\n\nTo implement assertions for your own types, chances are some of the built-in ones (like those for\n`Equatable`) already work. But it's also trivial to write your own by writing extensions for\n`Assertion`.\n\n## Example usage\n\nSee example repository in [AssertThatExample](https://github.com/jmp/AssertThatExample).\n\nFirst, import the module:\n\n```swift\nimport AssertThat\n```\n\nCommon assertions for optionals:\n\n```swift\nassertThat(x).isNil()\nassertThat(x).isNotNil()\n```\n\nBooleans:\n\n```swift\nassertThat(x).isTrue()\nassertThat(x).isFalse()\n```\n\nEquatables:\n\n```swift\nassertThat(x).isEqualTo(y)\nassertThat(x).isNotEqualTo(y)\nassertThat(x).isIn(someSequence)\nassertThat(x).isNotIn(someSequence)\n```\n\nComparables:\n\n```swift\nassertThat(x).isGreaterThanOrEqualTo(y)\nassertThat(x).isLessThan(y)\nassertThat(x).isBetween(y, z)\nassertThat(x).isStrictlyBetween(y, z)\n```\n\nStrings:\n\n```swift\nassertThat(someString).isEmpty()\nassertThat(someString).isNotEmpty()\n\n// Chaining is also supported\nassertThat(someString)\n    .startsWith(\"foo\")\n    .contains(\"bar\")\n    .endsWith(\"baz\")\n    .matches(\"^foobar.+$\")\n    .doesNotMatch(\"^Test$\")\n```\n\nSequences:\n\n```swift\nassertThat(sequence).contains(x)\nassertThat(sequence).doesNotContain(x)\n```\n\nCollections:\n\n```swift\nassertThat(collection).isEmpty()\nassertThat(collection).isNotEmpty()\n```\n\nNote that since a `Collection` also conforms to the `Sequence` protocol,\nyou can use any of the assertions of `Sequence` with collections:\n\n```swift\nassertThat(collection).contains(x)\nassertThat(collection).doesNotContain(x)\n```\n\nErrors:\n\n```swift\nassertThat { someBlockOfCode }.throws(SomeErrorType.someSpecificError)\nassertThat { someBlockOfCode }.throws(SomeErrorType.self)\nassertThat { someBlockOfCode }.doesNotThrow(someError)\nassertThat { someBlockOfCode }.throwsAnError() // Throws any error\nassertThat { someBlockOfCode }.doesNotThrowAnError() // Doesn't throws any error\n```\n\nBrowse the code and test cases for more complete examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmp%2Fassertthat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmp%2Fassertthat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmp%2Fassertthat/lists"}