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

Refactor stringassertion #393

Merged
merged 6 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractPropertyAssertion<T> implements ActualProperty<T>
protected final AbstractPropertyAssertion<T> parent;
protected PropertyAssertionConfig config;

public AbstractPropertyAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public AbstractPropertyAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
this.parent = parentAssertion;
this.provider = provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
public interface FileAssertion extends ActualProperty<File> {
QuantityAssertion<Long> bytes();
StringAssertion<String> name();
StringAssertion<String> extension();
StringAssertion<String> mimetype();
StringAssertion name();
StringAssertion extension();
StringAssertion mimetype();
BinaryAssertion<Boolean> exists();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.tsystems.mms.tic.testframework.internal.asserts;
martingrossmann marked this conversation as resolved.
Show resolved Hide resolved

public interface ObjectAssertion<T> extends BinaryAssertion<T> {
default boolean is(Object expected) {
return is(expected, null);
}
boolean is(Object expected, String subject);

default boolean isNot(Object expected) {
return isNot(expected, null);
}
boolean isNot(Object expected, String subject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@
* Allows numeric range based assertions
* @author Mike Reiche
*/
public interface QuantityAssertion<TYPE> extends BinaryAssertion<TYPE> {
default boolean is(Object expected) {
return is(expected, null);
}
boolean is(Object expected, String subject);

default boolean isNot(Object expected) {
return isNot(expected, null);
}
boolean isNot(Object expected, String subject);
public interface QuantityAssertion<TYPE> extends ObjectAssertion<TYPE> {

default boolean isGreaterThan(long expected) {
return isGreaterThan(new BigDecimal(expected));
Expand Down Expand Up @@ -124,7 +115,7 @@ default boolean isBetween(BigDecimal lower, BigDecimal higher) {
}
boolean isBetween(BigDecimal lower, BigDecimal higher, String subject);

<MAPPED_TYPE> StringAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction);
<MAPPED_TYPE> QuantityAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction);

QuantityAssertion<BigDecimal> absolute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Allows string based assertions
* @author Mike Reiche
*/
public interface StringAssertion<T> extends QuantityAssertion<T> {
public interface StringAssertion extends ObjectAssertion<String> {
BinaryAssertion <Boolean> contains(String expected);
BinaryAssertion <Boolean> startsWith(String expected);
BinaryAssertion <Boolean> endsWith(String expected);
Expand All @@ -54,4 +55,6 @@ default boolean isContaining(String text) {
default boolean isNotContaining(String text) {
return contains(text).is(false);
}

StringAssertion map(Function<String, String> mapFunction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class AbstractTestedPropertyAssertion<T> extends AbstractPropert
protected static final Assertion assertionImpl = Testerra.getInjector().getInstance(Assertion.class);
private static final TestController.Overrides overrides = Testerra.getInjector().getInstance(TestController.Overrides.class);

public AbstractTestedPropertyAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public AbstractTestedPropertyAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author Mike Reiche
*/
public class DefaultBinaryAssertion<T> extends AbstractTestedPropertyAssertion<T> implements BinaryAssertion<T> {
public DefaultBinaryAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public DefaultBinaryAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class DefaultFileAssertion extends AbstractPropertyAssertion<File> implements FileAssertion {

public DefaultFileAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<File> provider) {
public DefaultFileAssertion(AbstractPropertyAssertion<File> parentAssertion, AssertionProvider<File> provider) {
super(parentAssertion, provider);
}

Expand All @@ -52,7 +52,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> name() {
public StringAssertion name() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -67,7 +67,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> extension() {
public StringAssertion extension() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -82,7 +82,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> mimetype() {
public StringAssertion mimetype() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package eu.tsystems.mms.tic.testframework.internal.asserts;
martingrossmann marked this conversation as resolved.
Show resolved Hide resolved

public class DefaultObjectAssertion<T> extends DefaultBinaryAssertion<T> implements ObjectAssertion<T> {

public DefaultObjectAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}
@Override
public boolean is(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(expectedBoolean, failMessage);
}
return testSequence(
provider,
(actual) -> assertionImpl.equals(actual, expected),
(actual) -> assertionImpl.formatExpectEquals(null, expected, createFailMessage(failMessage))
);
}

@Override
public boolean isNot(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(!expectedBoolean, failMessage);
} else {
return testSequence(
provider,
(actual) -> assertionImpl.notEquals(actual, expected),
(actual) -> assertionImpl.formatExpectNotEquals(null, expected, createFailMessage(failMessage))
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class DefaultPatternAssertion extends AbstractTestedPropertyAssertion<Matcher> implements PatternAssertion {

public DefaultPatternAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<Matcher> provider) {
public DefaultPatternAssertion(AbstractPropertyAssertion<Matcher> parentAssertion, AssertionProvider<Matcher> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
*/
public class DefaultPropertyAssertionFactory implements PropertyAssertionFactory, Loggable {

public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createAssertion(
public <ASSERTION extends AbstractPropertyAssertion, T> ASSERTION createAssertion(
Class<ASSERTION> assertionClass,
AbstractPropertyAssertion parentAssertion,
AssertionProvider<TYPE> provider
AssertionProvider<T> provider
) {
ASSERTION assertion;
try {
Expand All @@ -48,10 +48,10 @@ public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createAsser
}

@Override
public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createWithParent(
public <ASSERTION extends AbstractPropertyAssertion, T> ASSERTION createWithParent(
Class<ASSERTION> assertionClass,
AbstractPropertyAssertion parentAssertion,
AssertionProvider<TYPE> provider
AssertionProvider<T> provider
) {
ASSERTION assertion = createAssertion(assertionClass, parentAssertion, provider);
assertion.config = parentAssertion.config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,12 @@
*
* @author Mike Reiche
*/
public class DefaultQuantityAssertion<TYPE> extends DefaultBinaryAssertion<TYPE> implements QuantityAssertion<TYPE> {
public class DefaultQuantityAssertion<TYPE> extends DefaultObjectAssertion<TYPE> implements QuantityAssertion<TYPE> {

public DefaultQuantityAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<TYPE> provider) {
super(parentAssertion, provider);
}

@Override
public boolean is(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(expectedBoolean, failMessage);
}
return testSequence(
provider,
(actual) -> assertionImpl.equals(actual, expected),
(actual) -> assertionImpl.formatExpectEquals(null, expected, createFailMessage(failMessage))
);
}

@Override
public boolean isNot(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(!expectedBoolean, failMessage);
} else {
return testSequence(
provider,
(actual) -> assertionImpl.notEquals(actual, expected),
(actual) -> assertionImpl.formatExpectNotEquals(null, expected, createFailMessage(failMessage))
);
}
}

private BigDecimal toBigDecimal(Object given) {
if (given == null) {
return null;
Expand Down Expand Up @@ -116,8 +89,8 @@ public boolean isBetween(BigDecimal lower, BigDecimal higher, String failMessage
}

@Override
public <MAPPED_TYPE> StringAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction) {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<MAPPED_TYPE>() {
public <MAPPED_TYPE> QuantityAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction) {
return propertyAssertionFactory.createWithParent(DefaultQuantityAssertion.class, this, new AssertionProvider<MAPPED_TYPE>() {
@Override
public MAPPED_TYPE getActual() {
TYPE actual = provider.getActual();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import eu.tsystems.mms.tic.testframework.logging.Loggable;

import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -33,9 +34,9 @@
*
* @author Mike Reiche
*/
public class DefaultStringAssertion<T> extends DefaultQuantityAssertion<T> implements StringAssertion<T>, Loggable {
public class DefaultStringAssertion extends DefaultObjectAssertion<String> implements StringAssertion, Loggable {

public DefaultStringAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public DefaultStringAssertion(AbstractPropertyAssertion<String> parentAssertion, AssertionProvider<String> provider) {
super(parentAssertion, provider);
}

Expand Down Expand Up @@ -110,7 +111,7 @@ public BinaryAssertion<Boolean> hasWords(List<String> words) {
.collect(Collectors.joining("|"));
final Pattern wordsPattern = Pattern.compile(wordsList, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

final String wordsListWithoutRegex = String.join("|", words);
final String wordsListWithoutRegex = java.lang.String.join("|", words);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why java.lang is used here?


return propertyAssertionFactory.createWithParent(DefaultBinaryAssertion.class, this, new AssertionProvider<Boolean>() {
@Override
Expand Down Expand Up @@ -142,4 +143,24 @@ public String createSubject() {
}
});
}

@Override
public StringAssertion map(Function<String, String> mapFunction) {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
String actual = provider.getActual();
if (actual == null) {
return null;
} else {
return mapFunction.apply(provider.getActual());
}
}

@Override
public java.lang.String createSubject() {
return "mapped to " + Format.shortString(getActual());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DefaultPageAssertions(Page page, PropertyAssertionConfig config) {
}

@Override
public StringAssertion<String> title() {
public StringAssertion title() {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -75,7 +75,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> url() {
public StringAssertion url() {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public DefaultUiElementAssertion(UiElement uiElement, Assertion useAssertion) {
}

@Override
public StringAssertion<String> tagName() {
public StringAssertion tagName() {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new UiElementAssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -138,7 +138,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> text() {
public StringAssertion text() {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new UiElementAssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -153,7 +153,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> attribute(String attribute) {
public StringAssertion attribute(String attribute) {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new UiElementAssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -168,7 +168,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> css(String property) {
public StringAssertion css(String property) {
return propertyAssertionFactory.createWithConfig(DefaultStringAssertion.class, this.propertyAssertionConfig, new UiElementAssertionProvider<String>() {
@Override
public String getActual() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import eu.tsystems.mms.tic.testframework.internal.asserts.StringAssertion;

public interface PageAssertions extends ScreenshotAssertion {
StringAssertion<String> title();
StringAssertion<String> url();
StringAssertion title();
StringAssertion url();
BinaryAssertion<Boolean> displayed();

default boolean displayed(boolean bool) {
Expand Down
Loading