-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standing up Feature.Detect test runner
- Loading branch information
Showing
6 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Spec.Test; | ||
using Wacs.Core; | ||
using Wacs.Core.Runtime; | ||
using Wacs.Core.Types; | ||
using Xunit; | ||
|
||
namespace Feature.Detect; | ||
|
||
public class DetectFeatures | ||
{ | ||
[Theory] | ||
[ClassData(typeof(FeatureDetectTestData))] | ||
public void Detect(FeatureJson.FeatureJson file) | ||
{ | ||
if (!string.IsNullOrEmpty(file.Module)) | ||
{ | ||
try | ||
{ | ||
var runtime = new WasmRuntime(); | ||
|
||
//Mutable globals | ||
var mutableGlobal = new GlobalType(ValType.I32, Mutability.Mutable); | ||
runtime.BindHostGlobal(("a", "b"), mutableGlobal, 1); | ||
|
||
var filepath = Path.Combine(file.Path, file.Module); | ||
using var fileStream = new FileStream(filepath, FileMode.Open); | ||
var module = BinaryModuleParser.ParseWasm(fileStream); | ||
var modInst = runtime.InstantiateModule(module); | ||
var moduleName = !string.IsNullOrEmpty(file.Name)?file.Name:$"{filepath}"; | ||
module.SetName(moduleName); | ||
} | ||
catch (Exception e) | ||
{ | ||
Assert.Fail($"{file.Name} support not detected.\n{e}"); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
Assert.Fail($"{file.Name} not supported."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// /* | ||
// * Copyright 2024 Kelvin Nishikawa | ||
// * | ||
// * Licensed under the Apache License, Version 2.0 (the "License"); | ||
// * you may not use this file except in compliance with the License. | ||
// * You may obtain a copy of the License at | ||
// * | ||
// * http://www.apache.org/licenses/LICENSE-2.0 | ||
// * | ||
// * Unless required by applicable law or agreed to in writing, software | ||
// * distributed under the License is distributed on an "AS IS" BASIS, | ||
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// * See the License for the specific language governing permissions and | ||
// * limitations under the License. | ||
// */ | ||
|
||
using System.Collections; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Feature.Detect.FeatureJson; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Spec.Test | ||
{ | ||
public class FeatureDetectTestData : IEnumerable<object[]> | ||
{ | ||
private static readonly IConfiguration Configuration; | ||
|
||
static FeatureDetectTestData() | ||
{ | ||
// Use ConfigurationFixture to get the JSON directory path | ||
Configuration = new ConfigurationBuilder() | ||
.SetBasePath(AppContext.BaseDirectory) // Set to current directory | ||
.AddJsonFile("testsettings.json", optional: false, reloadOnChange: true) | ||
.Build(); | ||
} | ||
|
||
private static string JsonDirectory => Path.Combine(AppContext.BaseDirectory, Configuration["JsonDirectory"] ?? ""); | ||
|
||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
var files = Directory.GetFiles(JsonDirectory, "*.json", SearchOption.AllDirectories).OrderBy(path => path); | ||
foreach (var file in files) | ||
{ | ||
var testData = LoadFeatureDefinition(file); | ||
yield return new object[] { testData }; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
static FeatureJson LoadFeatureDefinition(string jsonPath) | ||
{ | ||
string json = File.ReadAllText(jsonPath); | ||
|
||
var options = new JsonSerializerOptions | ||
{ | ||
Converters = | ||
{ | ||
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) | ||
}, | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
var testDefinition = JsonSerializer.Deserialize<FeatureJson>(json, options); | ||
if (testDefinition == null) | ||
throw new JsonException($"Error while parsing {jsonPath}"); | ||
|
||
testDefinition.Path = Path.GetDirectoryName(jsonPath)!; | ||
return testDefinition; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// /* | ||
// * Copyright 2024 Kelvin Nishikawa | ||
// * | ||
// * Licensed under the Apache License, Version 2.0 (the "License"); | ||
// * you may not use this file except in compliance with the License. | ||
// * You may obtain a copy of the License at | ||
// * | ||
// * http://www.apache.org/licenses/LICENSE-2.0 | ||
// * | ||
// * Unless required by applicable law or agreed to in writing, software | ||
// * distributed under the License is distributed on an "AS IS" BASIS, | ||
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// * See the License for the specific language governing permissions and | ||
// * limitations under the License. | ||
// */ | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Feature.Detect.FeatureJson; | ||
|
||
public class FeatureJson | ||
{ | ||
[JsonPropertyName("source")] | ||
public string? Source { get; set; } | ||
|
||
public string? Path { get; set; } | ||
|
||
[JsonPropertyName("module")] | ||
public string? Module { get; set; } | ||
|
||
[JsonPropertyName("options")] | ||
public Options? Options { get; set; } | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name { get; set; } | ||
|
||
[JsonPropertyName("proposal")] | ||
public string? Proposal { get; set; } | ||
|
||
[JsonPropertyName("features")] | ||
public List<string>? Features { get; set; } // New property for features list | ||
|
||
public override string ToString() | ||
{ | ||
return $"Name: {Name}\n Proposal: {Proposal}\n Features: {string.Join(", ", Features ?? new List<string>())}\n Module: {Module}\n Options: {Options}"; | ||
} | ||
|
||
} | ||
|
||
public class Options | ||
{ | ||
[JsonPropertyName("builtins")] | ||
public List<string>? Builtins { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"JsonDirectory": "../../../generated-wasm" | ||
} |