-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#389 Support More Complex CSS Selectors
Test transformer.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/test/java/de/retest/web/selenium/css/TransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package de.retest.web.selenium.css; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import de.retest.recheck.ui.descriptors.Element; | ||
|
||
@ExtendWith( MockitoExtension.class ) | ||
class TransformerTest { | ||
|
||
private static final String SELECTOR_PATTERN = "[a-z;]+"; | ||
private static final String SELECTED_PART = "abc;"; | ||
private static final String REMAINING_PART = "b l u b"; | ||
private static final String TRIMMED_REMAINING_PART = REMAINING_PART.trim(); | ||
@Mock | ||
private Function<String, Predicate<Element>> factory; | ||
@Mock | ||
private Element element; | ||
|
||
@Test | ||
void does_not_match() throws Exception { | ||
final Pattern cssPattern = Pattern.compile( "^(" + SELECTOR_PATTERN + ";)(.*)" ); | ||
final Transformer transformer = new Transformer( cssPattern, factory ); | ||
|
||
final String selectorString = "002;blub"; | ||
final Selector cssSelector = transformer.transform( selectorString ); | ||
|
||
assertAll( () -> assertFalse( cssSelector.matches() ), | ||
() -> assertFalse( cssSelector.predicate().test( element ) ), | ||
() -> assertThat( cssSelector.remainingSelector() ).isEqualTo( selectorString ) ); | ||
} | ||
|
||
@Test | ||
void matches_regex() throws Exception { | ||
when( factory.apply( SELECTED_PART ) ).thenReturn( e -> true ); | ||
final Transformer transformer = newTransformer(); | ||
|
||
final Selector cssSelector = transformer.transform( SELECTED_PART + REMAINING_PART ); | ||
|
||
assertAll( () -> assertTrue( cssSelector.matches() ), | ||
() -> assertTrue( cssSelector.predicate().test( element ) ), | ||
() -> assertThat( cssSelector.remainingSelector() ).isEqualTo( TRIMMED_REMAINING_PART ) ); | ||
} | ||
|
||
private Transformer newTransformer() { | ||
final Pattern cssPattern = Pattern.compile( "^(" + SELECTOR_PATTERN + ";)(.*)" ); | ||
return new Transformer( cssPattern, factory ); | ||
} | ||
} |