private static void HandlePublicSetter(bool isMake, bool hasEvents, PropertyInfo baseProperty, List <string> propertyImplementations)
        {
            var setMethod     = baseProperty.SetMethod;
            var setVisibility = setMethod.IsPublic ? string.Empty : CodeTemplates.GetVisibility(setMethod.IsFamily, setMethod.IsFamilyOrAssembly);

            if (isMake)
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertySetForMake(setVisibility));
            }
            else if (setMethod.GetParameters()[0].ParameterType.IsSpanLike())
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertySetForSpanLike(setVisibility));
            }
            else
            {
                var setArgumentNameList = setMethod.GetArgumentNameList();
                var setDelegateCast     = setMethod.GetDelegateCast();

                if (setMethod.GetParameters().Length > 0)
                {
                    var setExpectationChecks           = setMethod.GetExpectationChecks();
                    var setExpectationExceptionMessage = setMethod.GetExpectationExceptionMessage();
                    propertyImplementations.Add(PropertyTemplates.GetPropertySet(
                                                    setMethod.MetadataToken, setArgumentNameList, setExpectationChecks, setDelegateCast,
                                                    setExpectationExceptionMessage, setVisibility, hasEvents));
                }
                else
                {
                    propertyImplementations.Add(PropertyTemplates.GetPropertySetAndNoIndexers(
                                                    setMethod.MetadataToken, setArgumentNameList, setDelegateCast, setVisibility, hasEvents));
                }
            }
        }
 private static void CreateCharacterCharacteristic()
 {
     CodeTemplates.CreateFromTemplate(
         "NameCharacterCharacteristic.cs",
         @"Assets/Scripts/Editor/CodeTemplates/CharacterCharacteristic.txt",
         "CharacterCharacteristic");
 }
Beispiel #3
0
        ///--------------------------------------------------------------------------------
        /// <summary>This method updates the view model data and sends update command back
        /// to the solution builder.</summary>
        ///--------------------------------------------------------------------------------
        protected override void OnUpdate()
        {
            // send update for any updated children
            foreach (CodeTemplateViewModel item in CodeTemplates)
            {
                if (item.IsEdited == true)
                {
                    item.Update();
                }
            }
            // send update for any new children
            foreach (CodeTemplateViewModel item in ItemsToAdd.OfType <CodeTemplateViewModel>())
            {
                item.Update();
                CodeTemplates.Add(item);
            }
            ItemsToAdd.Clear();

            // send delete for any deleted children
            foreach (CodeTemplateViewModel item in ItemsToDelete.OfType <CodeTemplateViewModel>())
            {
                item.Delete();
                CodeTemplates.Remove(item);
            }
            ItemsToDelete.Clear();

            // reset modified for children
            foreach (CodeTemplateViewModel item in CodeTemplates)
            {
                item.ResetModified(false);
            }
        }
 private static void CreateTileNodeLogic()
 {
     CodeTemplates.CreateFromTemplate(
         "TileNodeLogic.cs",
         @"Assets/Scripts/Editor/CodeTemplates/TileNodeLogic.txt",
         "TileNodeLogic");
 }
        private static bool HandleNonPrivateAbstractProperty(SortedSet <string> namespaces, bool requiresObsoleteSuppression,
                                                             List <string> generatedProperties, PropertyMockableResult property, PropertyInfo baseProperty, ParameterInfo[] indexers, MethodInfo propertyMethod)
        {
            var propertyImplementations = new List <string>();
            var visibility = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                             string.Empty : CodeTemplates.GetVisibility(propertyMethod.IsFamily, propertyMethod.IsFamilyOrAssembly);

            if (property.Accessors == PropertyAccessors.Get || property.Accessors == PropertyAccessors.GetAndSet)
            {
                var getVisibility = CodeTemplates.GetVisibility(baseProperty.GetMethod.IsFamily, baseProperty.GetMethod.IsFamilyOrAssembly);

                if (getVisibility == visibility)
                {
                    getVisibility = string.Empty;
                }

                propertyImplementations.Add(PropertyTemplates.GetNonPublicPropertyGet(getVisibility));
            }

            if (property.Accessors == PropertyAccessors.Set || property.Accessors == PropertyAccessors.GetAndSet)
            {
                var setVisibility = CodeTemplates.GetVisibility(baseProperty.SetMethod.IsFamily, baseProperty.SetMethod.IsFamilyOrAssembly);

                if (setVisibility == visibility)
                {
                    setVisibility = string.Empty;
                }

                propertyImplementations.Add(PropertyTemplates.GetNonPublicPropertySet(setVisibility));
            }

            var explicitInterfaceName = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                                        $"{property.Value.DeclaringType.GetFullName(namespaces)}." : string.Empty;

            if (indexers.Length > 0)
            {
                var parameters = string.Join(", ",
                                             from indexer in indexers
                                             let _ = namespaces.Add(indexer.ParameterType.Namespace)
                                                     select $"{indexer.ParameterType.Name} {indexer.Name}");

                // Indexer
                generatedProperties.Add(PropertyTemplates.GetNonPublicPropertyIndexer(visibility,
                                                                                      $"{baseProperty.PropertyType.GetFullName(namespaces)}", parameters,
                                                                                      string.Join(Environment.NewLine, propertyImplementations), explicitInterfaceName));
            }
            else
            {
                // Normal
                generatedProperties.Add(PropertyTemplates.GetNonPublicProperty(visibility,
                                                                               $"{baseProperty.PropertyType.GetFullName(namespaces)}", baseProperty.Name,
                                                                               string.Join(Environment.NewLine, propertyImplementations), explicitInterfaceName));
            }

            requiresObsoleteSuppression |= baseProperty.GetCustomAttribute <ObsoleteAttribute>() != null;
            return(requiresObsoleteSuppression);
        }
        /// <summary>
        /// Gets the full source code by applying an appropriate template based on the current <see cref="CodeTaskFactoryCodeType"/>.
        /// </summary>
        internal static void ApplySourceCodeTemplate(TaskInfo taskInfo, ICollection <TaskPropertyInfo> parameters)
        {
            if (taskInfo?.SourceCode != null && CodeTemplates.ContainsKey(taskInfo.CodeLanguage) && CodeTemplates[taskInfo.CodeLanguage].ContainsKey(taskInfo.CodeType))
            {
                string usingStatement = String.Join(Environment.NewLine, GetNamespaceStatements(taskInfo.CodeLanguage, DefaultNamespaces.Union(taskInfo.Namespaces, StringComparer.OrdinalIgnoreCase)));
                string properties     = parameters == null ? String.Empty : String.Join(Environment.NewLine, GetPropertyStatements(taskInfo.CodeLanguage, parameters).Select(i => $"        {i}"));

                // Apply the corresponding template based on the code type
                //
                taskInfo.SourceCode = String.Format(CodeTemplates[taskInfo.CodeLanguage][taskInfo.CodeType], usingStatement, taskInfo.Name, properties, taskInfo.SourceCode);
            }
        }
        private static void HandlePublicGetter(SortedSet <string> namespaces, bool isMake, bool hasEvents,
                                               PropertyInfo baseProperty, List <string> propertyImplementations)
        {
            var getMethod     = baseProperty.GetMethod;
            var getVisibility = getMethod.IsPublic ? string.Empty : CodeTemplates.GetVisibility(getMethod.IsFamily, getMethod.IsFamilyOrAssembly);
            var returnType    = $"{getMethod.ReturnType.GetFullName(namespaces, getMethod.ReturnParameter)}";

            if (isMake)
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertyGetForMake(getVisibility));
            }
            else if (getMethod.ReturnType.IsSpanLike())
            {
                propertyImplementations.Add(PropertyTemplates.GetPropertyGetForSpanLike(getVisibility));
            }
            else
            {
                var getArgumentNameList = getMethod.GetArgumentNameList();
                var getDelegateCast     = getMethod.GetDelegateCast();

                if (getMethod.GetParameters().Length > 0)
                {
                    var getExpectationChecks           = getMethod.GetExpectationChecks();
                    var getExpectationExceptionMessage = getMethod.GetExpectationExceptionMessage();
                    propertyImplementations.Add(getMethod.ReturnType.RequiresExplicitCast() ?
                                                PropertyTemplates.GetPropertyGetWithValueTypeReturnValue(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType,
                                                    getExpectationChecks, getDelegateCast, getExpectationExceptionMessage, getVisibility, hasEvents) :
                                                PropertyTemplates.GetPropertyGetWithReferenceTypeReturnValue(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType,
                                                    getExpectationChecks, getDelegateCast, getExpectationExceptionMessage, getVisibility, hasEvents));
                }
                else
                {
                    propertyImplementations.Add(getMethod.ReturnType.RequiresExplicitCast() ?
                                                PropertyTemplates.GetPropertyGetWithValueTypeReturnValueAndNoIndexers(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType, getDelegateCast, getVisibility, hasEvents) :
                                                PropertyTemplates.GetPropertyGetWithReferenceTypeReturnValueAndNoIndexers(
                                                    getMethod.MetadataToken, getArgumentNameList, returnType, getDelegateCast, getVisibility, hasEvents));
                }
            }
        }
Beispiel #8
0
        internal static string GetClass(string usingStatements, string mockTypeName, string baseType,
                                        string implementedMethods, string implementedProperties, string implementedEvents, string generatedConstructors, string baseTypeNamespace,
                                        string classAttributes, string noArgumentConstructor, string additionalCode, bool isUnsafe, string baseTypeConstraints,
                                        string mockType, string raiseImplementation) =>
        $@"#pragma warning disable CS8019
using R = Rocks;
using RE = Rocks.Exceptions;
using S = System;
using SCG = System.Collections.Generic;
using SCO = System.Collections.ObjectModel;
using SR = System.Reflection;
using STT = System.Threading.Tasks;
{usingStatements}
#pragma warning restore CS8019

{(!string.IsNullOrWhiteSpace(baseTypeNamespace) ? $"namespace {baseTypeNamespace}" : string.Empty)}
{(!string.IsNullOrWhiteSpace(baseTypeNamespace) ? "{" : string.Empty)}
	{classAttributes}
	public {CodeTemplates.GetIsUnsafe(isUnsafe)} sealed class {mockTypeName}
		: {baseType}, {mockType} {baseTypeConstraints}
	{{
		private SCO.ReadOnlyDictionary<int, SCO.ReadOnlyCollection<R.HandlerInformation>> handlers;

		{noArgumentConstructor}

		{generatedConstructors}

		{implementedMethods}

		{implementedProperties}

		{implementedEvents}

		SCO.ReadOnlyDictionary<int, SCO.ReadOnlyCollection<R.HandlerInformation>> R.IMock.Handlers => this.handlers;

		{raiseImplementation}

		{additionalCode}
	}}
{(!string.IsNullOrWhiteSpace(baseTypeNamespace) ? "}" : string.Empty)}";
Beispiel #9
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, MethodInformationBuilder builder)
        {
            var requiresObsoleteSuppression = false;
            var generatedEvents             = new List <string>();

            foreach (var @event in baseType.GetMockableEvents(generator))
            {
                var eventHandlerType     = @event.EventHandlerType;
                var eventHandlerTypeInfo = eventHandlerType;

                namespaces.Add(eventHandlerType.Namespace);

                var eventMethod = @event.AddMethod;

                var methodInformation = builder.Build(new MockableResult <MethodInfo>(
                                                          eventMethod, RequiresExplicitInterfaceImplementation.No));
                var @override = methodInformation.DescriptionWithOverride.Contains("override", StringComparison.Ordinal) ? "override " : string.Empty;

                if (eventMethod.IsPublic)
                {
                    generatedEvents.Add(EventTemplates.GetEvent(@override,
                                                                eventHandlerType.GetFullName(namespaces), @event.Name));
                }
                else if (!eventMethod.IsPrivate && eventMethod.IsAbstract)
                {
                    var visibility = CodeTemplates.GetVisibility(eventMethod.IsFamily, eventMethod.IsFamilyOrAssembly);

                    generatedEvents.Add(EventTemplates.GetNonPublicEvent(visibility,
                                                                         eventHandlerType.GetFullName(namespaces), @event.Name));
                }

                requiresObsoleteSuppression |= @event.GetCustomAttribute <ObsoleteAttribute>() != null;
            }

            var result = generatedEvents.Count > 0 ? EventTemplates.GetEvents(generatedEvents.AsReadOnly()) : string.Empty;

            return(new GenerateResults(result, requiresObsoleteSuppression, false));
        }
Beispiel #10
0
 private static string TestCode(string code) => CodeTemplates.LinqContext(code);
 public static void GetExpectation() =>
 Assert.That(CodeTemplates.GetExpectation("a", "b"), Is.EqualTo(
                 "((R.ArgumentExpectation<b>)methodHandler.Expectations[\"a\"]).IsValid(a)"));
Beispiel #12
0
 internal static string GetExpectationChecks(this MethodInfo @this) =>
 string.Join(" && ",
             @this.GetParameters()
             .Where(_ => !TypeDissector.Create(_.ParameterType).IsPointer)
             .Select(_ => CodeTemplates.GetExpectation(_.Name, $"{_.ParameterType.GetFullName(_)}")));
Beispiel #13
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, MethodInformationBuilder informationBuilder, bool isMake,
                                                 Action <MethodInfo, MethodInformation> handleRefOutMethod, bool hasEvents)
        {
            var requiresObsoleteSuppression = false;
            var methods          = baseType.GetMockableMethods(generator);
            var generatedMethods = new List <string>(methods.Count);

            foreach (var method in methods)
            {
                var methodInformation = informationBuilder.Build(method);
                var baseMethod        = method.Value;
                var argumentNameList  = baseMethod.GetArgumentNameList();
                var outInitializers   = !methodInformation.ContainsDelegateConditions ? string.Empty : baseMethod.GetOutInitializers();

                if (baseMethod.IsPublic)
                {
                    var visibility = method.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                                     string.Empty : CodeTemplates.Public;

                    // Either the base method contains no Span<T> or ReadOnlySpan<T>
                    // and it doesn't have refs /outs, or the user specified a delegate
                    // to use to handle that method (remember, types with methods with refs/outs are gen'd
                    // each time, and that's the only reason the handlers are passed in).
                    if (!methodInformation.IsSpanLike)
                    {
                        if (isMake || !methodInformation.ContainsDelegateConditions || !string.IsNullOrWhiteSpace(methodInformation.DelegateCast))
                        {
                            if (!methodInformation.ContainsDelegateConditions && baseMethod.GetParameters().Length > 0)
                            {
                                generatedMethods.Add(MethodsGenerator.GenerateMethodWithNoRefOutParameters(
                                                         baseMethod, methodInformation.DelegateCast, argumentNameList, outInitializers,
                                                         methodInformation.DescriptionWithOverride, visibility,
                                                         method.RequiresNewImplementation, namespaces, isMake, hasEvents));
                            }
                            else
                            {
                                generatedMethods.Add(MethodsGenerator.GenerateMethodWithRefOutOrNoParameters(
                                                         baseMethod, methodInformation.DelegateCast, argumentNameList, outInitializers, methodInformation.DescriptionWithOverride,
                                                         visibility, method.RequiresNewImplementation,
                                                         namespaces, isMake, hasEvents));

                                if (methodInformation.ContainsDelegateConditions)
                                {
                                    handleRefOutMethod(baseMethod, methodInformation);
                                }
                            }
                        }
                        else
                        {
                            generatedMethods.Add(MethodTemplates.GetNotImplementedMethod(methodInformation.DescriptionWithOverride));
                        }
                    }
                    else
                    {
                        generatedMethods.Add(MethodTemplates.GetNotImplementedMethod(methodInformation.DescriptionWithOverride));
                    }

                    requiresObsoleteSuppression |= baseMethod.GetCustomAttribute <ObsoleteAttribute>() != null;
                }
                else if (!baseMethod.IsPrivate && baseMethod.IsAbstract)
                {
                    var visibility = CodeTemplates.GetVisibility(baseMethod.IsFamily, baseMethod.IsFamilyOrAssembly);

                    generatedMethods.Add(baseMethod.ReturnType != typeof(void) ?
                                         MethodTemplates.GetNonPublicFunctionImplementation(visibility, methodInformation.Description,
                                                                                            outInitializers, baseMethod.ReturnType,
                                                                                            method.RequiresNewImplementation == RequiresIsNewImplementation.Yes ? "new" : string.Empty,
                                                                                            baseMethod.ReturnParameter.GetAttributes(true, namespaces)) :
                                         MethodTemplates.GetNonPublicActionImplementation(visibility, methodInformation.Description,
                                                                                          outInitializers, method.RequiresNewImplementation == RequiresIsNewImplementation.Yes ? "new" : string.Empty));

                    requiresObsoleteSuppression |= baseMethod.GetCustomAttribute <ObsoleteAttribute>() != null;
                }
            }

            return(new GenerateResults(string.Join(Environment.NewLine, generatedMethods),
                                       requiresObsoleteSuppression, false));
        }
Beispiel #14
0
 private static void CreateScript()
 {
     CodeTemplates.CreateFromTemplate("NewScript.cs", @"Assets/Editor/ScriptExtensions/Template/ScriptTemplate.txt");
 }
Beispiel #15
0
 private static void CreateInterface()
 {
     CodeTemplates.CreateFromTemplate("NewInterface.cs", @"Assets/Editor/ScriptExtensions/Template/InterfaceTemplate.txt");
 }
Beispiel #16
0
    public override void Action(int instanceId, string pathName, string resourceFile)
    {
        Object o = CodeTemplates.CreateScript(pathName, resourceFile);

        ProjectWindowUtil.ShowCreatedAsset(o);
    }
Beispiel #17
0
 private static void CreateMonoBehaviourScript()
 {
     CodeTemplates.CreateFromTemplate("NewMonoBehaviourScript.cs", @"Assets/Editor/ScriptExtensions/Template/MonoBehaviourTemplate.txt");
 }