/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.xml.stream;

import static org.jenetics.xml.stream.Reader.attr;
import static org.jenetics.xml.stream.Reader.elem;
import static org.jenetics.xml.stream.Reader.elems;
import static org.jenetics.xml.stream.Reader.text;

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import javax.xml.stream.XMLStreamException;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 */
public class ReaderTest {

	static Object fromBytes(final byte[] bytes, final Reader<?> reader)
		throws XMLStreamException
	{
		final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		try (AutoCloseableXMLStreamReader xml = XML.reader(in)) {
			xml.next();
			return reader.read(xml);
		}
	}

	@Test(dataProvider = "readerExamples")
	public void reader(
		final String xml,
		final Reader<?> reader,
		final Object expected
	)
		throws Exception
	{
		Assert.assertEquals(fromBytes(xml.getBytes(), reader), expected);
	}

	@DataProvider(name = "readerExamples")
	public Object[][] readerExamples() {
		return new Object[][] {
			{
				"<element length=\"123\"/>",
				elem(
					v -> v[0],
					"element",
					attr("length").map(Integer::parseInt)
				),
				123
			},
			{
				"<element>123</element>",
				elem(
					v -> v[0],
					"element",
					text().map(Integer::parseInt)
				),
				123
			},
			{
				"<min><element>123</element></min>",
				elem("min",
					elem(
						v -> v[0],
						"element",
						text().map(Integer::parseInt)
					)
				),
				123
			},
			{
				"<properties length=\"3\">" +
					"<property>1</property>" +
					"<property>2</property>" +
					"<property>3</property>" +
				"</properties>",
				elem(
					v -> v[0],
					"properties",
					elems(elem("property", text().map(Integer::parseInt)))
				),
				Arrays.asList(1, 2, 3)
			}
		};
	}

}

@@ -2399,24 +2399,26 @@
 h=%5C%223%5C%22%3E
+%5Cn
 %22 +%0A%09%09%09%09
 %09%22%3Cprope
@@ -2401,34 +2401,37 @@
 %5C%223%5C%22%3E%5Cn%22 +%0A%09%09%09%09
-%09
 %22
+    
 %3Cproperty%3E1%3C/pro
@@ -2432,32 +2432,37 @@
 1%3C/property%3E
+   %5Cn
 %22 +%0A%09%09%09%09
 %09%22%3Cproperty%3E
@@ -2441,34 +2441,37 @@
 ty%3E   %5Cn%22 +%0A%09%09%09%09
-%09
 %22
+    
 %3Cproperty%3E2%3C/pro
@@ -2480,18 +2480,24 @@
 rty%3E
+ %5Cn
 %22 +%0A
-%09
 %09%09%09%09%22
+    
 %3Cpro
@@ -2514,16 +2514,18 @@
 roperty%3E
+%5Cn
 %22 +%0A%09%09%09%09

Fix text reading.