예제 #1
0
        private string MakeCode()
        {
            var hasEvents    = this.BaseType.HasEvents();
            var methods      = this.GetGeneratedMethods(hasEvents);
            var constructors = this.GetGeneratedConstructors();
            var properties   = this.GetGeneratedProperties(hasEvents);
            var events       = this.GetGeneratedEvents();

            this.IsUnsafe |= constructors.IsUnsafe || events.IsUnsafe || methods.IsUnsafe || properties.IsUnsafe;
            this.RequiresObsoleteSuppression |= this.BaseType.GetCustomAttribute <ObsoleteAttribute>() != null ||
                                                constructors.RequiresObsoleteSuppression || events.RequiresObsoleteSuppression ||
                                                methods.RequiresObsoleteSuppression || properties.RequiresObsoleteSuppression;

            this.Namespaces.Remove(this.BaseType.Namespace);

            var(_, constraints) = this.BaseType.GetGenericArguments(this.Namespaces);

            var namespaces = string.Join(Environment.NewLine,
                                         (from @namespace in this.Namespaces
                                          select $"using {@namespace};"));

            var @class = ClassTemplates.GetClass(namespaces,
                                                 this.TypeName, this.BaseType.GetFullName(),
                                                 methods.Result, properties.Result, events.Result, constructors.Result,
                                                 this.BaseType.Namespace,
                                                 this.Options.Serialization == SerializationOption.Supported ?
                                                 "[Serializable]" : string.Empty,
                                                 this.Options.Serialization == SerializationOption.Supported ?
                                                 ConstructorTemplates.GetConstructorWithNoArguments(this.GetTypeNameWithNoGenerics()) : string.Empty,
                                                 this.GetAdditionNamespaceCode(),
                                                 this.IsUnsafe, constraints,
                                                 hasEvents ? "R.IMockWithEvents" : "R.IMock",
                                                 hasEvents ? ClassTemplates.GetRaiseImplementation() : string.Empty);

            if (this.RequiresObsoleteSuppression)
            {
                @class = ClassTemplates.GetClassWithObsoleteSuppression(@class);
            }

            return(@class);
        }
예제 #2
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, string constructorName)
        {
            var isUnsafe = false;
            var requiresObsoleteSuppression = false;

            var generatedConstructors = new List <string>();

            if (baseType.IsInterface)
            {
                generatedConstructors.Add(ConstructorTemplates.GetConstructor(
                                              constructorName, string.Empty, string.Empty));
            }
            else
            {
                foreach (var constructor in baseType.GetMockableConstructors(generator))
                {
                    var baseConstructor = constructor.Value;

                    var parameters = baseConstructor.GetParameters(namespaces);

                    if (!string.IsNullOrWhiteSpace(parameters))
                    {
                        parameters = $", {parameters}";
                    }

                    generatedConstructors.Add(ConstructorTemplates.GetConstructor(
                                                  constructorName, baseConstructor.GetArgumentNameList(), parameters));
                    isUnsafe |= baseConstructor.IsUnsafeToMock();
                    requiresObsoleteSuppression |= baseConstructor.GetCustomAttribute <ObsoleteAttribute>() != null;
                }
            }

            return(new GenerateResults(string.Join(Environment.NewLine, generatedConstructors),
                                       requiresObsoleteSuppression, isUnsafe));
        }
예제 #3
0
        public static void GetConstructor() =>
        Assert.That(ConstructorTemplates.GetConstructor("a", "b", "c"), Is.EqualTo(
                        @"public a(SCO.ReadOnlyDictionary<int, SCO.ReadOnlyCollection<R.HandlerInformation>> handlersc)
	: base(b) =>
	this.handlers = handlers;"    ));
예제 #4
0
        public static void GetConstructorNoArguments() =>
        Assert.That(ConstructorTemplates.GetConstructorWithNoArguments("a"), Is.EqualTo(
                        @"public a() =>
	this.handlers = new SCO.ReadOnlyDictionary<int, SCO.ReadOnlyCollection<R.HandlerInformation>>(
		new SCG.Dictionary<int, SCO.ReadOnlyCollection<R.HandlerInformation>>());"        ));