Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Dec 6, 2023
1 parent ca88c96 commit 70faced
Show file tree
Hide file tree
Showing 15 changed files with 616 additions and 430 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
with:
fetch-depth: 0

- name: Install .NET Core
- name: Install .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x

- name: Restore Dependencies
run: dotnet restore
Expand Down
5 changes: 4 additions & 1 deletion src/AssemblyMetadata.Generators/AssemblyConstant.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace AssemblyMetadata.Generators;

[ExcludeFromCodeCoverage]
public class AssemblyConstant : IEquatable<AssemblyConstant>
{
public AssemblyConstant(string name, string value)
Expand All @@ -26,7 +29,7 @@ public bool Equals(AssemblyConstant other)

public override bool Equals(object value) => value is AssemblyConstant assemblyContant && Equals(assemblyContant);

public override int GetHashCode() => HashCode.Combine(Name, Value);
public override int GetHashCode() => HashCode.Seed.Combine(Name).Combine(Value);

public static bool operator ==(AssemblyConstant left, AssemblyConstant right) => Equals(left, right);

Expand Down
16 changes: 13 additions & 3 deletions src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,28 @@ private static GeneratorContext SemanticTransform(GeneratorAttributeSyntaxContex
name = name.Substring(0, name.Length - 9);

var argument = attribute.ConstructorArguments.FirstOrDefault();
var value = argument.Value as string;
var value = argument.Value?.ToString() ?? string.Empty;

if (string.IsNullOrWhiteSpace(value))
continue;

var constant = new AssemblyConstant(name, value);
constants.Add(constant);
}
else if (name == nameof(AssemblyMetadataAttribute) && attribute.ConstructorArguments.Length == 2)
{
var nameArgument = attribute.ConstructorArguments[0];
var key = nameArgument.Value as string;
var key = nameArgument.Value?.ToString() ?? string.Empty;

var valueArgument = attribute.ConstructorArguments[1];
var value = valueArgument.Value as string;
var value = valueArgument.Value?.ToString() ?? string.Empty;

if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value))
continue;

// prevent duplicates
if (constants.Any(c => c.Name == key))
continue;

var constant = new AssemblyConstant(key, value);
constants.Add(constant);
Expand Down
19 changes: 14 additions & 5 deletions src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
using System.Reflection;

namespace AssemblyMetadata.Generators;

public static class AssemblyMetadataWriter
{
private static readonly Lazy<string> _informationVersion = new(() =>
{
var assembly = typeof(AssemblyMetadataWriter).Assembly;
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attribute?.InformationalVersion ?? "1.0.0.0";
});

public static string Generate(EquatableArray<AssemblyConstant> constants)
{
if (constants == null)
Expand All @@ -18,10 +27,10 @@ public static string Generate(EquatableArray<AssemblyConstant> constants)
.AppendLine("/// </summary>");

codeBuilder
.Append("[global::System.CodeDom.Compiler.GeneratedCode(\"")
.Append("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"")
.Append("AssemblyMetadata.Generators")
.Append("\", \"")
.Append("1.0.0.0")
.Append(_informationVersion.Value)
.AppendLine("\")]");

codeBuilder
Expand Down Expand Up @@ -53,20 +62,20 @@ public static string Generate(EquatableArray<AssemblyConstant> constants)
return codeBuilder.ToString();
}

private static string SafeName(string name)
public static string SafeName(string name)
{
return ToPropertyName(name.AsSpan());
}

private static string SafeValue(string value)
public static string SafeValue(string value)
{
return value
.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace(Environment.NewLine, "\\r\\n");
}

private static string ToPropertyName(ReadOnlySpan<char> span)
public static string ToPropertyName(ReadOnlySpan<char> span)
{
if (span.IsEmpty)
return string.Empty;
Expand Down
19 changes: 9 additions & 10 deletions src/AssemblyMetadata.Generators/EquatableArray.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace AssemblyMetadata.Generators;

public readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T>
[ExcludeFromCodeCoverage]
public readonly struct EquatableArray<T> : IReadOnlyCollection<T>, IEquatable<EquatableArray<T>>
where T : IEquatable<T>
{
public static readonly EquatableArray<T> Empty = new(Array.Empty<T>());
Expand All @@ -14,7 +16,7 @@ public EquatableArray(T[] array)
_array = array;
}

public EquatableArray(IEnumerable<T> array)
public EquatableArray(IEnumerable<T>? array)
{
array ??= Enumerable.Empty<T>();
_array = array.ToArray();
Expand All @@ -35,12 +37,7 @@ public override int GetHashCode()
if (_array is null)
return 0;

HashCode hashCode = default;

foreach (T item in _array)
hashCode.Add(item);

return hashCode.ToHashCode();
return HashCode.Seed.CombineAll(_array);
}

public ReadOnlySpan<T> AsSpan()
Expand All @@ -50,12 +47,14 @@ public ReadOnlySpan<T> AsSpan()

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return ((IEnumerable<T>)(_array ?? Array.Empty<T>())).GetEnumerator();
IEnumerable<T> array = _array ?? Array.Empty<T>();
return array.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)(_array ?? Array.Empty<T>())).GetEnumerator();
IEnumerable<T> array = _array ?? Array.Empty<T>();
return array.GetEnumerator();
}

public int Count => _array?.Length ?? 0;
Expand Down
7 changes: 5 additions & 2 deletions src/AssemblyMetadata.Generators/GeneratorContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;

using Microsoft.CodeAnalysis;

namespace AssemblyMetadata.Generators;

[ExcludeFromCodeCoverage]
public class GeneratorContext : IEquatable<GeneratorContext>
{
public GeneratorContext(IEnumerable<Diagnostic> diagnostics, IEnumerable<AssemblyConstant> constants)
Expand All @@ -28,7 +31,7 @@ public bool Equals(GeneratorContext other)

public override bool Equals(object value) => value is GeneratorContext context && Equals(context);

public override int GetHashCode() => HashCode.Combine(Diagnostics, Constants);
public override int GetHashCode() => HashCode.Seed.CombineAll(Diagnostics).CombineAll(Constants);

public static bool operator ==(GeneratorContext left, GeneratorContext right) => Equals(left, right);

Expand Down
Loading

0 comments on commit 70faced

Please sign in to comment.