Exemplo n.º 1
0
        public void Should_return_path_honoring_the_tuid()
        {
            // Arrange
            var sut          = new ContextualFileResolver();
            var expectedPath = Path.Combine(Sample.ProjectDirectory, nameof(Tests), "ClassLvl", $"{Id}.approved.txt");

            // Act
            var result1 = sut.GetApprovedFilePath(".txt");

            // Assert
            result1.ShouldBe(expectedPath);
        }
Exemplo n.º 2
0
        public void Should_return_path_honoring_the_parameters_on_the_method(char arg)
        {
            // Arrange
            var sut          = new ContextualFileResolver();
            var expectedPath = Path.Combine(Sample.ProjectDirectory, nameof(Tests), nameof(DataTestMethodAttribute), $"{nameof(ContextFileResolverTestA)}-{nameof(Should_return_path_honoring_the_parameters_on_the_method)}[{arg}].approved.txt");

            // Act
            var result = sut.GetApprovedFilePath(".txt", arg);

            // Assert
            result.ShouldBe(expectedPath);
        }
Exemplo n.º 3
0
        public void Should_return_path_honoring_the_attribute_on_the_class()
        {
            // Arrange
            var sut          = new ContextualFileResolver();
            var expectedPath = Path.Combine(Sample.ProjectDirectory, nameof(Tests), "ClassLvl", $"{nameof(ContextFileResolverTestA)}-{nameof(Should_return_path_honoring_the_attribute_on_the_class)}[c,d].approved.txt");

            // Act
            var result1 = sut.GetApprovedFilePath(".txt", 'c', 'd');

            // Assert
            result1.ShouldBe(expectedPath);
        }
Exemplo n.º 4
0
        public async Task Should_return_path_to_async_test()
        {
            System.Diagnostics.Debug.WriteLine($"thread: '{Thread.CurrentThread.Name}'({Thread.CurrentThread.ManagedThreadId})");

            // Arrange
            var sut          = new ContextualFileResolver();
            var expectedPath = Path.Combine(Sample.ProjectDirectory, nameof(Tests), "Async", $"{nameof(ContextFileResolverTestA)}-{nameof(Should_return_path_to_async_test)}.approved.txt");

            // Act
            await Task.Delay(100);

            var result1 = sut.GetApprovedFilePath(".txt");

            // Assert
            result1.ShouldBe(expectedPath);
        }
Exemplo n.º 5
0
        public void Should_return_path_honoring_the_attribute_on_the_assembly()
        {
            // Arrange
            var sut          = new ContextualFileResolver();
            var expectedPath = Path.Combine(Sample.ProjectDirectory, nameof(Tests), nameof(Diffa), $"{nameof(ContextFileResolverTestB)}-{nameof(Should_return_path_honoring_the_attribute_on_the_assembly)}.approved.txt");

            // Act
            var result1 = sut.GetApprovedFilePath(".txt");
            var result3 = StaticFunc(sut);
            var result2 = localFunc();

            // Assert
            result1.ShouldBe(expectedPath);
            result2.ShouldBe(expectedPath);
            result2.ShouldBe(expectedPath);

            string localFunc() => sut.GetApprovedFilePath(".txt");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Assert that the serialized <paramref name="subject" /> is equal to it's approved file.
        /// </summary>
        /// <param name="approver">The approver.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="testParameters">The test parameters supplied by parameterized test.</param>
        /// <param name="fileExtension">The file extension (with or without a dot).</param>
        /// <param name="reporter">The reporter.</param>
        /// <param name="fileResolver">The file resolver.</param>
        /// <exception cref="Acklann.Diffa.Exceptions.ResultNotApprovedException">The subject did not match the approved-file.</exception>
        public static void Approve(IApprover approver, object subject, object[] testParameters, string fileExtension = ".txt", IReporter reporter = default, IFileResolver fileResolver = default)
        {
            var contextBuilder = new StackTraceParser();

            if (fileResolver == null)
            {
                fileResolver = new ContextualFileResolver(contextBuilder);
            }

            string resultFile   = fileResolver.GetResultFilePath(fileExtension, testParameters);
            string approvedFile = fileResolver.GetApprovedFilePath(fileExtension, testParameters);

            if (approver.Approve(subject, resultFile, approvedFile, out string reasonWhyItWasNotApproved) == false)
            {
                if (_shouldReport)
                {
                    if (reporter == null)
                    {
                        ReporterAttribute attribute = contextBuilder.Context.ReporterAttribute;
                        if (attribute?.Reporter == null)
                        {
                            reporter = _reporterFactory.GetFirstAvailableReporter(attribute.ShouldInterrupt);
                        }
                        else
                        {
                            reporter = (IReporter)Activator.CreateInstance(attribute.Reporter, args: attribute.ShouldInterrupt);
                        }
                    }

                    if (reporter.Launch(resultFile, approvedFile))
                    {
                        // Checking the results again because the user may have updated the approved file when the reporter was launched.
                        if (approver.Approve(subject, resultFile, approvedFile, out reasonWhyItWasNotApproved))
                        {
                            return;
                        }
                    }
                }

                throw new ResultNotApprovedException(ExceptionMessage.GetResultWasNotApproved(resultFile, approvedFile, reasonWhyItWasNotApproved));
            }
        }
        private static int ExecuteApp(CommandOption language, CommandOption output, CommandOption includes, CommandArgument files)
        {
            // Currently we only support CSharp...
            if (language.Value() != "CSharp")
            {
                throw new NotSupportedException("Unsupported language: " + language.Value());
            }

            // Create a new file resolver and include the passed directories
            var resolver = new ContextualFileResolver();

            foreach (var include in includes.Values)
            {
                resolver.Include(include);
            }

            // Load in the passed files
            var pf = new DefinitionFile
            {
                Resolver = resolver
            };

            pf.PopulateBuiltins();

            foreach (var file in files.Values)
            {
                pf.Load(file);
            }

            // Generate. TODO: write in a way that easily allows for using different target languages
            var settings = new CSharpTargetSettings
            {
                OutDirectory = output.Value()
            };
            var generator = new CSharpGenerator(pf);

            generator.Generate(settings);
            return(0);
        }
Exemplo n.º 8
0
        public static void Main(string[] args)
        {
            var resolver = new ContextualFileResolver();

            resolver.Include("../Doc/");

            // Create a new definition file with a file resolver pointing to ../Doc/
            var pf = new DefinitionFile
            {
                Resolver = resolver
            };

            pf.PopulateBuiltins();
            pf.Load("chatroom.kpdl");

            var settings = new CSharpTargetSettings
            {
                OutDirectory = "Gen/"
            };
            var generator = new CSharpGenerator(pf);

            generator.Generate(settings);
        }
Exemplo n.º 9
0
 private static string StaticFunc(ContextualFileResolver sut) => sut.GetApprovedFilePath(".txt");