From 44f4c01d49a694e851816af30a9a890d67a299e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 21 May 2024 07:36:41 +0200 Subject: [PATCH] Add tests for ForEachVariableStatementSyntaxWrapper #2377 --- ...StatementSyntaxWrapperCSharp10UnitTests.cs | 11 + ...StatementSyntaxWrapperCSharp11UnitTests.cs | 11 + ...StatementSyntaxWrapperCSharp12UnitTests.cs | 11 + ...EachVariableStatementSyntaxWrapperTests.cs | 237 +++++++++++++++++ ...eStatementSyntaxWrapperCSharp8UnitTests.cs | 251 ++++++++++++++++++ ...eStatementSyntaxWrapperCSharp9UnitTests.cs | 11 + ...EachVariableStatementSyntaxWrapperTests.cs | 70 +++++ 7 files changed, 602 insertions(+) create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/Lightup/ForEachVariableStatementSyntaxWrapperCSharp10UnitTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/ForEachVariableStatementSyntaxWrapperCSharp11UnitTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/Lightup/ForEachVariableStatementSyntaxWrapperCSharp12UnitTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/ForEachVariableStatementSyntaxWrapperCSharp8UnitTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/ForEachVariableStatementSyntaxWrapperCSharp9UnitTests.cs create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/Lightup/ForEachVariableStatementSyntaxWrapperCSharp10UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/Lightup/ForEachVariableStatementSyntaxWrapperCSharp10UnitTests.cs new file mode 100644 index 000000000..f34d0cff6 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/Lightup/ForEachVariableStatementSyntaxWrapperCSharp10UnitTests.cs @@ -0,0 +1,11 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp10.Lightup +{ + using StyleCop.Analyzers.Test.CSharp9.Lightup; + + public partial class ForEachVariableStatementSyntaxWrapperCSharp10UnitTests : ForEachVariableStatementSyntaxWrapperCSharp9UnitTests + { + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/ForEachVariableStatementSyntaxWrapperCSharp11UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/ForEachVariableStatementSyntaxWrapperCSharp11UnitTests.cs new file mode 100644 index 000000000..a552d63ec --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/ForEachVariableStatementSyntaxWrapperCSharp11UnitTests.cs @@ -0,0 +1,11 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp11.Lightup +{ + using StyleCop.Analyzers.Test.CSharp10.Lightup; + + public partial class ForEachVariableStatementSyntaxWrapperCSharp11UnitTests : ForEachVariableStatementSyntaxWrapperCSharp10UnitTests + { + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/Lightup/ForEachVariableStatementSyntaxWrapperCSharp12UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/Lightup/ForEachVariableStatementSyntaxWrapperCSharp12UnitTests.cs new file mode 100644 index 000000000..692e8455b --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/Lightup/ForEachVariableStatementSyntaxWrapperCSharp12UnitTests.cs @@ -0,0 +1,11 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp12.Lightup +{ + using StyleCop.Analyzers.Test.CSharp11.Lightup; + + public partial class ForEachVariableStatementSyntaxWrapperCSharp12UnitTests : ForEachVariableStatementSyntaxWrapperCSharp11UnitTests + { + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs new file mode 100644 index 000000000..dfc34012c --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs @@ -0,0 +1,237 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp7.Lightup +{ + using System; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.CSharp.Syntax; + using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; + using Xunit; + + public class ForEachVariableStatementSyntaxWrapperTests + { + [Fact] + public void TestNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Null(wrapper.SyntaxNode); + Assert.Throws(() => wrapper.AttributeLists); + Assert.Throws(() => wrapper.AwaitKeyword); + Assert.Throws(() => wrapper.ForEachKeyword); + Assert.Throws(() => wrapper.OpenParenToken); + Assert.Throws(() => wrapper.Variable); + Assert.Throws(() => wrapper.InKeyword); + Assert.Throws(() => wrapper.Expression); + Assert.Throws(() => wrapper.CloseParenToken); + Assert.Throws(() => wrapper.Statement); + Assert.Throws(() => wrapper.WithAttributeLists(default(SyntaxList))); + Assert.Throws(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); + Assert.Throws(() => wrapper.WithForEachKeyword(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); + Assert.Throws(() => wrapper.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); + Assert.Throws(() => wrapper.WithVariable(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithInKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword))); + Assert.Throws(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); + Assert.Throws(() => wrapper.WithStatement(SyntaxFactory.EmptyStatement())); + } + + [Fact] + public void TestWrapperIdentity() + { + var syntaxNode = this.CreateForEachVariableStatement(); + Assert.True(syntaxNode.IsKind(SyntaxKind.ForEachVariableStatement)); + + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode, wrapper.SyntaxNode); + } + + [Fact] + public void TestAttributeListKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(default, wrapper.AttributeLists); + + var newPropertyValue = default(SyntaxList); + _ = wrapper.WithAttributeLists(newPropertyValue); + + newPropertyValue = new SyntaxList( + SyntaxFactory.AttributeList( + SyntaxFactory.SeparatedList( + new[] + { + SyntaxFactory.Attribute( + SyntaxFactory.IdentifierName("x"), + null), + }))); + Assert.Throws(() => wrapper.WithAttributeLists(newPropertyValue)); + } + + [Fact] + public void TestAwaitKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(default, wrapper.AwaitKeyword); + + Assert.Throws(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); + } + + [Fact] + public void TestForEachKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.ForEachKeyword, wrapper.ForEachKeyword); + + wrapper = wrapper.WithForEachKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.ForEachKeyword.IsEquivalentTo(wrapper.ForEachKeyword)); + } + + [Fact] + public void TestOpenParenToken() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.OpenParenToken, wrapper.OpenParenToken); + + wrapper = wrapper.WithOpenParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.OpenParenToken.IsEquivalentTo(wrapper.OpenParenToken)); + } + + [Fact] + public void TestVariable() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Variable, wrapper.Variable); + + wrapper = wrapper.WithVariable(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Variable, wrapper.Variable); + Assert.False(syntaxNode.Variable.IsEquivalentTo(wrapper.Variable)); + } + + [Fact] + public void TestInKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.InKeyword, wrapper.InKeyword); + + wrapper = wrapper.WithInKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.InKeyword))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.InKeyword.IsEquivalentTo(wrapper.InKeyword)); + } + + [Fact] + public void TestExpression() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Expression, wrapper.Expression); + + wrapper = wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Expression, wrapper.Expression); + Assert.False(syntaxNode.Expression.IsEquivalentTo(wrapper.Expression)); + } + + [Fact] + public void TestCloseParenToken() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.CloseParenToken, wrapper.CloseParenToken); + + wrapper = wrapper.WithCloseParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.CloseParenToken.IsEquivalentTo(wrapper.CloseParenToken)); + } + + [Fact] + public void TestStatement() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Statement, wrapper.Statement); + + wrapper = wrapper.WithStatement(SyntaxFactory.Block()); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Statement, wrapper.Statement); + Assert.False(syntaxNode.Statement.IsEquivalentTo(wrapper.Statement)); + } + + [Fact] + public void TestIsInstance() + { + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(null)); + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + + var syntaxNode = this.CreateForEachVariableStatement(); + Assert.True(ForEachVariableStatementSyntaxWrapper.IsInstance(syntaxNode)); + } + + [Fact] + public void TestConversionsNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + + StatementSyntax syntax = wrapper; + Assert.Null(syntax); + + wrapper = (ForEachVariableStatementSyntaxWrapper)(CommonForEachStatementSyntaxWrapper)syntaxNode; + + syntax = wrapper; + Assert.Null(syntax); + } + + [Fact] + public void TestConversions() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + + StatementSyntax syntax = wrapper; + Assert.Same(syntaxNode, syntax); + + wrapper = (ForEachVariableStatementSyntaxWrapper)(CommonForEachStatementSyntaxWrapper)syntaxNode; + + syntax = wrapper; + Assert.Same(syntaxNode, syntax); + } + + [Fact] + public void TestInvalidConversion() + { + var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression); + Assert.Throws(() => (ForEachVariableStatementSyntaxWrapper)syntaxNode); + } + + private ForEachVariableStatementSyntax CreateForEachVariableStatement() + { + return SyntaxFactory.ForEachVariableStatement( + SyntaxFactory.Token(SyntaxKind.ForEachKeyword), + SyntaxFactory.Token(SyntaxKind.OpenParenToken), + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), + SyntaxFactory.Token(SyntaxKind.InKeyword), + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), + SyntaxFactory.Token(SyntaxKind.CloseParenToken), + SyntaxFactory.EmptyStatement()); + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/ForEachVariableStatementSyntaxWrapperCSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/ForEachVariableStatementSyntaxWrapperCSharp8UnitTests.cs new file mode 100644 index 000000000..a70b99da3 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/ForEachVariableStatementSyntaxWrapperCSharp8UnitTests.cs @@ -0,0 +1,251 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp8.Lightup +{ + using System; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.CSharp.Syntax; + using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; + using Xunit; + + public class ForEachVariableStatementSyntaxWrapperCSharp8UnitTests + { + [Fact] + public void TestNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Null(wrapper.SyntaxNode); + Assert.Throws(() => wrapper.AttributeLists); + Assert.Throws(() => wrapper.AwaitKeyword); + Assert.Throws(() => wrapper.ForEachKeyword); + Assert.Throws(() => wrapper.OpenParenToken); + Assert.Throws(() => wrapper.Variable); + Assert.Throws(() => wrapper.InKeyword); + Assert.Throws(() => wrapper.Expression); + Assert.Throws(() => wrapper.CloseParenToken); + Assert.Throws(() => wrapper.Statement); + Assert.Throws(() => wrapper.WithAttributeLists(default(SyntaxList))); + Assert.Throws(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); + Assert.Throws(() => wrapper.WithForEachKeyword(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); + Assert.Throws(() => wrapper.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); + Assert.Throws(() => wrapper.WithVariable(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithInKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword))); + Assert.Throws(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); + Assert.Throws(() => wrapper.WithStatement(SyntaxFactory.EmptyStatement())); + } + + [Fact] + public void TestWrapperIdentity() + { + var syntaxNode = this.CreateForEachVariableStatement(); + Assert.True(syntaxNode.IsKind(SyntaxKind.ForEachVariableStatement)); + + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode, wrapper.SyntaxNode); + } + + [Fact] + public void TestAttributeList() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.AttributeLists, wrapper.AttributeLists); + + var newPropertyValue = new SyntaxList( + SyntaxFactory.AttributeList( + SyntaxFactory.SeparatedList( + new[] + { + SyntaxFactory.Attribute( + SyntaxFactory.IdentifierName("y"), + null), + }))); + wrapper = wrapper.WithAttributeLists(newPropertyValue); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + //// NOTE: IsEquivalentTo() is normally called here, but SyntaxList doesn't have that method + Assert.NotEqual(syntaxNode.AttributeLists, wrapper.AttributeLists); + } + + [Fact] + public void TestAwaitKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.AwaitKeyword, wrapper.AwaitKeyword); + + wrapper = wrapper.WithAwaitKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.AwaitKeyword.IsEquivalentTo(wrapper.AwaitKeyword)); + } + + [Fact] + public void TestForEachKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.ForEachKeyword, wrapper.ForEachKeyword); + + wrapper = wrapper.WithForEachKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.ForEachKeyword.IsEquivalentTo(wrapper.ForEachKeyword)); + } + + [Fact] + public void TestOpenParenToken() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.OpenParenToken, wrapper.OpenParenToken); + + wrapper = wrapper.WithOpenParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.OpenParenToken.IsEquivalentTo(wrapper.OpenParenToken)); + } + + [Fact] + public void TestVariable() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Variable, wrapper.Variable); + + wrapper = wrapper.WithVariable(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Variable, wrapper.Variable); + Assert.False(syntaxNode.Variable.IsEquivalentTo(wrapper.Variable)); + } + + [Fact] + public void TestInKeyword() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.InKeyword, wrapper.InKeyword); + + wrapper = wrapper.WithInKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.InKeyword))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.InKeyword.IsEquivalentTo(wrapper.InKeyword)); + } + + [Fact] + public void TestExpression() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Expression, wrapper.Expression); + + wrapper = wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Expression, wrapper.Expression); + Assert.False(syntaxNode.Expression.IsEquivalentTo(wrapper.Expression)); + } + + [Fact] + public void TestCloseParenToken() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Equal(syntaxNode.CloseParenToken, wrapper.CloseParenToken); + + wrapper = wrapper.WithCloseParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.False(syntaxNode.CloseParenToken.IsEquivalentTo(wrapper.CloseParenToken)); + } + + [Fact] + public void TestStatement() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Same(syntaxNode.Statement, wrapper.Statement); + + wrapper = wrapper.WithStatement(SyntaxFactory.Block()); + Assert.NotNull(wrapper.SyntaxNode); + Assert.NotSame(syntaxNode, wrapper.SyntaxNode); + Assert.NotSame(syntaxNode.Statement, wrapper.Statement); + Assert.False(syntaxNode.Statement.IsEquivalentTo(wrapper.Statement)); + } + + [Fact] + public void TestIsInstance() + { + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(null)); + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + + var syntaxNode = this.CreateForEachVariableStatement(); + Assert.True(ForEachVariableStatementSyntaxWrapper.IsInstance(syntaxNode)); + } + + [Fact] + public void TestConversionsNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + + StatementSyntax syntax = wrapper; + Assert.Null(syntax); + + wrapper = (ForEachVariableStatementSyntaxWrapper)(CommonForEachStatementSyntaxWrapper)syntaxNode; + + syntax = wrapper; + Assert.Null(syntax); + } + + [Fact] + public void TestConversions() + { + var syntaxNode = this.CreateForEachVariableStatement(); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + + StatementSyntax syntax = wrapper; + Assert.Same(syntaxNode, syntax); + + wrapper = (ForEachVariableStatementSyntaxWrapper)(CommonForEachStatementSyntaxWrapper)syntaxNode; + + syntax = wrapper; + Assert.Same(syntaxNode, syntax); + } + + [Fact] + public void TestInvalidConversion() + { + var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression); + Assert.Throws(() => (ForEachVariableStatementSyntaxWrapper)syntaxNode); + } + + private ForEachVariableStatementSyntax CreateForEachVariableStatement() + { + return SyntaxFactory.ForEachVariableStatement( + new SyntaxList( + SyntaxFactory.AttributeList( + SyntaxFactory.SeparatedList( + new[] + { + SyntaxFactory.Attribute( + SyntaxFactory.IdentifierName("x"), + null), + }))), + SyntaxFactory.Token(SyntaxKind.AwaitKeyword), + SyntaxFactory.Token(SyntaxKind.ForEachKeyword), + SyntaxFactory.Token(SyntaxKind.OpenParenToken), + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), + SyntaxFactory.Token(SyntaxKind.InKeyword), + SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), + SyntaxFactory.Token(SyntaxKind.CloseParenToken), + SyntaxFactory.EmptyStatement()); + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/ForEachVariableStatementSyntaxWrapperCSharp9UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/ForEachVariableStatementSyntaxWrapperCSharp9UnitTests.cs new file mode 100644 index 000000000..9f3a60328 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/ForEachVariableStatementSyntaxWrapperCSharp9UnitTests.cs @@ -0,0 +1,11 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.CSharp9.Lightup +{ + using StyleCop.Analyzers.Test.CSharp8.Lightup; + + public partial class ForEachVariableStatementSyntaxWrapperCSharp9UnitTests : ForEachVariableStatementSyntaxWrapperCSharp8UnitTests + { + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs new file mode 100644 index 000000000..3ffc5d3d1 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/ForEachVariableStatementSyntaxWrapperTests.cs @@ -0,0 +1,70 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.Lightup +{ + using System; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.CSharp.Syntax; + using StyleCop.Analyzers.Lightup; + using Xunit; + + public class ForEachVariableStatementSyntaxWrapperTests + { + [Fact] + public void TestNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + Assert.Null(wrapper.SyntaxNode); + Assert.Throws(() => wrapper.AttributeLists); + Assert.Throws(() => wrapper.AwaitKeyword); + Assert.Throws(() => wrapper.ForEachKeyword); + Assert.Throws(() => wrapper.OpenParenToken); + Assert.Throws(() => wrapper.Variable); + Assert.Throws(() => wrapper.InKeyword); + Assert.Throws(() => wrapper.Expression); + Assert.Throws(() => wrapper.CloseParenToken); + Assert.Throws(() => wrapper.Statement); + Assert.Throws(() => wrapper.WithAttributeLists(default(SyntaxList))); + Assert.Throws(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); + Assert.Throws(() => wrapper.WithForEachKeyword(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); + Assert.Throws(() => wrapper.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); + Assert.Throws(() => wrapper.WithVariable(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithInKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword))); + Assert.Throws(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + Assert.Throws(() => wrapper.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); + Assert.Throws(() => wrapper.WithStatement(SyntaxFactory.EmptyStatement())); + } + + [Fact] + public void TestIsInstance() + { + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(null)); + Assert.False(ForEachVariableStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); + } + + [Fact] + public void TestConversionsNull() + { + var syntaxNode = default(SyntaxNode); + var wrapper = (ForEachVariableStatementSyntaxWrapper)syntaxNode; + + StatementSyntax syntax = wrapper; + Assert.Null(syntax); + + wrapper = (ForEachVariableStatementSyntaxWrapper)(CommonForEachStatementSyntaxWrapper)syntaxNode; + + syntax = wrapper; + Assert.Null(syntax); + } + + [Fact] + public void TestInvalidConversion() + { + var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression); + Assert.Throws(() => (ForEachVariableStatementSyntaxWrapper)syntaxNode); + } + } +}