Questions tagged [matcher]

Matchers are objects used among other things by testing libraries to check, if an object matches an abstract description of an expected state. Do not use this tag for use of the Matcher class for regular expression matching, use [regex] instead.

The most basic matcher is a function from an object to boolean, where the return value denotes whether the argument matches the description represented by the matcher. Apart from doing the actual matching matchers most of the time also provide some information about why a matcher did not match, and they can be combined using methods or functions, that take matchers as an argument and return new matchers (see the example below).

Matchers allow expectations to be written in an internal DSL. For example using Hamcrest and JUnit assertions can be written like this:

assertThat("nuts", biscuit.nutCount(), allOf(greaterThan(3), evenNumber()));

This is relatively easy to understand, even without knowing how it exactly works, and it will provide failure messages that explain the reason why the assertion failed. In the above example allOf(), greaterThan() and evenNumber() are all methods returning matchers.

Matchers are also used in other libraries, for example mock libraries use matchers in order to formulate expectations about calls to mocks, stubs or spies, like in the following example with Mockito.

when(mockedList.get(anyInt())).thenReturn("element");

Here anyInt() returns a matcher that matches any Integer or int.

458 questions
13
votes
2 answers

How to avoid double-extracting of overlapping patterns in SpaCy with Matcher?

I need to extract item combination from 2 lists by means of python Spacy Matcher. The problem is following: Let us have 2 lists: colors=['red','bright red','black','brown','dark brown'] animals=['fox','bear','hare','squirrel','wolf'] I match the…
Victoria
  • 395
  • 3
  • 13
11
votes
1 answer

Is there any way to use Jasmine default matchers within custom matchers?

I have a custom matcher in some Jasmine test specs of the form: this.addMatchers({ checkContains: function(elem){ var found = false; $.each( this.actual, function( actualItem ){ // Check if these objects contain the…
Fishtoaster
  • 1,809
  • 2
  • 20
  • 36
11
votes
8 answers

Jest: expect object not to have property

I want to write a test that asserts a given object does not have certain properties. Say I have a function function removeFooAndBar(input) { delete input.foo; delete input.bar; return input; } Now I want to write a…
joegomain
  • 536
  • 1
  • 5
  • 22
11
votes
2 answers

Matching multiple properties in one Matcher

I need to write Matcher which will check multiple properties. For single property i've used: import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasProperty; import org.hamcrest.Matcher; import org.hamcrest.Matchers; …
Szympek
  • 183
  • 1
  • 2
  • 7
11
votes
1 answer

How to show custom failure message in Specs2 (Scala)?

For example, for code like this: myNum must beEqualTo("SOME INTERESTING TEXT") The message will be like the following: java.lang.Exception: ArrayBuffer() doesn't have size 1 but size 0 Is there an elegant way to get customised message displayed…
Johnny
  • 14,397
  • 15
  • 77
  • 118
11
votes
2 answers

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

My method in the service and test class : public void updateSubModuleOrder(Long[] data, Long moduleSysId, Long userId) { try { for (int i = 0; i < data.length; i++) { SubModule subModule=new SubModule(); int temp…
user2375298
  • 1,001
  • 4
  • 15
  • 28
10
votes
5 answers

Problem with using spacy.matcher.matcher.Matcher.add() method

I am getting an error when trying to use spacy matcher: ~\Anaconda3\lib\site-packages\spacy\matcher\matcher.pyx in spacy.matcher.matcher.Matcher.add() TypeError: add() takes exactly 2 positional arguments (3 given) Is there any alternate function…
Vignesh c s
  • 101
  • 1
  • 4
10
votes
2 answers

How to use the isA-Matcher

I have a certain method that delivers a Restriction-object (where Restriction is an interface). And since its implementation is already testet, I just want to test if my method actually delivers a RestrictionImpl-object. I saw that there are…
danielspaniol
  • 2,228
  • 21
  • 38
9
votes
2 answers

In spacy, Is it possible to get the corresponding rule id in a match of matches

In Spacy 2.x, I use the matcher to find specific tokens in my text corpus. Each rule has an ID ('class-1_0' for example). During parse, I use the callback on_match to handle each match. Is there a solution to retrieve the rule used to find the match…
k3z
  • 538
  • 5
  • 14
8
votes
2 answers

Random RSpec failures comparing Floats (Eq matcher)

I'm not even sure where to begin here. Sorry if this is a duplicate, but I don't even know what to search for or what this particular issue is called. Randomly, and not all that often, a test in my RSpec suite will fail and I'll get an error like…
danielricecodes
  • 3,446
  • 21
  • 23
8
votes
1 answer

How to make ExampleMatcher to match just one property?

How to implement ExampleMatcher, to match just one property randomly from my class and ignore the other properties? Assume my class like this : Public Class Teacher() { String id; String name; String address; String phone; int…
Jigu Jigu
  • 145
  • 1
  • 10
8
votes
1 answer

Mockito matcher to match a method with generics and a supplier

I'm using Java 1.8.0_131, Mockito 2.8.47 and PowerMock 1.7.0. My question is not related to PowerMock, it is released to a Mockito.when(…) matcher. I need a solution to mock this method which is called by my class under test: public static
McPringle
  • 1,939
  • 2
  • 16
  • 19
8
votes
1 answer

OpenCV Finding Correct Threshold to Determine Image Match or Not with Matching Score

I'm currently making a recognition program using various feature extractor and various matcher. Using the score from the matcher, I want to create a score threshold which can further determine if it's a correct match or incorrect one. I am trying to…
Arwego
  • 155
  • 3
  • 12
8
votes
1 answer

Using clang matchers to detect sequence of patterns

Is it possible to use clang matchers to identify sequence of patterns in a program? For example I need to find cases in which pattern1 happens before pattern2. For instance: Pattern1 = assigning a value to pointer P pattern2 = dereferencing pointer…
Mehrnoosh EP
  • 161
  • 7
8
votes
1 answer

Mock a method with an object parameter with Mockito

In my unit test i want to mock the interaction with elasticsearch by doing the following when(cityDefinitionRepository.findCitiesNearby(geoPoint, SOURCE,…
Glenn Van Schil
  • 1,059
  • 3
  • 15
  • 33
1 2
3
30 31