From 29ea2ea45c7ad69f1d782a0ffb3c57653bd2b817 Mon Sep 17 00:00:00 2001 From: routersys Date: Fri, 3 Jul 2026 19:40:28 +0900 Subject: [PATCH] =?UTF-8?q?is=20not=20=E6=A7=8B=E6=96=87=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/LuaSyntaxExtensionsTests.cs | 123 ++++++++++++++++++ LuaScript.Tests/NativeWorkerRoundTripTests.cs | 20 +++ 2 files changed, 143 insertions(+) create mode 100644 LuaScript.Tests/LuaSyntaxExtensionsTests.cs diff --git a/LuaScript.Tests/LuaSyntaxExtensionsTests.cs b/LuaScript.Tests/LuaSyntaxExtensionsTests.cs new file mode 100644 index 0000000..c65d2e5 --- /dev/null +++ b/LuaScript.Tests/LuaSyntaxExtensionsTests.cs @@ -0,0 +1,123 @@ +using LuaScript.Compat; +using MoonSharp.Interpreter; + +namespace LuaScript.Tests +{ + public class LuaSyntaxExtensionsTests + { + private static string Rewrite(string source) => LuaSyntaxExtensions.Rewrite(source); + + [Fact] + public void IsNot_RewritesToNotEqual() + { + Assert.Equal("a ~= b", Rewrite("a is not b")); + } + + [Fact] + public void IsNot_RewritesInCondition() + { + Assert.Equal("if x ~= nil then end", Rewrite("if x is not nil then end")); + } + + [Theory] + [InlineData("a is not b", "a ~= b")] + [InlineData("a is\tnot b", "a ~= b")] + [InlineData("a is\t not b", "a ~= b")] + public void IsNot_AllowsHorizontalWhitespace(string source, string expected) + { + Assert.Equal(expected, Rewrite(source)); + } + + [Fact] + public void IsNot_RewritesMultipleOccurrences() + { + Assert.Equal("a ~= b and c ~= d", Rewrite("a is not b and c is not d")); + } + + [Fact] + public void Newline_BetweenIsAndNot_IsNotAnOperator() + { + const string source = "a is\nnot b"; + Assert.Equal(source, Rewrite(source)); + } + + [Theory] + [InlineData("island")] + [InlineData("basis not x")] + [InlineData("a isnot b")] + [InlineData("a is nothing")] + [InlineData("local is = 1\nx = is + 1")] + public void WordBoundaries_AreRespected(string source) + { + Assert.Equal(source, Rewrite(source)); + } + + [Theory] + [InlineData("a.is not b")] + [InlineData("a:is not b")] + public void MemberAccess_IsNotRewritten(string source) + { + Assert.Equal(source, Rewrite(source)); + } + + [Fact] + public void ShortString_ContentIsPreserved() + { + const string source = "x = \"a is not b\""; + Assert.Equal(source, Rewrite(source)); + } + + [Fact] + public void ShortComment_ContentIsPreserved() + { + const string source = "-- a is not b\nx = 1"; + Assert.Equal(source, Rewrite(source)); + } + + [Fact] + public void LongComment_ContentIsPreserved() + { + const string source = "--[[ a is not b ]]\nx = 1"; + Assert.Equal(source, Rewrite(source)); + } + + [Fact] + public void LongString_ContentIsPreserved() + { + const string source = "x = [==[ a is not b ]==]"; + Assert.Equal(source, Rewrite(source)); + } + + [Fact] + public void RewrittenCode_PreservesLineCount() + { + const string source = "a = 1\nb = a is not 2\nc = 3"; + string rewritten = Rewrite(source); + Assert.Equal(source.Split('\n').Length, rewritten.Split('\n').Length); + Assert.Contains("b = a ~= 2", rewritten); + } + + [Fact] + public void NoIsToken_ReturnsSameReference() + { + const string source = "obj.x = 1\nobj.y = 2"; + Assert.Same(source, Rewrite(source)); + } + + [Fact] + public void IsNot_EvaluatesTrueWhenDifferent_ThroughTransform() + { + var script = new Script(CoreModules.Preset_SoftSandbox); + script.DoString(AviUtlScript.Transform("result = 1 is not 2")); + Assert.True(script.Globals.Get("result").Boolean); + } + + [Fact] + public void IsNot_EvaluatesFalseWhenEqual_ThroughTransform() + { + var script = new Script(CoreModules.Preset_SoftSandbox); + script.DoString(AviUtlScript.Transform("result = 2 is not 2")); + Assert.False(script.Globals.Get("result").Boolean); + } + } +} diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index e3f4fef..1c23c4c 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using LuaScript.Compat; using LuaScript.Engine; namespace LuaScript.Tests @@ -151,6 +152,25 @@ namespace LuaScript.Tests Assert.Equal(System.Text.Encoding.UTF8.GetByteCount("C:/サンプル/画像.png"), fields[NativeProtocol.Alpha]); } + [Fact] + public void IsNotOperator_RunsAfterTransform() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var fields = Fields(2, 2, 0d); + var pixels = new byte[16]; + + string script = AviUtlScript.Transform("obj.x = (1 is not 2) and 7 or 0\nobj.y = (2 is not 2) and 7 or 0"); + + bool ok = RunWorker( + script, + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.Equal(7d, fields[NativeProtocol.X]); + Assert.Equal(0d, fields[NativeProtocol.Y]); + } + [Fact] public void GetInfo_ExposesEnvironmentInfo() {