From 1ff1bc7b13604082b45f6e509e629adaecd675f5 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Thu, 17 Oct 2024 11:37:46 -0700 Subject: [PATCH] Testing coverage changes (#169) * Testing coverage changes * fix --- .../CommonUtilTests.cs | 13 ++++--- .../CompilationDataTests.cs | 2 +- .../PolyfillTests.cs | 23 +++++++++++++ src/Basic.CompilerLog.UnitTests/TestBase.cs | 13 +++++-- src/Basic.CompilerLog.Util/Polyfill.cs | 34 +++++++++---------- src/Shared/DotnetUtil.cs | 16 --------- 6 files changed, 57 insertions(+), 44 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs b/src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs index 5411949..54f5986 100644 --- a/src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs +++ b/src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs @@ -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 } -} - +} \ No newline at end of file diff --git a/src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs b/src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs index cb88aaa..5837bfd 100644 --- a/src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs +++ b/src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs @@ -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); diff --git a/src/Basic.CompilerLog.UnitTests/PolyfillTests.cs b/src/Basic.CompilerLog.UnitTests/PolyfillTests.cs index 1e355ec..a1a8690 100644 --- a/src/Basic.CompilerLog.UnitTests/PolyfillTests.cs +++ b/src/Basic.CompilerLog.UnitTests/PolyfillTests.cs @@ -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.Empty)); + Assert.Equal(0, TestBase.DefaultEncoding.GetByteCount((ReadOnlySpan)default)); + } + + [Fact] + public void GetBytesCountEmpty() + { + var buffer = new byte[10]; + Assert.Equal(0, TestBase.DefaultEncoding.GetBytes(ReadOnlySpan.Empty, buffer.AsSpan())); + Assert.Throws(() => TestBase.DefaultEncoding.GetBytes("hello".AsSpan(), buffer.AsSpan(0, 0))); + } } diff --git a/src/Basic.CompilerLog.UnitTests/TestBase.cs b/src/Basic.CompilerLog.UnitTests/TestBase.cs index 701d85b..f241fff 100644 --- a/src/Basic.CompilerLog.UnitTests/TestBase.cs +++ b/src/Basic.CompilerLog.UnitTests/TestBase.cs @@ -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 + /// /// Get all of the supported /// @@ -36,7 +45,7 @@ public static IEnumerable GetSupportedBasicAnalyzerKinds() yield return new object[] { BasicAnalyzerKind.None }; yield return new object[] { BasicAnalyzerKind.OnDisk }; - if (DotnetUtil.IsNetCore) + if (IsNetCore) { yield return new object[] { BasicAnalyzerKind.InMemory }; } @@ -51,7 +60,7 @@ public static IEnumerable GetSimpleBasicAnalyzerKinds() { yield return new object[] { BasicAnalyzerKind.None }; - if (DotnetUtil.IsNetCore) + if (IsNetCore) { yield return new object[] { BasicAnalyzerKind.OnDisk }; yield return new object[] { BasicAnalyzerKind.InMemory }; diff --git a/src/Basic.CompilerLog.Util/Polyfill.cs b/src/Basic.CompilerLog.Util/Polyfill.cs index 752490b..d7c351f 100644 --- a/src/Basic.CompilerLog.Util/Polyfill.cs +++ b/src/Basic.CompilerLog.Util/Polyfill.cs @@ -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; @@ -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(Span span) => + ref (span.Length != 0) + ? ref MemoryMarshal.GetReference(span) + : ref Unsafe.AsRef((void*)1); + + internal static unsafe ref T GetNonNullPinnableReference(ReadOnlySpan span) => + ref (span.Length != 0) + ? ref MemoryMarshal.GetReference(span) + : ref Unsafe.AsRef((void*)1); } #if !NET @@ -92,12 +103,7 @@ internal static void WriteLine(this TextWriter @this, ReadOnlySpan buffer) internal static unsafe int GetByteCount(this Encoding @this, ReadOnlySpan chars) { - if (chars.IsEmpty) - { - return 0; - } - - fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) + fixed (char* charsPtr = &Polyfill.GetNonNullPinnableReference(chars)) { return @this.GetByteCount(charsPtr, chars.Length); } @@ -105,18 +111,8 @@ internal static unsafe int GetByteCount(this Encoding @this, ReadOnlySpan internal static unsafe int GetBytes(this Encoding @this, ReadOnlySpan chars, Span 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); } @@ -143,6 +139,7 @@ internal static partial class PolyfillExtensions namespace System.Diagnostics.CodeAnalysis { /// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it. + [ExcludeFromCodeCoverage] [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] public sealed class NotNullWhenAttribute : Attribute { @@ -157,6 +154,7 @@ public sealed class NotNullWhenAttribute : Attribute } /// Specifies that the output will be non-null if the named parameter is non-null. + [ExcludeFromCodeCoverage] [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] public sealed class NotNullIfNotNullAttribute : Attribute { diff --git a/src/Shared/DotnetUtil.cs b/src/Shared/DotnetUtil.cs index 640785b..baa57d2 100644 --- a/src/Shared/DotnetUtil.cs +++ b/src/Shared/DotnetUtil.cs @@ -16,22 +16,6 @@ internal static class DotnetUtil { private static readonly Lazy> _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 CreateDotnetEnvironmentVariables() { // The CLI, particularly when run from dotnet test, will set the MSBuildSDKsPath environment variable