Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Mar 5, 2024
1 parent 255dd95 commit 452a14a
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ GetValueOrDefault CreateHashCode(byte[][] keys)
this.functorHashCode3 = CreateHashCode(this.keys3);
this.functorHashCode7 = CreateHashCode(this.keys7);

Trace.Assert(this.keys3.Select(x => x.Length).SequenceEqual(new[] { 2, 4, 7 }));
Trace.Assert(this.keys7.Select(x => x.Length).SequenceEqual(new[] { 15, 0, 14, 4, 13, 8, 12 }));
Trace.Assert(this.keys3.Select(x => x.Length).SequenceEqual([2, 4, 7]));
Trace.Assert(this.keys7.Select(x => x.Length).SequenceEqual([15, 0, 14, 4, 13, 8, 12]));
Trace.Assert(Invoke(this.functorLongData3, this.keys3) == 3);
Trace.Assert(Invoke(this.functorHashCode3, this.keys3) == 3);
Trace.Assert(Invoke(this.functorLongData7, this.keys7) == 21);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Decoder<T> GetDecoder<T, E>(Converter<E> converter, Expression<Act
var result = Expression.Variable(typeof(T), "result");
var target = Expression.Label("break");
var expressions = Expression.Block(
new[] { result },
[result],
Expression.Assign(result, Expression.New(constructor)),
Expression.Loop(
Expression.IfThenElse(
Expand Down
2 changes: 1 addition & 1 deletion code/Benchmarks.CollectionTests/CollectionBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Setup()
var generator = Generator.CreateDefaultBuilder().AddConverter(converter).Build();

this.hashSet = [1313];
this.linkedList = new LinkedList<int>(new[] { 1313 });
this.linkedList = new LinkedList<int>([1313]);
this.dictionary = new Dictionary<int, int> { [1313] = 1313 };

this.hashSetConverter = generator.GetConverter<HashSet<int>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public readonly struct CustomValueEnumerable<T>(IEnumerable<T>? collection) : IE
{
public readonly IEnumerable<T>? Collection = collection;

public IEnumerator<T> GetEnumerator() => (this.Collection ?? Array.Empty<T>()).GetEnumerator();
public IEnumerator<T> GetEnumerator() => (this.Collection ?? []).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Expand All @@ -108,7 +108,7 @@ public readonly struct CustomValueEnumerable<T>(IEnumerable<T>? collection) : IE

internal CustomValueEnumerableInternalConstructor(IEnumerable<T>? collection) => this.Collection = collection;

public IEnumerator<T> GetEnumerator() => (this.Collection ?? Array.Empty<T>()).GetEnumerator();
public IEnumerator<T> GetEnumerator() => (this.Collection ?? []).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static IEnumerable<object[]> EnumerableTypesData()
yield return new object[] { new Dictionary<int, string> { { 7, "8" } }, new KeyValuePair<int, string>(7, "8"), "SequenceConverter`1.*Dictionary`2.*Int32.*String" };
yield return new object[] { ImmutableArray.Create(1), 1, "SpanLikeConverter`1.*ImmutableArray`1.*Int32" };
yield return new object[] { ImmutableArray.Create("2"), "2", "SpanLikeConverter`1.*ImmutableArray`1.*String" };
yield return new object[] { new LinkedList<int>(new[] { 3 }), 3, "LinkedListConverter`1.*Int32" };
yield return new object[] { new LinkedList<string>(new[] { "4" }), "4", "LinkedListConverter`1.*String" };
yield return new object[] { new LinkedList<int>([3]), 3, "LinkedListConverter`1.*Int32" };
yield return new object[] { new LinkedList<string>(["4"]), "4", "LinkedListConverter`1.*String" };
}

public static IEnumerable<object[]> ImmutableCollectionTypesData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void PlainObjectEncodeDecodeTest()
var bufferSecond = converterSecond.Encode(source);
var token = new Token(generator, buffer);
var tokenSecond = new Token(generatorSecond, bufferSecond);
var keys = new HashSet<string>(new[] { "PublicInstanceField", "PublicInstanceProperty" });
var keys = new HashSet<string>(["PublicInstanceField", "PublicInstanceProperty"]);
Assert.Equal(keys, token.Children.Keys.ToHashSet());
Assert.Equal(keys, tokenSecond.Children.Keys.ToHashSet());

Expand Down
4 changes: 2 additions & 2 deletions code/Binary.SourceGeneration.Tests/CompilationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static Compilation CreateCompilation(string source, ImmutableArray<Metada
const string AssemblyName = "TestAssembly";
var compilation = CSharpCompilation.Create(
AssemblyName,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, ParseOptions) },
references: references.ToArray(),
syntaxTrees: [CSharpSyntaxTree.ParseText(source, ParseOptions)],
references: [.. references],
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
var diagnostics = compilation.GetDiagnostics();
Assert.Empty(diagnostics.Where(x => x.Severity is DiagnosticSeverity.Error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Plain<T>
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location),
]);
var generator = new SourceGenerator();
var driver = CSharpGeneratorDriver.Create(generators: new[] { generator.AsSourceGenerator() }, parseOptions: CompilationModule.ParseOptions);
var driver = CSharpGeneratorDriver.Create(generators: [generator.AsSourceGenerator()], parseOptions: CompilationModule.ParseOptions);
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var outputDiagnostics);
var diagnostics = compilation.GetDiagnostics();
var outputCompilationDiagnostics = outputCompilation.GetDiagnostics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public void IncludeErrorTypeTest(string source, string diagnosticId)
const string AssemblyName = "TestAssembly";
var compilation = CSharpCompilation.Create(
AssemblyName,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, CompilationModule.ParseOptions) },
syntaxTrees: [CSharpSyntaxTree.ParseText(source, CompilationModule.ParseOptions)],
references: builder.ToArray(),
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
var originDiagnostics = compilation.GetDiagnostics();
Assert.Equal(diagnosticId, Assert.Single(originDiagnostics.Where(x => x.Severity is DiagnosticSeverity.Error)).Id);

var generator = new SourceGenerator();
var driver = CSharpGeneratorDriver.Create(generators: new[] { generator.AsSourceGenerator() }, parseOptions: CompilationModule.ParseOptions);
var driver = CSharpGeneratorDriver.Create(generators: [generator.AsSourceGenerator()], parseOptions: CompilationModule.ParseOptions);
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var outputDiagnostics);
var outputCompilationDiagnostics = outputCompilation.GetDiagnostics();
Assert.Equal(diagnosticId, Assert.Single(outputCompilationDiagnostics.Where(x => x.Severity is DiagnosticSeverity.Error)).Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public void InvalidInlineArrayTest(string source)
};
var compilation = CSharpCompilation.Create(
"TestAssembly",
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, CompilationModule.ParseOptions) },
references: references.ToArray(),
syntaxTrees: [CSharpSyntaxTree.ParseText(source, CompilationModule.ParseOptions)],
references: [.. references],
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
var driver = CSharpGeneratorDriver.Create(generators: new ISourceGenerator[] { new SourceGenerator().AsSourceGenerator() }, parseOptions: CompilationModule.ParseOptions);
var driver = CSharpGeneratorDriver.Create(generators: [new SourceGenerator().AsSourceGenerator()], parseOptions: CompilationModule.ParseOptions);
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var outputDiagnostics);
var diagnostic = Assert.Single(outputDiagnostics);
var outputCompilationDiagnostics = outputCompilation.GetDiagnostics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NamedTupleTests
public void NamedTupleTest()
{
var dictionary = NamedTupleSourceGeneratorContext.ConverterCreators;
var expectedTypes = new HashSet<Type>(new[] { typeof((int, string)), typeof(TypeWithNamedTuple) });
var expectedTypes = new HashSet<Type>([typeof((int, string)), typeof(TypeWithNamedTuple)]);
Assert.Equal(expectedTypes, dictionary.Keys.ToHashSet());

var generator = Generator.CreateAotBuilder().AddConverterCreators(dictionary.Values).Build();
Expand Down
8 changes: 4 additions & 4 deletions code/Binary.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static IEnumerable<ArgumentNullException> ArgumentNullUnsafeTest<T, R>(M
{
var func = (Func<T, R>)Delegate.CreateDelegate(typeof(Func<T, R>), method);
var a = Assert.Throws<ArgumentNullException>(() => func.Invoke(null!));
return new[] { a };
return [a];
}

private static IEnumerable<ArgumentNullException> ArgumentNullUnsafeTest<T, U, R>(MethodInfo method) where T : class where U : class
Expand All @@ -39,7 +39,7 @@ private static IEnumerable<ArgumentNullException> ArgumentNullUnsafeTest<T, U, R
var u = Unsafe.As<object, U>(ref instance);
var a = Assert.Throws<ArgumentNullException>(() => func.Invoke(null!, u));
var b = Assert.Throws<ArgumentNullException>(() => func.Invoke(t, null!));
return new[] { a, b };
return [a, b];
}

private static IEnumerable<ArgumentNullException> ArgumentNullUnsafeTest<T, U, S, R>(MethodInfo method) where T : class where U : class where S : class
Expand All @@ -55,13 +55,13 @@ private static IEnumerable<ArgumentNullException> ArgumentNullUnsafeTest<T, U, S
var a = Assert.Throws<ArgumentNullException>(() => func.Invoke(null!, u, s));
var b = Assert.Throws<ArgumentNullException>(() => func.Invoke(t, null!, s));
var c = Assert.Throws<ArgumentNullException>(() => func.Invoke(t, u, null!));
return new[] { a, b, c };
return [a, b, c];
}

public static void ArgumentNullExceptionTest(MethodInfo method)
{
var parameters = method.GetParameters();
var types = parameters.Select(x => x.ParameterType).Concat(new[] { method.ReturnType }).ToArray();
var types = parameters.Select(x => x.ParameterType).Concat([method.ReturnType]).ToArray();
var test = ArgumentNullTestMethods[parameters.Length - 1].MakeGenericMethod(types);
var errors = Assert.IsAssignableFrom<IEnumerable<ArgumentNullException>>(test.Invoke(null, [method]));
var expectedParameterNames = parameters.Select(x => x.Name).ToList();
Expand Down
6 changes: 3 additions & 3 deletions code/Binary.Tests/Components/NamedObjectConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void ArgumentCollectionEmptyTest()
{
var generator = Generator.CreateAot();
var b = generator.GetConverter<string>();
var h = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, Array.Empty<string>(), new[] { false }));
var i = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, new[] { string.Empty }, Array.Empty<bool>()));
var h = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, [], [false]));
var i = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, [string.Empty], []));
Assert.Null(h.ParamName);
Assert.Null(i.ParamName);
Assert.Equal($"Sequence contains no element.", h.Message);
Expand All @@ -52,7 +52,7 @@ public void ArgumentCollectionLengthsNotMatch()
{
var generator = Generator.CreateAot();
var b = generator.GetConverter<string>();
var h = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, new[] { string.Empty }, new[] { false, true }));
var h = Assert.Throws<ArgumentException>(() => new FakeNamedObjectConverter<object>(b, [string.Empty], [false, true]));
Assert.Null(h.ParamName);
Assert.Equal($"Sequence lengths not match.", h.Message);
}
Expand Down
6 changes: 3 additions & 3 deletions code/Binary.Tests/Components/TupleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void OverflowTest()
public void SequenceNullTest()
{
var methodInfo = new Func<IEnumerable<IConverter>, int>(TupleObject.GetConverterLength).Method;
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength(new IConverter[] { null! }));
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength([null!]));
var parameters = methodInfo.GetParameters();
Assert.Equal(parameters[0].Name, error.ParamName);
Assert.StartsWith("Sequence contains null or invalid element.", error.Message);
Expand All @@ -66,7 +66,7 @@ public void SequenceNullTest()
public void SequenceInvalidTest()
{
var methodInfo = new Func<IEnumerable<IConverter>, int>(TupleObject.GetConverterLength).Method;
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength(new IConverter[] { new HideConverter() }));
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength([new HideConverter()]));
var parameters = methodInfo.GetParameters();
Assert.Equal(parameters[0].Name, error.ParamName);
Assert.StartsWith("Sequence contains null or invalid element.", error.Message);
Expand All @@ -76,7 +76,7 @@ public void SequenceInvalidTest()
public void SequenceEmptyTest()
{
var methodInfo = new Func<IEnumerable<IConverter>, int>(TupleObject.GetConverterLength).Method;
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength(Array.Empty<IConverter>()));
var error = Assert.Throws<ArgumentException>(() => TupleObject.GetConverterLength([]));
var parameters = methodInfo.GetParameters();
Assert.Equal(parameters[0].Name, error.ParamName);
Assert.StartsWith("Sequence contains no element.", error.Message);
Expand Down
2 changes: 1 addition & 1 deletion code/Binary.Tests/Contexts/GeneratorAotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void GetEnumConverterTest<T>(T source) where T : unmanaged
public void GetEnumConverterInternalTest<T>(T source) where T : unmanaged
{
var method = typeof(Generator).GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
.Single(x => x.Name.Contains("Invoke") && x.GetParameters().Select(x => x.ParameterType).SequenceEqual(new[] { typeof(bool) }));
.Single(x => x.Name.Contains("Invoke") && x.GetParameters().Select(x => x.ParameterType).SequenceEqual([typeof(bool)]));
var invoke = (Func<bool, Converter<T>>)Delegate.CreateDelegate(typeof(Func<bool, Converter<T>>), method.MakeGenericMethod(typeof(T)));

var converterNative = invoke.Invoke(true);
Expand Down
9 changes: 4 additions & 5 deletions code/Binary.Tests/Contexts/GeneratorBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

public class GeneratorBuilderExtensionsTests
Expand Down Expand Up @@ -85,7 +84,7 @@ public void AddConvertersWithEmptyCollection()
{
var builder = new FakeLinkedListGeneratorBuilder();
Assert.Equal(0, builder.Id);
var returned = Assert.IsType<FakeLinkedListGeneratorBuilder>(builder.AddConverters(Enumerable.Empty<IConverter>()));
var returned = Assert.IsType<FakeLinkedListGeneratorBuilder>(builder.AddConverters([]));
Assert.Equal(0, builder.Id);
Assert.True(ReferenceEquals(builder, returned));
}
Expand Down Expand Up @@ -117,7 +116,7 @@ public void AddConverterCreatorsWithEmptyCollection()
{
var builder = new FakeLinkedListGeneratorBuilder();
Assert.Equal(0, builder.Id);
var returned = Assert.IsType<FakeLinkedListGeneratorBuilder>(builder.AddConverterCreators(Enumerable.Empty<IConverterCreator>()));
var returned = Assert.IsType<FakeLinkedListGeneratorBuilder>(builder.AddConverterCreators([]));
Assert.Equal(0, builder.Id);
Assert.True(ReferenceEquals(builder, returned));
}
Expand All @@ -136,7 +135,7 @@ public void AddConverterCreatorsWithLinkedListBuilder()
[Fact(DisplayName = "Add Converters (argument null)")]
public void AddConvertersArgumentNull()
{
var a = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverters(null!, Array.Empty<IConverter>()));
var a = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverters(null!, []));
var b = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverters(new FakeGeneratorBuilder(), null!));
var method = new Func<IGeneratorBuilder, IEnumerable<IConverter>, IGeneratorBuilder>(GeneratorBuilderExtensions.AddConverters).Method;
Assert.Equal("builder", a.ParamName);
Expand All @@ -148,7 +147,7 @@ public void AddConvertersArgumentNull()
[Fact(DisplayName = "Add Converter Creators (argument null)")]
public void AddConverterCreatorsArgumentNull()
{
var a = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverterCreators(null!, Array.Empty<IConverterCreator>()));
var a = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverterCreators(null!, []));
var b = Assert.Throws<ArgumentNullException>(() => GeneratorBuilderExtensions.AddConverterCreators(new FakeGeneratorBuilder(), null!));
var method = new Func<IGeneratorBuilder, IEnumerable<IConverterCreator>, IGeneratorBuilder>(GeneratorBuilderExtensions.AddConverterCreators).Method;
Assert.Equal("builder", a.ParamName);
Expand Down
16 changes: 8 additions & 8 deletions code/Binary.Tests/Converters.Constants/DecimalConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public void GetConverter()
Assert.Equal(sizeof(int) * 4, converter.Length);
}

public static IEnumerable<object[]> DataNumber => new List<object[]>
{
new object[] { decimal.MaxValue },
new object[] { decimal.MinusOne },
new object[] { decimal.MinValue },
new object[] { decimal.One },
new object[] { decimal.Zero },
};
public static IEnumerable<object[]> DataNumber =>
[
[decimal.MaxValue],
[decimal.MinusOne],
[decimal.MinValue],
[decimal.One],
[decimal.Zero],
];

[Theory(DisplayName = "Encode Decode")]
[MemberData(nameof(DataNumber))]
Expand Down
12 changes: 6 additions & 6 deletions code/Binary.Tests/Converters/BigIntegerConverterInternalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

public class BigIntegerConverterInternalTests
{
public static readonly IEnumerable<object?[]> DataNotEnoughSpace = new List<object?[]>
{
new object?[] { 0, new BigInteger() },
new object?[] { 1, new BigInteger(1024) },
new object?[] { 3, new BigInteger(int.MaxValue) },
};
public static readonly IEnumerable<object?[]> DataNotEnoughSpace =
[
[0, new BigInteger()],
[1, new BigInteger(1024)],
[3, new BigInteger(int.MaxValue)],
];

[Theory(DisplayName = "Not Enough Space For Writing")]
[MemberData(nameof(DataNotEnoughSpace))]
Expand Down
16 changes: 8 additions & 8 deletions code/Binary.Tests/Converters/BigIntegerConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public void GetConverter()
Assert.Equal(0, converter.Length);
}

public static IEnumerable<object[]> DataNumber => new List<object[]>
{
new object[] { new BigInteger() },
new object[] { new BigInteger(long.MaxValue) },
new object[] { new BigInteger(long.MinValue) },
new object[] { BigInteger.Parse("91389681247993671255432112000000") },
new object[] { BigInteger.Parse("-90315837410896312071002088037140000") },
};
public static IEnumerable<object[]> DataNumber =>
[
[new BigInteger()],
[new BigInteger(long.MaxValue)],
[new BigInteger(long.MinValue)],
[BigInteger.Parse("91389681247993671255432112000000")],
[BigInteger.Parse("-90315837410896312071002088037140000")],
];

[Theory(DisplayName = "Encode Decode")]
[MemberData(nameof(DataNumber))]
Expand Down
10 changes: 5 additions & 5 deletions code/Binary.Tests/Converters/IPAddressConverterInternalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

public class IPAddressConverterInternalTests
{
public static readonly IEnumerable<object?[]> DataNotEnoughSpace = new List<object?[]>
{
new object?[] { 3, IPAddress.Loopback },
new object?[] { 15, IPAddress.IPv6Loopback },
};
public static readonly IEnumerable<object?[]> DataNotEnoughSpace =
[
[3, IPAddress.Loopback],
[15, IPAddress.IPv6Loopback],
];

[Theory(DisplayName = "Not Enough Space For Writing")]
[MemberData(nameof(DataNotEnoughSpace))]
Expand Down
Loading

0 comments on commit 452a14a

Please sign in to comment.