513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Scripts;
|
|
|
|
/// <summary>
|
|
/// Shared Script Library tests — compile, register, execute inline.
|
|
/// </summary>
|
|
public class SharedScriptLibraryTests
|
|
{
|
|
private readonly ScriptCompilationService _compilationService;
|
|
private readonly SharedScriptLibrary _library;
|
|
|
|
/// <summary>Initializes a new instance of the <c>SharedScriptLibraryTests</c> class.</summary>
|
|
public SharedScriptLibraryTests()
|
|
{
|
|
_compilationService = new ScriptCompilationService(
|
|
NullLogger<ScriptCompilationService>.Instance);
|
|
_library = new SharedScriptLibrary(
|
|
_compilationService, NullLogger<SharedScriptLibrary>.Instance);
|
|
}
|
|
|
|
/// <summary>Verifies that compile and register valid script succeeds.</summary>
|
|
[Fact]
|
|
public void CompileAndRegister_ValidScript_Succeeds()
|
|
{
|
|
var result = _library.CompileAndRegister("add", "1 + 2");
|
|
Assert.True(result);
|
|
Assert.True(_library.Contains("add"));
|
|
}
|
|
|
|
/// <summary>Verifies that compile and register invalid script returns false.</summary>
|
|
[Fact]
|
|
public void CompileAndRegister_InvalidScript_ReturnsFalse()
|
|
{
|
|
var result = _library.CompileAndRegister("bad", "this is not valid {{{");
|
|
Assert.False(result);
|
|
Assert.False(_library.Contains("bad"));
|
|
}
|
|
|
|
/// <summary>Verifies that compile and register forbidden api returns false.</summary>
|
|
[Fact]
|
|
public void CompileAndRegister_ForbiddenApi_ReturnsFalse()
|
|
{
|
|
var result = _library.CompileAndRegister("evil", "System.IO.File.Delete(\"/tmp\")");
|
|
Assert.False(result);
|
|
}
|
|
|
|
/// <summary>Verifies that compile and register replaces existing script.</summary>
|
|
[Fact]
|
|
public void CompileAndRegister_Replaces_ExistingScript()
|
|
{
|
|
_library.CompileAndRegister("calc", "1 + 1");
|
|
_library.CompileAndRegister("calc", "2 + 2");
|
|
|
|
Assert.True(_library.Contains("calc"));
|
|
// Should have only one entry
|
|
Assert.Equal(1, _library.GetRegisteredScriptNames().Count(n => n == "calc"));
|
|
}
|
|
|
|
/// <summary>Verifies that remove registered script returns true.</summary>
|
|
[Fact]
|
|
public void Remove_RegisteredScript_ReturnsTrue()
|
|
{
|
|
_library.CompileAndRegister("temp", "42");
|
|
Assert.True(_library.Remove("temp"));
|
|
Assert.False(_library.Contains("temp"));
|
|
}
|
|
|
|
/// <summary>Verifies that remove nonexistent script returns false.</summary>
|
|
[Fact]
|
|
public void Remove_NonexistentScript_ReturnsFalse()
|
|
{
|
|
Assert.False(_library.Remove("nonexistent"));
|
|
}
|
|
|
|
/// <summary>Verifies that get registered script names returns all names.</summary>
|
|
[Fact]
|
|
public void GetRegisteredScriptNames_ReturnsAllNames()
|
|
{
|
|
_library.CompileAndRegister("a", "1");
|
|
_library.CompileAndRegister("b", "2");
|
|
_library.CompileAndRegister("c", "3");
|
|
|
|
var names = _library.GetRegisteredScriptNames();
|
|
Assert.Equal(3, names.Count);
|
|
Assert.Contains("a", names);
|
|
Assert.Contains("b", names);
|
|
Assert.Contains("c", names);
|
|
}
|
|
|
|
/// <summary>Verifies that execute async nonexistent script throws.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ExecuteAsync_NonexistentScript_Throws()
|
|
{
|
|
await Assert.ThrowsAsync<InvalidOperationException>(
|
|
() => _library.ExecuteAsync("missing", null!));
|
|
}
|
|
}
|