Skip to content

Commit

Permalink
Make unit tests assertions relative allowing them to pass independent…
Browse files Browse the repository at this point in the history
… of the OS. Two unit tests needed to be ignored as they were testing exact values which differ between systems.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Sep 9, 2014
1 parent 6e1a7d4 commit 3674a72
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 41 deletions.
Expand Up @@ -110,23 +110,29 @@ public void testResize() throws Exception {
TextOutline outline = new TextOutline("Cl", font);
AtomSymbol symbol = new AtomSymbol(outline, Collections.<TextOutline> emptyList());
AtomSymbol transformed = symbol.resize(2, 2);
Rectangle2D orgBounds = outline.getBounds();
Rectangle2D newBounds = transformed.getOutlines().get(0).getBounds2D();
assertThat(newBounds.getX(), closeTo(-4.27, 0.01));
assertThat(newBounds.getY(), closeTo(-13.75, 0.01));
assertThat(newBounds.getMaxX(), closeTo(15.52, 0.01));
assertThat(newBounds.getMaxY(), closeTo(4.79, 0.01));
assertThat(newBounds.getX(), closeTo(orgBounds.getX() - orgBounds.getWidth() / 2, 0.01));
assertThat(newBounds.getY(), closeTo(orgBounds.getY() - orgBounds.getHeight() / 2, 0.01));
assertThat(newBounds.getMaxX(), closeTo(orgBounds.getMaxX() + orgBounds.getWidth() / 2, 0.01));
assertThat(newBounds.getMaxY(), closeTo(orgBounds.getMaxY() + orgBounds.getHeight() / 2, 0.01));
}

@Test
public void testCenter() throws Exception {
TextOutline outline = new TextOutline("Cl", font);
AtomSymbol symbol = new AtomSymbol(outline, Collections.<TextOutline> emptyList());
AtomSymbol transformed = symbol.center(2, 2);
Rectangle2D oBounds = outline.getBounds();
Rectangle2D newBounds = transformed.getOutlines().get(0).getBounds2D();
assertThat(newBounds.getX(), closeTo(-2.95, 0.01));
assertThat(newBounds.getY(), closeTo(-2.63, 0.01));
assertThat(newBounds.getMaxX(), closeTo(6.95, 0.01));
assertThat(newBounds.getMaxY(), closeTo(6.63, 0.01));

double dx = 2 - oBounds.getCenterX();
double dy = 2 - oBounds.getCenterY();

assertThat(newBounds.getX(), closeTo(oBounds.getMinX() + dx, 0.01));
assertThat(newBounds.getY(), closeTo(oBounds.getMinY() + dy, 0.01));
assertThat(newBounds.getMaxX(), closeTo(oBounds.getMaxX() + dx, 0.01));
assertThat(newBounds.getMaxY(), closeTo(oBounds.getMaxY() + dy, 0.01));
}

@Test
Expand Down
Expand Up @@ -24,13 +24,16 @@

package org.openscience.cdk.renderer.generators.standard;

import org.junit.Ignore;
import org.junit.Test;

import java.awt.Font;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.Assert.*;

Expand All @@ -44,7 +47,7 @@ public void getOutline() throws Exception {
// values?
}

@Test
@Ignore("Font bounds vary between systems")
public void untransformedBounds() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font);
Rectangle2D bounds = clOutline.getBounds();
Expand All @@ -56,25 +59,29 @@ public void untransformedBounds() throws Exception {

@Test
public void boundsTransformedWithXTranslation() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font).translate(5, 0);
Rectangle2D bounds = clOutline.getBounds();
assertThat(bounds.getX(), closeTo(5.67, 0.01));
assertThat(bounds.getY(), closeTo(-9.12, 0.01));
assertThat(bounds.getWidth(), closeTo(9.90, 0.01));
assertThat(bounds.getHeight(), closeTo(9.28, 0.01));
TextOutline original = new TextOutline("Cl", font);
TextOutline transformed = original.translate(5, 0);
Rectangle2D oBounds = original.getBounds();
Rectangle2D tBounds = transformed.getBounds();
assertThat(tBounds.getX(), closeTo(oBounds.getX() + 5, 0.01));
assertThat(tBounds.getY(), closeTo(oBounds.getY(), 0.01));
assertThat(tBounds.getWidth(), closeTo(oBounds.getWidth(), 0.01));
assertThat(tBounds.getHeight(), closeTo(oBounds.getHeight(), 0.01));
}

@Test
public void boundsTransformedWithYTranslation() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font).translate(0, -5);
Rectangle2D bounds = clOutline.getBounds();
assertThat(bounds.getX(), closeTo(0.67, 0.01));
assertThat(bounds.getY(), closeTo(-14.12, 0.01));
assertThat(bounds.getWidth(), closeTo(9.90, 0.01));
assertThat(bounds.getHeight(), closeTo(9.28, 0.01));
TextOutline original = new TextOutline("Cl", font);
TextOutline transformed = original.translate(0, -5);
Rectangle2D oBounds = original.getBounds();
Rectangle2D tBounds = transformed.getBounds();
assertThat(tBounds.getX(), closeTo(oBounds.getX(), 0.01));
assertThat(tBounds.getY(), closeTo(oBounds.getY() - 5, 0.01));
assertThat(tBounds.getWidth(), closeTo(oBounds.getWidth(), 0.01));
assertThat(tBounds.getHeight(), closeTo(oBounds.getHeight(), 0.01));
}

@Test
@Ignore("Font bounds vary between systems")
public void untransformedCenter() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font);
Point2D center = clOutline.getCenter();
Expand All @@ -84,36 +91,40 @@ public void untransformedCenter() throws Exception {

@Test
public void transformedCenter() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font).translate(5, 5);
Point2D center = clOutline.getCenter();
assertThat(center.getX(), closeTo(10.62, 0.01));
assertThat(center.getY(), closeTo(0.52, 0.01));
TextOutline original = new TextOutline("Cl", font);
TextOutline transformed = original.translate(0, -5);
Point2D oCenter = original.getCenter();
Point2D tCenter = transformed.getCenter();
assertThat(tCenter.getX(), closeTo(oCenter.getX(), 0.01));
assertThat(tCenter.getY(), closeTo(oCenter.getY() - 5, 0.01));
}

@Test
public void testGetFirstGlyphCenter() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font);
Point2D center = clOutline.getFirstGlyphCenter();
assertThat(center.getX(), closeTo(4.29, 0.01));
assertThat(center.getY(), closeTo(-4.36, 0.01));
TextOutline original = new TextOutline("Cl", font);
Point2D oCenter = original.getCenter();
Point2D tCenter = original.getFirstGlyphCenter();
assertThat(tCenter.getX(), lessThan(oCenter.getX()));
}

@Test
public void testGetLastGlyphCenter() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font);
Point2D center = clOutline.getLastGlyphCenter();
assertThat(center.getX(), closeTo(10.02, 0.01));
assertThat(center.getY(), closeTo(-4.55, 0.01));
TextOutline original = new TextOutline("Cl", font);
Point2D oCenter = original.getCenter();
Point2D tCenter = original.getLastGlyphCenter();
assertThat(tCenter.getX(), greaterThan(oCenter.getX()));
}

@Test
public void resizeModifiesBounds() throws Exception {
TextOutline clOutline = new TextOutline("Cl", font).resize(2, 2);
Rectangle2D bounds = clOutline.getBounds();
assertThat(bounds.getX(), closeTo(-4.27, 0.01));
assertThat(bounds.getY(), closeTo(-13.75, 0.01));
assertThat(bounds.getWidth(), closeTo(19.80, 0.01));
assertThat(bounds.getHeight(), closeTo(18.55, 0.01));
TextOutline original = new TextOutline("Cl", font);
TextOutline transformed = original.resize(2, 2);
Rectangle2D oBounds = original.getBounds();
Rectangle2D tBounds = transformed.getBounds();
assertThat(tBounds.getX(), closeTo(oBounds.getX() - oBounds.getWidth() / 2, 0.01));
assertThat(tBounds.getY(), closeTo(oBounds.getY() - oBounds.getHeight() / 2, 0.01));
assertThat(tBounds.getWidth(), closeTo(oBounds.getWidth() * 2, 0.01));
assertThat(tBounds.getHeight(), closeTo(oBounds.getHeight() * 2, 0.01));
}

@Test
Expand All @@ -136,6 +147,15 @@ public void firstAndLastCenterIsTheSameForSingleLetterOutline() throws Exception

@Test
public void testToString() throws Exception {
assertThat(new TextOutline("Cl", font).toString(), is("Cl [x=0.67, y=-9.12, w=9.90, h=9.28]"));
TextOutline outline = new TextOutline("Cl", font);
Rectangle2D bounds = outline.getBounds();
assertThat(outline.toString(), is("Cl [x=" + toString(bounds.getX())
+ ", y=" + toString(bounds.getY())
+ ", w=" + toString(bounds.getWidth())
+ ", h=" + toString(bounds.getHeight()) + "]"));
}

static String toString(double x) {
return String.format("%.2f", x);
}
}

0 comments on commit 3674a72

Please sign in to comment.