Exemplo n.º 1
0
        public override IQueryElement VisitClass_delcaration([NotNull] QueryGrammarParser.Class_delcarationContext context)
        {
            ClassDeclaration classDeclaration = new ClassDeclaration();

            IQueryElement className = Visit(context.class_name());

            classDeclaration.Add(className);

            if (context.parent_type() != null)
            {
                IQueryElement parentClasses = Visit(context.parent_type());
                classDeclaration.Add(parentClasses);
            }

            foreach (var attribute in context.cls_attribute_dec_stm())
            {
                IQueryElement attributeDeclaration = Visit(attribute);
                classDeclaration.Add(attributeDeclaration);
            }

            foreach (var method in context.cls_method_dec_stm())
            {
                IQueryElement methodDeclaration = Visit(method);
                classDeclaration.Add(methodDeclaration);
            }

            return(classDeclaration);
        }
Exemplo n.º 2
0
        public void ClassAlreadyExistsTest()
        {
            var existingClass = new Class
            {
                ClassId = new ClassId {
                    Id = 1, Name = "Test1"
                },
                Name = "Test1"
            };

            var classes = new ConcurrentDictionary <ClassId, Class>();

            classes.TryAdd(existingClass.ClassId, existingClass);

            Mock <IDatabaseParameters> database = new Mock <IDatabaseParameters>();

            database.Setup(d => d.Schema.Classes).Returns(classes);

            Mock <IStorage> storage = new Mock <IStorage>();

            storage.Setup(s => s.SaveSchema(It.IsAny <IDatabaseSchema>())).Verifiable();

            Mock <Action <String, MessageLevel> > log = new Mock <Action <String, MessageLevel> >();

            ClassDeclaration classDeclarationStatement = new ClassDeclaration();

            Mock <ClassName> className = new Mock <ClassName>();

            className.Setup(cn => cn.Execute(It.IsAny <QueryParameters>())).Returns(new QueryDTO {
                QueryClass = existingClass
            });
            classDeclarationStatement.Add(className.Object);

            QueryParameters parameters = new QueryParameters {
                Database = database.Object, Storage = storage.Object, Log = log.Object
            };
            var result = classDeclarationStatement.Execute(parameters);

            storage.Verify(s => s.SaveSchema(It.IsAny <IDatabaseSchema>()), Times.Never);
            Assert.AreEqual(ResultType.StringResult, result.Result.QueryResultType);
            Assert.AreEqual("Class or interface with name: " + existingClass.Name + " arleady exists!", result.Result.StringOutput);
        }
Exemplo n.º 3
0
        public void ClassWithParentDeclarationTest()
        {
            String newClassName = "newClass";
            int    newClassId   = 3;

            Property property1 = new Property {
                Type = Property.INT, PropertyId = new PropertyId {
                    Id = 1
                }
            };
            Property property2 = new Property {
                Type = Property.STRING, PropertyId = new PropertyId {
                    Id = 2
                }
            };
            Property property3 = new Property {
                Type = Property.INT, PropertyId = new PropertyId {
                    Id = 3
                }
            };
            var properties = new ConcurrentDictionary <PropertyId, Property>();

            properties.TryAdd(property1.PropertyId, property1);
            properties.TryAdd(property2.PropertyId, property2);
            properties.TryAdd(property3.PropertyId, property3);


            var existingClass = new Class
            {
                ClassId = new ClassId {
                    Id = 1, Name = "Test1"
                },
                Name = "Test1"
            };

            var otherExistingClass = new Class
            {
                ClassId = new ClassId {
                    Id = 2, Name = "Test2"
                },
                Name = "Test2"
            };

            var classes = new ConcurrentDictionary <ClassId, Class>();

            classes.TryAdd(existingClass.ClassId, existingClass);
            classes.TryAdd(otherExistingClass.ClassId, otherExistingClass);

            Mock <IDatabaseParameters> database = new Mock <IDatabaseParameters>();

            database.Setup(d => d.Schema.Classes).Returns(classes);
            database.Setup(d => d.Schema.Methods).Returns(new ConcurrentDictionary <ClassId, List <IMethod> >());

            Mock <IStorage> storage      = new Mock <IStorage>();
            Class           createdClass = null;

            storage.Setup(s => s.SaveSchema(It.IsAny <IDatabaseSchema>())).Callback <IDatabaseSchema>(
                schema => createdClass = schema.Classes.Values.Where(storable => storable.ClassId.Id == newClassId).SingleOrDefault());

            Mock <Action <String, MessageLevel> > log = new Mock <Action <String, MessageLevel> >();

            ClassDeclaration classDeclarationStatement = new ClassDeclaration();

            Mock <ClassName> className = new Mock <ClassName>();

            className.Setup(cn => cn.Execute(It.IsAny <QueryParameters>())).Returns(new QueryDTO
            {
                QueryClass = new Class
                {
                    Name = newClassName
                },
                Result = new DTOQueryResult
                {
                    QueryResultType = ResultType.StringResult,
                    StringOutput    = "No class"
                }
            });
            classDeclarationStatement.Add(className.Object);

            Mock <ParentClasses> parentClassDeclaration = new Mock <ParentClasses>();

            parentClassDeclaration.Setup(cn => cn.Execute(It.IsAny <QueryParameters>())).Returns(new QueryDTO {
                Value = new HashSet <Class> {
                    existingClass
                }
            });
            classDeclarationStatement.Add(parentClassDeclaration.Object);

            QueryParameters parameters = new QueryParameters {
                Database = database.Object, Storage = storage.Object, Log = log.Object
            };

            var result = classDeclarationStatement.Execute(parameters);

            Assert.AreEqual(ResultType.StringResult, result.Result.QueryResultType);
            Assert.AreEqual("New class: " + newClassName + " created.", result.Result.StringOutput);
            storage.Verify(s => s.SaveSchema(It.IsAny <IDatabaseSchema>()), Times.Once);

            Assert.AreEqual(newClassId, createdClass.ClassId.Id);
            Assert.AreEqual(newClassName, createdClass.Name);
            Assert.AreEqual(existingClass, createdClass.Parent.Single());
            Assert.IsFalse(createdClass.Interface);
        }