Example #1
0
        public void WhenIGenerateTestsForTheClass(string strategy)
        {
            var options      = new UnitTestGeneratorOptions(new GenerationOptions(_context.TargetFramework, _context.MockFramework), new VersionOptions());
            var frameworkSet = FrameworkSetFactory.Create(options);

            IClassGenerationStrategy generationStrategy = null;

            if (strategy == "StandardClassGenerationStrategy")
            {
                generationStrategy = new StandardClassGenerationStrategy(frameworkSet);
            }

            if (strategy == "StaticClassGenerationStrategy")
            {
                generationStrategy = new StaticClassGenerationStrategy(frameworkSet);
            }

            if (strategy == "AbstractClassGenerationStrategy")
            {
                generationStrategy = new AbstractClassGenerationStrategy(frameworkSet);
            }

            if (generationStrategy == null)
            {
                throw new InvalidOperationException();
            }

            _context.Result       = generationStrategy.Create(_context.ClassModel);
            _context.CurrentClass = _context.Result;

            using (var workspace = new AdhocWorkspace())
            {
                Console.WriteLine(Formatter.Format(_context.Result, workspace));
            }
        }
Example #2
0
        public async Task WhenIRegenerateTests()
        {
            var options = new UnitTestGeneratorOptions(new GenerationOptions(_context.TargetFramework, _context.MockFramework), new VersionOptions());
            var result  = await CoreGenerator.Generate(_context.SemanticModel, _context.ClassModel.Constructors.First().Node, _context.TestModel, true, options, x => x + ".Tests");

            var tree = CSharpSyntaxTree.ParseText(result.FileContent, new CSharpParseOptions(LanguageVersion.Latest));

            _context.Result       = tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>().First();
            _context.CurrentClass = _context.Result;
        }
        [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);
            }
        }
        public void CanConstruct()
        {
            var instance = new UnitTestGeneratorOptions(_generationOptions, _versioningOptions);

            Assert.That(instance, Is.Not.Null);
        }
 public void SetUp()
 {
     _generationOptions = Substitute.For <IGenerationOptions>();
     _versioningOptions = Substitute.For <IVersioningOptions>();
     _testClass         = new UnitTestGeneratorOptions(_generationOptions, _versioningOptions);
 }