[LIB-9] Separate chesshog-format module
[chesshog.git] / chesshog-format / src / test / java / org / hedgecode / chess / ascii / ASCIIBuilderTest.java
diff --git a/chesshog-format/src/test/java/org/hedgecode/chess/ascii/ASCIIBuilderTest.java b/chesshog-format/src/test/java/org/hedgecode/chess/ascii/ASCIIBuilderTest.java
new file mode 100644 (file)
index 0000000..2c5e177
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * 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 org.hedgecode.chess.ascii;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import org.hedgecode.chess.AbstractPositionTest;
+import org.hedgecode.chess.position.Builder;
+import org.hedgecode.chess.position.Position;
+
+/**
+ * Tests for {@link ASCIIBuilder}.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+@RunWith(JUnit4.class)
+public class ASCIIBuilderTest extends AbstractPositionTest implements ASCIIPositionTest {
+
+    private static final String LINE_REGEX = "\\r?\\n";
+
+    @Test
+    public void testBuild() throws Exception {
+        Builder asciiBuilder = ASCIIBuilder.getInstance();
+        for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(AbstractXMLConfigTest.TESTS)) {
+            setPositionName(
+                    String.format(
+                            MSG_ASCII_POSITION, testConfig.getString(AbstractPositionTest.NAME)
+                    )
+            );
+            String ascii = testConfig.getString(ASCII_TAG);
+            Position position = assignPosition(
+                    testConfig.configurationAt(AbstractPositionTest.POSITION)
+            );
+            if (testConfig.containsKey(ASCII_TYPE)) {
+                ASCII.setType(
+                        ASCIIBoardType.valueOf(
+                                testConfig.getString(ASCII_TYPE)
+                        )
+                );
+            }
+            boolean withNotation = false;
+            if (testConfig.containsKey(ASCII_NOTATION)) {
+                withNotation = testConfig.getBoolean(ASCII_NOTATION);
+            }
+            ASCII.setNotation(withNotation);
+            assertStringEquals(
+                    ascii,
+                    asciiBuilder.build(position)
+            );
+        }
+    }
+
+    @Override
+    protected void assertStringEquals(String expected, String actual) {
+        Assert.assertNotNull(expected);
+        Assert.assertNotNull(actual);
+        String[] expectedLines = expected.split(LINE_REGEX);
+        String[] actualLines = actual.split(LINE_REGEX);
+        Assert.assertEquals(
+                expectedLines.length,
+                actualLines.length
+        );
+        for (int i = 0; i < expectedLines.length; ++i) {
+            super.assertStringEquals(
+                    expectedLines[i].trim(),
+                    actualLines[i].trim()
+            );
+        }
+    }
+
+    @Override
+    public List<XMLConfiguration> getXMLPositions() throws Exception {
+        List<XMLConfiguration> xmlPositions = new LinkedList<>();
+        for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(AbstractXMLConfigTest.TESTS)) {
+            xmlPositions.add(
+                    new XMLConfiguration(
+                            testConfig.configurationAt(AbstractPositionTest.POSITION)
+                    )
+            );
+        }
+        return xmlPositions;
+    }
+
+}
\ No newline at end of file