-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3011 from sharwell/csharp8-lightup
Implement part of the C# 8 lightup layer
- Loading branch information
Showing
35 changed files
with
2,844 additions
and
2 deletions.
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
...yzers/StyleCop.Analyzers.Test.CSharp7/Lightup/CommonForEachStatementSyntaxWrapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. 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 CommonForEachStatementSyntaxWrapperTests | ||
{ | ||
[Fact] | ||
public void TestNull() | ||
{ | ||
var syntaxNode = default(SyntaxNode); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Null(wrapper.SyntaxNode); | ||
Assert.Throws<NullReferenceException>(() => wrapper.AwaitKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.ForEachKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.OpenParenToken); | ||
Assert.Throws<NullReferenceException>(() => wrapper.InKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.Expression); | ||
Assert.Throws<NullReferenceException>(() => wrapper.CloseParenToken); | ||
Assert.Throws<NullReferenceException>(() => wrapper.Statement); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithForEachKeyword(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithInKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithStatement(SyntaxFactory.EmptyStatement())); | ||
} | ||
|
||
[Fact] | ||
public void TestWrapperIdentity() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
Assert.True(syntaxNode.IsKind(SyntaxKind.ForEachStatement)); | ||
|
||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Same(syntaxNode, wrapper.SyntaxNode); | ||
} | ||
|
||
[Fact] | ||
public void TestAwaitKeyword() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(default, wrapper.AwaitKeyword); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); | ||
} | ||
|
||
[Fact] | ||
public void TestForEachKeyword() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(syntaxNode.ForEachKeyword, wrapper.ForEachKeyword); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithForEachKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.ForEachKeyword)))); | ||
} | ||
|
||
[Fact] | ||
public void TestOpenParenToken() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(syntaxNode.OpenParenToken, wrapper.OpenParenToken); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithOpenParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.OpenParenToken)))); | ||
} | ||
|
||
[Fact] | ||
public void TestInKeyword() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(syntaxNode.InKeyword, wrapper.InKeyword); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithInKeyword(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.InKeyword)))); | ||
} | ||
|
||
[Fact] | ||
public void TestExpression() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Same(syntaxNode.Expression, wrapper.Expression); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)))); | ||
} | ||
|
||
[Fact] | ||
public void TestCloseParenToken() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(syntaxNode.CloseParenToken, wrapper.CloseParenToken); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithCloseParenToken(SpacingExtensions.WithoutTrivia(SyntaxFactory.Token(SyntaxKind.CloseParenToken)))); | ||
} | ||
|
||
[Fact] | ||
public void TestStatement() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Same(syntaxNode.Statement, wrapper.Statement); | ||
|
||
Assert.Throws<NotSupportedException>(() => wrapper.WithStatement(SyntaxFactory.Block())); | ||
} | ||
|
||
[Fact] | ||
public void TestIsInstance() | ||
{ | ||
Assert.False(CommonForEachStatementSyntaxWrapper.IsInstance(null)); | ||
Assert.False(CommonForEachStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); | ||
|
||
var syntaxNode = this.CreateForEachStatement(); | ||
Assert.True(CommonForEachStatementSyntaxWrapper.IsInstance(syntaxNode)); | ||
} | ||
|
||
[Fact] | ||
public void TestConversionsNull() | ||
{ | ||
var syntaxNode = default(SyntaxNode); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
|
||
StatementSyntax syntax = wrapper; | ||
Assert.Null(syntax); | ||
} | ||
|
||
[Fact] | ||
public void TestConversions() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
|
||
StatementSyntax syntax = wrapper; | ||
Assert.Same(syntaxNode, syntax); | ||
} | ||
|
||
[Fact] | ||
public void TestInvalidConversion() | ||
{ | ||
var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression); | ||
Assert.Throws<InvalidCastException>(() => (CommonForEachStatementSyntaxWrapper)syntaxNode); | ||
} | ||
|
||
private ForEachStatementSyntax CreateForEachStatement() | ||
{ | ||
return SyntaxFactory.ForEachStatement( | ||
SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)), | ||
"x", | ||
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), | ||
SyntaxFactory.EmptyStatement()); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/LanguageVersionExTestsCSharp7.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp7.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.Lightup; | ||
|
||
public class LanguageVersionExTestsCSharp7 : LanguageVersionExTests | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/MethodKindExTestsCSharp7.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp7.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.Lightup; | ||
|
||
public class MethodKindExTestsCSharp7 : MethodKindExTests | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/SyntaxKindExTestsCSharp7.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp7.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.Lightup; | ||
|
||
public class SyntaxKindExTestsCSharp7 : SyntaxKindExTests | ||
{ | ||
} | ||
} |
187 changes: 187 additions & 0 deletions
187
...yzers/StyleCop.Analyzers.Test.CSharp8/Lightup/CommonForEachStatementSyntaxWrapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the Apache License, Version 2.0. 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 CommonForEachStatementSyntaxWrapperTests | ||
{ | ||
[Fact] | ||
public void TestNull() | ||
{ | ||
var syntaxNode = default(SyntaxNode); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Null(wrapper.SyntaxNode); | ||
Assert.Throws<NullReferenceException>(() => wrapper.AwaitKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.ForEachKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.OpenParenToken); | ||
Assert.Throws<NullReferenceException>(() => wrapper.InKeyword); | ||
Assert.Throws<NullReferenceException>(() => wrapper.Expression); | ||
Assert.Throws<NullReferenceException>(() => wrapper.CloseParenToken); | ||
Assert.Throws<NullReferenceException>(() => wrapper.Statement); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithForEachKeyword(SyntaxFactory.Token(SyntaxKind.ForEachKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithInKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))); | ||
Assert.Throws<NullReferenceException>(() => wrapper.WithStatement(SyntaxFactory.EmptyStatement())); | ||
} | ||
|
||
[Fact] | ||
public void TestWrapperIdentity() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
Assert.True(syntaxNode.IsKind(SyntaxKind.ForEachStatement)); | ||
|
||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Same(syntaxNode, wrapper.SyntaxNode); | ||
} | ||
|
||
[Fact] | ||
public void TestAwaitKeyword() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
Assert.Equal(default, 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.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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 TestInKeyword() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)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(CommonForEachStatementSyntaxWrapper.IsInstance(null)); | ||
Assert.False(CommonForEachStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))); | ||
|
||
var syntaxNode = this.CreateForEachStatement(); | ||
Assert.True(CommonForEachStatementSyntaxWrapper.IsInstance(syntaxNode)); | ||
} | ||
|
||
[Fact] | ||
public void TestConversionsNull() | ||
{ | ||
var syntaxNode = default(SyntaxNode); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
|
||
StatementSyntax syntax = wrapper; | ||
Assert.Null(syntax); | ||
} | ||
|
||
[Fact] | ||
public void TestConversions() | ||
{ | ||
var syntaxNode = this.CreateForEachStatement(); | ||
var wrapper = (CommonForEachStatementSyntaxWrapper)syntaxNode; | ||
|
||
StatementSyntax syntax = wrapper; | ||
Assert.Same(syntaxNode, syntax); | ||
} | ||
|
||
[Fact] | ||
public void TestInvalidConversion() | ||
{ | ||
var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression); | ||
Assert.Throws<InvalidCastException>(() => (CommonForEachStatementSyntaxWrapper)syntaxNode); | ||
} | ||
|
||
private ForEachStatementSyntax CreateForEachStatement() | ||
{ | ||
return SyntaxFactory.ForEachStatement( | ||
SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)), | ||
"x", | ||
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), | ||
SyntaxFactory.EmptyStatement()); | ||
} | ||
} | ||
} |
Oops, something went wrong.