diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs | 124 |
1 files changed, 90 insertions, 34 deletions
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 @@ | |||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | ||
28 | using System.IO; | 29 | using System.IO; |
29 | using System.CodeDom.Compiler; | 30 | using System.CodeDom.Compiler; |
30 | using System.Collections.Generic; | 31 | using System.Collections.Generic; |
31 | using Microsoft.CSharp; | 32 | using Microsoft.CSharp; |
32 | using NUnit.Framework; | 33 | using NUnit.Framework; |
33 | using OpenSim.Region.ScriptEngine.Shared.CodeTools; | 34 | using OpenSim.Region.ScriptEngine.Shared.CodeTools; |
35 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
34 | using OpenSim.Tests.Common; | 36 | using OpenSim.Tests.Common; |
35 | 37 | ||
36 | namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests | 38 | namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests |
@@ -46,7 +48,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests | |||
46 | private string m_testDir; | 48 | private string m_testDir; |
47 | private CSharpCodeProvider m_CSCodeProvider; | 49 | private CSharpCodeProvider m_CSCodeProvider; |
48 | private CompilerParameters m_compilerParameters; | 50 | private CompilerParameters m_compilerParameters; |
49 | private CompilerResults m_compilerResults; | 51 | // private CompilerResults m_compilerResults; |
52 | private ResolveEventHandler m_resolveEventHandler; | ||
50 | 53 | ||
51 | /// <summary> | 54 | /// <summary> |
52 | /// Creates a temporary directory where build artifacts are stored. | 55 | /// Creates a temporary directory where build artifacts are stored. |
@@ -61,14 +64,26 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests | |||
61 | // Create the temporary directory for housing build artifacts. | 64 | // Create the temporary directory for housing build artifacts. |
62 | Directory.CreateDirectory(m_testDir); | 65 | Directory.CreateDirectory(m_testDir); |
63 | } | 66 | } |
67 | } | ||
68 | |||
69 | [SetUp] | ||
70 | public override void SetUp() | ||
71 | { | ||
72 | base.SetUp(); | ||
64 | 73 | ||
65 | // Create a CSCodeProvider and CompilerParameters. | 74 | // Create a CSCodeProvider and CompilerParameters. |
66 | m_CSCodeProvider = new CSharpCodeProvider(); | 75 | m_CSCodeProvider = new CSharpCodeProvider(); |
67 | m_compilerParameters = new CompilerParameters(); | 76 | m_compilerParameters = new CompilerParameters(); |
68 | 77 | ||
69 | string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin"); | 78 | string rootPath = System.AppDomain.CurrentDomain.BaseDirectory; |
79 | |||
80 | m_resolveEventHandler = new ResolveEventHandler(AssemblyResolver.OnAssemblyResolve); | ||
81 | |||
82 | System.AppDomain.CurrentDomain.AssemblyResolve += m_resolveEventHandler; | ||
83 | |||
70 | m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll")); | 84 | m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll")); |
71 | m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll")); | 85 | m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll")); |
86 | m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenMetaverseTypes.dll")); | ||
72 | m_compilerParameters.GenerateExecutable = false; | 87 | m_compilerParameters.GenerateExecutable = false; |
73 | } | 88 | } |
74 | 89 | ||
@@ -76,9 +91,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests | |||
76 | /// Removes the temporary build directory and any build artifacts | 91 | /// Removes the temporary build directory and any build artifacts |
77 | /// inside it. | 92 | /// inside it. |
78 | /// </summary> | 93 | /// </summary> |
79 | [TestFixtureTearDown] | 94 | [TearDown] |
80 | public void CleanUp() | 95 | public void CleanUp() |
81 | { | 96 | { |
97 | System.AppDomain.CurrentDomain.AssemblyResolve -= m_resolveEventHandler; | ||
98 | |||
82 | if (Directory.Exists(m_testDir)) | 99 | if (Directory.Exists(m_testDir)) |
83 | { | 100 | { |
84 | // Blow away the temporary directory with artifacts. | 101 | // Blow away the temporary directory with artifacts. |
@@ -86,52 +103,100 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests | |||
86 | } | 103 | } |
87 | } | 104 | } |
88 | 105 | ||
106 | private CompilerResults CompileScript( | ||
107 | string input, out Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap) | ||
108 | { | ||
109 | m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); | ||
110 | |||
111 | CSCodeGenerator cg = new CSCodeGenerator(); | ||
112 | string output = cg.Convert(input); | ||
113 | |||
114 | output = Compiler.CreateCSCompilerScript(output, "script1", typeof(ScriptBaseClass).FullName, null); | ||
115 | // System.Console.WriteLine(output); | ||
116 | |||
117 | positionMap = cg.PositionMap; | ||
118 | |||
119 | CompilerResults compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); | ||
120 | |||
121 | // foreach (KeyValuePair<int, int> key in positionMap.Keys) | ||
122 | // { | ||
123 | // KeyValuePair<int, int> val = positionMap[key]; | ||
124 | // | ||
125 | // System.Console.WriteLine("{0},{1} => {2},{3}", key.Key, key.Value, val.Key, val.Value); | ||
126 | // } | ||
127 | // | ||
128 | // foreach (CompilerError compErr in m_compilerResults.Errors) | ||
129 | // { | ||
130 | // System.Console.WriteLine("Error: {0},{1} => {2}", compErr.Line, compErr.Column, compErr); | ||
131 | // } | ||
132 | |||
133 | return compilerResults; | ||
134 | } | ||
135 | |||
136 | /// <summary> | ||
137 | /// Test that line number errors are resolved as expected when preceding code contains a jump. | ||
138 | /// </summary> | ||
139 | [Test] | ||
140 | public void TestJumpAndSyntaxError() | ||
141 | { | ||
142 | TestHelpers.InMethod(); | ||
143 | |||
144 | Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap; | ||
145 | |||
146 | CompilerResults compilerResults = CompileScript( | ||
147 | @"default | ||
148 | { | ||
149 | state_entry() | ||
150 | { | ||
151 | jump l; | ||
152 | @l; | ||
153 | i = 1; | ||
154 | } | ||
155 | }", out positionMap); | ||
156 | |||
157 | Assert.AreEqual( | ||
158 | new KeyValuePair<int, int>(7, 9), | ||
159 | positionMap[new KeyValuePair<int, int>(compilerResults.Errors[0].Line, compilerResults.Errors[0].Column)]); | ||
160 | } | ||
161 | |||
89 | /// <summary> | 162 | /// <summary> |
90 | /// Test the C# compiler error message can be mapped to the correct | 163 | /// Test the C# compiler error message can be mapped to the correct |
91 | /// line/column in the LSL source when an undeclared variable is used. | 164 | /// line/column in the LSL source when an undeclared variable is used. |
92 | /// </summary> | 165 | /// </summary> |
93 | //[Test] | 166 | [Test] |
94 | public void TestUseUndeclaredVariable() | 167 | public void TestUseUndeclaredVariable() |
95 | { | 168 | { |
96 | TestHelpers.InMethod(); | 169 | TestHelpers.InMethod(); |
97 | 170 | ||
98 | m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); | 171 | Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap; |
99 | 172 | ||
100 | string input = @"default | 173 | CompilerResults compilerResults = CompileScript( |
174 | @"default | ||
101 | { | 175 | { |
102 | state_entry() | 176 | state_entry() |
103 | { | 177 | { |
104 | integer y = x + 3; | 178 | integer y = x + 3; |
105 | } | 179 | } |
106 | }"; | 180 | }", out positionMap); |
107 | 181 | ||
108 | CSCodeGenerator cg = new CSCodeGenerator(); | 182 | Assert.AreEqual( |
109 | string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" + | 183 | new KeyValuePair<int, int>(5, 21), |
110 | "namespace SecondLife { " + | 184 | positionMap[new KeyValuePair<int, int>(compilerResults.Errors[0].Line, compilerResults.Errors[0].Column)]); |
111 | "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" + | ||
112 | "public Script() { } " + | ||
113 | cg.Convert(input) + | ||
114 | "} }\n"; | ||
115 | Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap = cg.PositionMap; | ||
116 | |||
117 | m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); | ||
118 | |||
119 | Assert.AreEqual(new KeyValuePair<int, int>(5, 21), | ||
120 | positionMap[new KeyValuePair<int, int>(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]); | ||
121 | } | 185 | } |
122 | 186 | ||
123 | /// <summary> | 187 | /// <summary> |
124 | /// Test that a string can be cast to string and another string | 188 | /// Test that a string can be cast to string and another string |
125 | /// concatenated. | 189 | /// concatenated. |
126 | /// </summary> | 190 | /// </summary> |
127 | //[Test] | 191 | [Test] |
128 | public void TestCastAndConcatString() | 192 | public void TestCastAndConcatString() |
129 | { | 193 | { |
130 | TestHelpers.InMethod(); | 194 | TestHelpers.InMethod(); |
131 | 195 | ||
132 | m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); | 196 | Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap; |
133 | 197 | ||
134 | string input = @"string s = "" a string""; | 198 | CompilerResults compilerResults = CompileScript( |
199 | @"string s = "" a string""; | ||
135 | 200 | ||
136 | default | 201 | default |
137 | { | 202 | { |
@@ -141,18 +206,9 @@ default | |||
141 | string tmp = (string) gAvatarKey + s; | 206 | string tmp = (string) gAvatarKey + s; |
142 | llSay(0, tmp); | 207 | llSay(0, tmp); |
143 | } | 208 | } |
144 | }"; | 209 | }", out positionMap); |
145 | 210 | ||
146 | CSCodeGenerator cg = new CSCodeGenerator(); | 211 | Assert.AreEqual(0, compilerResults.Errors.Count); |
147 | string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" + | ||
148 | "namespace SecondLife { " + | ||
149 | "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" + | ||
150 | "public Script() { } " + | ||
151 | cg.Convert(input) + | ||
152 | "} }\n"; | ||
153 | m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); | ||
154 | |||
155 | Assert.AreEqual(0, m_compilerResults.Errors.Count); | ||
156 | } | 212 | } |
157 | } | 213 | } |
158 | } \ No newline at end of file | 214 | } \ No newline at end of file |