JAL-4353 helper method and test for primary/secondary and auxiliary arguments with...
authorBen Soares <b.soares@dundee.ac.uk>
Tue, 12 Dec 2023 15:47:26 +0000 (15:47 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Tue, 12 Dec 2023 15:47:26 +0000 (15:47 +0000)
src/jalview/bin/argparser/ArgValuesMap.java
test/jalview/bin/argparser/ArgParserTest.java

index 8a28b87..219983f 100644 (file)
@@ -225,6 +225,25 @@ public class ArgValuesMap
     return av == null ? null : av.getValue();
   }
 
+  public List<String> getValues(Arg a)
+  {
+    return toValues(getArgValueList(a));
+  }
+
+  public static List<String> toValues(List<ArgValue> avl)
+  {
+    if (avl == null)
+    {
+      return null;
+    }
+    List<String> vl = new ArrayList<>();
+    for (ArgValue av : avl)
+    {
+      vl.add(av.getValue());
+    }
+    return vl;
+  }
+
   public boolean containsArg(Arg a)
   {
     if (m == null || !m.containsKey(a))
index 442812e..b29b4e9 100644 (file)
@@ -22,9 +22,13 @@ package jalview.bin.argparser;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.logging.log4j.util.Strings;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.AfterMethod;
@@ -32,6 +36,7 @@ import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.gui.Desktop;
 
 @Test(singleThreaded = true)
@@ -452,4 +457,260 @@ public class ArgParserTest
     };
   }
 
+  @Test(groups = "Functional", dataProvider = "argsWithinTypesData")
+  public void checkArgsWithinTypesTest(String commandLineArgs,
+          Object[] stuff)
+  {
+    String linkedId = "JALVIEW:0";
+    String[] args = commandLineArgs.split("\\s+");
+    ArgParser argparser = new ArgParser(args);
+    ArgValuesMap avm = argparser.getLinkedArgs(linkedId);
+
+    ArgAndValues avals = (ArgAndValues) stuff[0];
+
+    Object[] moreStuff = (Object[]) stuff[1];
+
+    ArgAndValues[] subAv = (ArgAndValues[]) moreStuff[0];
+
+    Object[] secondaryArgAndAuxStuff = (Object[]) stuff[2];
+
+    // Now look at the argparser
+
+    Arg primaryArg = avals.arg;
+
+    List<ArgValue> parsed_ArgValues = avm.getArgValueList(primaryArg);
+
+    Assert.assertTrue(
+            areEqualSets(avals.values,
+                    ArgValuesMap.toValues(parsed_ArgValues)),
+            "Primary arg (" + primaryArg.toString()
+                    + ") does not have the expected values");
+
+    for (int i = 0; i < parsed_ArgValues.size(); i++)
+    {
+      ArgValue parsed_ArgValue = parsed_ArgValues.get(i);
+      String value = avals.values.get(i);
+
+      Console.debug("- primary arg '" + primaryArg + "' = '" + value + "'");
+
+      Assert.assertEquals(parsed_ArgValue.getValue(), value,
+              "Primary arg value not as expected");
+
+      ArgAndValues[] aux_avals = (ArgAndValues[]) moreStuff[i];
+
+      for (ArgAndValues aux_aval : aux_avals)
+      {
+        Arg auxArg = aux_aval.arg;
+        List<String> auxValues = aux_aval.values;
+
+        for (String auxValue : auxValues)
+        {
+          String parsed_auxValue = avm.getFromSubValArgOrPref(auxArg,
+                  ArgValuesMap.Position.AFTER, parsed_ArgValue, null, null,
+                  null, null);
+
+          Console.debug("- + primary aux arg '" + auxArg + "' = '"
+                  + auxValue + "'");
+
+          Assert.assertEquals(parsed_auxValue, auxValue,
+                  "Primary auxiliary arg (" + auxArg.toString()
+                          + ") values do not match");
+
+        }
+      }
+
+      // Now for the secondary args
+      Object[] secondaryStuff = (Object[]) secondaryArgAndAuxStuff[i];
+      ArgAndValues secondaryArgAndValues = (ArgAndValues) secondaryStuff[0];
+      Arg secondaryArg = secondaryArgAndValues.arg;
+      List<String> secondaryValues = secondaryArgAndValues.values;
+
+      List<ArgValue> parsed_secondaryArgValues = avm
+              .getArgValueListFromSubValOrArg(parsed_ArgValue, secondaryArg,
+                      null);
+
+      Assert.assertTrue(
+              areEqualSets(secondaryValues,
+                      ArgValuesMap.toValues(parsed_secondaryArgValues)),
+              "Secondary arg (" + secondaryArg.toString()
+                      + ") does not have the expected values");
+
+      Object[] secondaryMoreStuff = (Object[]) secondaryStuff[1];
+
+      for (int j = 0; j < parsed_secondaryArgValues.size(); j++)
+      {
+        ArgValue parsed_secondaryArgValue = parsed_secondaryArgValues
+                .get(j);
+        String secondary_value = secondaryValues.get(j);
+
+        Console.debug("-- secondary arg '" + secondaryArg + "' = '"
+                + secondary_value + "'");
+
+        Assert.assertEquals(parsed_secondaryArgValue.getValue(),
+                secondary_value, "Secondary arg value not as expected");
+
+        ArgAndValues[] secondary_aux_avals = (ArgAndValues[]) secondaryMoreStuff[j];
+
+        for (ArgAndValues secondary_aux_aval : secondary_aux_avals)
+        {
+          Arg secondary_auxArg = secondary_aux_aval.arg;
+          List<String> secondary_auxValues = secondary_aux_aval.values;
+
+          for (String secondary_auxValue : secondary_auxValues)
+          {
+            String parsed_secondary_auxValue = avm.getValueFromSubValOrArg(
+                    parsed_secondaryArgValue, secondary_auxArg, null);
+
+            Console.debug("-- + secondary aux arg '" + secondary_auxArg
+                    + "' for value '" + secondary_auxValue + "'");
+
+            Assert.assertEquals(parsed_secondary_auxValue,
+                    secondary_auxValue,
+                    "Secondary auxiliary arg ("
+                            + secondary_auxArg.toString()
+                            + ") values do not match");
+          }
+        }
+      }
+    }
+  }
+
+  @DataProvider(name = "argsWithinTypesData")
+  public Object[][] argsWithinTypesData()
+  {
+    return new Object[][] {
+        /*
+         * { cmdline args },
+         * {
+         *   { Primary Arg, Secondary Args },
+         *   { { Secondary Arg => { Values } } }
+         *  },
+         */
+        /*
+        */
+        { //
+          // commandLineArgs
+            "--open=alignment.fa --structure=structure0.pdb"
+                    // structureimage0a and structureimage args
+                    + " --structureimage=image0a.png --bgcolour=bg0a --imagecolour=col0a"
+                    // structureimage0b and structureimage args
+                    + " --structureimage=image0b.png --bgcolour=bg0b --imagecolour=col0b"
+                    // more structure args
+                    + " --structureviewer=sv0 --paematrix=pae0" //
+                    // structure1
+                    + " --structure=structure1.pdb"
+                    // structure args
+                    + " --structureviewer=sv1 --paematrix=pae1"
+                    // structureimage1a with NO structureimage args
+                    // (see `--all --bgcolour=pineapple` later)
+                    + " --structureimage=image1a.png"
+                    // structureimage1b and structureimage args
+                    + " --structureimage=image1b.png --bgcolour=bg1b --imagecolour=col1b"
+                    // --all args, should apply to structureimage1a only
+                    + " --all --bgcolour=pineapple" //
+            ,
+            // stuff
+            new Object[]
+            {
+                // avals (0) and (1)
+                av(Arg.STRUCTURE, "structure0.pdb", "structure1.pdb"),
+                // moreStuff (0) and (1)
+                new ArgAndValues[][]
+                { //
+                    { av(Arg.STRUCTUREVIEWER, "sv0"),
+                        av(Arg.PAEMATRIX, "pae0") },
+                    { av(Arg.STRUCTUREVIEWER, "sv1"),
+                        av(Arg.PAEMATRIX, "pae1") } //
+                },
+                // secondaryArgAndAuxStuff
+                // (same size as values of avals)
+                new Object[][]
+                {
+                    // secondaryStuff (0)
+                    {
+                        // secondaryArgAndValues (a) and (b)
+                        av(Arg.STRUCTUREIMAGE, "image0a.png",
+                                "image0b.png"),
+                        // secondaryMoreStuff
+                        // (same size as values of secondaryArgAndValues)
+                        new ArgAndValues[][]
+                        {
+                            // secondary_aux_avals (a)
+                            { av(Arg.BGCOLOUR, "bg0a"),
+                                av(Arg.IMAGECOLOUR, "col0a") },
+                            // secondary_aux_avals (b)
+                            { av(Arg.BGCOLOUR, "bg0b"),
+                                av(Arg.IMAGECOLOUR, "col0b") }, //
+                        }, //
+                    },
+                    // secondaryStuff (1)
+                    {
+                        // secondaryArgAndValues (a) and (b)
+                        av(Arg.STRUCTUREIMAGE, "image1a.png",
+                                "image1b.png"),
+                        // secondaryMoreStuff
+                        new ArgAndValues[][]
+                        {
+                            // secondary_aux_avals (a)
+                            { av(Arg.BGCOLOUR, "pineapple"),
+                                av(Arg.IMAGECOLOUR) },
+                            // secondary_aux_avals (b)
+                            { av(Arg.BGCOLOUR, "bg1b"),
+                                av(Arg.IMAGECOLOUR, "col1b") }, //
+                        }, //
+                    }, //
+                } //
+            } //
+        } //
+    };
+  }
+
+  protected ArgAndValues av(Arg a, String... vals)
+  {
+    return new ArgAndValues(a, vals);
+  }
+
+  protected class ArgAndValues
+  {
+    protected Arg arg;
+
+    protected List<String> values;
+
+    ArgAndValues(Arg a, String... vals)
+    {
+      arg = a;
+      values = vals == null ? new ArrayList<String>() : Arrays.asList(vals);
+    }
+
+    @Override
+    public String toString()
+    {
+      StringBuilder sb = new StringBuilder();
+      sb.append(arg.argString());
+      sb.append("\n");
+      sb.append(Strings.join(values, ','));
+      sb.append("\n");
+      return sb.toString();
+    }
+  }
+
+  private static boolean areEqualSets(String[] strArray,
+          List<String> strList)
+  {
+    return areEqualSets(Arrays.asList(strArray), strList);
+  }
+
+  private static boolean areEqualSets(List<String> l1, List<String> l2)
+  {
+    if (l1 == null && l2 == null)
+    {
+      return true;
+    }
+    if (l1 == null || l2 == null)
+    {
+      return true;
+    }
+    return new HashSet<String>(l1).equals(new HashSet<String>(l2));
+  }
+
 }
\ No newline at end of file