{"id":19434504,"url":"https://github.com/mlin/should.ml","last_synced_at":"2025-04-24T20:32:07.970Z","repository":{"id":10245951,"uuid":"12351730","full_name":"mlin/should.ml","owner":"mlin","description":"Literate assertions for OCaml","archived":false,"fork":false,"pushed_at":"2019-10-03T23:58:14.000Z","size":172,"stargazers_count":18,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-03T10:37:57.416Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"OCaml","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/mlin.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":"2013-08-25T00:04:47.000Z","updated_at":"2023-03-31T12:07:05.000Z","dependencies_parsed_at":"2022-09-13T19:31:26.104Z","dependency_job_id":null,"html_url":"https://github.com/mlin/should.ml","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlin%2Fshould.ml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlin%2Fshould.ml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlin%2Fshould.ml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mlin%2Fshould.ml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mlin","download_url":"https://codeload.github.com/mlin/should.ml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250704833,"owners_count":21473769,"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":[],"created_at":"2024-11-10T14:46:38.042Z","updated_at":"2025-04-24T20:32:07.735Z","avatar_url":"https://github.com/mlin.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [should.ml](https://github.com/mlin/should.ml)\n**Maintainer: [Mike Lin](https://blog.mlin.net/)**\n\nThis OCaml library allows one to write assertion statements in a\ndomain-specific language roughly resembling plain English, making unit\ntests a bit nicer to read. No preprocessor or syntax extension is needed;\nrather, the DSL is expressed through some mild abuse of objects and\noperators.\n\nThis project is inspired by similar tools for various other languages,\nespecially [expect.js](https://github.com/LearnBoost/expect.js/) and its\npredecessor [should.js](https://github.com/visionmedia/should.js/).\n\n### Quick example\n\nRead $ as the letter 's', i.e. \"$hould\" as \"should\".\n\n```\nopen Should\n\nlet int_test_case () =\n    let x = 123 in begin\n        x $hould # equal 123;\n        x $hould # not # equal 0;\n        \n        x $hould # be # above 122;\n        x $hould # be # at # most 124;\n        \n        x $hould # be # within (122,123);\n        x $houldn't # be # within (1,3)\n    end\n\nlet list_test_case () =\n    let x = [1; 2; 3] in begin\n        list x $houldn't # be # empty;\n        list x $hould # have # length 3;\n        list x $hould # contain 2;\n        list x $hould # not # contain 0\n    end\n\nlet fun_test_case () =\n    let f = (function \"\" -\u003e invalid_arg \"f\" | s -\u003e String.lowercase s) in begin\n        calling f \"\" $hould # raise # any # exn;\n        calling f \"\" $hould # raise # exn # prefixed \"Invalid_argument\";\n        calling f \"foo\" $houldn't # raise # any # exn\n    end\n```\n\nSee also the library's [own unit tests](https://github.com/mlin/should.ml/blob/master/unit_tests.ml).\n\n### Installation\n\nShould.ml is available in [OPAM](http://opam.ocamlpro.com):\n`opam install should`. The findlib package name is also `should`.\n\nIf you don't use OPAM, set up [findlib](http://projects.camlcity.org/projects/findlib.html),\ndefine the `OCAMLFIND_DESTDIR` environment variable if necessary and\n\n```git clone https://github.com/mlin/should.ml.git \u0026\u0026 cd should.ml \u0026\u0026 ./configure \u0026\u0026 make \u0026\u0026 make install```\n\n[![Build Status](https://travis-ci.org/mlin/should.ml.png)](https://travis-ci.org/mlin/should.ml)\n\n### Reference\n\nThe assertions supported by should.ml are best shown by example. **Make sure to\n`open Should` so that all the necessary names are available.**\n\n#### Assertions on any type\n\n```\nx $hould # equal y\nx $hould # not # equal y\nx $hould # be # above y\nx $hould # not # be # above y\nx $hould # be # below y\nx $hould # not # be # below y\n```\n\n\nEvidently, any assertion can be negated by saying `$hould # not` instead of just\n`$hould`. One can also say `$houldn't`. **From this point on, the negated version\nof each assertion will not be shown.**\n\n```\nx $hould # be # at # least y\nx $hould # be # at # most y\nx $hould # physically # equal y\nx $hould # be # within (y,z)\nx $hould # be # strictly # within (y,z)\n```\n\n\"Physically equal\" uses `(==)` instead of `(=)`. \"Strictly within\" compares\nagainst the bounds using `(\u003e)` and `(\u003c)` rather than `(\u003e=)` and `(\u003c=)`.\n\n\nLastly, there's a generic assertion that the value satisfies given\npredicate, with `pred : 'a -\u003e bool`:\n\n```\nx $hould # satisfy pred\n```\n\n#### Float\n\nWhen `x : float`,\n```\nx $hould # be # nan\nx $hould # be # finite\n```\n\n#### String\n\nTo access some string-specific assertions, prefix the statement with `string`.\nWhen `x : string`,\n```\nstring x $hould # be # empty\nstring x $hould # have # length y\nstring x $hould # contain \"substring\"\nstring x $hould # be # matching (Str.regexp \"^The quick brown fox.*\")\n```\n\nThe `string` prefix is only used for these specific assertions, and cannot be\nused with the generic equality and comparison assertions found above. Of course,\nyou _can_ perform those assertions on string values; just don't include the\n`string` prefix. The same holds true for additional prefixes show below. This is\na minor wart in the DSL which may be possible to address with GADTs in the\nfuture.\n\n#### List and array\n\nWhen `x : list`,\n```\nlist x $hould # be # empty\nlist x $hould # have # length y\nlist x $hould # contain y\n```\n\nSimilarly, when `x : array`,\n```\narray x $hould # be # empty\narray x $hould # have # length y\narray x $hould # contain y\n```\n\nKeep in mind that the generic equality and comparison operators, with no prefix\nneeded, are also frequently useful with collections: e.g.\n`x $hould # equal [1; 2; 3]` or `List.length x $hould # be # above 3`.\n\nIt is possible to define additional container types for use with the `empty`,\n`length`, and `contain` assertions. An example for `Hashtbl`, which is not\navailable by default:\n\n```\nlet hashtbl x = object\n    method value = x\n    method length = Hashtbl.length x\n    method contains y = Hashtbl.mem x y\nend\n```\n\nOne can then say `hashtbl x $hould # contain some_key`, etc.\n\n#### Function calls\n\nOften one just wants to use the equality and comparison operators on the result\nof a function application, e.g. `f(x) $houldn't # be # nan`. A couple\ninteresting exception-related assertions are available with the `calling`\nprefix:\n\n```\ncalling f x $hould # raise # any # exn\ncalling f x $hould # raise # exn # prefixed \"MyException\"\ncalling f x $hould # raise # exn # satisfying pred\n```\n\nThe `prefixed` assertion checks that the function call raises an exception, and\nthat the given string is a prefix of `Printexc.to_string` on the exception. This\nusually starts with the exception constructor name, e.g. `\"Failure(\"`. For even\ngreater precision, there's the `satisfying` assertion, where\n`pred : exn -\u003e bool`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmlin%2Fshould.ml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmlin%2Fshould.ml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmlin%2Fshould.ml/lists"}