Skip to content

Commit

Permalink
Testing coverage changes (#169)
Browse files Browse the repository at this point in the history
* Testing coverage changes

* fix
  • Loading branch information
jaredpar authored Oct 17, 2024
1 parent 34270cf commit 1ff1bc7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
13 changes: 6 additions & 7 deletions src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public void GetAssemblyLoadContext()
public void Defines()
{
#if NET
Assert.True(DotnetUtil.IsNetCore);
Assert.False(DotnetUtil.IsNetFramework);
#elif NETFRAMEWORK
Assert.False(DotnetUtil.IsNetCore);
Assert.True(DotnetUtil.IsNetFramework);
Assert.True(TestBase.IsNetCore);
Assert.False(TestBase.IsNetFramework);
#else
Assert.False(TestBase.IsNetCore);
Assert.True(TestBase.IsNetFramework);
#endif
}
}

}
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void GetCompilationAfterGeneratorsDiagnostics()
var analyzers = rawData.Analyzers
.Where(x => x.FileName != "Microsoft.CodeAnalysis.NetAnalyzers.dll")
.ToList();
BasicAnalyzerHost host = DotnetUtil.IsNetCore
BasicAnalyzerHost host = IsNetCore
? new BasicAnalyzerHostInMemory(reader, analyzers)
: new BasicAnalyzerHostOnDisk(reader, analyzers);
var data = (CSharpCompilationData)reader.ReadCompilationData(0);
Expand Down
23 changes: 23 additions & 0 deletions src/Basic.CompilerLog.UnitTests/PolyfillTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,27 @@ public void WriteLineSimple()
writer.WriteLine(span);
Assert.Equal("hello" + Environment.NewLine, writer.ToString());
}

[Fact]
public void ContainsCharSimple()
{
var span = "test".AsSpan();
Assert.True(span.Contains('e'));
Assert.False(span.Contains('f'));
}

[Fact]
public void GetByteCountEmpty()
{
Assert.Equal(0, TestBase.DefaultEncoding.GetByteCount(ReadOnlySpan<char>.Empty));
Assert.Equal(0, TestBase.DefaultEncoding.GetByteCount((ReadOnlySpan<char>)default));
}

[Fact]
public void GetBytesCountEmpty()
{
var buffer = new byte[10];
Assert.Equal(0, TestBase.DefaultEncoding.GetBytes(ReadOnlySpan<char>.Empty, buffer.AsSpan()));
Assert.Throws<ArgumentException>(() => TestBase.DefaultEncoding.GetBytes("hello".AsSpan(), buffer.AsSpan(0, 0)));
}
}
13 changes: 11 additions & 2 deletions src/Basic.CompilerLog.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public abstract class TestBase : IDisposable
internal Util.LogReaderState State { get; }
internal string RootDirectory => Root.DirectoryPath;

// Have simple helpers in a real tfm (i.e. not netstandard)
#if NET
internal static bool IsNetCore => true;
internal static bool IsNetFramework => false;
#else
internal static bool IsNetCore => false;
internal static bool IsNetFramework => true;
#endif

/// <summary>
/// Get all of the supported <see cref="BasicAnalyzerKind"/>
/// </summary>
Expand All @@ -36,7 +45,7 @@ public static IEnumerable<object[]> GetSupportedBasicAnalyzerKinds()
yield return new object[] { BasicAnalyzerKind.None };
yield return new object[] { BasicAnalyzerKind.OnDisk };

if (DotnetUtil.IsNetCore)
if (IsNetCore)
{
yield return new object[] { BasicAnalyzerKind.InMemory };
}
Expand All @@ -51,7 +60,7 @@ public static IEnumerable<object[]> GetSimpleBasicAnalyzerKinds()
{
yield return new object[] { BasicAnalyzerKind.None };

if (DotnetUtil.IsNetCore)
if (IsNetCore)
{
yield return new object[] { BasicAnalyzerKind.OnDisk };
yield return new object[] { BasicAnalyzerKind.InMemory };
Expand Down
34 changes: 16 additions & 18 deletions src/Basic.CompilerLog.Util/Polyfill.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -31,6 +32,16 @@ internal static StreamWriter NewStreamWriter(Stream stream, Encoding? encoding =
#endif
return new StreamWriter(stream, encoding, bufferSize, leaveOpen);
}

internal static unsafe ref T GetNonNullPinnableReference<T>(Span<T> span) =>
ref (span.Length != 0)
? ref MemoryMarshal.GetReference(span)
: ref Unsafe.AsRef<T>((void*)1);

internal static unsafe ref T GetNonNullPinnableReference<T>(ReadOnlySpan<T> span) =>
ref (span.Length != 0)
? ref MemoryMarshal.GetReference(span)
: ref Unsafe.AsRef<T>((void*)1);
}

#if !NET
Expand Down Expand Up @@ -92,31 +103,16 @@ internal static void WriteLine(this TextWriter @this, ReadOnlySpan<char> buffer)

internal static unsafe int GetByteCount(this Encoding @this, ReadOnlySpan<char> chars)
{
if (chars.IsEmpty)
{
return 0;
}

fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
fixed (char* charsPtr = &Polyfill.GetNonNullPinnableReference(chars))
{
return @this.GetByteCount(charsPtr, chars.Length);
}
}

internal static unsafe int GetBytes(this Encoding @this, ReadOnlySpan<char> chars, Span<byte> bytes)
{
if (chars.IsEmpty)
{
return 0;
}

if (bytes.IsEmpty)
{
return 0;
}

fixed (char* charsPtr = &MemoryMarshal.GetReference(chars))
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
fixed (char* charsPtr = &Polyfill.GetNonNullPinnableReference(chars))
fixed (byte* bytesPtr = &Polyfill.GetNonNullPinnableReference(bytes))
{
return @this.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
Expand All @@ -143,6 +139,7 @@ internal static partial class PolyfillExtensions
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public sealed class NotNullWhenAttribute : Attribute
{
Expand All @@ -157,6 +154,7 @@ public sealed class NotNullWhenAttribute : Attribute
}

/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
public sealed class NotNullIfNotNullAttribute : Attribute
{
Expand Down
16 changes: 0 additions & 16 deletions src/Shared/DotnetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ internal static class DotnetUtil
{
private static readonly Lazy<Dictionary<string, string>> _lazyDotnetEnvironmentVariables = new(CreateDotnetEnvironmentVariables);

// Have simple helpers in a real tfm (i.e. not netstandard)
#if NET || NETFRAMEWORK
internal static bool IsNetCore
{
get
{
#if NET
return true;
#else
return false;
#endif
}
}
internal static bool IsNetFramework => !IsNetCore;
#endif

private static Dictionary<string, string> CreateDotnetEnvironmentVariables()
{
// The CLI, particularly when run from dotnet test, will set the MSBuildSDKsPath environment variable
Expand Down

0 comments on commit 1ff1bc7

Please sign in to comment.