CompilerTest.cs 6.3 KB

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