PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 19using System;
 20using System.IO;
 21using System.Linq;
 22using NUnit.Framework;
 23
 24namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
 25{
 26	[TestFixture]
 27	public class UsingDeclarationTests
 28	{
 29		[Test]
 30		public void WrongUsingTest()
 31		{
 32			string program = "using\n";
 33			CSharpParser parser = new CSharpParser();
 34			SyntaxTree syntaxTree = parser.Parse (program);
 35			Assert.AreEqual(0, syntaxTree.Children.Count());
 36			Assert.IsTrue(parser.HasErrors);
 37		}
 38		
 39		[Test]
 40		public void DeclarationTest()
 41		{
 42			string program = "using System;\n" +
 43				"using My.Name.Space;\n";
 44			CSharpParser parser = new CSharpParser();
 45			SyntaxTree syntaxTree = parser.Parse(program);
 46			Assert.IsFalse(parser.HasErrors);
 47			
 48			Assert.AreEqual(2, syntaxTree.Children.Count());
 49			Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingDeclaration);
 50			Assert.IsFalse(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
 51			UsingDeclaration ud = (UsingDeclaration)syntaxTree.Children.ElementAt(0);
 52			Assert.AreEqual("System", ud.Namespace);
 53			
 54			Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingDeclaration);
 55			Assert.IsFalse(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
 56			ud = (UsingDeclaration)syntaxTree.Children.ElementAt(1);
 57			Assert.AreEqual("My.Name.Space", ud.Namespace);
 58		}
 59		
 60		[Test]
 61		public void UsingAliasDeclarationTest()
 62		{
 63			string program = "using TESTME=System;\n" +
 64				"using myAlias=My.Name.Space;\n" +
 65				"using StringCollection = System.Collections.Generic.List<string>;\n";
 66			CSharpParser parser = new CSharpParser();
 67			SyntaxTree syntaxTree = parser.Parse(program);
 68			Assert.IsFalse(parser.HasErrors);
 69			
 70			Assert.AreEqual(3, syntaxTree.Children.Count());
 71			
 72			Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
 73			UsingAliasDeclaration ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(0);
 74			Assert.AreEqual("TESTME", ud.Alias);
 75			Assert.AreEqual("System", ud.Import.ToString());
 76			
 77			Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
 78			ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(1);
 79			Assert.AreEqual("myAlias", ud.Alias);
 80			Assert.AreEqual("My.Name.Space", ud.Import.ToString());
 81			
 82			Assert.IsTrue(syntaxTree.Children.ElementAt(2) is UsingAliasDeclaration);
 83			ud = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(2);
 84			Assert.AreEqual("StringCollection", ud.Alias);
 85			Assert.AreEqual("System.Collections.Generic.List<string>", ud.Import.ToString());
 86		}
 87		
 88		[Test]
 89		public void UsingWithAliasing()
 90		{
 91			string program = "using global::System;\n" +
 92				"using myAlias=global::My.Name.Space;\n" +
 93				"using a::b.c;\n";
 94			CSharpParser parser = new CSharpParser();
 95			SyntaxTree syntaxTree = parser.Parse(program);
 96			Assert.IsFalse(parser.HasErrors);
 97			
 98			Assert.AreEqual(3, syntaxTree.Children.Count());
 99			
100			Assert.IsTrue(syntaxTree.Children.ElementAt(0) is UsingDeclaration);
101			Assert.IsFalse(syntaxTree.Children.ElementAt(0) is UsingAliasDeclaration);
102			UsingDeclaration ud = (UsingDeclaration)syntaxTree.Children.ElementAt(0);
103			Assert.AreEqual("global::System", ud.Namespace);
104			
105			Assert.IsTrue(syntaxTree.Children.ElementAt(1) is UsingAliasDeclaration);
106			UsingAliasDeclaration uad = (UsingAliasDeclaration)syntaxTree.Children.ElementAt(1);
107			Assert.AreEqual("myAlias", uad.Alias);
108			Assert.AreEqual("global::My.Name.Space", uad.Import.ToString());
109			
110			Assert.IsTrue(syntaxTree.Children.ElementAt(2) is UsingDeclaration);
111			Assert.IsFalse(syntaxTree.Children.ElementAt(2) is UsingAliasDeclaration);
112			ud = (UsingDeclaration)syntaxTree.Children.ElementAt(2);
113			Assert.AreEqual("a::b.c", ud.Namespace);
114		}
115	}
116}