From 134f86e8d5c414409631b25b8c6f0ee45fbd8631 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Thu, 3 Nov 2016 21:44:39 +1000 Subject: Initial update to OpenSim 0.8.2.1 source code. --- .../Shared/CodeTools/Tests/CSCodeGeneratorTest.cs | 1 + .../Shared/CodeTools/Tests/CompilerTest.cs | 124 +++++-- .../Shared/CodeTools/Tests/LSL_EventTests.cs | 359 +++++++++++++++++++++ 3 files changed, 450 insertions(+), 34 deletions(-) create mode 100644 OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/LSL_EventTests.cs (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests') diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs index 77e087c..b92f3a3 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs @@ -762,6 +762,7 @@ default public void TestIfStatement() { TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); string input = @"// let's test if statements diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs index 05a8756..b476e32 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs @@ -25,12 +25,14 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System; using System.IO; using System.CodeDom.Compiler; using System.Collections.Generic; using Microsoft.CSharp; using NUnit.Framework; using OpenSim.Region.ScriptEngine.Shared.CodeTools; +using OpenSim.Region.ScriptEngine.Shared.ScriptBase; using OpenSim.Tests.Common; namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests @@ -46,7 +48,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests private string m_testDir; private CSharpCodeProvider m_CSCodeProvider; private CompilerParameters m_compilerParameters; - private CompilerResults m_compilerResults; + // private CompilerResults m_compilerResults; + private ResolveEventHandler m_resolveEventHandler; /// /// Creates a temporary directory where build artifacts are stored. @@ -61,14 +64,26 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests // Create the temporary directory for housing build artifacts. Directory.CreateDirectory(m_testDir); } + } + + [SetUp] + public override void SetUp() + { + base.SetUp(); // Create a CSCodeProvider and CompilerParameters. m_CSCodeProvider = new CSharpCodeProvider(); m_compilerParameters = new CompilerParameters(); - string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin"); + string rootPath = System.AppDomain.CurrentDomain.BaseDirectory; + + m_resolveEventHandler = new ResolveEventHandler(AssemblyResolver.OnAssemblyResolve); + + System.AppDomain.CurrentDomain.AssemblyResolve += m_resolveEventHandler; + m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll")); m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll")); + m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenMetaverseTypes.dll")); m_compilerParameters.GenerateExecutable = false; } @@ -76,9 +91,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests /// Removes the temporary build directory and any build artifacts /// inside it. /// - [TestFixtureTearDown] + [TearDown] public void CleanUp() { + System.AppDomain.CurrentDomain.AssemblyResolve -= m_resolveEventHandler; + if (Directory.Exists(m_testDir)) { // Blow away the temporary directory with artifacts. @@ -86,52 +103,100 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests } } + private CompilerResults CompileScript( + string input, out Dictionary, KeyValuePair> positionMap) + { + m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + + output = Compiler.CreateCSCompilerScript(output, "script1", typeof(ScriptBaseClass).FullName, null); + // System.Console.WriteLine(output); + + positionMap = cg.PositionMap; + + CompilerResults compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); + + // foreach (KeyValuePair key in positionMap.Keys) + // { + // KeyValuePair val = positionMap[key]; + // + // System.Console.WriteLine("{0},{1} => {2},{3}", key.Key, key.Value, val.Key, val.Value); + // } + // + // foreach (CompilerError compErr in m_compilerResults.Errors) + // { + // System.Console.WriteLine("Error: {0},{1} => {2}", compErr.Line, compErr.Column, compErr); + // } + + return compilerResults; + } + + /// + /// Test that line number errors are resolved as expected when preceding code contains a jump. + /// + [Test] + public void TestJumpAndSyntaxError() + { + TestHelpers.InMethod(); + + Dictionary, KeyValuePair> positionMap; + + CompilerResults compilerResults = CompileScript( +@"default +{ + state_entry() + { + jump l; + @l; + i = 1; + } +}", out positionMap); + + Assert.AreEqual( + new KeyValuePair(7, 9), + positionMap[new KeyValuePair(compilerResults.Errors[0].Line, compilerResults.Errors[0].Column)]); + } + /// /// Test the C# compiler error message can be mapped to the correct /// line/column in the LSL source when an undeclared variable is used. /// - //[Test] + [Test] public void TestUseUndeclaredVariable() { TestHelpers.InMethod(); - m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); + Dictionary, KeyValuePair> positionMap; - string input = @"default + CompilerResults compilerResults = CompileScript( +@"default { state_entry() { integer y = x + 3; } -}"; +}", out positionMap); - CSCodeGenerator cg = new CSCodeGenerator(); - string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" + - "namespace SecondLife { " + - "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" + - "public Script() { } " + - cg.Convert(input) + - "} }\n"; - Dictionary, KeyValuePair> positionMap = cg.PositionMap; - - m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); - - Assert.AreEqual(new KeyValuePair(5, 21), - positionMap[new KeyValuePair(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]); + Assert.AreEqual( + new KeyValuePair(5, 21), + positionMap[new KeyValuePair(compilerResults.Errors[0].Line, compilerResults.Errors[0].Column)]); } /// /// Test that a string can be cast to string and another string /// concatenated. /// - //[Test] + [Test] public void TestCastAndConcatString() { TestHelpers.InMethod(); - m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); + Dictionary, KeyValuePair> positionMap; - string input = @"string s = "" a string""; + CompilerResults compilerResults = CompileScript( +@"string s = "" a string""; default { @@ -141,18 +206,9 @@ default string tmp = (string) gAvatarKey + s; llSay(0, tmp); } -}"; +}", out positionMap); - CSCodeGenerator cg = new CSCodeGenerator(); - string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" + - "namespace SecondLife { " + - "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" + - "public Script() { } " + - cg.Convert(input) + - "} }\n"; - m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); - - Assert.AreEqual(0, m_compilerResults.Errors.Count); + Assert.AreEqual(0, compilerResults.Errors.Count); } } } \ No newline at end of file diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/LSL_EventTests.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/LSL_EventTests.cs new file mode 100644 index 0000000..67ce10a --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/LSL_EventTests.cs @@ -0,0 +1,359 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSimulator Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using NUnit.Framework; +using OpenSim.Region.ScriptEngine.Shared.CodeTools; +using OpenSim.Tests.Common; + +namespace OpenSim.Region.ScriptEngine.Shared.Tests +{ + public class LSL_EventTests : OpenSimTestCase + { + CSCodeGenerator m_cg = new CSCodeGenerator(); + + [Test] + public void TestBadEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestCompile("default { bad() {} }", true); + } + + [Test] + public void TestAttachEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestKeyArgEvent("attach"); + } + + [Test] + public void TestObjectRezEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestKeyArgEvent("object_rez"); + } + + [Test] + public void TestMovingEndEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("moving_end"); + } + + [Test] + public void TestMovingStartEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("moving_start"); + } + + [Test] + public void TestNoSensorEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("no_sensor"); + } + + [Test] + public void TestNotAtRotTargetEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("not_at_rot_target"); + } + + [Test] + public void TestNotAtTargetEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("not_at_target"); + } + + [Test] + public void TestStateEntryEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("state_entry"); + } + + [Test] + public void TestStateExitEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("state_exit"); + } + + [Test] + public void TestTimerEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVoidArgEvent("timer"); + } + + private void TestVoidArgEvent(string eventName) + { + TestCompile("default { " + eventName + "() {} }", false); + TestCompile("default { " + eventName + "(integer n) {} }", true); + } + + [Test] + public void TestChangedEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("changed"); + } + + [Test] + public void TestCollisionEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("collision"); + } + + [Test] + public void TestCollisionStartEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("collision_start"); + } + + [Test] + public void TestCollisionEndEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("collision_end"); + } + + [Test] + public void TestOnRezEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("on_rez"); + } + + [Test] + public void TestRunTimePermissionsEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("run_time_permissions"); + } + + [Test] + public void TestSensorEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("sensor"); + } + + [Test] + public void TestTouchEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("touch"); + } + + [Test] + public void TestTouchStartEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("touch_start"); + } + + [Test] + public void TestTouchEndEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntArgEvent("touch_end"); + } + + [Test] + public void TestLandCollisionEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVectorArgEvent("land_collision"); + } + + [Test] + public void TestLandCollisionStartEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVectorArgEvent("land_collision_start"); + } + + [Test] + public void TestLandCollisionEndEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestVectorArgEvent("land_collision_end"); + } + + [Test] + public void TestAtRotTargetEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntRotRotArgEvent("at_rot_target"); + } + + [Test] + public void TestAtTargetEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestIntVecVecArgEvent("at_target"); + } + + [Test] + public void TestControlEvent() + { + TestHelpers.InMethod(); +// TestHelpers.EnableLogging(); + + TestKeyIntIntArgEvent("control"); + } + + private void TestIntArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(integer n) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(integer n, integer o) {{}} }", true); + } + + private void TestKeyArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(key k) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(key k, key l) {{}} }", true); + } + + private void TestVectorArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(vector v) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(vector v, vector w) {{}} }", true); + } + + private void TestIntRotRotArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(integer n, rotation r, rotation s) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(integer n, rotation r, rotation s, rotation t) {{}} }", true); + } + + private void TestIntVecVecArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(integer n, vector v, vector w) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(integer n, vector v, vector w, vector x) {{}} }", true); + } + + private void TestKeyIntIntArgEvent(string eventName) + { + TestCompile("default { " + eventName + "(key k, integer n, integer o) {} }", false); + TestCompile("default { " + eventName + "{{}} }", true); + TestCompile("default { " + eventName + "(string s) {{}} }", true); + TestCompile("default { " + eventName + "(key k, integer n, integer o, integer p) {{}} }", true); + } + + private void TestCompile(string script, bool expectException) + { + bool gotException = false; + Exception ge = null; + + try + { + m_cg.Convert(script); + } + catch (Exception e) + { + gotException = true; + ge = e; + } + + Assert.That( + gotException, + Is.EqualTo(expectException), + "Failed on {0}, exception {1}", script, ge != null ? ge.ToString() : "n/a"); + } + } +} \ No newline at end of file -- cgit v1.1