[TestCase("Microsoft.Win32.RegistryValueKind")] // System.Enum
        public static async Task CanGenerate(string typeName)
        {
            var model = ClassModelProvider.Instance.SemanticModel;

            ITypeSymbol info;

#pragma warning disable CA1062 // it's a test case parameter
            if (typeName.EndsWith("[]", StringComparison.Ordinal))
            {
                info = model.Compilation.CreateArrayTypeSymbol(model.Compilation.GetTypeByMetadataName(typeName.Substring(0, typeName.Length - 2)));
            }
            else
            {
                var metadataName = typeName.IndexOf(',') > 0 ? typeName.Substring(0, typeName.IndexOf(',')) : typeName;
                info = model.Compilation.GetTypeByMetadataName(metadataName);
            }

            var generationOptions = Substitute.For <IGenerationOptions>();
            var versionOptions    = Substitute.For <IVersioningOptions>();
            generationOptions.FrameworkType.Returns(TestFrameworkTypes.NUnit3);
            generationOptions.MockingFrameworkType.Returns(MockingFrameworkType.NSubstitute);
            generationOptions.TestTypeNaming.Returns("{0}Tests");
            var options      = new UnitTestGeneratorOptions(generationOptions, versionOptions);
            var frameworkSet = FrameworkSetFactory.Create(options);
            var expression   = ValueGenerationStrategyFactory.GenerateFor(info, model, new HashSet <string>(StringComparer.OrdinalIgnoreCase), frameworkSet);

            string expressionText = string.Empty;
            try
            {
                using (var workspace = new AdhocWorkspace())
                {
                    expressionText = Formatter.Format(expression, workspace).ToString();
                    var scriptOptions = ScriptOptions.Default
                                        .AddReferences(typeof(System.Drawing.Brushes).Assembly)
                                        .AddImports("System")
                                        .AddImports("System.IO")
                                        .AddImports("System.Drawing")
                                        .AddImports("Microsoft.Win32")
                                        .AddImports("System.Globalization")
                                        .AddImports("System.Threading")
                                        .AddImports("System.Collections");
                    var result = await CSharpScript.EvaluateAsync <object>(expressionText, scriptOptions).ConfigureAwait(false);

                    var expectedType = Type.GetType(typeName);
                    Assert.That(result, Is.InstanceOf(expectedType));
                }
            }
            catch (CompilationErrorException e)
            {
                Assert.Fail("Expression compilation failed - " + expressionText + ": " + e.Message);
            }
        }
예제 #2
0
        public static ExpressionSyntax GetDefaultAssignmentValue(ITypeSymbol propertyType, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet)
        {
            if (propertyType == null)
            {
                throw new ArgumentNullException(nameof(propertyType));
            }

            if (visitedTypes == null)
            {
                throw new ArgumentNullException(nameof(visitedTypes));
            }

            if (frameworkSet == null)
            {
                throw new ArgumentNullException(nameof(frameworkSet));
            }

            frameworkSet.Context.AddEmittedType(propertyType);

            var fullName = propertyType.ToFullName();

            if (visitedTypes.Add(fullName))
            {
                if (propertyType is ITypeParameterSymbol typeParameterSymbol)
                {
                    return(ValueGenerationStrategyFactory.GenerateFor("string", typeParameterSymbol, model, visitedTypes, frameworkSet));
                }

                if (ValueGenerationStrategyFactory.IsSupported(propertyType))
                {
                    return(ValueGenerationStrategyFactory.GenerateFor(propertyType, model, visitedTypes, frameworkSet));
                }

                if (propertyType.TypeKind == TypeKind.Interface)
                {
                    return(frameworkSet.MockingFramework.MockInterface(propertyType.ToTypeSyntax(frameworkSet.Context)));
                }

                if (propertyType is INamedTypeSymbol namedType && (propertyType.TypeKind == TypeKind.Class || propertyType.TypeKind == TypeKind.Struct))
                {
                    return(GetClassDefaultAssignmentValue(model, visitedTypes, frameworkSet, namedType));
                }

                visitedTypes.Remove(fullName);
            }

            return(SyntaxFactory.DefaultExpression(propertyType.ToTypeSyntax(frameworkSet.Context)));
        }