/*
 * Copyright 2014 Matteo Ceccarello
 * 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 it.unipd.dei.experiment;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import clojure.lang.*;
import org.junit.Test;

public class EdnFormatterTest {

  public static final Keyword CLASS = Keyword.intern(null, "class");
  public static final Keyword NAME = Keyword.intern(null, "name");
  public static final Keyword DATE = Keyword.intern(null, "date");
  public static final Keyword NOTES = Keyword.intern(null, "notes");
  public static final Keyword TAGS = Keyword.intern(null, "tags");
  public static final Keyword TABLES = Keyword.intern(null, "tables");

  @Test
  public void testEdn1() {
    Experiment exp = new Experiment("exp-class", "exp-name");
    String edn = EdnFormatter.format(exp);
    Object parsed = RT.readString(edn);

    assertThat(parsed, is(instanceOf(IPersistentMap.class)));

    IPersistentMap expMap = (IPersistentMap) parsed;

    assertEquals(expMap.valAt(CLASS), "exp-class");
    assertEquals(expMap.valAt(NAME), "exp-name");
    assertEquals(expMap.valAt(NOTES), PersistentVector.EMPTY);
    assertEquals(expMap.valAt(TAGS), PersistentArrayMap.EMPTY);
    assertEquals(expMap.valAt(TABLES), PersistentArrayMap.EMPTY);
  }

  @Test
  public void testEdnNotes() {
    Experiment exp = new Experiment("exp-class", "exp-name");
    exp.note("This is a test note");

    String edn = EdnFormatter.format(exp);
    Object parsed = RT.readString(edn);

    IPersistentMap expMap = (IPersistentMap) parsed;
    IPersistentVector notes = (IPersistentVector) expMap.valAt(NOTES);

    assertThat(notes.length(), is(1));
    assertThat(notes.nth(0), is(instanceOf(IPersistentVector.class)));
    IPersistentVector note = (IPersistentVector) notes.nth(0);
    assertThat(note.length(), is(2));
    assertEquals(note.nth(1), "This is a test note");
  }

  @Test
  public void testEdnTags() {
    Experiment exp =
      new Experiment("exp-class", "exp-name")
        .tag("tag1", "value 1")
        .tag("tag2", 1234L);

    String edn = EdnFormatter.format(exp);
    Object parsed = RT.readString(edn);

    IPersistentMap expMap = (IPersistentMap) parsed;
    IPersistentMap tags = (IPersistentMap) expMap.valAt(TAGS);

    assertThat(tags.count(), is(2));
    assertEquals(tags.valAt("tag1"), "value 1");
    assertEquals(tags.valAt("tag2"), 1234L);
  }

}

@@ -2915,10 +2915,1476 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testEdnTables() %7B%0A    Experiment exp =%0A      new Experiment(%22exp-class%22, %22exp-name%22)%0A        .append(%22table1%22,%0A          %22head1%22, %22val1%22,%0A          %22head2%22, %22val2%22)%0A        .append(%22table1%22,%0A          %22head1%22, %22anotherVal%22,%0A          %22head2%22, %22anotherVal2%22)%0A        .append(%22table2%22,%0A          %22head1%22, %22val1%22,%0A          %22head2%22, %22val2%22);%0A%0A    String edn = EdnFormatter.format(exp);%0A    Object parsed = RT.readString(edn);%0A%0A    IPersistentMap expMap = (IPersistentMap) parsed;%0A    IPersistentMap tables = (IPersistentMap) expMap.valAt(TABLES);%0A%0A    assertThat(tables.count(), is(2));%0A    assertThat(tables.valAt(%22table1%22), is(instanceOf(IPersistentVector.class)));%0A    assertThat(tables.valAt(%22table2%22), is(instanceOf(IPersistentVector.class)));%0A%0A    IPersistentVector table1 = (IPersistentVector) tables.valAt(%22table1%22);%0A    assertThat(table1.count(), is(2));%0A    assertEquals(((IPersistentMap) table1.nth(0)).valAt(%22head1%22), %22val1%22);%0A    assertEquals(((IPersistentMap) table1.nth(0)).valAt(%22head2%22), %22val2%22);%0A    assertEquals(((IPersistentMap) table1.nth(1)).valAt(%22head1%22), %22anotherVal%22);%0A    assertEquals(((IPersistentMap) table1.nth(1)).valAt(%22head2%22), %22anotherVal2%22);%0A%0A    IPersistentVector table2 = (IPersistentVector) tables.valAt(%22table2%22);%0A    assertThat(table2.count(), is(1));%0A    assertEquals(((IPersistentMap) table2.nth(0)).valAt(%22head1%22), %22val1%22);%0A    assertEquals(((IPersistentMap) table2.nth(0)).valAt(%22head2%22), %22val2%22);%0A  %7D%0A%0A
 %7D%0A

Add test on EDN table formatting