/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/UsingDeclarationTests.cs

http://github.com/icsharpcode/ILSpy · C# · 116 lines · 84 code · 15 blank · 17 comment · 0 complexity · 6910fa05aaca7c326bd5a06a7e988070 MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.IO;
  20. using System.Linq;
  21. using NUnit.Framework;
  22. namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
  23. {
  24. [TestFixture]
  25. public class UsingDeclarationTests
  26. {
  27. [Test]
  28. public void WrongUsingTest()
  29. {
  30. string program = "using\n";
  31. CSharpParser parser = new CSharpParser();
  32. SyntaxTree syntaxTree = parser.Parse (program);
  33. Assert.AreEqual(0, syntaxTree.Children.Count());
  34. Assert.IsTrue(parser.HasErrors);
  35. }
  36. [Test]
  37. public void DeclarationTest()
  38. {
  39. string program = "using System;\n" +
  40. "using My.Name.Space;\n";
  41. CSharpParser parser = new CSharpParser();
  42. SyntaxTree syntaxTree = parser.Parse(program);
  43. Assert.IsFalse(parser.HasErrors);
  44. Assert.AreEqual(2, syntaxTree.Children.Count());
  45. Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingDeclaration);
  46. Assert.IsFalse(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
  47. UsingDeclaration ud = (UsingDeclaration)syntaxTree.Children.ElementAt(0);
  48. Assert.AreEqual("System", ud.Namespace);
  49. Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingDeclaration);
  50. Assert.IsFalse(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
  51. ud = (UsingDeclaration)syntaxTree.Children.ElementAt(1);
  52. Assert.AreEqual("My.Name.Space", ud.Namespace);
  53. }
  54. [Test]
  55. public void UsingAliasDeclarationTest()
  56. {
  57. string program = "using TESTME=System;\n" +
  58. "using myAlias=My.Name.Space;\n" +
  59. "using StringCollection = System.Collections.Generic.List<string>;\n";
  60. CSharpParser parser = new CSharpParser();
  61. SyntaxTree syntaxTree = parser.Parse(program);
  62. Assert.IsFalse(parser.HasErrors);
  63. Assert.AreEqual(3, syntaxTree.Children.Count());
  64. Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
  65. UsingAliasDeclaration ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(0);
  66. Assert.AreEqual("TESTME", ud.Alias);
  67. Assert.AreEqual("System", ud.Import.ToString());
  68. Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
  69. ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(1);
  70. Assert.AreEqual("myAlias", ud.Alias);
  71. Assert.AreEqual("My.Name.Space", ud.Import.ToString());
  72. Assert.IsTrue(syntaxTree.Children.ElementAt(2) is UsingAliasDeclaration);
  73. ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(2);
  74. Assert.AreEqual("StringCollection", ud.Alias);
  75. Assert.AreEqual("System.Collections.Generic.List<string>", ud.Import.ToString());
  76. }
  77. [Test]
  78. public void UsingWithAliasing()
  79. {
  80. string program = "using global::System;\n" +
  81. "using myAlias=global::My.Name.Space;\n" +
  82. "using a::b.c;\n";
  83. CSharpParser parser = new CSharpParser();
  84. SyntaxTree syntaxTree = parser.Parse(program);
  85. Assert.IsFalse(parser.HasErrors);
  86. Assert.AreEqual(3, syntaxTree.Children.Count());
  87. Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingDeclaration);
  88. Assert.IsFalse(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
  89. UsingDeclaration ud = (UsingDeclaration)syntaxTree.Children.ElementAt(0);
  90. Assert.AreEqual("global::System", ud.Namespace);
  91. Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
  92. UsingAliasDeclaration uad = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(1);
  93. Assert.AreEqual("myAlias", uad.Alias);
  94. Assert.AreEqual("global::My.Name.Space", uad.Import.ToString());
  95. Assert.IsTrue(syntaxTree.Children.ElementAt(2) is UsingDeclaration);
  96. Assert.IsFalse(syntaxTree.Children.ElementAt(2) is UsingAliasDeclaration);
  97. ud = (UsingDeclaration)syntaxTree.Children.ElementAt(2);
  98. Assert.AreEqual("a::b.c", ud.Namespace);
  99. }
  100. }
  101. }