<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using Assent;
using DbUp.Engine;
using DbUp.Oracle;
using NSubstitute.Core;
using Xunit;

namespace DbUp.Tests
{
    public class ApiTests
    {
        [Theory]
        [InlineData(typeof(UpgradeEngine))]
        [InlineData(typeof(SQLiteExtensions))]
        [InlineData(typeof(MySqlExtensions))]
        [InlineData(typeof(OracleExtensions))]
        [InlineData(typeof(PostgresqlExtensions))]
#if !NETCORE
        [InlineData(typeof(FirebirdExtensions))]
        [InlineData(typeof(SqlCeExtensions))]
        [InlineData(typeof(SqlAnywhereExtensions))]
#endif
        public void NoPublicApiChanges(Type type)
        {
            var assembly = type.Assembly;
            var result = GetPublicApi(assembly);

            var config = new Configuration()
                .UsingExtension("cs")
                .UsingNamer(m => Path.Combine(Path.GetDirectoryName(m.FilePath), "ApprovalFiles", assembly.GetName().Name));

            this.Assent(result, config);
        }

        private string GetPublicApi(Assembly assembly)
        {
            var sb = new StringBuilder();

            var customAttributes = assembly.GetCustomAttributesData();

            AppendAttributes(sb, 0, customAttributes, true);

            sb.AppendLine();

            var namespaces = from t in assembly.GetTypes()
                             where t.IsPublic
                             group t by t.Namespace;

            foreach (var ns in namespaces.OrderBy(n => n.Key))
            {
                if (ns.Key == null)
                {
                    AppendTypes(sb, 0, ns);
                }
                else
                {
                    sb.AppendLine($"namespace {ns.Key}");
                    sb.AppendLine("{");
                    AppendTypes(sb, 4, ns);
                    sb.AppendLine("}");
                }
            }

            return sb.ToString();
        }

        private static void AppendAttributes(StringBuilder sb, int indent, IList<CustomAttributeData> customAttributes, bool isAssembly)
        {
            var c = customAttributes.Where(a => a.AttributeType.Namespace != "System.Reflection")
                .Where(a => a.AttributeType.Namespace != "System.Diagnostics")
                .Where(a => a.AttributeType.Namespace != "System.Runtime.CompilerServices")
                .Where(a => a.AttributeType != typeof(TargetFrameworkAttribute))
                .OrderBy(a => a.AttributeType.FullName);

            foreach (var attribute in c)
            {
                sb
                    .Append(' ', indent)
                    .Append("[");

                if (isAssembly)
                    sb.Append("assembly: ");

                sb.Append(GetTypeName(attribute.AttributeType));

                AppendAttributeArguments(sb, attribute);

                sb.AppendLine("]");
            }
        }

        private static void AppendAttributeArguments(StringBuilder sb, CustomAttributeData attribute)
        {
            var isFirst = true;
            sb.Append("(");

            void Append(object argumentValue, string argumentName = null)
            {
                if (!isFirst)
                    sb.Append(", ");
                isFirst = false;

                if (argumentName != null)
                {
                    sb.Append(argumentName)
                        .Append(" = ");
                }

                sb.Append(FormatValue(argumentValue));
            }
<commit_msg>Remove brackets around single-line statement<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using Assent;
using DbUp.Engine;
using DbUp.Oracle;
using NSubstitute.Core;
using Xunit;

namespace DbUp.Tests
{
    public class ApiTests
    {
        [Theory]
        [InlineData(typeof(UpgradeEngine))]
        [InlineData(typeof(SQLiteExtensions))]
        [InlineData(typeof(MySqlExtensions))]
        [InlineData(typeof(OracleExtensions))]
        [InlineData(typeof(PostgresqlExtensions))]
#if !NETCORE
        [InlineData(typeof(FirebirdExtensions))]
        [InlineData(typeof(SqlCeExtensions))]
        [InlineData(typeof(SqlAnywhereExtensions))]
#endif
        public void NoPublicApiChanges(Type type)
        {
            var assembly = type.Assembly;
            var result = GetPublicApi(assembly);

            var config = new Configuration()
                .UsingExtension("cs")
                .UsingNamer(m => Path.Combine(Path.GetDirectoryName(m.FilePath), "ApprovalFiles", assembly.GetName().Name));

            this.Assent(result, config);
        }

        private string GetPublicApi(Assembly assembly)
        {
            var sb = new StringBuilder();

            var customAttributes = assembly.GetCustomAttributesData();

            AppendAttributes(sb, 0, customAttributes, true);

            sb.AppendLine();

            var namespaces = from t in assembly.GetTypes()
                             where t.IsPublic
                             group t by t.Namespace;

            foreach (var ns in namespaces.OrderBy(n => n.Key))
            {
                if (ns.Key == null)
                {
                    AppendTypes(sb, 0, ns);
                }
                else
                {
                    sb.AppendLine($"namespace {ns.Key}");
                    sb.AppendLine("{");
                    AppendTypes(sb, 4, ns);
                    sb.AppendLine("}");
                }
            }

            return sb.ToString();
        }

        private static void AppendAttributes(StringBuilder sb, int indent, IList<CustomAttributeData> customAttributes, bool isAssembly)
        {
            var c = customAttributes.Where(a => a.AttributeType.Namespace != "System.Reflection")
                .Where(a => a.AttributeType.Namespace != "System.Diagnostics")
                .Where(a => a.AttributeType.Namespace != "System.Runtime.CompilerServices")
                .Where(a => a.AttributeType != typeof(TargetFrameworkAttribute))
                .OrderBy(a => a.AttributeType.FullName);

            foreach (var attribute in c)
            {
                sb
                    .Append(' ', indent)
                    .Append("[");

                if (isAssembly)
                    sb.Append("assembly: ");

                sb.Append(GetTypeName(attribute.AttributeType));

                AppendAttributeArguments(sb, attribute);

                sb.AppendLine("]");
            }
        }

        private static void AppendAttributeArguments(StringBuilder sb, CustomAttributeData attribute)
        {
            var isFirst = true;
            sb.Append("(");

            void Append(object argumentValue, string argumentName = null)
            {
                if (!isFirst)
                    sb.Append(", ");
                isFirst = false;

                if (argumentName != null)
                    sb.Append(argumentName).Append(" = ");

                sb.Append(FormatValue(argumentValue));
            }

{'fraction_non_alphanumeric': 0.10326530612244898, 'fraction_numerical': 0.0008163265306122449, 'mean_word_length': 1.6740632957439068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6116192', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1783, 'n_words': 373}