/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */

package cx2x.translator.language;

import static org.junit.Assert.*;

import cx2x.xcodeml.exception.IllegalDirectiveException;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * JUnit test class for the CLAW language parser and information holding class.
 *
 * @author clementval
 */
public class ClawLanguageTest {

  /**
   * Test various input for the CLAW loop fusion directive.
   */
  @Test
  public void FusionTest(){
    // Valid directives
    analyzeValidClawLoopFusion("claw loop-fusion", null);
    analyzeValidClawLoopFusion("claw loop-fusion group(g1)", "g1");
    analyzeValidClawLoopFusion("claw loop-fusion group( g1 )", "g1");
    analyzeValidClawLoopFusion("claw loop-fusion group ( g1   ) ", "g1");
    analyzeValidClawLoopFusion("claw loop-fusiongroup(g1)", "g1");

    // Unvalid directives
    analyzeUnvalidClawLanguage("claw loop-fusion group");
    analyzeUnvalidClawLanguage("claw loop-fusion (i,j,k)");
    analyzeUnvalidClawLanguage("claw loop-fusion group()");
    analyzeUnvalidClawLanguage("claw loop-fusion group(   )");
  }

  /**
   * Assert the result for valid loop fusion CLAW directive
   * @param raw       Raw string valud of the CLAW directive to be analyzed.
   * @param groupName Group name to be found if any.
   */
  private void analyzeValidClawLoopFusion(String raw, String groupName){
    try {
      ClawLanguage l = ClawLanguage.analyze(raw);
      assertEquals(ClawDirective.LOOP_FUSION, l.getDirective());
      if(groupName != null){
        assertTrue(l.hasGroupOption());
        assertEquals(groupName, l.getGroupName());
      } else {
        assertFalse(l.hasGroupOption());
        assertNull(l.getGroupName());
      }
    } catch(IllegalDirectiveException idex){
      fail();
    }
  }

  /**
   * Assert any unvalid claw raw input
   * @param raw Raw string valud of the CLAW directive to be analyzed.
   */
  private void analyzeUnvalidClawLanguage(String raw){
    ClawLanguage l = null;
    try {
      l = ClawLanguage.analyze(raw);
      fail();
    } catch (IllegalDirectiveException pex){
      assertNull(l);
      assertNotNull(pex);
      assertNotNull(pex.getMessage());
    }
  }

  /**
   * Test various input for the CLAW loop interchange directive.
   */
  @Test
  public void InterchangeTest(){
    // Valid directives
    analyzeValidClawLoopInterchange("claw loop-interchange", null);
    analyzeValidClawLoopInterchange("claw loop-interchange (i,j,k)",
        Arrays.asList("i", "j", "k"));
    analyzeValidClawLoopInterchange("claw loop-interchange (  i,j,k  ) ",
        Arrays.asList("i", "j", "k"));

    // Unvalid directives
    analyzeUnvalidClawLanguage("claw loop-interchange ()");
    analyzeUnvalidClawLanguage("claw loop-interchange (i,j,k) group");
  }

  /**
   * Assert the result for valid loop interchange CLAW directive
   * @param raw       Raw string valud of the CLAW directive to be analyzed.
   * @param indexes   List of indexes to be found if any.
   */
  private void analyzeValidClawLoopInterchange(String raw,
                                               List<String> indexes)
  {
    try {
      ClawLanguage l = ClawLanguage.analyze(raw);
      assertEquals(ClawDirective.LOOP_INTERCHANGE, l.getDirective());
      if(indexes != null){
        assertTrue(l.hasIndexes());
        assertEquals(indexes.size(), l.getIndexes().size());

      } else {
        assertFalse(l.hasIndexes());
        assertNull(l.getIndexes());
      }
    } catch(IllegalDirectiveException idex){
      fail();
    }
  }

}

@@ -3469,25 +3469,24 @@
 ().size());%0A
-%0A
       %7D else
@@ -3634,15 +3634,971 @@
 ;%0A    %7D%0A
-
   %7D%0A%0A
+  /**%0A   * Test various input for the CLAW remove directive.%0A   */%0A  @Test%0A  public void RemoveTest()%7B%0A    // Valid directives%0A    analyzeValidSimpleClaw(%22claw remove%22, ClawDirective.REMOVE);%0A    analyzeValidSimpleClaw(%22claw end remove%22, ClawDirective.END_REMOVE);%0A    analyzeValidSimpleClaw(%22claw   end   remove  %22, ClawDirective.END_REMOVE);%0A%0A    // Unvalid directives%0A    analyzeUnvalidClawLanguage(%22claw%22);%0A    analyzeUnvalidClawLanguage(%22claw dummy%22);%0A    analyzeUnvalidClawLanguage(%22claw end re move%22);%0A  %7D%0A%0A  /**%0A   * Assert the result for valid lo CLAW directive%0A   * @param raw       Raw string valud of the CLAW directive to be analyzed.%0A   * @param directive Directive to be match.%0A   */%0A  private void analyzeValidSimpleClaw(String raw, ClawDirective directive) %7B%0A    try %7B%0A      ClawLanguage l = ClawLanguage.analyze(raw);%0A      assertEquals(directive, l.getDirective());%0A    %7D catch(IllegalDirectiveException idex)%7B%0A      fail();%0A    %7D%0A  %7D%0A%0A%0A
 %7D%0A

Add test for the remove directive