Beispiel #1
0
        public static void AddWithNullModel()
        {
            var compilationUnit = SyntaxFactory.ParseCompilationUnit("public static class Test { }", options: Shared.ParseOptions);
            var aggregator      = new StyleAggregator();

            Assert.That(() => aggregator.Add(compilationUnit, null !), Throws.TypeOf <ArgumentNullException>());
        }
Beispiel #2
0
        public static void Add()
        {
            var aggregator1      = new StyleAggregator();
            var compilationUnit1 = SyntaxFactory.ParseCompilationUnit(
                @"public static class Test
{
	public static void VarFoo()
	{
		var a = 1;
	}
}", options: Shared.ParseOptions);

            aggregator1 = aggregator1.Add(compilationUnit1, StyleAggregatorTests.CreateModel(compilationUnit1.SyntaxTree));

            var aggregator2      = new StyleAggregator();
            var compilationUnit2 = SyntaxFactory.ParseCompilationUnit(
                @"public static class Test
{
	public static void TypeFoo()
	{
		int a = 1;
	}
}", options: Shared.ParseOptions);

            aggregator2 = aggregator2.Add(compilationUnit2, StyleAggregatorTests.CreateModel(compilationUnit2.SyntaxTree));

            var aggregator3 = aggregator1.Update(aggregator2);
            var data3       = aggregator3.Set.CSharpStyleVarForBuiltInTypesStyle.Data;

            Assert.That(data3.TotalOccurences, Is.EqualTo(2u), nameof(data3.TotalOccurences));
            Assert.That(data3.TrueOccurences, Is.EqualTo(1u), nameof(data3.TrueOccurences));
            Assert.That(data3.FalseOccurences, Is.EqualTo(1u), nameof(data3.FalseOccurences));
        }
    public static async Task <string> Generate(DirectoryInfo directory, TextWriter writer, bool generateStatistics = false)
    {
        if (directory is null)
        {
            throw new ArgumentNullException(nameof(directory));
        }

        if (writer is null)
        {
            throw new ArgumentNullException(nameof(writer));
        }

        var aggregator = new StyleAggregator();

        await writer.WriteLineAsync($"Analyzing {directory}...").ConfigureAwait(false);

        foreach (var file in directory.EnumerateFiles("*.cs", SearchOption.AllDirectories))
        {
            await writer.WriteLineAsync($"\tAnalyzing {file.FullName}...").ConfigureAwait(false);

            var(unit, model) = StyleGenerator.GetCompilationInformation(file.FullName);
            aggregator       = aggregator.Update(new StyleAggregator().Add(unit, model));
        }

        if (generateStatistics)
        {
            using var statisticsWriter = File.CreateText("statistics.json");
            new JsonSerializer().Serialize(statisticsWriter, aggregator);
        }

        return(aggregator.GenerateConfiguration());
    }
    public static string GenerateFromDocument(FileInfo document, TextWriter writer, bool generateStatistics = false)
    {
        if (document is null)
        {
            throw new ArgumentNullException(nameof(document));
        }

        if (writer is null)
        {
            throw new ArgumentNullException(nameof(writer));
        }

        if (document.Extension.ToUpperInvariant() == ".CS")
        {
            writer.WriteLine($"Analyzing {document.FullName}...");
            var(unit, model) = StyleGenerator.GetCompilationInformation(document.FullName);
            var aggregator = new StyleAggregator().Add(unit, model);

            if (generateStatistics)
            {
                using var statisticsWriter = File.CreateText("statistics.json");
                new JsonSerializer().Serialize(statisticsWriter, aggregator);
            }

            return(aggregator.GenerateConfiguration());
        }

        return(string.Empty);
    }
    public StyleAggregator Update(StyleAggregator aggregator)
    {
        if (aggregator is null)
        {
            throw new ArgumentNullException(nameof(aggregator));
        }

        return(new StyleAggregator()
        {
            Set = this.Set.Update(aggregator.Set)
        });
    }
Beispiel #6
0
        private static void TestStyleVisitation(string code, string stylePropertyName)
        {
            var aggregator        = new StyleAggregator();
            var setProperty       = aggregator.GetType().GetProperty(nameof(StyleAggregator.Set)) !;
            var styleProperty     = setProperty.PropertyType.GetProperty(stylePropertyName) !;
            var styleDataProperty = styleProperty.PropertyType.GetProperty("Data") !;
            var data = styleDataProperty.GetValue(styleProperty.GetValue(setProperty.GetValue(aggregator)));

            var compilationUnit = SyntaxFactory.ParseCompilationUnit(code, options: Shared.ParseOptions);

            aggregator = aggregator.Add(compilationUnit, StyleAggregatorTests.CreateModel(compilationUnit.SyntaxTree));
            var newData = styleDataProperty.GetValue(styleProperty.GetValue(setProperty.GetValue(aggregator)));

            Assert.That(newData, Is.Not.EqualTo(data));
        }