CompilerTest.cs 6.4 KB

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