Skip to content

Commit

Permalink
#389 Support More Complex CSS Selectors
Browse files Browse the repository at this point in the history
Test transformer.
  • Loading branch information
briemla committed Dec 1, 2019
1 parent 7bee507 commit 2990e09
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/test/java/de/retest/web/selenium/css/TransformerTest.java
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 );
}
}

0 comments on commit 2990e09

Please sign in to comment.