aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs')
-rw-r--r--OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs153
1 files changed, 153 insertions, 0 deletions
diff --git a/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs
new file mode 100644
index 0000000..7725d8d
--- /dev/null
+++ b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs
@@ -0,0 +1,153 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System.IO;
29using System.CodeDom.Compiler;
30using System.Collections.Generic;
31using Microsoft.CSharp;
32using NUnit.Framework;
33using OpenSim.Region.ScriptEngine.Shared.CodeTools;
34
35namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
36{
37 /// <summary>
38 /// Tests the LSL compiler. Among other things, test that error messages
39 /// generated by the C# compiler can be mapped to prper lines/columns in
40 /// the LSL source.
41 /// </summary>
42 [TestFixture]
43 public class CompilerTest
44 {
45 private string m_testDir;
46 private CSharpCodeProvider m_CSCodeProvider;
47 private CompilerParameters m_compilerParameters;
48 private CompilerResults m_compilerResults;
49
50 /// <summary>
51 /// Creates a temporary directory where build artifacts are stored.
52 /// </summary>
53 [TestFixtureSetUp]
54 public void Init()
55 {
56 m_testDir = Path.Combine(Path.GetTempPath(), "opensim_compilerTest_" + Path.GetRandomFileName());
57
58 if (!Directory.Exists(m_testDir))
59 {
60 // Create the temporary directory for housing build artifacts.
61 Directory.CreateDirectory(m_testDir);
62 }
63
64 // Create a CSCodeProvider and CompilerParameters.
65 m_CSCodeProvider = new CSharpCodeProvider();
66 m_compilerParameters = new CompilerParameters();
67
68 string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin");
69 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll"));
70 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
71 m_compilerParameters.GenerateExecutable = false;
72 }
73
74 /// <summary>
75 /// Removes the temporary build directory and any build artifacts
76 /// inside it.
77 /// </summary>
78 [TestFixtureTearDown]
79 public void CleanUp()
80 {
81 if (Directory.Exists(m_testDir))
82 {
83 // Blow away the temporary directory with artifacts.
84 Directory.Delete(m_testDir, true);
85 }
86 }
87
88 /// <summary>
89 /// Test the C# compiler error message can be mapped to the correct
90 /// line/column in the LSL source when an undeclared variable is used.
91 /// </summary>
92 //[Test]
93 public void TestUseUndeclaredVariable()
94 {
95 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
96
97 string input = @"default
98{
99 state_entry()
100 {
101 integer y = x + 3;
102 }
103}";
104
105 CSCodeGenerator cg = new CSCodeGenerator();
106 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
107 "namespace SecondLife { " +
108 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
109 "public Script() { } " +
110 cg.Convert(input) +
111 "} }\n";
112 Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap = cg.PositionMap;
113
114 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
115
116 Assert.AreEqual(new KeyValuePair<int, int>(5, 21),
117 positionMap[new KeyValuePair<int, int>(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]);
118 }
119
120 /// <summary>
121 /// Test that a string can be cast to string and another string
122 /// concatenated.
123 /// </summary>
124 //[Test]
125 public void TestCastAndConcatString()
126 {
127 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
128
129 string input = @"string s = "" a string"";
130
131default
132{
133 state_entry()
134 {
135 key gAvatarKey = llDetectedKey(0);
136 string tmp = (string) gAvatarKey + s;
137 llSay(0, tmp);
138 }
139}";
140
141 CSCodeGenerator cg = new CSCodeGenerator();
142 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
143 "namespace SecondLife { " +
144 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
145 "public Script() { } " +
146 cg.Convert(input) +
147 "} }\n";
148 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
149
150 Assert.AreEqual(0, m_compilerResults.Errors.Count);
151 }
152 }
153}