Exemple #1
0
    void when_generating()
    {
        it["executes data providers, generators and post processors"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            var files = generator.Generate();

            files.Length.should_be(2);

            files[0].fileName.should_be("FileName0-Approved!");
            files[1].fileName.should_be("FileName1-Approved!");
        };

        it["performs a dry run (no post processors)"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            var files = generator.DryRun();

            files.Length.should_be(2);

            files[0].fileName.should_be("FileName0");
            files[1].fileName.should_be("FileName1");
        };

        it["runs post processors based on priority"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new ICodeGenFilePostProcessor[] { new TestPostProcessor(), new Test2PostProcessor() }
                );

            var files = generator.Generate();

            files.Length.should_be(2);

            files[0].fileName.should_be("FileName0First!-Approved!");
            files[1].fileName.should_be("FileName1First!-Approved!");
        };

        it["uses returned CodeGenFiles"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new ICodeGenFilePostProcessor[] { new Test3PostProcessor(), new TestPostProcessor() }
                );

            var files = generator.Generate();

            files.Length.should_be(1);

            files[0].fileName.should_be("FileName0-Approved!");
        };

        it["cancels"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            generator.OnProgress += (title, info, progress) => generator.Cancel();

            var files = generator.Generate();

            files.Length.should_be(0);
        };

        it["cancels dry run"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            generator.OnProgress += (title, info, progress) => generator.Cancel();

            var files = generator.DryRun();

            files.Length.should_be(0);
        };

        it["can generate again after cancel"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            GeneratorProgress onProgress = (title, info, progress) => generator.Cancel();
            generator.OnProgress += onProgress;

            generator.Generate();

            generator.OnProgress -= onProgress;

            var files = generator.Generate();

            files.Length.should_be(2);
        };

        it["can do dry run after cancel"] = () => {
            var generator = new CodeGenerator(
                new [] { new TestDataProvider() },
                new [] { new TestCodeGenerator() },
                new [] { new TestPostProcessor() }
                );

            GeneratorProgress onProgress = (title, info, progress) => generator.Cancel();
            generator.OnProgress += onProgress;

            generator.Generate();

            generator.OnProgress -= onProgress;

            var files = generator.DryRun();

            files.Length.should_be(2);
        };
    }
    void when_generating()
    {
        context["generate"] = () => {
            it["executes data providers, generators and post processors"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                var files = generator.Generate();

                files.Length.should_be(2);

                files[0].fileName.should_be("Test1File0-Processed1");
                files[0].fileContent.should_be("data1");

                files[1].fileName.should_be("Test1File1-Processed1");
                files[1].fileContent.should_be("data2");
            };

            it["uses returned CodeGenFiles"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new ICodeGenFilePostProcessor[] { new Processed1PostProcessor(), new NoFilesPostProcessor() }
                    );

                var files = generator.Generate();

                files.Length.should_be(1);

                files[0].fileName.should_be("Test1File0-Processed1");
            };
        };

        context["dry run"] = () => {
            it["skips plugins which don't run in dry run"] = () => {
                var generator = new CodeGenerator(
                    new ICodeGeneratorDataProvider[] { new Data_1_2_Provider(), new DisabledDataProvider() },
                    new ICodeGenerator[] { new DataFile1CodeGenerator(), new DisabledCodeGenerator() },
                    new ICodeGenFilePostProcessor[] { new Processed1PostProcessor(), new DisabledPostProcessor() }
                    );

                var files = generator.DryRun();

                files.Length.should_be(2);

                files[0].fileName.should_be("Test1File0-Processed1");
                files[1].fileName.should_be("Test1File1-Processed1");
            };
        };

        context["priority"] = () => {
            it["runs data provider based on priority"] = () => {
                var generator = new CodeGenerator(
                    new ICodeGeneratorDataProvider[] { new Data_3_4_Provider(), new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                var files = generator.Generate();

                files.Length.should_be(4);

                files[0].fileName.should_be("Test1File0-Processed1");
                files[0].fileContent.should_be("data1");

                files[1].fileName.should_be("Test1File1-Processed1");
                files[1].fileContent.should_be("data2");

                files[2].fileName.should_be("Test1File2-Processed1");
                files[2].fileContent.should_be("data3");

                files[3].fileName.should_be("Test1File3-Processed1");
                files[3].fileContent.should_be("data4");
            };

            it["runs code generators based on priority"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new ICodeGenerator[] { new DataFile2CodeGenerator(), new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                var files = generator.Generate();

                files.Length.should_be(4);

                files[0].fileName.should_be("Test1File0-Processed1");
                files[1].fileName.should_be("Test1File1-Processed1");
                files[2].fileName.should_be("Test2File0-Processed1");
                files[3].fileName.should_be("Test2File1-Processed1");
            };

            it["runs post processors based on priority"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new ICodeGenFilePostProcessor[] { new Processed2PostProcessor(), new Processed1PostProcessor() }
                    );

                var files = generator.Generate();

                files.Length.should_be(2);

                files[0].fileName.should_be("Test1File0-Processed1-Processed2");
                files[1].fileName.should_be("Test1File1-Processed1-Processed2");
            };
        };

        context["cancel"] = () => {
            it["cancels"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                generator.OnProgress += (title, info, progress) => generator.Cancel();

                var files = generator.Generate();

                files.Length.should_be(0);
            };

            it["cancels dry run"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                generator.OnProgress += (title, info, progress) => generator.Cancel();

                var files = generator.DryRun();

                files.Length.should_be(0);
            };

            it["can generate again after cancel"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                GeneratorProgress onProgress = (title, info, progress) => generator.Cancel();
                generator.OnProgress += onProgress;

                generator.Generate();

                generator.OnProgress -= onProgress;

                var files = generator.Generate();

                files.Length.should_be(2);
            };

            it["can do dry run after cancel"] = () => {
                var generator = new CodeGenerator(
                    new [] { new Data_1_2_Provider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                GeneratorProgress onProgress = (title, info, progress) => generator.Cancel();
                generator.OnProgress += onProgress;

                generator.Generate();

                generator.OnProgress -= onProgress;

                var files = generator.DryRun();

                files.Length.should_be(2);
            };
        };

        context["caching"] = () => {
            it["registers object to shared cache"] = () => {
                var generator = new CodeGenerator(
                    new [] { new CachableProvider(), new CachableProvider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                var files = generator.Generate();
                files.Length.should_be(2);
                files[0].fileContent.should_be(files[1].fileContent);
            };

            it["resets cache before each new run"] = () => {
                var generator = new CodeGenerator(
                    new [] { new CachableProvider(), new CachableProvider() },
                    new [] { new DataFile1CodeGenerator() },
                    new [] { new Processed1PostProcessor() }
                    );

                var result1 = generator.Generate()[0].fileContent;
                var result2 = generator.Generate()[0].fileContent;
                result1.should_not_be(result2);
            };
        };
    }