/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */
package claw.tatsu.xcodeml.xnode.common;

import helper.TestConstant;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Collections;

import static org.junit.Assert.*;

/**
 * Test the features of the XcodeProgram class
 *
 * @author clementval
 */

public class XcodeProgTest {

  @Test
  public void basicXcodeProgTest() {
    File f = new File(TestConstant.TEST_DATA);
    assertTrue(f.exists());
    XcodeProgram xcodeml = XcodeProgram.createFromFile(TestConstant.TEST_DATA);
    assertNotNull(xcodeml);
    assertNotNull(xcodeml.getTime());
    assertNotNull(xcodeml.getCompilerInfo());
    assertNotNull(xcodeml.getVersion());
    assertNotNull(xcodeml.getLanguage());
    assertNotNull(xcodeml.getSource());
    assertNotNull(xcodeml.getSourceFileOnly());
    assertEquals("original_code.f90", xcodeml.getSourceFileOnly());
    assertEquals(8, xcodeml.getTypeTable().size());
    assertEquals(2, xcodeml.getGlobalSymbolsTable().size());
    assertEquals(2, xcodeml.getGlobalDeclarationsTable().size());

    xcodeml.addError("", 0);
    xcodeml.addError("", null);
    assertTrue(xcodeml.getErrors().isEmpty());

    xcodeml.addError("Error1", 1);
    assertEquals(1, xcodeml.getErrors().size());

    xcodeml.addWarning(null, 0);
    xcodeml.addWarning("", 0);
    xcodeml.addWarning(null, Collections.<Integer>emptyList());
    assertTrue(xcodeml.getWarnings().isEmpty());

    xcodeml.addWarning("New warning 1", 1);
    assertEquals(1, xcodeml.getWarnings().size());
  }

  @Test
  public void creationFailingTest() {
    XcodeProgram xc1 = XcodeProgram.createFromDocument(null);
    assertNull(xc1);

    // Simulate STDIN
    ByteArrayInputStream in =
        new ByteArrayInputStream("<dummy></dummy>".getBytes());
    System.setIn(in);

    XcodeProgram xc2 = XcodeProgram.createFromStdInput();
    assertNull(xc2);
  }
}

@@ -135,16 +135,54 @@
 ommon;%0A%0A
+import claw.wani.language.ClawPragma;%0A
 import h
@@ -201,16 +201,16 @@
 nstant;%0A
-
 import o
@@ -1183,32 +1183,84 @@
 ble().size());%0A%0A
+    // Try to add not meaningful messages in errors%0A
     xcodeml.addE
@@ -1300,24 +1300,68 @@
 (%22%22, null);%0A
+    xcodeml.addError(%22%22, new ClawPragma());%0A
     assertTr
@@ -1477,24 +1477,78 @@
 ).size());%0A%0A
+    // Try to add not meaningful messages in warnings%0A
     xcodeml.
@@ -1659,24 +1659,70 @@
 ptyList());%0A
+    xcodeml.addWarning(%22%22, new ClawPragma());%0A
     assertTr
@@ -1974,24 +1974,27 @@
 %0A    assertN
+otN
 ull(xc1);%0A%0A 
@@ -1990,16 +1990,49 @@
 ll(xc1);
+%0A    assertTrue(xc1.hasErrors());
 %0A%0A    //
@@ -2233,16 +2233,19 @@
  assertN
+otN
 ull(xc2)
@@ -2246,14 +2246,47 @@
 l(xc2);%0A
+    assertTrue(xc2.hasErrors());%0A
   %7D%0A%7D%0A

Update test with new semantic