package net.morimekta.config.format;

import net.morimekta.config.Config;
import net.morimekta.config.ConfigException;

import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

/**
 * Tests for the JSON config format.
 */
public class TomlConfigTest {
    private JsonConfigFormatter formatter;
    private TomlConfigParser    parser;

    @Before
    public void setUp() throws ConfigException {
        formatter = new JsonConfigFormatter(true);
        parser    = new TomlConfigParser();
    }

    @Test
    public void testFormat() throws ConfigException {
        Config config = parser.parse(getClass().getResourceAsStream("/net/morimekta/config/format/config.toml"));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        formatter.format(config, baos);
        String result = new String(baos.toByteArray(), UTF_8);
        assertEquals("{\n" +
                     "    \"b\": true,\n" +
                     "    \"conf.real\": 1234.5678,\n" +
                     "    \"conf.sub_str\": \"another string value.\",\n" +
                     "    \"date\": \"2016-07-02T16:01:02.055Z\",\n" +
                     "    \"i\": 1234,\n" +
                     "    \"s\": \"string value.\",\n" +
                     "    \"seq_b\": [\n" +
                     "        false,\n" +
                     "        false,\n" +
                     "        false,\n" +
                     "        true\n" +
                     "    ],\n" +
                     "    \"seq_i\": [\n" +
                     "        1,\n" +
                     "        2.2,\n" +
                     "        3.7,\n" +
                     "        -4\n" +
                     "    ],\n" +
                     "    \"seq_s\": [\n" +
                     "        \"a\",\n" +
                     "        \"b\",\n" +
                     "        \"c\"\n" +
                     "    ]\n" +
                     "}", result);
    }
}

@@ -158,24 +158,61 @@
 unit.Test;%0A%0A
+import java.io.ByteArrayInputStream;%0A
 import java.
@@ -337,16 +337,105 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A/**%0A *
@@ -2190,16 +2190,485 @@
 result);
+%0A%0A        assertEquals(1467475262055L, config.getDate(%22date%22).getTime());%0A    %7D%0A%0A    @Test%0A    public void testEmptyCollection() %7B%0A        ByteArrayInputStream in = new ByteArrayInputStream((%0A                %22%5Bsection%5D%5Cn%22 +%0A                %22value = %5B%5D%5Cn%22 +%0A                %22%22).getBytes(UTF_8));%0A%0A        Config config = parser.parse(in);%0A%0A        assertNotNull(config.getCollection(%22section.value%22));%0A        assertTrue(config.getCollection(%22section.value%22).isEmpty());
 %0A    %7D%0A%7D

Add more tests for TOML config parsing.