opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 OpenSimulator 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  
28 using System.IO;
29 using System.CodeDom.Compiler;
30 using System.Collections.Generic;
31 using Microsoft.CSharp;
32 using NUnit.Framework;
33 using OpenSim.Region.ScriptEngine.Shared.CodeTools;
34 using OpenSim.Tests.Common;
35  
36 namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
37 {
38 /// <summary>
39 /// Tests the LSL compiler. Among other things, test that error messages
40 /// generated by the C# compiler can be mapped to prper lines/columns in
41 /// the LSL source.
42 /// </summary>
43 [TestFixture]
44 public class CompilerTest : OpenSimTestCase
45 {
46 private string m_testDir;
47 private CSharpCodeProvider m_CSCodeProvider;
48 private CompilerParameters m_compilerParameters;
49 private CompilerResults m_compilerResults;
50  
51 /// <summary>
52 /// Creates a temporary directory where build artifacts are stored.
53 /// </summary>
54 [TestFixtureSetUp]
55 public void Init()
56 {
57 m_testDir = Path.Combine(Path.GetTempPath(), "opensim_compilerTest_" + Path.GetRandomFileName());
58  
59 if (!Directory.Exists(m_testDir))
60 {
61 // Create the temporary directory for housing build artifacts.
62 Directory.CreateDirectory(m_testDir);
63 }
64  
65 // Create a CSCodeProvider and CompilerParameters.
66 m_CSCodeProvider = new CSharpCodeProvider();
67 m_compilerParameters = new CompilerParameters();
68  
69 string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin");
70 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"));
72 m_compilerParameters.GenerateExecutable = false;
73 }
74  
75 /// <summary>
76 /// Removes the temporary build directory and any build artifacts
77 /// inside it.
78 /// </summary>
79 [TestFixtureTearDown]
80 public void CleanUp()
81 {
82 if (Directory.Exists(m_testDir))
83 {
84 // Blow away the temporary directory with artifacts.
85 Directory.Delete(m_testDir, true);
86 }
87 }
88  
89 /// <summary>
90 /// 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.
92 /// </summary>
93 //[Test]
94 public void TestUseUndeclaredVariable()
95 {
96 TestHelpers.InMethod();
97  
98 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
99  
100 string input = @"default
101 {
102 state_entry()
103 {
104 integer y = x + 3;
105 }
106 }";
107  
108 CSCodeGenerator cg = new CSCodeGenerator();
109 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
110 "namespace SecondLife { " +
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 }
122  
123 /// <summary>
124 /// Test that a string can be cast to string and another string
125 /// concatenated.
126 /// </summary>
127 //[Test]
128 public void TestCastAndConcatString()
129 {
130 TestHelpers.InMethod();
131  
132 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
133  
134 string input = @"string s = "" a string"";
135  
136 default
137 {
138 state_entry()
139 {
140 key gAvatarKey = llDetectedKey(0);
141 string tmp = (string) gAvatarKey + s;
142 llSay(0, tmp);
143 }
144 }";
145  
146 CSCodeGenerator cg = new CSCodeGenerator();
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 }
157 }
158 }