Ejemplo n.º 1
0
        public void PropertiesInMixinAndChild_Mix_OnlyMissingPropertiesToImplement()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(PersonWithFullName));
            var mixinField = personClass.FindMixinReference("_name");

            var child = new ClassFactory(sourceCode.Semantic).Create(personClass);
            var mixin = new MixinReferenceFactory(sourceCode.Semantic).Create(mixinField);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // Assert: all properties of mixin should be implemented
            Assert.AreEqual(2, mixer.MembersToImplement.Count());
        }
Ejemplo n.º 2
0
        public void PropertiesInBaseClass_Mix_NoPropertyToImplement()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(ThirdPersonClass));
            var mixinField = personClass.FindMixinReference("_name");

            var child = new ClassFactory(sourceCode.Semantic).Create(personClass);
            var mixin = new MixinReferenceFactory(sourceCode.Semantic).Create(mixinField);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // Assert: all properties of mixin should be implemented
            Assert.IsEmpty(mixer.MembersToImplement);
        }
Ejemplo n.º 3
0
        public void MixinWithStaticMethod_Include_MethodNotIncluded()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(PersonWithStaticMethodMixin));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // no method to implement
            Assert.IsEmpty(mixer.MethodsToImplement);
        }
Ejemplo n.º 4
0
        public void MethodImplementedWithOtherParameter_Include_MethodIncluded()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(PersonWithOtherWorkMethod));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // no method to implement
            Assert.AreEqual(1,mixer.MethodsToImplement.Count(x => x.Name == "Work"));
        }
Ejemplo n.º 5
0
        public void MixinWithMethod_Include_MethodsIncluded()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(Person));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            Assert.AreEqual(mixer.MethodsToImplement.Count(), mixin.Class.Methods.Count());
            foreach (var service in mixin.Class.Methods)
                Assert.AreEqual(1, mixer.MethodsToImplement.Count(x => x.Name == service.Name));
        }
Ejemplo n.º 6
0
        public void MixinClassWithProperty_Include_PropertiesIncluded()
        {
            // arrange
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(Person));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // assert all properties of the mixin should have been added
            Assert.AreEqual(mixin.Class.Properties.Count(),mixer.PropertiesToImplement.Count());
            // check that every method of mixin appears only once in the child
            foreach (var service in mixin.Class.Properties)
                Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.Name == service.Name));
        }
Ejemplo n.º 7
0
        public void MixinWithPropertyOnlyGetter_Include_PropertyHasGetter()
        {
            // arrange
            // 1. load source files and get class and mixin declarations
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(PersonWithGetterName));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            // 2. create instances for mixin and child mixin
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // assert. Only one property in resulting class
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count());
            // that one property should only have a getter
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.IsReadOnly));
        }
Ejemplo n.º 8
0
        public void MixinInterfaceWithProperty_Include_PropertiesIncluded()
        {
            // arrange
            // 1. load source files and get class and mixin declarations
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(Person));
            var mixinReference = personClass.FindMixinReference("_interfaceName");
            var semanticModel = sourceCode.Semantic;
            // 2. create instances for mixin and child mixin
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // assert. The child should have the same properties as the mixin
            Assert.AreEqual(mixin.Class.Properties.Count(), mixer.PropertiesToImplement.Count());
            // check that every method of mixin appears only once in the child
            foreach (var service in mixin.Class.Properties)
                Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.Name == service.Name));
        }
Ejemplo n.º 9
0
        public void ChildWithAbstractProperty_Include_AbstractPropertyOverridden()
        {
            // we need Person file and NotCompilable because
            // the base class is defined in Person file
            var sourceCode = new SourceCode(Files.Person, Files.NotCompilable, Files.Name);
            var personClass = sourceCode.Class("PersonFromAbstractName");
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should be two methods, from base mixin and derived mixin
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count());
            // only one method from the mixin should be implemented, the other one
            // is alredy implemented by childs base
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.Name == "Name"));
            Assert.IsTrue(mixer.PropertiesToImplement.Single().IsOverride);
        }
Ejemplo n.º 10
0
        public void ChildWithOverrideProperty_Include_PropertyOverrideNotCreated()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(PersonWithOverriddenProperty));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // no property to override because child overrides it already
            Assert.AreEqual(0, mixer.PropertiesToImplement.Count());
        }
Ejemplo n.º 11
0
        public void MixinWithGenericProperty_Include_PropertyImplemented()
        {
            // arrange
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(PersonWithGenericMixin));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // child should have "Name" property
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.Name == "Names"));
            // name property should be of type "IEnumerable<string>"
            var typeName = mixer.PropertiesToImplement.Single().Type.ToString();
            Assert.AreEqual("System.Collections.Generic.IEnumerable<string>", typeName);
        }
Ejemplo n.º 12
0
        public void MixinWithIndexer_Include_IndexerImplemented()
        {
            // arrange
            var sourceCode = new SourceCode(Files.Person, Files.Collection);
            var personClass = sourceCode.Class(nameof(PersonWithIndexer));
            var mixinReference = personClass.FindMixinReference("_collection");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // child should also have an indexer property now
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count());
            Assert.AreEqual("string this[int index]", mixer.PropertiesToImplement.Single().ToString());            
        }
Ejemplo n.º 13
0
        public void ChildHasInterfaceWithProperty_Include_PropertyImplemented()
        {
            // arrange
            var sourceCode = new SourceCode(Files.NotCompilable, Files.Name);
            var personClass = sourceCode.Class("DerivedFromInterfaceClass");
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // assert: Child should have one "Name" property
            Assert.AreEqual(1, mixer.PropertiesToImplement.Count(x => x.Name == "Name"));
            // this one property should have only getter (signature from interface and mixin is the same)
            Assert.IsTrue(mixer.PropertiesToImplement.Single(x => x.Name == "Name").IsReadOnly);
        }
Ejemplo n.º 14
0
        public void MixinWithStaticProperty_Include_PropertyNotImplemented()
        {
            // arrange
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(PersonWithStaticMixin));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // assert: Child should not have a "Name" property
            Assert.IsEmpty(mixer.PropertiesToImplement);
        }
Ejemplo n.º 15
0
        public void MixinWithBaseClass_Include_BothMethodsImplemented()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(PersonWithDerivedWorker));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should be two methods, from base mixin and derived mixin
            Assert.AreEqual(2, mixer.MethodsToImplement.Count());
            // method from base should be in the implementation list
            Assert.AreEqual(1, mixer.MethodsToImplement.Count(x => x.Name == "Work"));
            // method from derived should be in the implementation list
            Assert.AreEqual(1, mixer.MethodsToImplement.Count(x => x.Name == "AdditionalWork"));            
        }
Ejemplo n.º 16
0
        public void ChildWithBaseMethod_Include_BaseMethodNotImplemented()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(DerivedPerson));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should be two methods, from base mixin and derived mixin
            Assert.AreEqual(1, mixer.MethodsToImplement.Count());
            // only one method from the mixin should be implemented, the other one
            // is alredy implemented by childs base
            Assert.AreEqual(1, mixer.MethodsToImplement.Count(x => x.Name == "Work"));
        }
Ejemplo n.º 17
0
        public void MixinWithToString_Include_ToStringShouldBeImplemented()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(PersonWithToString));
            var mixinReference = personClass.FindMixinReference("_toString");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // ToString should be in list of methods to override
            Assert.IsTrue(mixer.MethodsToImplement.Any(x => x.Name == "ToString"));
            // ToString in mixin must have override keyword
            Assert.IsTrue(mixer.MethodsToImplement.Single(x => x.Name == "ToString").IsOverrideFromObject);
        }
Ejemplo n.º 18
0
        public void ChildWithAbstractMethodFromBase_Include_AbstractMethodOverridden()
        {
            // file Person with base class is also needed here
            var sourceCode = new SourceCode(Files.Person, Files.NotCompilable, Files.Worker);
            var personClass = sourceCode.Class("PersonFromAbstractWork");
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should be one method that overrides the abstract method
            Assert.AreEqual(1, mixer.MethodsToImplement.Count());
            // only one method from the mixin should be implemented, the other one
            // is alredy implemented by childs base
            Assert.AreEqual(1, mixer.MethodsToImplement.Count(x => x.Name == "Work"));
            Assert.IsTrue(mixer.MethodsToImplement.Single().IsOverride);
        }
Ejemplo n.º 19
0
        public void ChildWithOverrideMethod_Include_MethodOverrideNotCreated()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker); 
            var personClass = sourceCode.Class(nameof(PersonWithOverriddenMethod));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should not be any method to override,
            // because the child itself already overrides the method
            Assert.AreEqual(0, mixer.MethodsToImplement.Count());
        }
Ejemplo n.º 20
0
        public void ChildHasBaseClassWithBaseClassesWithProperty_Include_PropertyNotReimplemented()
        {
            // arrange
            var sourceCode = new SourceCode(Files.Person, Files.Name);
            var personClass = sourceCode.Class(nameof(ThirdPersonClass));
            var mixinReference = personClass.FindMixinReference("_name");
            var semanticModel = sourceCode.Semantic;
            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            // act 
            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // nothing to implement for the mixer, child already has property
            Assert.IsEmpty(mixer.PropertiesToImplement);
        }
Ejemplo n.º 21
0
        public void MixinWithGenericParameter_Include_MixinImplemented()
        {
            var sourceCode = new SourceCode(Files.Person, Files.Worker);
            var personClass = sourceCode.Class(nameof(PersonWithGenericClassMixin));
            var mixinReference = personClass.FindMixinReference("_worker");
            var semanticModel = sourceCode.Semantic;

            var mixin = new MixinReferenceFactory(semanticModel).Create(mixinReference);
            var child = new ClassFactory(semanticModel).Create(personClass);

            var mixer = new Mixer();
            mixer.IncludeMixinInChild(mixin, child);

            // there should be one method to implement
            Assert.AreEqual(1, mixer.MethodsToImplement.Count());
            // parameter and return type of the method should be int
            Assert.AreEqual("int", mixer.MethodsToImplement.Single().ReturnType.ToString());
            Assert.AreEqual("int", mixer.MethodsToImplement.Single().GetParameter(0).Type.ToString());
        }