Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSharpCodeGenerator tests #33886

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ private void GenerateNamespace(CodeNamespace e)

private void GenerateStatement(CodeStatement e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

if (e.StartDirectives.Count > 0)
{
GenerateDirectives(e.StartDirectives);
Expand Down Expand Up @@ -683,26 +688,14 @@ private void GeneratePrimitiveExpression(CodePrimitiveExpression e)
Output.Write(((ulong)e.Value).ToString(CultureInfo.InvariantCulture));
Output.Write("ul");
}
else
{
GeneratePrimitiveExpressionBase(e);
}
}

private void GeneratePrimitiveExpressionBase(CodePrimitiveExpression e)
{
if (e.Value == null)
else if (e.Value == null)
{
Output.Write(NullToken);
}
else if (e.Value is string)
{
Output.Write(QuoteSnippetString((string)e.Value));
}
else if (e.Value is char)
{
Output.Write("'" + e.Value.ToString() + "'");
}
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
else if (e.Value is byte)
{
Output.Write(((byte)e.Value).ToString(CultureInfo.InvariantCulture));
Expand Down Expand Up @@ -744,7 +737,7 @@ private void GeneratePrimitiveExpressionBase(CodePrimitiveExpression e)
}
else
{
throw new ArgumentException(SR.Format(SR.InvalidPrimitiveType, e.Value.GetType()));
throw new ArgumentException(SR.Format(SR.InvalidPrimitiveType, e.Value.GetType()), nameof(e));
}
}

Expand Down Expand Up @@ -929,8 +922,8 @@ private void GenerateConditionStatement(CodeConditionStatement e)
GenerateStatements(e.TrueStatements);
Indent--;

CodeStatementCollection falseStatemetns = e.FalseStatements;
if (falseStatemetns.Count > 0)
CodeStatementCollection falseStatements = e.FalseStatements;
if (falseStatements.Count > 0)
{
Output.Write('}');
if (_options.ElseOnClosing)
Expand Down Expand Up @@ -2557,7 +2550,11 @@ private void GenerateAttributes(CodeAttributeDeclarationCollection attributes, s

private void GenerateAttributes(CodeAttributeDeclarationCollection attributes, string prefix, bool inLine)
{
if (attributes.Count == 0) return;
if (attributes.Count == 0)
{
return;
}

bool paramArray = false;
foreach (CodeAttributeDeclaration current in attributes)
{
Expand Down Expand Up @@ -2666,12 +2663,17 @@ public void ValidateIdentifier(string value)
{
if (!IsValidIdentifier(value))
{
throw new ArgumentException(SR.Format(SR.InvalidIdentifier, value));
throw new ArgumentException(SR.Format(SR.InvalidIdentifier, value), nameof(value));
}
}

public string CreateValidIdentifier(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

if (CSharpHelpers.IsPrefixTwoUnderscore(name))
{
name = "_" + name;
Expand All @@ -2687,6 +2689,11 @@ public string CreateValidIdentifier(string name)

public string CreateEscapedIdentifier(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

return CSharpHelpers.CreateEscapedIdentifier(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,8 @@ protected override void GenerateConditionStatement(CodeConditionStatement e)
GenerateVBStatements(e.TrueStatements);
Indent--;

CodeStatementCollection falseStatemetns = e.FalseStatements;
if (falseStatemetns.Count > 0)
CodeStatementCollection falseStatements = e.FalseStatements;
if (falseStatements.Count > 0)
{
Output.Write("Else");
Output.WriteLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ private void ValidateConditionStatement(CodeConditionStatement e)
ValidateExpression(e.Condition);
ValidateStatements(e.TrueStatements);

CodeStatementCollection falseStatemetns = e.FalseStatements;
if (falseStatemetns.Count > 0)
CodeStatementCollection falseStatements = e.FalseStatements;
if (falseStatements.Count > 0)
{
ValidateStatements(e.FalseStatements);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;

namespace System.CodeDom.Compiler
Expand All @@ -9,7 +10,8 @@ internal sealed class ExposedTabStringIndentedTextWriter : IndentedTextWriter
{
public ExposedTabStringIndentedTextWriter(TextWriter writer, string tabString) : base(writer, tabString)
{
TabString = tabString ?? IndentedTextWriter.DefaultTabString;
Debug.Assert(tabString != null, "CodeGeneratorOptions can never have a null TabString");
TabString = tabString;
}

internal void InternalOutputTabs()
Expand Down Expand Up @@ -46,14 +48,16 @@ internal string IndentationString

switch (_indent)
{
case 0: _s = string.Empty; break;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
case 1: _s = tabString; break;
case 2: _s = tabString + tabString; break;
case 3: _s = tabString + tabString + tabString; break;
case 4: _s = tabString + tabString + tabString + tabString; break;
default:
var args = new string[_indent];
for (int i = 0; i < args.Length; i++) args[i] = tabString;
for (int i = 0; i < args.Length; i++)
{
args[i] = tabString;
}
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
return string.Concat(args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Compile Include="System\CodeDom\Compiler\CompilerInfoTests.cs" />
<Compile Include="System\CodeDom\Compiler\CompilerParametersTests.cs" />
<Compile Include="System\CodeDom\Compiler\CompilerResultsTests.cs" />
<Compile Include="System\CodeDom\Compiler\CSharpCodeGeneratorTests.cs" />
<Compile Include="System\CodeDom\TempFileCollectionTests.cs" />
<Compile Include="System\CodeDom\CodeObjectCreateExpressionTests.cs" />
<Compile Include="System\CodeDom\CodeParameterDeclarationExpressionTests.cs" />
Expand Down
Loading