Skip to content

Commit

Permalink
Add CodeGenerator tests and fix NREs/bugs (dotnet/corefx#40838)
Browse files Browse the repository at this point in the history
* Add CodeGenerator tests and fix NREs/bugs

* Reverting empty collection checks

* Small typo

* Skipping tests on full framework

* Some more need to be skipped on framework

* Fix test failure

* Adding format for ToString


Commit migrated from dotnet/corefx@7c867c2
  • Loading branch information
hughbe authored and stephentoub committed Nov 8, 2019
1 parent 4a7b24b commit 8dd6b6b
Show file tree
Hide file tree
Showing 3 changed files with 4,446 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ protected void GenerateNamespaces(CodeCompileUnit e)

protected void GenerateTypes(CodeNamespace e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

foreach (CodeTypeDeclaration c in e.Types)
{
if (_options.BlankLinesBetweenMembers)
Expand Down Expand Up @@ -396,6 +401,10 @@ void ICodeGenerator.GenerateCodeFromStatement(CodeStatement e, TextWriter w, Cod

public virtual void GenerateCodeFromMember(CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
{
if (member == null)
{
throw new ArgumentNullException(nameof(member));
}
if (_output != null)
{
throw new InvalidOperationException(SR.CodeGenReentrance);
Expand Down Expand Up @@ -704,6 +713,11 @@ private void GenerateSnippetMembers(CodeTypeDeclaration e)

protected virtual void GenerateSnippetCompileUnit(CodeSnippetCompileUnit e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

GenerateDirectives(e.StartDirectives);

if (e.LinePragma != null)
Expand Down Expand Up @@ -789,6 +803,11 @@ protected virtual void GenerateCompileUnit(CodeCompileUnit e)

protected virtual void GenerateNamespace(CodeNamespace e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

GenerateCommentStatements(e.Comments);
GenerateNamespaceStart(e);

Expand All @@ -801,11 +820,23 @@ protected virtual void GenerateNamespace(CodeNamespace e)

protected void GenerateNamespaceImports(CodeNamespace e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

foreach (CodeNamespaceImport imp in e.Imports)
{
if (imp.LinePragma != null) GenerateLinePragmaStart(imp.LinePragma);
if (imp.LinePragma != null)
{
GenerateLinePragmaStart(imp.LinePragma);
}

GenerateNamespaceImport(imp);
if (imp.LinePragma != null) GenerateLinePragmaEnd(imp.LinePragma);
if (imp.LinePragma != null)
{
GenerateLinePragmaEnd(imp.LinePragma);
}
}
}

Expand Down Expand Up @@ -846,6 +877,11 @@ private void GenerateProperties(CodeTypeDeclaration e)

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

if (e.StartDirectives.Count > 0)
{
GenerateDirectives(e.StartDirectives);
Expand Down Expand Up @@ -938,6 +974,11 @@ protected void GenerateStatement(CodeStatement e)

protected void GenerateStatements(CodeStatementCollection stmts)
{
if (stmts == null)
{
throw new ArgumentNullException(nameof(stmts));
}

foreach (CodeStatement stmt in stmts)
{
((ICodeGenerator)this).GenerateCodeFromStatement(stmt, _output.InnerWriter, _options);
Expand All @@ -946,7 +987,15 @@ protected void GenerateStatements(CodeStatementCollection stmts)

protected virtual void OutputAttributeDeclarations(CodeAttributeDeclarationCollection attributes)
{
if (attributes.Count == 0) return;
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
if (attributes.Count == 0)
{
return;
}

GenerateAttributeDeclarationsStart(attributes);
bool first = true;
foreach (CodeAttributeDeclaration current in attributes)
Expand Down Expand Up @@ -985,6 +1034,11 @@ protected virtual void OutputAttributeDeclarations(CodeAttributeDeclarationColle

protected virtual void OutputAttributeArgument(CodeAttributeArgument arg)
{
if (arg == null)
{
throw new ArgumentNullException(nameof(arg));
}

if (!string.IsNullOrEmpty(arg.Name))
{
OutputIdentifier(arg.Name);
Expand Down Expand Up @@ -1236,6 +1290,11 @@ protected virtual void OutputOperator(CodeBinaryOperatorType op)

protected virtual void OutputParameters(CodeParameterDeclarationExpressionCollection parameters)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}

bool first = true;
bool multiline = parameters.Count > ParameterMultilineThreshold;
if (multiline)
Expand Down Expand Up @@ -1269,6 +1328,11 @@ protected virtual void OutputParameters(CodeParameterDeclarationExpressionCollec

protected virtual void GenerateBinaryOperatorExpression(CodeBinaryOperatorExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

bool indentedExpression = false;
Output.Write('(');

Expand Down Expand Up @@ -1320,6 +1384,11 @@ protected virtual void GenerateBinaryOperatorExpression(CodeBinaryOperatorExpres

protected virtual void GenerateParameterDeclarationExpression(CodeParameterDeclarationExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

if (e.CustomAttributes.Count > 0)
{
OutputAttributeDeclarations(e.CustomAttributes);
Expand All @@ -1332,12 +1401,22 @@ protected virtual void GenerateParameterDeclarationExpression(CodeParameterDecla

protected virtual void GenerateDirectionExpression(CodeDirectionExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

OutputDirection(e.Direction);
GenerateExpression(e.Expression);
}

protected virtual void GeneratePrimitiveExpression(CodePrimitiveExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

if (e.Value == null)
{
Output.Write(NullToken);
Expand Down Expand Up @@ -1391,7 +1470,7 @@ protected virtual void GeneratePrimitiveExpression(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 All @@ -1413,11 +1492,21 @@ protected virtual void GenerateDefaultValueExpression(CodeDefaultValueExpression

protected virtual void GenerateTypeReferenceExpression(CodeTypeReferenceExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

OutputType(e.Type);
}

protected virtual void GenerateTypeOfExpression(CodeTypeOfExpression e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

Output.Write("typeof(");
OutputType(e.Type);
Output.Write(')');
Expand All @@ -1428,6 +1517,10 @@ protected virtual void GenerateTypeOfExpression(CodeTypeOfExpression e)
protected abstract void GenerateThrowExceptionStatement(CodeThrowExceptionStatement e);
protected virtual void GenerateCommentStatement(CodeCommentStatement e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}
if (e.Comment == null)
{
throw new ArgumentException(SR.Format(SR.Argument_NullComment, nameof(e)), nameof(e));
Expand All @@ -1437,6 +1530,11 @@ protected virtual void GenerateCommentStatement(CodeCommentStatement e)

protected virtual void GenerateCommentStatements(CodeCommentStatementCollection e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

foreach (CodeCommentStatement comment in e)
{
GenerateCommentStatement(comment);
Expand All @@ -1452,7 +1550,17 @@ protected virtual void GenerateCommentStatements(CodeCommentStatementCollection
protected abstract void GenerateRemoveEventStatement(CodeRemoveEventStatement e);
protected abstract void GenerateGotoStatement(CodeGotoStatement e);
protected abstract void GenerateLabeledStatement(CodeLabeledStatement e);
protected virtual void GenerateSnippetStatement(CodeSnippetStatement e) => Output.WriteLine(e.Value);

protected virtual void GenerateSnippetStatement(CodeSnippetStatement e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

Output.WriteLine(e.Value);
}

protected abstract void GenerateVariableDeclarationStatement(CodeVariableDeclarationStatement e);
protected abstract void GenerateLinePragmaStart(CodeLinePragma e);
protected abstract void GenerateLinePragmaEnd(CodeLinePragma e);
Expand All @@ -1469,6 +1577,11 @@ protected virtual void GenerateCommentStatements(CodeCommentStatementCollection

protected virtual void GenerateCompileUnitStart(CodeCompileUnit e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

if (e.StartDirectives.Count > 0)
{
GenerateDirectives(e.StartDirectives);
Expand All @@ -1477,6 +1590,11 @@ protected virtual void GenerateCompileUnitStart(CodeCompileUnit e)

protected virtual void GenerateCompileUnitEnd(CodeCompileUnit e)
{
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}

if (e.EndDirectives.Count > 0)
{
GenerateDirectives(e.EndDirectives);
Expand All @@ -1495,7 +1613,7 @@ protected virtual 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));
}
}

Expand Down
Loading

0 comments on commit 8dd6b6b

Please sign in to comment.