Implementation of a Configuration for an ICodeGenerator.
Inheritance: CodeGeneratorParametersBase
 public void ConstructWithParametersOutputDirectoryTest()
 {
     this.outputDirectory = "Value of outputDirectory";
     this.testObject = new CodeGeneratorParameters(this.outputDirectory);
     Assert.Throws<ArgumentException>(() => new CodeGeneratorParameters(string.Empty));
     Assert.Throws<ArgumentNullException>(() => new CodeGeneratorParameters(null));
 }
 public void SetUp()
 {
     //var buildSystem = new StandardBuildSystem();
     var buildSystem = new FakeBuildSystem();
     CodeNamespace codeNamespace = new CodeNamespace(_sampleNamespace);
     // Todo: Mock this
     var testBuilders = MemberBuilderFactory.Default;
     var configuration = new CodeGeneratorParameters(_outputDirectory);
     _CSharpMbUnitCodeGenerator =
         new CSharpMbUnitCodeGenerator(buildSystem, codeNamespace, testBuilders, configuration);
 }
示例#3
0
        public void Construct()
        {
            var buildSystem = new FakeBuildSystem();
            var codeNamespace = new CodeNamespace(_sampleNamespace);
            ICodeGeneratorParameters configuration = new CodeGeneratorParameters(_outputDirectory);
            var testBuilders = MemberBuilderFactory.Default;

            Assert.Throws<ArgumentNullException>(() => new CSharpCodeGenerator(null, codeNamespace, testBuilders, configuration));
            Assert.Throws<ArgumentNullException>(() => new CSharpCodeGenerator(buildSystem, null, testBuilders, configuration));
            new CSharpCodeGenerator(buildSystem, codeNamespace, null, configuration);
            Assert.Throws<ArgumentException>(() => new CSharpCodeGenerator(buildSystem, codeNamespace, testBuilders, null));

            configuration = MockRepository.GenerateStub<ICodeGeneratorParameters>();
            configuration.Expect((e)=> e.OutputDirectory).Return(null);
            Assert.Throws<ArgumentNullException>(() => new CSharpCodeGenerator(buildSystem, codeNamespace, testBuilders, configuration));
            configuration.Expect((e) => e.OutputDirectory).Return(string.Empty);
            Assert.Throws<ArgumentException>(() => new CSharpCodeGenerator(buildSystem, codeNamespace, testBuilders, configuration));

            configuration = new CodeGeneratorParameters(_outputDirectory);
            buildSystem = new FakeBuildSystem() { FakeDirectoryExists = false };
            Assert.Throws<ApplicationException>(() => new CSharpCodeGenerator(buildSystem, codeNamespace, testBuilders, configuration));
        }
示例#4
0
        /*
                private CodeNamespace CreateNamespace(TestNode treeNode)
                {
                    return null;
                }
        */
        /// <summary>
        /// Writes the test file.
        /// </summary>
        /// <param name="calculatedOutputDirectory">The calculated output directory.</param>
        /// <param name="codeNamespace">The code namespace.</param>
        private void WriteTestFile(
            string calculatedOutputDirectory, CodeNamespace codeNamespace)
        {
            // Now write the test file
            // NStubCore nStub =
            // new NStubCore(codeNamespace, outputDirectory,
            // new CSharpCodeGenerator(codeNamespace, outputDirectory));
            // NStubCore nStub =
            // new NStubCore(codeNamespace, outputDirectory,
            // new CSharpMbUnitCodeGenerator(codeNamespace, outputDirectory));
            // var nStub =
            // new NStubCore(
            // codeNamespace,
            // outputDirectory,
            // new CSharpMbUnitRhinoMocksCodeGenerator(codeNamespace, outputDirectory));

            // var testBuilders = new TestBuilderFactory(new PropertyBuilder(), new EventBuilder(), new MethodBuilder());

            ICodeGeneratorParameters configuration = null;
            if (this.CustomGeneratorParameters == null)
            {
                configuration = new CodeGeneratorParameters(calculatedOutputDirectory);
            }
            else
            {
                configuration = new CodeGeneratorParameters(this.CustomGeneratorParameters, calculatedOutputDirectory);
            }

            // var testBuilders = new TestBuilderFactory();
            // var codeGenerator = (ICodeGenerator)Activator.CreateInstance(generatorType, new object[]
            // {
            // sbs, codeNamespace, testBuilders, configuration
            // });
            var buildSystem = this.sbs;
            var codeGenerator = this.OnCreateCodeGenerator(codeNamespace, configuration, buildSystem);

            // codeNamespace.Dump(3);
            var nstub = new NStubCore(buildSystem, codeNamespace, calculatedOutputDirectory, codeGenerator);
            nstub.GenerateCode();

            // Add all of our classes to the project
            foreach (CodeTypeDeclaration codeType in nstub.CodeNamespace.Types)
            {
                string fileName = codeType.Name;
                fileName = fileName.Remove(0, fileName.LastIndexOf(".") + 1);
                fileName += ".cs";
                this.csharpProjectGenerator.ClassFiles.Add(fileName);
                Thread.Sleep(1);
                if (this.logger != null)
                {
                    this.logger("Writing '" + fileName + "'");
                }
            }
        }
 public void TearDown()
 {
     this.testObject = null;
 }
 public void SetUp()
 {
     this.outputDirectory = "Value of outputDirectory";
     this.testObject = new CodeGeneratorParameters(this.outputDirectory);
 }
        public void GenerateWithAssemblyAndModuleAndClassesAndMethod()
        {
            //expected = false;
            var outputFolder = "Value ? of ou$tputFol*der";
            var generatorType = typeof(object);
            var inputAssemblyPath = "Value of inputAssemblyPath";
            char directorySeparator = '\\';
            var firstModuleName = "TheTestNode";
            var firstModuleTag = "FirstModule";
            var className1Namespace = "The.Namespace";
            var className1 = "VeryPrettyFactory";
            var className1FullName = className1Namespace + "." + className1;
            var classTag = this.GetType();
            var methodName = "MyMethod";
            var methodTag = this.GetType().GetMethod("FakeMethod");

            var assemblyNode = new TestNode(firstModuleName + ".dll", TestNodeType.Assembly, null) { Checked = true };
            var moduleNode = new TestNode(firstModuleName + ".dll", TestNodeType.Module, firstModuleTag) { Checked = true };
            var classNode = new TestNode(className1FullName, TestNodeType.Class, classTag) { Checked = true };
            var methodNode = new TestNode(methodName, TestNodeType.Method, methodTag) { Checked = true };
            assemblyNode.Nodes.Add(moduleNode);
            moduleNode.Nodes.Add(classNode);
            classNode.Nodes.Add(methodNode);
            var mainNodes = new[] { assemblyNode };

            var referencedAssemblies = typeof(CSharpTestProjectBuilderTest).Assembly.GetReferencedAssemblies();
            var para = new CodeGeneratorParameters(outputFolder);
            var runnerData = GeneratorRunnerData.Create(
                outputFolder, generatorType, inputAssemblyPath, mainNodes, referencedAssemblies);

            Expect.Call(sbs.DirectorySeparatorChar).Return(directorySeparator).Repeat.Any();
            Expect.Call(sbs.GetFileNameWithoutExtension(firstModuleName + ".dll")).Return(firstModuleName);
            var expectedDirectory = outputFolder + directorySeparator + firstModuleName + ".Tests";
            Expect.Call(sbs.CreateDirectory(expectedDirectory)).Return(null);
            Expect.Call(sbs.DirectoryExists(expectedDirectory)).Return(true);

            var prjReferencedAssemblies = new List<AssemblyName>();
            Expect.Call(projectGenerator.ReferencedAssemblies).Return(prjReferencedAssemblies).Repeat.Any();
            Expect.Call(projectGenerator.GenerateProjectFile).Repeat.Once();
            var classFilesList = new List<string>();
            Expect.Call(projectGenerator.ClassFiles).Return(classFilesList).Repeat.Once();

            var codeGenerator = mocks.StrictMock<ICodeGenerator>();
            CodeNamespace generatorCodeNamespace = null;
            IBuildDataDictionary generatorBuildData = null;

            Expect.Call(createGeneratorCallback(sbs, null, null, null))
                //.Constraints(Is.Same(sbs), Is.TypeOf<CodeGeneratorParameters>(), Is.TypeOf<CodeNamespace>())
                .Constraints(
                Is.Same(sbs),
                Is.TypeOf<IBuildDataDictionary>() && Is.NotNull(),
                Is.TypeOf<CodeGeneratorParameters>() && Property.Value("OutputDirectory", expectedDirectory),
                Is.TypeOf<CodeNamespace>())
                //.Return(codeGenerator)
                .Do((Func<IBuildSystem,IBuildDataDictionary, ICodeGeneratorParameters, CodeNamespace, ICodeGenerator>)
                delegate(IBuildSystem buildSys,IBuildDataDictionary data, ICodeGeneratorParameters cgp, CodeNamespace cn)
                {
                    Assert.AreEqual(expectedDirectory, cgp.OutputDirectory);
                    Assert.IsFalse(cgp.UseSetupAndTearDown);

                    generatorBuildData = data;
                    generatorCodeNamespace = cn;
                    return codeGenerator;
                });

            Expect.Call(codeGenerator.CodeNamespace = null).PropertyBehavior();
            Expect.Call(codeGenerator.OutputDirectory = expectedDirectory);
            Expect.Call(codeGenerator.GenerateCode);

            // logging
            Expect.Call(delegate { this.logger(null); }).Constraints(Is.NotNull()).Repeat.AtLeastOnce();
            Expect.Call(projectGenerator.ProjectName).Return(firstModuleName).Repeat.Any();

            mocks.ReplayAll();

            testObject.GenerateTests(runnerData);
            //Assert.AreEqual(expected, actual);

            Assert.IsNotEmpty(generatorBuildData);
            Assert.Count(1, generatorBuildData);
            Assert.IsTrue(generatorBuildData.Contains(this.buildDataItem));

            Assert.IsNotEmpty(prjReferencedAssemblies);
            Assert.IsNotEmpty(classFilesList);

            Assert.IsEmpty(generatorCodeNamespace.Comments);
            Assert.IsEmpty(generatorCodeNamespace.Imports);
            Assert.AreEqual(className1Namespace, generatorCodeNamespace.Name);
            Assert.IsNotEmpty(generatorCodeNamespace.Types);
            Assert.IsEmpty(generatorCodeNamespace.UserData);

            var classDefinition1 = generatorCodeNamespace.Types[0];
            Assert.AreEqual(MemberAttributes.Private | MemberAttributes.Final, classDefinition1.Attributes);
            Assert.IsEmpty(classDefinition1.BaseTypes);
            Assert.IsEmpty(classDefinition1.Comments);
            Assert.IsEmpty(classDefinition1.CustomAttributes);

            Assert.AreEqual(className1FullName, classDefinition1.Name);
            Assert.IsTrue(classDefinition1.IsClass);
            Assert.IsEmpty(classDefinition1.StartDirectives);
            Assert.IsEmpty(classDefinition1.TypeParameters);

            Assert.IsNotEmpty(classDefinition1.UserData);
            Assert.Count(1, classDefinition1.UserData);
            Assert.AreEqual(classTag, classDefinition1.UserData[NStubConstants.UserDataClassTypeKey]);

            Assert.IsNotEmpty(classDefinition1.Members);
            var class1Members = classDefinition1.Members;

            // Todo: More checks on the generated types.
            mocks.VerifyAll();
        }