Esempio n. 1
0
        public void TestProgramClassTypeCollection_1()
        {
            ProgramClassTypeCollection collection = new ProgramClassTypeCollection();
            ProgramClass     testProgramClass;
            ProgramInterface testProgramInterface;
            List <string>    list = new List <string>();
            List <string>    testClassModifiers     = new List <string>();
            List <string>    testClassGenerics      = new List <string>();
            List <string>    testInterfaceModifiers = new List <string>();
            List <string>    testInterfaceGenerics  = new List <string>();

            testClassModifiers.Add("x");
            testClassGenerics.Add("y");
            testInterfaceModifiers.Add("x");
            testInterfaceGenerics.Add("y");

            testProgramClass     = new ProgramClass("item2", testClassModifiers, testClassGenerics);
            testProgramInterface = new ProgramInterface("item2", testInterfaceModifiers, testInterfaceGenerics);

            collection.Add(new ProgramClass("item0", list, list));
            collection.Add(new ProgramInterface("item1", list, list));
            collection.Add(new ProgramClass("item2", list, list));
            collection.Add(new ProgramClass("item3", list, list));

            Assert.AreEqual(collection[2], collection["item2"]);
            Assert.AreEqual(collection[2], testProgramClass);
            Assert.AreEqual(collection["item2"], testProgramClass);
            Assert.AreNotEqual(collection[2], testProgramInterface);
            Assert.AreNotEqual(collection["item2"], testProgramInterface);
        }
        public void TestFunctionSignature_F()
        {
            ProgramFile programFile = new ProgramFile("path", "name", "text");
            ProgramClassTypeCollection expectedClassTypeCollection = new ProgramClassTypeCollection();
            ProgramClassTypeCollection actualClassTypeCollection   = new ProgramClassTypeCollection();
            CodeProcessor   codeProcessor = new CodeProcessor(programFile, actualClassTypeCollection);
            ProgramClass    expectedProgramClass;
            ProgramClass    actualProgramClass;
            ProgramFunction expectedProgramFunction;
            ProgramFunction actualProgramFunction;

            string[] fileTextDataArray = { "class",  "ClassName", " ",   "{",      " ", "public", "virtual", "(",
                                           "List",   "<",         "int", ">",      ",", "bool",   ",",       "float",")",  "Test",   "<", "Object", ">", "(",
                                           "double", "n",         ",",   "double", "m", ")",      " ",       "{",    " ",  "return", "(", "0",      ",", "0",")",
                                           ";",      " ",         "}",   " ",      "}" };

            programFile.FileTextData.AddRange(fileTextDataArray);

            string        name  = "Test";
            List <string> empty = new List <string>();
            List <string> ModF  = new List <string>();
            List <string> RetF  = new List <string>();
            List <string> GenF  = new List <string>();
            List <string> ParF  = new List <string>();

            ModF.Add("public"); ModF.Add("virtual");
            RetF.Add("List"); RetF.Add("<"); RetF.Add("int"); RetF.Add(">"); RetF.Add(","); RetF.Add("bool"); RetF.Add(","); RetF.Add("float");
            GenF.Add("Object");
            ParF.Add("double"); ParF.Add("n"); ParF.Add(","); ParF.Add("double"); ParF.Add("m");

            expectedProgramClass    = new ProgramClass("ClassName", empty, empty);
            expectedProgramFunction = new ProgramFunction(name, ModF, RetF, GenF, ParF, empty);

            expectedProgramClass.ChildList.Add(expectedProgramFunction);
            expectedClassTypeCollection.Add(expectedProgramClass);

            codeProcessor.ProcessFileCode();

            CollectionAssert.AreEqual(expectedClassTypeCollection, actualClassTypeCollection);

            actualProgramClass = (ProgramClass)actualClassTypeCollection[0];

            Assert.AreEqual(expectedProgramClass.ChildList.Count, actualProgramClass.ChildList.Count);

            actualProgramFunction = (ProgramFunction)actualProgramClass.ChildList[0];

            Assert.AreEqual(expectedProgramFunction.Name, actualProgramFunction.Name);
            CollectionAssert.AreEqual(expectedProgramFunction.Modifiers, actualProgramFunction.Modifiers);
            CollectionAssert.AreEqual(expectedProgramFunction.ReturnTypes, actualProgramFunction.ReturnTypes);
            CollectionAssert.AreEqual(expectedProgramFunction.Generics, actualProgramFunction.Generics);
            CollectionAssert.AreEqual(expectedProgramFunction.Parameters, actualProgramFunction.Parameters);
        }
Esempio n. 3
0
        public void TestProgramClassTypeCollection_2()
        {
            ProgramClassTypeCollection collection = new ProgramClassTypeCollection();
            List <string> list = new List <string>();
            List <string> testClassModifiers = new List <string>();
            ProgramClass  testProgramClass;

            testClassModifiers.Add("x");

            testProgramClass = new ProgramClass("item2", testClassModifiers, list);

            collection.Add(new ProgramClass("item0", list, list));
            collection.Add(new ProgramInterface("item1", list, list));
            collection.Add(new ProgramClass("item2", list, list));
            collection.Add(new ProgramClass("item3", list, list));

            collection[2].Name = "item2_newName";

            Assert.ThrowsException <KeyNotFoundException>(() => collection["item2"]);
            Assert.AreEqual(collection[2], collection["item2_newName"]);
            Assert.AreNotEqual(collection[2], testProgramClass);
            Assert.AreNotEqual(collection["item2_newName"], testProgramClass);
        }
        public void TestFunctionSignature_B()
        {
            ProgramFile programFile = new ProgramFile("path", "name", "text");
            ProgramClassTypeCollection expectedClassTypeCollection = new ProgramClassTypeCollection();
            ProgramClassTypeCollection actualClassTypeCollection   = new ProgramClassTypeCollection();
            CodeProcessor   codeProcessor = new CodeProcessor(programFile, actualClassTypeCollection);
            ProgramClass    expectedProgramClass;
            ProgramClass    actualProgramClass;
            ProgramFunction expectedProgramFunction;
            ProgramFunction actualProgramFunction;

            string[] fileTextDataArray = { "class", "ClassName", " ", "{", " ",   "void", "Test", "<", "V",
                                           ",",     "W",         ">", "(", "int", "x",    ")",    " ", "{"," ","return", "(", "0", ",", "0", ")",
                                           ";",     " ",         "}", " ", "}" };

            programFile.FileTextData.AddRange(fileTextDataArray);

            string        name  = "Test";
            List <string> empty = new List <string>();
            List <string> RetB  = new List <string>();
            List <string> GenB  = new List <string>();
            List <string> ParB  = new List <string>();

            RetB.Add("void");
            GenB.Add("V"); GenB.Add(","); GenB.Add("W");
            ParB.Add("int"); ParB.Add("x");

            expectedProgramClass    = new ProgramClass("ClassName", empty, empty);
            expectedProgramFunction = new ProgramFunction(name, empty, RetB, GenB, ParB, empty);

            expectedProgramClass.ChildList.Add(expectedProgramFunction);
            expectedClassTypeCollection.Add(expectedProgramClass);

            codeProcessor.ProcessFileCode();

            CollectionAssert.AreEqual(expectedClassTypeCollection, actualClassTypeCollection);

            actualProgramClass = (ProgramClass)actualClassTypeCollection[0];

            Assert.AreEqual(expectedProgramClass.ChildList.Count, actualProgramClass.ChildList.Count);

            actualProgramFunction = (ProgramFunction)actualProgramClass.ChildList[0];

            Assert.AreEqual(expectedProgramFunction.Name, actualProgramFunction.Name);
            CollectionAssert.AreEqual(expectedProgramFunction.Modifiers, actualProgramFunction.Modifiers);
            CollectionAssert.AreEqual(expectedProgramFunction.ReturnTypes, actualProgramFunction.ReturnTypes);
            CollectionAssert.AreEqual(expectedProgramFunction.Generics, actualProgramFunction.Generics);
            CollectionAssert.AreEqual(expectedProgramFunction.Parameters, actualProgramFunction.Parameters);
        }
        public void TestFunctionSignature_O()
        {
            ProgramFile programFile = new ProgramFile("path", "name", "text");
            ProgramClassTypeCollection expectedClassTypeCollection = new ProgramClassTypeCollection();
            ProgramClassTypeCollection actualClassTypeCollection   = new ProgramClassTypeCollection();
            CodeProcessor   codeProcessor = new CodeProcessor(programFile, actualClassTypeCollection);
            ProgramClass    expectedProgramClass;
            ProgramClass    actualProgramClass;
            ProgramFunction expectedProgramFunction;
            ProgramFunction actualProgramFunction;

            string[] fileTextDataArray = { "class", "ClassName", " ",       "{",        " ", "public", "static",
                                           "Test",  ".",         "NewType", "TestFunc", "(", ")",      " ",     "{"," ", "return", "(", "0",
                                           ",",     "0",         ")",       ";",        " ", "}",      " ",     "}" };

            programFile.FileTextData.AddRange(fileTextDataArray);

            string        name  = "TestFunc";
            List <string> empty = new List <string>();
            List <string> ModO  = new List <string>();
            List <string> RetO  = new List <string>();

            ModO.Add("public"); ModO.Add("static");
            RetO.Add("Test"); RetO.Add("."); RetO.Add("NewType");

            expectedProgramClass    = new ProgramClass("ClassName", empty, empty);
            expectedProgramFunction = new ProgramFunction(name, ModO, RetO, empty, empty, empty);

            expectedProgramClass.ChildList.Add(expectedProgramFunction);
            expectedClassTypeCollection.Add(expectedProgramClass);

            codeProcessor.ProcessFileCode();

            CollectionAssert.AreEqual(expectedClassTypeCollection, actualClassTypeCollection);

            actualProgramClass = (ProgramClass)actualClassTypeCollection[0];

            Assert.AreEqual(expectedProgramClass.ChildList.Count, actualProgramClass.ChildList.Count);

            actualProgramFunction = (ProgramFunction)actualProgramClass.ChildList[0];

            Assert.AreEqual(expectedProgramFunction.Name, actualProgramFunction.Name);
            CollectionAssert.AreEqual(expectedProgramFunction.Modifiers, actualProgramFunction.Modifiers);
            CollectionAssert.AreEqual(expectedProgramFunction.ReturnTypes, actualProgramFunction.ReturnTypes);
            CollectionAssert.AreEqual(expectedProgramFunction.Generics, actualProgramFunction.Generics);
            CollectionAssert.AreEqual(expectedProgramFunction.Parameters, actualProgramFunction.Parameters);
        }
        public void TestCodeProcessor_ProcessFileCode_Structure()
        {
            CodeProcessor codeProcessor;
            ProgramFile   expectedProgramFile;
            ProgramClassTypeCollection expectedProgramClassTypeCollection = new ProgramClassTypeCollection();
            FileProcessor fileProcessorForActual;
            ProgramFile   actualProgramFile;
            ProgramClassTypeCollection actualProgramClassTypeCollection = new ProgramClassTypeCollection();

            ProgramClassType tempProgramClassType;
            List <string>    empty = new List <string>();

            string filePath = Path.GetFullPath("..\\..\\..\\CodeAnalyzerTests\\TestInputFiles\\TestInputFile.cs");
            string fileName = "TestInputFile.cs";
            string fileText = "";

            if (File.Exists(filePath))
            {
                fileText = File.ReadAllText(filePath);
            }

            expectedProgramFile  = new ProgramFile(filePath, fileName, fileText);
            tempProgramClassType = new ProgramInterface("IAnimalActions", empty, empty);
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramInterface("IHumanActions", empty, empty);
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramClass("Animal", empty, empty);
            tempProgramClassType.ChildList.Add(new ProgramFunction("Animal", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("Move", empty, empty, empty, empty, empty));
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramClass("Pet", empty, empty);
            tempProgramClassType.ChildList.Add(new ProgramFunction("Pet", empty, empty, empty, empty, empty));
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramClass("Dog", empty, empty);
            tempProgramClassType.ChildList.Add(new ProgramFunction("Dog", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("Talk", empty, empty, empty, empty, empty));
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramClass("Human", empty, empty);
            tempProgramClassType.ChildList.Add(new ProgramFunction("Human", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("Talk", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("Move", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("GoToSchool", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("GraduateSchool", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("GoToWork", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("BuyPet", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("BuyDog", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("BuyCar", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("SellCar", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("FillCarFuelTank", empty, empty, empty, empty, empty));
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);
            tempProgramClassType = new ProgramClass("Car", empty, empty);
            tempProgramClassType.ChildList.Add(new ProgramFunction("Car", empty, empty, empty, empty, empty));
            tempProgramClassType.ChildList.Add(new ProgramFunction("FillTank", empty, empty, empty, empty, empty));
            expectedProgramFile.ChildList.Add(tempProgramClassType);
            expectedProgramClassTypeCollection.Add(tempProgramClassType);

            actualProgramFile      = new ProgramFile(filePath, fileName, fileText);
            fileProcessorForActual = new FileProcessor(actualProgramFile);
            fileProcessorForActual.ProcessFile();
            codeProcessor = new CodeProcessor(actualProgramFile, actualProgramClassTypeCollection);
            codeProcessor.ProcessFileCode();

            CheckAllChildLists(expectedProgramFile, actualProgramFile);
            CollectionAssert.AreEqual(expectedProgramClassTypeCollection, actualProgramClassTypeCollection);
        }
        public void TestRelationshipProcessor_ProcessRelationships()
        {
            ProgramFile   programFile;
            CodeProcessor codeProcessor;
            FileProcessor fileProcessor;
            ProgramClassTypeCollection expectedProgramClassTypeCollection = new ProgramClassTypeCollection();
            ProgramClassTypeCollection actualProgramClassTypeCollection   = new ProgramClassTypeCollection();
            List <string> empty = new List <string>();

            string filePath = Path.GetFullPath("..\\..\\..\\CodeAnalyzerTests\\TestInputFiles\\TestInputFile.cs");
            string fileName = "TestInputFile.cs";
            string fileText = "";

            if (File.Exists(filePath))
            {
                fileText = File.ReadAllText(filePath);
            }

            ProgramInterface IAnimalActions = new ProgramInterface("IAnimalActions", empty, empty);
            ProgramInterface IHumanActions  = new ProgramInterface("IHumanActions", empty, empty);
            ProgramClass     Animal         = new ProgramClass("Animal", empty, empty);
            ProgramClass     Pet            = new ProgramClass("Pet", empty, empty);
            ProgramClass     Dog            = new ProgramClass("Dog", empty, empty);
            ProgramClass     Human          = new ProgramClass("Human", empty, empty);
            ProgramClass     Car            = new ProgramClass("Car", empty, empty);

            IAnimalActions.SubClasses.Add(Dog);
            IAnimalActions.SubClasses.Add(Human);
            IHumanActions.SubClasses.Add(Human);
            Animal.SubClasses.Add(Pet);
            Animal.SubClasses.Add(Human);
            Pet.SuperClasses.Add(Animal);
            Pet.SubClasses.Add(Dog);
            Pet.OwnedByClasses.Add(Human);
            Pet.UsedByClasses.Add(Human);
            Dog.SuperClasses.Add(Pet);
            Dog.SuperClasses.Add(IAnimalActions);
            Dog.OwnedByClasses.Add(Human);
            Human.SuperClasses.Add(Animal);
            Human.SuperClasses.Add(IAnimalActions);
            Human.SuperClasses.Add(IHumanActions);
            Human.OwnedClasses.Add(Car);
            Human.OwnedClasses.Add(Pet);
            Human.OwnedClasses.Add(Dog);
            Human.UsedClasses.Add(Pet);
            Car.OwnedByClasses.Add(Human);

            expectedProgramClassTypeCollection.Add(IAnimalActions);
            expectedProgramClassTypeCollection.Add(IHumanActions);
            expectedProgramClassTypeCollection.Add(Animal);
            expectedProgramClassTypeCollection.Add(Pet);
            expectedProgramClassTypeCollection.Add(Dog);
            expectedProgramClassTypeCollection.Add(Human);
            expectedProgramClassTypeCollection.Add(Car);

            programFile   = new ProgramFile(filePath, fileName, fileText);
            fileProcessor = new FileProcessor(programFile);
            codeProcessor = new CodeProcessor(programFile, actualProgramClassTypeCollection);

            fileProcessor.ProcessFile();
            codeProcessor.ProcessFileCode();

            foreach (ProgramClassType programClassType in actualProgramClassTypeCollection)
            {
                RelationshipProcessor relationshipProcessor = new RelationshipProcessor(programClassType, actualProgramClassTypeCollection);
                relationshipProcessor.ProcessRelationships();
            }

            CollectionAssert.AreEquivalent(expectedProgramClassTypeCollection, actualProgramClassTypeCollection);

            foreach (ProgramClassType expectedProgramClassType in expectedProgramClassTypeCollection)
            {
                if (actualProgramClassTypeCollection.Contains(expectedProgramClassType.Name))
                {
                    ProgramClassType actualProgramClassType = actualProgramClassTypeCollection[expectedProgramClassType.Name];
                    CollectionAssert.AreEquivalent(expectedProgramClassType.SubClasses, actualProgramClassType.SubClasses);
                    CollectionAssert.AreEquivalent(expectedProgramClassType.SuperClasses, actualProgramClassType.SuperClasses);

                    if (expectedProgramClassType.GetType() == typeof(ProgramClass))
                    {
                        ProgramClass expectedProgramClass = (ProgramClass)expectedProgramClassType;
                        ProgramClass actualProgramClass   = (ProgramClass)actualProgramClassType;
                        CollectionAssert.AreEquivalent(expectedProgramClass.OwnedClasses, actualProgramClass.OwnedClasses);
                        CollectionAssert.AreEquivalent(expectedProgramClass.OwnedByClasses, actualProgramClass.OwnedByClasses);
                        CollectionAssert.AreEquivalent(expectedProgramClass.UsedClasses, actualProgramClass.UsedClasses);
                        CollectionAssert.AreEquivalent(expectedProgramClass.UsedByClasses, actualProgramClass.UsedByClasses);
                    }
                }
            }
        }