{"id":18832028,"url":"https://github.com/hamcrest/ramcrest","last_synced_at":"2025-04-14T04:22:26.508Z","repository":{"id":3767825,"uuid":"4844425","full_name":"hamcrest/ramcrest","owner":"hamcrest","description":"Hamcrest in Ruby","archived":false,"fork":false,"pushed_at":"2020-02-29T08:38:28.000Z","size":65,"stargazers_count":56,"open_issues_count":3,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-09T17:15:57.137Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hamcrest.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-30T17:46:26.000Z","updated_at":"2024-09-11T08:18:24.000Z","dependencies_parsed_at":"2022-08-17T23:00:47.338Z","dependency_job_id":null,"html_url":"https://github.com/hamcrest/ramcrest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamcrest%2Framcrest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamcrest%2Framcrest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamcrest%2Framcrest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamcrest%2Framcrest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hamcrest","download_url":"https://codeload.github.com/hamcrest/ramcrest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248819746,"owners_count":21166539,"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-08T01:56:35.675Z","updated_at":"2025-04-14T04:22:26.487Z","avatar_url":"https://github.com/hamcrest.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Ramcrest: a Ruby port of Hamcrest [![Build Status](https://secure.travis-ci.org/hamcrest/ramcrest.png?branch=master)](http://travis-ci.org/hamcrest/ramcrest)\n---------------------------------\n\nHamcrest is a powerful set of classes and functions that allow building\nup complicated matching expressions. The matchers provide useful descriptions\nof what they are trying to match, as well as a description of why a particular\nobject is not matched (a mismatch description).\n\nThe tests for the various matchers should provide great examples of usage because\neach matcher is actually tested using the Ramcrest framework.\n\nThere are integrations into MiniTest via `assert_that(actual, matcher)` and \ninto RSpec 2.11 via `expect(actual).that matcher`.\n\nHow do I use this?\n------------------\n\nIn your tests you need to\n\n````ruby\nrequire 'ramcrest'\n````\n\nwhich will bring in all of the base Ramcrest matchers and give you a new\n`assert_that(subject, matcher)` assertion for your MiniTest tests. Once you\nhave loaded the matchers you can now `include` them in your tests and start\nmatching away!\n\n````ruby\ndescribe My::Funky::Class do\n  include Ramcrest::HasAttribute\n  include Ramcrest::IncludesExactly\n\n  it \"only knows the funky chicken by default\" do\n    assert_that My::Funky::Class.new, has_attribute(:dances, includes_exactly(:funky_chicken))\n  end\nend\n````\n\nThis matches an object that has an attribute (actually just a no arg method)\ncalled `dances` where the result of calling that method is an `Enumerable` that\nincludes only the symbol `:funky_chicken`.\n\nWhat matchers are included?\n---------------------------\n\nThis is the list of current matchers. This list will grow over time and some\nnames might change in order to allow for integration into various testing\nframeworks.\n\n* `Ramcrest::Aint` - Logical negation.\n\n````ruby\nassert_that 1, aint(2)\nassert_that [2], aint(includes_exactly(1))\n````\n\n* `Ramcrest::Anything` - Everything's OK!\n\n````ruby\nassert_that \"a value\", anything\n````\n\n* `Ramcrest::Comparable` - Matchers for ordering comparisons.\n\n````ruby\nassert_that 1, greater_than(0)\nassert_that 2, less_than(3)\nassert_that 3, greater_or_equal_to(3)\nassert_that 4, less_or_equal_to(4)\n````\n\n* `Ramcrest::EqualTo` - Equality (via `==`).\n\n````ruby\nassert_that \"my value\", equal_to(\"my value\")\n````\n\n* `Ramcrest::HasAttribute` - Object attribute matching.\n\n````ruby\ndance = Struct.new(:twist).new(2)\nassert_that dance, has_attribute(:funk) # the attribute exits\nassert_that dance, has_attribute(:funk, equal_to(2)) # the attribute exists with a value\n````\n\n* `Ramcrest::HasSize` - Collection size.\n\n````ruby\nassert_that [1, 2], has_size(2)\nassert_that { :a =\u003e 1 }, has_size(less_or_equal_to(3))\n````\n\n* `Ramcrest::Includes` - Enumerable inclusion.\n\n````ruby\nassert_that [1, 2, 3], includes(2, 3)\nassert_that [6, 7, 2], includes(6, greater_than(6))\n````\n\n* `Ramcrest::IncludesExactly` - Enumerable equality.\n\n````ruby\nassert_that [1, 2, 3], includes_exactly(1, 2, 3)\nassert_that [3, 2, 1], aint(includes_exactly(1, 2, 3))\nassert_that [1], aint(includes_exactly(1, 2))\n````\n\n* `Ramcrest::IncludesInAnyOrderExactly` - Enumerable set equality.\n\n````ruby\nassert_that [2, 1, 3], includes_in_any_order_exactly(1, 2, 3)\nassert_that [1, 3, 4], aint(includes_in_any_order_exactly(1, 3))\n````\n\n* `Ramcrest::Is` - Syntactic sugar for equality.\n\n````ruby\nassert_that 1, is(1)\nassert_that 2, is(greater_than(1))\n````\n\n* `Ramcrest::SuchThat` - Ad hoc matchers.\n\n````ruby\nassert_that \"my string\", such_that do |value| \n  value =~ /string/ ? success : mismatch(\"didn't contain 'string'\")\nend\n````\n\nWriting your own matchers\n-------------------------\n\nThe simplest way to get started writing your own own matchers is to just make\nad-hoc matchers using `such_that` and chained match results. For example to put\ntogether a matcher for a certain type that has two properties you can simply\ndo:\n\n````ruby\ndef a_token(with_attributes)\n  name = has_attribute(:name, equal_to(with_attributes[:named]))\n  string = has_attribute(:string, equal_to(with_attributes[:string]))\n  description = \"a token named \u003c#{with_attributes[:named]}\u003e with string \u003c#{with_attributes[:string]}\u003e\"\n\n  such_that(description) do |actual|\n    name.matches?(actual).and_also { string.matches?(actual) }\n  end\nend\n````\n\nUsing `such_that` should be able to allow you to write any matcher that you\nwant in a simple and straight-forward way. If you outgrow these kinds of\nmatchers and want to move onto much larger possiblities, then you just need to\nimplement a class with two methods: `matches?(actual)` and `descriptionf`. The\n`matches?` method needs to return either `success` or `mismatch(description)`.\n\n````ruby\nclass AToken\n  include Ramcrest::Matcher\n\n  def initialize(name)\n    @matchers = [Ramcrest::HasAttribute.has_attribute(:name, equal_to(name))]\n  end\n\n  def with_string(string)\n    @matchers \u003c\u003c Ramcrest::HasAttribute.has_attribute(:string, equal_to(string))\n    self\n  end\n\n  def matches?(actual)\n    @matchers.\n      collect { |matcher| matcher.matches?(actual) }.\n      find(method(:success)) { |result| !result.matched? }\n  end\n\n  def description\n    \"a token that #{@matchers.collect(\u0026:description).join(' and ')}\"\n  end\nend\n\ndef a_token_named(name)\n  AToken.new(name)\nend\n\nassert_that something, is(a_token_named(\"foo\").with_string(\"bar\"))\nassert_that something_else, is(a_token_named(\"baz\"))\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamcrest%2Framcrest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhamcrest%2Framcrest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamcrest%2Framcrest/lists"}