-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#14 naive implementation comparing merged cells (uses a count rather …
…than a literal comparison of merged cells)
- Loading branch information
1 parent
14df8cf
commit 61b9542
Showing
5 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/bad/robot/excel/matchers/MergedRegionMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/bad/robot/excel/matchers/MergedRegionMatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.