Skip to content

✅enable to run unit tests in .NET 472 #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static unsafe string GetString(this Encoding encoding, Span<byte> bytes)
{
fixed (byte* bytesPtr = bytes)
{
if(bytesPtr == null) return string.Empty;
return encoding.GetString(bytesPtr, bytes.Length);
}
}
Expand All @@ -29,6 +30,7 @@ public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte>
{
fixed (byte* bytesPtr = bytes)
{
if(bytesPtr == null) return string.Empty;
return encoding.GetString(bytesPtr, bytes.Length);
}
}
Expand All @@ -39,6 +41,7 @@ public static unsafe int GetBytes(this Encoding encoding, Span<char> chars, Span
fixed (char* charsPtr = chars)
fixed (byte* bytesPtr = bytes)
{
if(bytesPtr == null) return 0;
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
}
Expand All @@ -49,6 +52,7 @@ public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> cha
fixed (char* charsPtr = chars)
fixed (byte* bytesPtr = bytes)
{
if(bytesPtr == null) return 0;
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
}
Expand All @@ -59,6 +63,7 @@ public static unsafe int GetBytes(this Encoding encoding, string chars, Span<byt
fixed (char* charsPtr = chars)
fixed (byte* bytesPtr = bytes)
{
if(bytesPtr == null) return 0;
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/MemoryImportBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public void ItFailsToInstantiateWithNullStore()
action
.Should()
.Throw<ArgumentNullException>()
.WithMessage("Value cannot be null. (Parameter 'store')");
#if NETFRAMEWORK
.WithMessage("Value cannot be null.\r\nParameter name: store");
#else
.WithMessage("Value cannot be null. (Parameter 'store')");
#endif
}

[Fact]
Expand All @@ -67,7 +71,11 @@ public void ItFailsToInstantiateWithNegativeMinimum()
action
.Should()
.Throw<ArgumentOutOfRangeException>()
#if NETFRAMEWORK
.WithMessage("Specified argument was out of the range of valid values.\r\nParameter name: minimum");
#else
.WithMessage("Specified argument was out of the range of valid values. (Parameter 'minimum')");
#endif
}

[Fact]
Expand All @@ -78,7 +86,11 @@ public void ItFailsToInstantiateWithNegativeMaximum()
action
.Should()
.Throw<ArgumentOutOfRangeException>()
#if NETFRAMEWORK
.WithMessage("Specified argument was out of the range of valid values.\r\nParameter name: maximum");
#else
.WithMessage("Specified argument was out of the range of valid values. (Parameter 'maximum')");
#endif
}

[Fact]
Expand All @@ -89,7 +101,12 @@ public void ItFailsToInstantiateWithMaximumLessThanMinimum()
action
.Should()
.Throw<ArgumentException>()
#if NETFRAMEWORK
.WithMessage("The maximum cannot be less than the minimum.\r\nParameter name: maximum");
#else
.WithMessage("The maximum cannot be less than the minimum. (Parameter 'maximum')");
#endif

}

[Fact]
Expand Down
2 changes: 0 additions & 2 deletions tests/StoreDataTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics.Metrics;
using System.Linq;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Wasmtime.Tests
Expand Down
50 changes: 49 additions & 1 deletion tests/WasiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,54 @@

namespace Wasmtime.Tests
{
#if NETFRAMEWORK // the following code is a polyfill as it already exists in .Net
using System.Runtime.CompilerServices;
using System.Collections;

sealed class ReferenceEqualityComparer : IEqualityComparer<object?>, IEqualityComparer

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
private ReferenceEqualityComparer() { }

/// <summary>
/// Gets the singleton <see cref="ReferenceEqualityComparer"/> instance.
/// </summary>
public static ReferenceEqualityComparer Instance { get; } = new ReferenceEqualityComparer();

/// <summary>
/// Determines whether two object references refer to the same object instance.
/// </summary>
/// <param name="x">The first object to compare.</param>
/// <param name="y">The second object to compare.</param>
/// <returns>
/// <see langword="true"/> if both <paramref name="x"/> and <paramref name="y"/> refer to the same object instance
/// or if both are <see langword="null"/>; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>
/// This API is a wrapper around <see cref="object.ReferenceEquals(object?, object?)"/>.
/// It is not necessarily equivalent to calling <see cref="object.Equals(object?, object?)"/>.
/// </remarks>
public new bool Equals(object? x, object? y) => ReferenceEquals(x, y);

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Returns a hash code for the specified object. The returned hash code is based on the object
/// identity, not on the contents of the object.
/// </summary>
/// <param name="obj">The object for which to retrieve the hash code.</param>
/// <returns>A hash code for the identity of <paramref name="obj"/>.</returns>
/// <remarks>
/// This API is a wrapper around <see cref="RuntimeHelpers.GetHashCode(object)"/>.
/// It is not necessarily equivalent to calling <see cref="object.GetHashCode()"/>.
/// </remarks>
public int GetHashCode(object? obj)

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 48 in tests/WasiTests.cs

View workflow job for this annotation

GitHub Actions / Test .NET embedding of Wasmtime (windows-release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
// Depending on target framework, RuntimeHelpers.GetHashCode might not be annotated
// with the proper nullability attribute. We'll suppress any warning that might
// result.
return RuntimeHelpers.GetHashCode(obj!);
}
}
#endif

public class WasiTests
{
[Theory]
Expand Down Expand Up @@ -70,7 +118,7 @@

for (int i = 0; i < env.Count; ++i)
{
var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split("=");
var kvp = memory.ReadNullTerminatedString(memory.ReadInt32(i * 4)).Split('=');
Assert.Equal(env[kvp[0]], kvp[1]);
}
}
Expand Down
11 changes: 9 additions & 2 deletions tests/Wasmtime.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<TargetFrameworks>net8.0;net472</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="!($([MSBuild]::IsOSPlatform('Windows')))">
<TargetFrameworks>net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down