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

import claw.wani.language.ClawPragma;
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());

    // Try to add not meaningful messages in errors
    xcodeml.addError("");
    xcodeml.addError("", 0);
    xcodeml.addError(null, 0);
    xcodeml.addError("", null);
    xcodeml.addError("", new ClawPragma());
    assertFalse(xcodeml.hasErrors());
    assertTrue(xcodeml.getErrors().isEmpty());

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

    // Try to add not meaningful messages in warnings
    xcodeml.addWarning("");
    xcodeml.addWarning(null, 0);
    xcodeml.addWarning("", 0);
    xcodeml.addWarning(null, Collections.<Integer>emptyList());
    xcodeml.addWarning("", new ClawPragma());
    assertFalse(xcodeml.hasWarnings());
    assertTrue(xcodeml.getWarnings().isEmpty());

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

  @Test
  public void creationFailingTest() {
    XcodeProgram xc1 = XcodeProgram.createFromDocument(null);
    assertNotNull(xc1);
    assertTrue(xc1.hasErrors());

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

    XcodeProgram xc2 = XcodeProgram.createFromStdInput();
    assertNotNull(xc2);
    assertTrue(xc2.hasErrors());
  }
}

@@ -2098,16 +2098,1026 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void createUnvalidXcodeProgram() %7B%0A%0A    String d1 = %22%3CXcodeProgram source=%5C%22original_code.f90%5C%22%5Cn%22 +%0A        %22language=%5C%22Fortran%5C%22%5Cn%22 +%0A        %22time=%5C%222015-11-25 14:47:59%5C%22%5Cn%22 +%0A        %22compiler-info=%5C%22XcodeML/Fortran-FrontEnd%5C%22%5Cn%22 +%0A        %22version=%5C%221.5%5C%22%3E%3C/XcodeProgram%3E%22;%0A    String d2 = %22%3CXcodeProgram source=%5C%22original_code.f90%5C%22%5Cn%22 +%0A        %22language=%5C%22C%5C%22%5Cn%22 +%0A        %22time=%5C%222015-11-25 14:47:59%5C%22%5Cn%22 +%0A        %22compiler-info=%5C%22XcodeML/Fortran-FrontEnd%5C%22%5Cn%22 +%0A        %22version=%5C%221.0%5C%22%3E%3C/XcodeProgram%3E%22;%0A%0A    // Simulate STDIN%0A    ByteArrayInputStream in = new ByteArrayInputStream(d1.getBytes());%0A    System.setIn(in);%0A%0A    // Version not valid%0A    XcodeProgram xc1 = XcodeProgram.createFromStdInput();%0A    assertNotNull(xc1);%0A    assertTrue(xc1.hasErrors());%0A%0A    in = new ByteArrayInputStream(d2.getBytes());%0A    System.setIn(in);%0A%0A    // Language not valid%0A    XcodeProgram xc2 = XcodeProgram.createFromStdInput();%0A    assertNotNull(xc2);%0A    assertTrue(xc2.hasErrors());%0A  %7D%0A%0A
   @Test%0A

Add some test