package nxm.sys.test.junit.lib;

import nxm.sys.lib.*;
import nxm.sys.test.junit.NMTestCase;

/** This will test the Parser class.

    @author  Neon Ngo
    @since   NeXtMidas 2.5.4
*/
public class ParserTest extends NMTestCase {
  /** public constructor to allow running this test case directly with JUnit. */
  public ParserTest (String methodName) { this(methodName,null); }

  /** Creates a new instance of the class.
      @param methodName The name of the method being called.
      @param midas      The midas context testTo use when running the tests.
    */
  public ParserTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  public void testMask() {
    // NTN: this is not a full test, it is a start for the test case to BUG 1298
    String   list       = "A,B,C,D,E";
    int      defmask    = 0x12;   // default mask
    int      allmask    = 0x1F;   // A|B|C|D|E
    String[] matchlists = { "A",    "B",    "C",      "D",      "E",
                            "A|B",  "B|A",  "|A|B",   "A|B|",   "|A|B|",
                            "0X1",  "0X2",  "0X3",    "0XA2",   "0XFFC",
                            "0X04",
                            "NONE", "ALL",  "DEF",    "DEFAULT"
                          };
    int[]    expected   = {  1,     2,       4,       8,        16,
                             3,     3,       3,       3,        3,
                             1,     2,       3,       162,      4092,
                             4,
                             0,     allmask, defmask, defmask
                          };

    for (int i=0; i<matchlists.length; i++) {
      String matchlist = matchlists[i];
      int result = Parser.mask(list, matchlist, defmask);
      assertEquals("Parser.mask() "+matchlist, expected[i], result);
    }
  }

  public void testFind() {
    // NTN: this is not a full test, it is a start for the test case
    String   list       = "A,AB,BC,CDE,CD,DE";
    int      defindex   = -9; // using something besides -1
    String[] findlists = { "A",  "AB",    "BC",     "CDE",  "CD", "DE",
                           "a",  "ab",    "bc",     "cde",  "cd", "de",
                           "X",  "",      null,     "ABC",  ",",  "|",
                           "27",  "-3",  // <-- test undocumented feature
                           "0.##E0" // <-- test case for BUG 2259
                         };
    int[]    expected   = { 1,   2,        3,        4,      4,    6,
                            1,   2,        3,        4,      4,    6,
                            -1,  defindex, defindex, -1,     -1,   -1,
                            27, -3,  // <-- test undocumented feature
                            -1, // <-- expected result for BUG 2259 test case
                          };

    info("You should see warnings below for 'Ambiguous items match 'CD'.'");
    for (int i=0; i<findlists.length; i++) {
      String match = findlists[i];
      int result = Parser.find(list, match, defindex);
      assertEquals("Parser.find() match="+match, expected[i], result);
    }

    // test new parser.find over-ride added in 2.9.0 for Bug 1716
    try {
      info("You should see a warning below for an not found in a list.");
      Parser.find(list, "BOGUSENTRY", 0, 0, Parser.E_WARNING);
      Parser.find(list, "BOGUSENTRY", 0, 0, Parser.E_ERROR);  // this should throw MidasException
      fail("finding bogus entry with ERROR flag should cause a MidasException");
    }
    catch (MidasException e) {
      // if we got to here then success
      assertEquals("Parser.find() bogus data errored out", 1, 1);
    }
    catch (Exception e) {
      if (e.getMessage().equals("BOGUSDATA")) {
        assertEquals("Parser.find() bogus data errored out", "SUCCESS", "FAIL");
      }
    }
  }


  /** Test case for nxm.sys.lib.Parser.findIndex(..) methods.
      @since NeXtMidas 2.5.5
   */
  public void testFindIndex() {
    int      defindex   = -9; // use something besides -1 for the default index
    String   list       = "A,AB,BC,CDE,CD,DE";
    String[] findlists = { "A",  "AB",    "BC",     "CDE",  "CD", "DE",
                           "a",  "ab",    "bc",     "cde",  "cd", "de",
                           "X",  "",      null,     "ABC",  ",",  "|",
                         };
    int[]    expected   = { 1,   2,        3,        4,      5,    6,
                            1,   2,        3,        4,      5,    6,
                           -1,  defindex,  defindex, -1,    -1,   -1,
                          };

    // test for non case sensitive (ones-based and zero-based indexes)
    for (int i=0; i<findlists.length; i++) {
      String match = findlists[i];
      int result1 = Parser.findIndex(list, match);
      int result0 = Parser.findIndex(list, match, defindex, -1, null, false);
      int expect   = expected[i];
      int expect0  = expect-1; // for zero-based index
      if (expect == defindex) expect0 += 1; // for default case, we want the same value (-9)
      assertEquals("Parser.findIndex(zero base) match="+match, expect0, result0);
      if (expect == defindex) expect = -1;  // defindex for this findIndex(list,match) is -1
      assertEquals("Parser.findIndex(one base) match="+match, expect, result1);
    }

    // test for zero and ones based index along with case sensitive comparisons
    int[]    expectedCS  = { 1,   2,        3,        4,      5,    6,
                            -1,  -1,       -1,       -1,     -1,   -1,
                            -1,  defindex, defindex, -1,     -1,   -1,
                           }; // ones base expected results
    for (int i=0; i<findlists.length; i++) {
      String match = findlists[i];
      int result0 = Parser.findIndex(list, match, defindex, -1, null, true); // zero based
      int result1 = Parser.findIndex(list, match, defindex, 0, null, true); // ones based
      int expect   = expectedCS[i];
      assertEquals("Parser.findIndex(case-sensitive, one-based) match="+match, expect, result1);
      int expect0  = expect-1; // for zero-based index
      if (expect == defindex) expect0 += 1; // for default case, we want the same value (-9)
      assertEquals("Parser.findIndex(case-sensitive, zero-based) match="+match, expect0, result0);
    }

    // test using different delimiters
    String list2       = "a,bb#cc#dd|ee,ff|gg";
    String[] findlist2 = { "a,bb",       "Cc",          "dd|ee,ff|gg", // # delimited
                           "a",          "Bb#cc#dd|ee", "ff|gg",       // , delimited
                           "a,bb#cc#dd", "Ee,ff",       "gg",          // | delimited
                           "",            null,         "bah",
                         };
    int[]    expectedpound = { 1,           2,              3,
                              -1,          -1,             -1,
                              -1,          -1,             -1,
                              defindex, defindex,          -1,
                             };
    int[]    expectedcomma = { -1,          -1,             -1,
                                1,           2,              3,
                               -1,          -1,             -1,
                               defindex, defindex,          -1,
                             };
    int[]    expectedpipe  = { -1,          -1,             -1,
                               -1,          -1,             -1,
                                1,           2,              3,
                               defindex, defindex,          -1,
                             };
    for (int i=0; i<findlist2.length; i++) {
      String match = findlist2[i];
      int result1 = Parser.findIndex(list2, match, defindex, 0, "#", false); // one-based
      int result2 = Parser.findIndex(list2, match, defindex, 0, ",", false); // one-based
      int result3 = Parser.findIndex(list2, match, defindex, 0, "|", false); // one-based
      int expect1   = expectedpound[i];
      int expect2   = expectedcomma[i];
      int expect3   = expectedpipe[i];
      assertEquals("Parser.findIndex('#' delim) match="+match, expect1, result1);
      assertEquals("Parser.findIndex(',' delim) match="+match, expect2, result2);
      assertEquals("Parser.findIndex('|' delim) match="+match, expect3, result3);
    }
  }

}
