Skip to content

Commit

Permalink
#14 naive implementation comparing merged cells (uses a count rather …
Browse files Browse the repository at this point in the history
…than a literal comparison of merged cells)
  • Loading branch information
tobyweston committed Oct 28, 2019
1 parent 14df8cf commit 61b9542
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/main/java/bad/robot/excel/matchers/MergedRegionMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2012-2019, bad robot (london) ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bad.robot.excel.matchers;

import org.apache.poi.ss.usermodel.Sheet;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class MergedRegionMatcher extends TypeSafeDiagnosingMatcher<Sheet> {

private final Sheet expected;

public static MergedRegionMatcher hasSameNumberOfMergedRegions(Sheet expected) {
return new MergedRegionMatcher(expected);
}

private MergedRegionMatcher(Sheet expected) {
this.expected = expected;
}

@Override
protected boolean matchesSafely(Sheet actual, Description mismatch) {
if (expected.getMergedRegions().size() != actual.getMergedRegions().size()) {
mismatch.appendText("got ")
.appendValue(numberOfRegionsIn(actual))
.appendText(" merged region(s) in sheet ")
.appendValue(actual.getSheetName())
.appendText(" expected ")
.appendValue(numberOfRegionsIn(expected));
return false;
}
return true;
}

@Override
public void describeTo(Description description) {
description.appendValue(numberOfRegionsIn(expected)).appendText(" merged region(s) in sheet ").appendValue(expected.getSheetName());
}

private static int numberOfRegionsIn(Sheet sheet) {
return sheet.getMergedRegions().size();
}
}
4 changes: 4 additions & 0 deletions src/main/java/bad/robot/excel/matchers/SheetsMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import static bad.robot.excel.matchers.MergedRegionMatcher.hasSameNumberOfMergedRegions;
import static bad.robot.excel.matchers.RowNumberMatcher.hasSameNumberOfRowAs;
import static bad.robot.excel.matchers.RowsMatcher.hasSameRowsAs;
import static bad.robot.excel.sheet.SheetIterable.sheetsOf;
Expand All @@ -38,6 +39,9 @@ protected boolean matchesSafely(Workbook actual, Description mismatch) {
for (Sheet expectedSheet : sheetsOf(expected)) {
Sheet actualSheet = actual.getSheet(expectedSheet.getSheetName());

if (!hasSameNumberOfMergedRegions(expectedSheet).matchesSafely(actualSheet, mismatch))
return false;

if (!hasSameNumberOfRowAs(expectedSheet).matchesSafely(actualSheet, mismatch))
return false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2012-2019, bad robot (london) ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bad.robot.excel.matchers;

import org.apache.poi.ss.usermodel.Sheet;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static bad.robot.excel.WorkbookResource.firstSheetOf;
import static bad.robot.excel.matchers.MergedRegionMatcher.hasSameNumberOfMergedRegions;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

public class MergedRegionMatcherTest {

private Sheet sheetWithMergedRegions;
private Sheet emptySheet;

@Before
public void loadWorkbookAndSheets() throws IOException {
sheetWithMergedRegions = firstSheetOf("mergedRegionSheet.xlsx");
emptySheet = firstSheetOf("emptySheet.xlsx");
}

@Test
public void exampleUsages() {
assertThat(sheetWithMergedRegions, hasSameNumberOfMergedRegions(sheetWithMergedRegions));
assertThat(emptySheet, not(hasSameNumberOfMergedRegions(sheetWithMergedRegions)));
}

@Test
public void matches() {
assertThat(hasSameNumberOfMergedRegions(sheetWithMergedRegions).matches(sheetWithMergedRegions), is(true));
}

@Test
public void doesNotMatch() {
assertThat(hasSameNumberOfMergedRegions(sheetWithMergedRegions).matches(emptySheet), is(false));
}

@Test
public void description() {
Description description = new StringDescription();
hasSameNumberOfMergedRegions(sheetWithMergedRegions).describeTo(description);
assertThat(description.toString(), is("<1> merged region(s) in sheet \"Sheet1\""));
}

@Test
public void mismatch() {
Description description = new StringDescription();
hasSameNumberOfMergedRegions(sheetWithMergedRegions).matchesSafely(emptySheet, description);
assertThat(description.toString(), is("got <0> merged region(s) in sheet \"Sheet1\" expected <1>"));
}

}
4 changes: 1 addition & 3 deletions src/test/java/bad/robot/excel/workbook/PoiWorkbookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -153,8 +152,7 @@ public void replaceThenLoadBlankCell() throws IOException {
}

@Test
@Ignore
public void shouldCountMergedCellsWhenSingleEmptySheetHasMergedCells() throws IOException {
public void anEmptySheetWithMergedCellsIsNotTheSameAsAnEmptySheet() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
int firstMergedCol = 1;
Expand Down
Binary file not shown.

0 comments on commit 61b9542

Please sign in to comment.