<commit_before>/*
 * Created on 30.08.2004
 */
package classycle.dependency;

import java.util.ArrayList;
import java.util.HashMap;

import junit.framework.TestCase;

/**
 * @author  <NAME>
 */
public class DependencyDefinitionParserTest extends TestCase
{
  
  public void testShowStatements() 
  {
    check("show a b c", new String[] {"[a, b, c]"});
    check("show a\n show b c", new String[] {"[a]", "[b, c]"});
  }
  
  public void testDependencyStatements() 
  {
    check("check java.lang.* independentOf java.awt.*", 
          new String[] {"check java.lang.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "show shortestPathsOnly\n"
          + "check [lang] independentOf java.awt.*",  
          new String[] {"[shortestPathsOnly]",
                        "check [lang] independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "  check   [lang]   java.util.*   independentOf java.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "  check   [lang]   java.util.*   independentOf\\\njava.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("######\n"
          + "[lang] = java.lang.*\n"
          + "# this is a comment\n"
          + "    # and this too\n\r\n"
          + "check [lang] java.util.* independentOf \\ \n  java.awt.*\n"  
          + "check java.util.* independentOf \\\n   javax.swing.* java.awt.*\n",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*", 
                 "check java.util.* independentOf javax.swing.* java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "check [lang] java.util.* independentOf\\  \n# hello\njava.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "[a] = [lang] excluding java.lang.Class\n"
          + "check [a] independentOf java.awt.*",  
          new String[] {"check [a] independentOf java.awt.*"});
  }
  
  public void testLayeringStatements() 
  {
    check("layer a = java.lang.*\n"
          + "layer b = java.util.*\n"
          + "check layeringOf a b", new String[] {"check layeringOf a b"});
    check("layer a = java.lang.*\n"
          + "layer b = java.util.*\n"
          + "check strictLayeringOf a b", 
          new String[] {"check strictLayeringOf a b"});
  }
  
  public void testSetDefinition() 
  {
    check("[a] = j.*", new String[][] {{"[a]", "j.*"}});
    check("[a] = j.* k.*", new String[][] {{"[a]", "(j.* k.*)"}});
    check("[a] = j.* k.* excluding a.b", 
          new String[][] {{"[a]", "((j.* k.*) & !a.b)"}});
    check("[a] = j.* k.* excluding a.b c.d", 
          new String[][] {{"[a]", "((j.* k.*) & !(a.b c.d))"}});
    check("[a] = k$t.*_$ excluding a.b c.d", 
          new String[][] {{"[a]", "(k$t.*_$ & !(a.b c.d))"}});
    check("[a] = excluding a.b c.d", 
          new String[][] {{"[a]", "!(a.b c.d)"}});
  }
  
  public void testSetDefinitions() 
  {
    check("[a] = a.*\n"
          + "[b] = b.*\n"
          + "[c] = [a] excluding j.*", 
          new String[][] {{"[a]", "a.*"},
                          {"[b]", "b.*"},
                          {"[c]", "(a.* & !j.*)"}
                         });
    check("[a] = a.*\n"
          + "[b] = b.*\n"
          + "[c] = [a] j.*\n"
          + "[d] = j.* [b] excluding [c] k.*\n", 
          new String[][] {{"[a]", "a.*"},
                          {"[b]", "b.*"},
                          {"[c]", "(a.* j.*)"},
                          {"[d]", "((j.* b.*) & !((a.* j.*) k.*))"}
                         });
  }
  
  private void check(String definition, String[] expectedStatements) 
  {
    DependencyDefinitionParser parser 
        = new DependencyDefinitionParser(definition, new MockResultRenderer());
    Statement[] statements = parser.getStatements();
    int len = Math.min(statements.length, expectedStatements.length);
    for (int i = 0; i < len; i++)
    {
      assertEquals("Statement " + (i + 1), expectedStatements[i], 
                   statements[i].toString());
    }
    if (len < statements.length)
    {
      fail(statements.length - len + " additional statements");
    }
    if (len < expectedStatements.length)
    {
      fail(expectedStatements.length - len + " missing statements");
    }
  }
  
  private void check(String definition, String[][] expectedSets) 
  {
    DependencyDefinitionParser parser 
        = new DependencyDefinitionParser(definition, new MockResultRenderer());
    SetDefinitionRepository definitions = parser._setDefinitions;
    for (int i = 0; i < expectedSets.length; i++)
    {
      String setName = expectedSets[i][0];
      String expectedSet = expectedSets[i][1];
      assertEquals("Set " + setName, expectedSet, 
                   definitions.getPattern(setName) + "");
    }
  }
  
  private static class MockPreference implements Preference
  {
    String _preference;
    public MockPreference(String preference)
    {<commit_msg>add testLayerDefinitions()<commit_after>/*
 * Created on 30.08.2004
 */
package classycle.dependency;

import java.util.ArrayList;
import java.util.HashMap;

import classycle.util.StringPattern;

import junit.framework.TestCase;

/**
 * @author  <NAME>
 */
public class DependencyDefinitionParserTest extends TestCase
{
  
  public void testShowStatements() 
  {
    check("show a b c", new String[] {"[a, b, c]"});
    check("show a\n show b c", new String[] {"[a]", "[b, c]"});
  }
  
  public void testDependencyStatements() 
  {
    check("check java.lang.* independentOf java.awt.*", 
          new String[] {"check java.lang.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "show shortestPathsOnly\n"
          + "check [lang] independentOf java.awt.*",  
          new String[] {"[shortestPathsOnly]",
                        "check [lang] independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "  check   [lang]   java.util.*   independentOf java.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "  check   [lang]   java.util.*   independentOf\\\njava.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("######\n"
          + "[lang] = java.lang.*\n"
          + "# this is a comment\n"
          + "    # and this too\n\r\n"
          + "check [lang] java.util.* independentOf \\ \n  java.awt.*\n"  
          + "check java.util.* independentOf \\\n   javax.swing.* java.awt.*\n",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*", 
                 "check java.util.* independentOf javax.swing.* java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "check [lang] java.util.* independentOf\\  \n# hello\njava.awt.*",  
          new String[] {"check [lang] java.util.* independentOf java.awt.*"});
    check("[lang] = java.lang.*\n"
          + "[a] = [lang] excluding java.lang.Class\n"
          + "check [a] independentOf java.awt.*",  
          new String[] {"check [a] independentOf java.awt.*"});
  }
  
  public void testLayeringStatements() 
  {
    check("layer a = java.lang.*\n"
          + "layer b = java.util.*\n"
          + "check layeringOf a b", new String[] {"check layeringOf a b"});
    check("layer a = java.lang.*\n"
          + "layer b = java.util.*\n"
          + "check strictLayeringOf a b", 
          new String[] {"check strictLayeringOf a b"});
  }
  
  public void testSetDefinition() 
  {
    check("[a] = j.*", new String[][] {{"[a]", "j.*"}});
    check("[a] = j.* k.*", new String[][] {{"[a]", "(j.* k.*)"}});
    check("[a] = j.* k.* excluding a.b", 
          new String[][] {{"[a]", "((j.* k.*) & !a.b)"}});
    check("[a] = j.* k.* excluding a.b c.d", 
          new String[][] {{"[a]", "((j.* k.*) & !(a.b c.d))"}});
    check("[a] = k$t.*_$ excluding a.b c.d", 
          new String[][] {{"[a]", "(k$t.*_$ & !(a.b c.d))"}});
    check("[a] = excluding a.b c.d", 
          new String[][] {{"[a]", "!(a.b c.d)"}});
  }
  
  public void testSetDefinitions() 
  {
    check("[a] = a.*\n"
          + "[b] = b.*\n"
          + "[c] = [a] excluding j.*", 
          new String[][] {{"[a]", "a.*"},
                          {"[b]", "b.*"},
                          {"[c]", "(a.* & !j.*)"}
                         });
    check("[a] = a.*\n"
          + "[b] = b.*\n"
          + "[c] = [a] j.*\n"
          + "[d] = j.* [b] excluding [c] k.*\n", 
          new String[][] {{"[a]", "a.*"},
                          {"[b]", "b.*"},
                          {"[c]", "(a.* j.*)"},
                          {"[d]", "((j.* b.*) & !((a.* j.*) k.*))"}
                         });
  }
  
  public void testLayerDefinitions()
  {
    check("[a] = a.*\n"
          + "[b] = b.*\n"
          + "[c] = [a] excluding j.*\n"
          + "layer a = z.*\n"
          + "layer b = [c]\n"
          + "layer bla = [a] [c]\n"
          + "layer c = [a] s.*\n",
          new String[] {"a", "b", "bla", "c"},
          new String[][] {{"z.*"},
                          {"(a.* & !j.*)"},
                          {"a.*", "(a.* & !j.*)"},
                          {"a.*", "s.*"}
                         });    
  }
  
  private void check(String definition, String[] expectedStatements) 
  {
    DependencyDefinitionParser parser 
        = new DependencyDefinitionParser(definition, new MockResultRenderer());
    Statement[] statements = parser.getStatements();
    int len = Math.min(statements.length, expectedStatements.length);
    for (int i = 0; i < len; i++)
    {
      assertEquals("Statement " + (i + 1), expectedStatements[i], 
                   statements[i].toString());
    }
    if (len < statements.length)
    {
      fail(statements.length - len + " additional statements");
    }
    if (len < expectedStatements.length)
    {
      fail(expectedStatements.length - len + " missing statements");
    }
  }
  
  private void check(String definition, String[][] expectedSets) 
  {
    DependencyDefinitionParser parser 
        = new DependencyDefinitionParser(definition, new MockResultRenderer());
    SetDefinitionRepository definitions = parser._setDefinitions;
    for (int i = 0; i < expectedSets.length; i++)
    {
      String setName = expectedSets[i][0];
      String expectedSet = expectedSets[i][1];
      assertEquals("Set " + setName, expectedSet, 
                   definitions.getPattern(setName) + "");
    }
  }
  
  private void check(String definition, String[] layerNames, 
                     String[][] expectedLayers)
  {
    DependencyDefinitionParser parser 
        = new DependencyDefinitionParser(definition, new MockResultRenderer());
    LayerDefinitionRepository definitions = parser._layerDefinitions;
    for (int i = 0; i < expectedLayers.length; i++)
    {
      StringPattern[] layer = definitions.getLayer(layerNames[i]);
      int n = Math.min(layer.length, expectedLayers[i].length);
      for (int j = 0; j < n; j++)
      {
        assertEquals("Layer " + layerNames[i] + " " + j, 
                     expectedLayers[i][j], layer[j] + "");
      }
      int d = expectedLayers[i].length - layer.length;
      if (d > 0)
      {
        fail(d + " terms missed");
      } else if (d < 0) 
      {
        fail(-d + " unexpected terms");
      }
    }
  }
  
  private static class MockPreference implements Preference
  {
    String _preference;
    public MockPreference(String preference)
    {
{'fraction_non_alphanumeric': 0.20017248814144029, 'fraction_numerical': 0.00258732212160414, 'mean_word_length': 1.9940614510715209, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3635504', 'n_tokens_mistral': 3827, 'n_tokens_neox': 3705, 'n_words': 890}