Skip to content

Commit

Permalink
#383 add missing tests for SizePropertyBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 16, 2024
1 parent d7947f3 commit 671d438
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<PropertyDeclaration> buildDeclarations(CSSName cssName, List<? exten
protected void assertFoundUpToValues(CSSName cssName, List<? extends CSSPrimitiveValue> values, int max) {
int found = values.size();
if (found < 1 || found > max) {
throw new CSSParseException("Found %d value(s) for %s when between %d and %d value(s) were expected"
throw new CSSParseException("Found %d values for %s when between %d and %d value(s) were expected"
.formatted(found, cssName, 1, max), -1);
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ protected void checkIdentOrIntegerType(CSSName cssName, CSSPrimitiveValue value)

protected void checkInteger(CSSName cssName, CSSPrimitiveValue value) {
int type = value.getPrimitiveType();
if (type != CSS_NUMBER ||
if (type != CSS_NUMBER ||
(int) value.getFloatValue(CSS_NUMBER) != Math.round(value.getFloatValue(CSS_NUMBER))) {
throw new CSSParseException("Value for " + cssName + " must be an integer", -1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.xhtmlrenderer.css.parser.property;

import org.junit.jupiter.api.Test;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.parser.CSSParseException;
import org.xhtmlrenderer.css.parser.PropertyValue;
import org.xhtmlrenderer.css.sheet.PropertyDeclaration;

import java.util.List;

import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.w3c.dom.css.CSSPrimitiveValue.CSS_IDENT;
import static org.w3c.dom.css.CSSPrimitiveValue.CSS_MM;
import static org.xhtmlrenderer.css.constants.CSSName.getByPropertyName;
import static org.xhtmlrenderer.css.constants.IdentValue.AUTO;

class SizePropertyBuilderTest {
private final SizePropertyBuilder builder = new SizePropertyBuilder();
private final PropertyValue width = new PropertyValue(CSS_MM, 0, "43mm");
private final PropertyValue height = new PropertyValue(CSS_MM, 0, "25mm");
private final PropertyValue landscape = new PropertyValue(CSS_IDENT, 0, "landscape");
private final CSSName cssName = getByPropertyName("size");

@Test
void buildDeclarationsFromOneValue() {
List<PropertyDeclaration> result = builder.buildDeclarations(cssName, List.of(width), 6, false);

assertThat(result).usingRecursiveFieldByFieldElementComparator().containsExactly(
new PropertyDeclaration(getByPropertyName("-fs-page-orientation"), new PropertyValue(AUTO), false, 6),
new PropertyDeclaration(getByPropertyName("-fs-page-width"), width, false, 6),
new PropertyDeclaration(getByPropertyName("-fs-page-height"), width, false, 6)
);
}

@Test
void buildDeclarationsFromTwoValues() {
List<PropertyDeclaration> result = builder.buildDeclarations(cssName, List.of(width, height), 6, false);

assertThat(result).usingRecursiveFieldByFieldElementComparator().containsExactly(
new PropertyDeclaration(getByPropertyName("-fs-page-orientation"), new PropertyValue(AUTO), false, 6),
new PropertyDeclaration(getByPropertyName("-fs-page-width"), width, false, 6),
new PropertyDeclaration(getByPropertyName("-fs-page-height"), height, false, 6)
);
}

@Test
void buildDeclarationsFromThreeValues() {
List<PropertyDeclaration> result = builder.buildDeclarations(cssName, List.of(width, height, landscape), 0, false);

assertThat(result).usingRecursiveFieldByFieldElementComparator().containsExactly(
new PropertyDeclaration(getByPropertyName("-fs-page-width"), width, false, 0),
new PropertyDeclaration(getByPropertyName("-fs-page-height"), height, false, 0),
new PropertyDeclaration(getByPropertyName("-fs-page-orientation"), landscape, false, 0)
);
}

@Test
void declarationMustHaveAtLeastOneValue() {
assertThatThrownBy(() -> builder.buildDeclarations(cssName, emptyList(), 6, false))
.isInstanceOf(CSSParseException.class)
.hasMessageStartingWith("Found 0 values for size");
}

@Test
void declarationMustHaveAtMostThreeValue() {
assertThatThrownBy(() -> builder.buildDeclarations(cssName, List.of(width, height, landscape, landscape), 6, false))
.isInstanceOf(CSSParseException.class)
.hasMessageStartingWith("Found 4 values for size");
}
}

0 comments on commit 671d438

Please sign in to comment.