Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DarcyWebMatchers with HasClass matcher #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/redhat/darcy/web/matchers/DarcyWebMatchers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.redhat.darcy.web.matchers;

import com.redhat.darcy.web.api.elements.HtmlElement;

import org.hamcrest.Matcher;

public abstract class DarcyWebMatchers {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worthwhile to extend darcy-ui's DarcyMatchers. This makes it easier to consume all matchers, and prevents name collisions (which is important because I imagine these will be ripe for static imports).

public static <T extends HtmlElement> Matcher<T> hasClass(Matcher<? super String> matcher) {
return new HasClass<>(matcher);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/redhat/darcy/web/matchers/HasClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redhat.darcy.web.matchers;

import com.redhat.darcy.web.api.elements.HtmlElement;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class HasClass<T extends HtmlElement> extends TypeSafeMatcher<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend TypeSafeDiagnosingMatcher. This allows us to describe the element that didn't match. Otherwise, it's actually better to use assertThat(element.getClasses(), hasItem(equalTo("foo")) because this will give you more context on the error. It may still be more flexible to assert against the class set because you have all of the collections matchers available to you, however those can't give information about the underlying element of course, so I could still see some value in this darcy web matcher if we provided that information.

private Matcher<? super String> matcher;

public HasClass(Matcher<? super String> matcher) {
this.matcher = matcher;
}

@Override
protected boolean matchesSafely(T t) {
for (String clazz : t.getClasses()) {
if (matcher.matches(clazz)) {
return true;
}
}

return false;
}

@Override
public void describeTo(Description description) {
description.appendText("an Element that has the class");
matcher.describeTo(description);
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/redhat/darcy/web/matchers/HasClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.redhat.darcy.web.matchers;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.redhat.darcy.web.api.elements.HtmlElement;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.ArrayList;
import java.util.List;

@RunWith(JUnit4.class)
public class HasClassTest {
@Test
public void shouldMatchFullClass() {
HasClass<HtmlElement> matcher = new HasClass<>(equalTo("clazz"));
HtmlElement mockElement = mock(HtmlElement.class);

List<String> classes = new ArrayList<>();
classes.add("clazz");
when(mockElement.getClasses()).thenReturn(classes);

assertTrue(matcher.matches(mockElement));
}

@Test
public void shouldMatchPartialClass() {
HasClass<HtmlElement> matcher = new HasClass<>(containsString("cla"));
HtmlElement mockElement = mock(HtmlElement.class);

List<String> classes = new ArrayList<>();
classes.add("clazz");
when(mockElement.getClasses()).thenReturn(classes);

assertTrue(matcher.matches(mockElement));
}

@Test
public void shouldNotMatchMissingClass() {
HasClass<HtmlElement> matcher = new HasClass<>(containsString("missing"));
HtmlElement mockElement = mock(HtmlElement.class);

List<String> classes = new ArrayList<>();
classes.add("clazz");
when(mockElement.getClasses()).thenReturn(classes);

assertFalse(matcher.matches(mockElement));
}
}