-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for basic if/elif directives
closes #15
- Loading branch information
Showing
12 changed files
with
436 additions
and
8 deletions.
There are no files selected for viewing
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
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
80 changes: 80 additions & 0 deletions
80
Src/CSharpier.Tests/SyntaxPrinter/PreprocessorSymbolsTests.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,80 @@ | ||
using System; | ||
using System.Linq; | ||
using CSharpier.SyntaxPrinter; | ||
using FluentAssertions; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using NUnit.Framework; | ||
|
||
namespace CSharpier.Tests.SyntaxPrinter | ||
{ | ||
public class PreprocessorSymbolsTests | ||
{ | ||
[TestCase("BASIC_IF", "BASIC_IF")] | ||
[TestCase("!NOT_IF", "NOT_IF")] | ||
[TestCase("EQUALS_TRUE == true", "EQUALS_TRUE")] | ||
[TestCase("EQUALS_FALSE == false", "EQUALS_FALSE")] | ||
[TestCase("true == TRUE_EQUALS", "TRUE_EQUALS")] | ||
[TestCase("false == FALSE_EQUALS", "FALSE_EQUALS")] | ||
[TestCase("LEFT_OR || RIGHT_OR", "LEFT_OR")] | ||
[TestCase("LEFT_AND && RIGHT_AND", "LEFT_AND", "RIGHT_AND")] | ||
[TestCase("(EQUALS_TRUE_IN_PARENS == true)", "EQUALS_TRUE_IN_PARENS")] | ||
public void AddSymbolSet_For_If(string condition, params string[] symbols) | ||
{ | ||
var trivia = WhenIfDirectiveHasCondition(condition); | ||
|
||
var result = AddSymbolSet(trivia); | ||
|
||
result.Should().ContainInOrder(symbols); | ||
} | ||
|
||
[Test] | ||
public void AddSymbolSet_For_Basic_Elif_Adds_Symbol() | ||
{ | ||
var trivia = WhenElifDirectiveHasCondition("DEBUG"); | ||
|
||
var result = AddSymbolSet(trivia); | ||
|
||
result.Should().ContainInOrder("DEBUG"); | ||
} | ||
|
||
private string[] AddSymbolSet(ConditionalDirectiveTriviaSyntax trivia) | ||
{ | ||
TestablePreprocessorSymbols.Reset(); | ||
|
||
TestablePreprocessorSymbols.AddSymbolSet(trivia); | ||
|
||
return TestablePreprocessorSymbols.GetSymbolSets().FirstOrDefault() | ||
?? Array.Empty<string>(); | ||
} | ||
|
||
private IfDirectiveTriviaSyntax WhenIfDirectiveHasCondition(string condition) | ||
{ | ||
return SyntaxFactory.IfDirectiveTrivia( | ||
SyntaxFactory.ParseExpression(condition), | ||
true, | ||
true, | ||
true | ||
); | ||
} | ||
|
||
private ElifDirectiveTriviaSyntax WhenElifDirectiveHasCondition(string condition) | ||
{ | ||
return SyntaxFactory.ElifDirectiveTrivia( | ||
SyntaxFactory.ParseExpression(condition), | ||
true, | ||
true, | ||
true | ||
); | ||
} | ||
|
||
private class TestablePreprocessorSymbols : PreprocessorSymbols | ||
{ | ||
public static void AddSymbolSet( | ||
ConditionalDirectiveTriviaSyntax conditionalDirectiveTriviaSyntax | ||
) { | ||
AddSymbolSetForConditional(conditionalDirectiveTriviaSyntax); | ||
} | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
Src/CSharpier.Tests/TestFiles/Directives/PreprocessorSymbols.cst
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,52 @@ | ||
public class ClassName | ||
{ | ||
#if BASIC_IF | ||
public string ShortPropertyName; | ||
#elif BASIC_ELIF | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if !NOT_IF | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if EQUALS_TRUE == true | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if true == TRUE_EQUALS | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if NOT_EQUALS_TRUE != true | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if true != TRUE_NOT_EQUALS | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if LEFT_AND && RIGHT_AND | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if (LEFT_PAREN && RIGHT_PAREN) | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
} |
52 changes: 52 additions & 0 deletions
52
Src/CSharpier.Tests/TestFiles/Directives/PreprocessorSymbols.expected.cst
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,52 @@ | ||
public class ClassName | ||
{ | ||
#if BASIC_IF | ||
public string ShortPropertyName; | ||
#elif BASIC_ELIF | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if !NOT_IF | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if EQUALS_TRUE == true | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if true == TRUE_EQUALS | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if NOT_EQUALS_TRUE != true | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if true != TRUE_NOT_EQUALS | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if LEFT_AND && RIGHT_AND | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
|
||
#if (LEFT_PAREN && RIGHT_PAREN) | ||
public string ShortPropertyName; | ||
#else | ||
public string ShortPropertyName; | ||
#endif | ||
} |
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
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
Oops, something went wrong.