[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / chesshog-format / src / test / java / org / hedgecode / chess / ascii / ASCIIBuilderTest.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.ascii;
18
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import org.apache.commons.configuration.HierarchicalConfiguration;
23 import org.apache.commons.configuration.XMLConfiguration;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29
30 import org.hedgecode.chess.AbstractPositionTest;
31 import org.hedgecode.chess.position.Builder;
32 import org.hedgecode.chess.position.Position;
33
34 /**
35  * Tests for {@link ASCIIBuilder}.
36  *
37  * @author Dmitry Samoshin aka gotty
38  */
39 @RunWith(JUnit4.class)
40 public class ASCIIBuilderTest extends AbstractPositionTest implements ASCIIPositionTest {
41
42     private static final String LINE_REGEX = "\\r?\\n";
43
44     @Test
45     public void testBuild() throws Exception {
46         Builder asciiBuilder = ASCIIBuilder.getInstance();
47         for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(AbstractXMLConfigTest.TESTS)) {
48             setPositionName(
49                     String.format(
50                             MSG_ASCII_POSITION, testConfig.getString(AbstractPositionTest.NAME)
51                     )
52             );
53             String ascii = testConfig.getString(ASCII_TAG);
54             Position position = assignPosition(
55                     testConfig.configurationAt(AbstractPositionTest.POSITION)
56             );
57             if (testConfig.containsKey(ASCII_TYPE)) {
58                 ASCII.setType(
59                         ASCIIBoardType.valueOf(
60                                 testConfig.getString(ASCII_TYPE)
61                         )
62                 );
63             }
64             boolean withNotation = false;
65             if (testConfig.containsKey(ASCII_NOTATION)) {
66                 withNotation = testConfig.getBoolean(ASCII_NOTATION);
67             }
68             ASCII.setNotation(withNotation);
69             assertStringEquals(
70                     ascii,
71                     asciiBuilder.build(position)
72             );
73         }
74     }
75
76     @Override
77     protected void assertStringEquals(String expected, String actual) {
78         Assert.assertNotNull(expected);
79         Assert.assertNotNull(actual);
80         String[] expectedLines = expected.split(LINE_REGEX);
81         String[] actualLines = actual.split(LINE_REGEX);
82         Assert.assertEquals(
83                 expectedLines.length,
84                 actualLines.length
85         );
86         for (int i = 0; i < expectedLines.length; ++i) {
87             super.assertStringEquals(
88                     expectedLines[i].trim(),
89                     actualLines[i].trim()
90             );
91         }
92     }
93
94     @Override
95     public List<XMLConfiguration> getXMLPositions() throws Exception {
96         List<XMLConfiguration> xmlPositions = new LinkedList<>();
97         for (HierarchicalConfiguration testConfig : getXMLConfig().configurationsAt(AbstractXMLConfigTest.TESTS)) {
98             xmlPositions.add(
99                     new XMLConfiguration(
100                             testConfig.configurationAt(AbstractPositionTest.POSITION)
101                     )
102             );
103         }
104         return xmlPositions;
105     }
106
107 }