Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit 50e8db3

Browse files
hughbekrwq
authored andcommitted
Add CodeDom.CodeValidator tests and fix some bugs (#39585)
* Add CodeDom.CodeValidator tests and fix some bugs * Fix netfx tests * PR feedback
1 parent 27ddbe9 commit 50e8db3

File tree

6 files changed

+2763
-16
lines changed

6 files changed

+2763
-16
lines changed

src/Common/src/System/CSharpHelpers.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ internal static bool IsValidTypeNameOrIdentifier(string value, bool isTypeName)
143143
{
144144
bool nextMustBeStartChar = true;
145145

146-
if (value.Length == 0)
146+
if (string.IsNullOrEmpty(value))
147+
{
147148
return false;
149+
}
148150

149151
// each char must be Lu, Ll, Lt, Lm, Lo, Nd, Mn, Mc, Pc
150152
//

src/Common/tests/System/Runtime/Serialization/Formatters/BinaryFormatterHelpers.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ internal static T Clone<T>(T obj,
4444
}
4545

4646
public static void AssertExceptionDeserializationFails<T>() where T : Exception
47+
{
48+
AssertExceptionDeserializationFails(typeof(T));
49+
}
50+
51+
public static void AssertExceptionDeserializationFails(Type exceptionType)
4752
{
4853
// .NET Core and .NET Native throw PlatformNotSupportedExceptions when deserializing many exceptions.
4954
// The .NET Framework supports full deserialization.
@@ -55,7 +60,7 @@ public static void AssertExceptionDeserializationFails<T>() where T : Exception
5560
// Construct a valid serialization payload. This is necessary as most constructors call
5661
// the base constructor before throwing a PlatformNotSupportedException, and the base
5762
// constructor validates the SerializationInfo passed.
58-
var info = new SerializationInfo(typeof(T), new FormatterConverter());
63+
var info = new SerializationInfo(exceptionType, new FormatterConverter());
5964
info.AddValue("ClassName", "ClassName");
6065
info.AddValue("Message", "Message");
6166
info.AddValue("InnerException", null);
@@ -69,7 +74,7 @@ public static void AssertExceptionDeserializationFails<T>() where T : Exception
6974

7075
// Serialization constructors are of the form .ctor(SerializationInfo, StreamingContext).
7176
ConstructorInfo constructor = null;
72-
foreach (ConstructorInfo c in typeof(T).GetTypeInfo().DeclaredConstructors)
77+
foreach (ConstructorInfo c in exceptionType.GetTypeInfo().DeclaredConstructors)
7378
{
7479
ParameterInfo[] parameters = c.GetParameters();
7580
if (parameters.Length == 2 && parameters[0].ParameterType == typeof(SerializationInfo) && parameters[1].ParameterType == typeof(StreamingContext))

src/System.CodeDom/src/System/CodeDom/Compiler/CodeValidator.cs

+20-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ internal void ValidateIdentifiers(CodeObject e)
5454
{
5555
ValidateCodeDirective((CodeDirective)e);
5656
}
57+
else if (e == null)
58+
{
59+
throw new ArgumentNullException(nameof(e));
60+
}
5761
else
5862
{
5963
throw new ArgumentException(SR.Format(SR.InvalidElementType, e.GetType().FullName), nameof(e));
@@ -159,7 +163,11 @@ private void ValidateNamespaceImports(CodeNamespace e)
159163
{
160164
foreach (CodeNamespaceImport imp in e.Imports)
161165
{
162-
if (imp.LinePragma != null) ValidateLinePragmaStart(imp.LinePragma);
166+
if (imp.LinePragma != null)
167+
{
168+
ValidateLinePragmaStart(imp.LinePragma);
169+
}
170+
163171
ValidateNamespaceImport(imp);
164172
}
165173
}
@@ -395,9 +403,8 @@ private void ValidateTypeStart(CodeTypeDeclaration e)
395403
}
396404

397405
ValidateIdentifier(e, nameof(e.Name), e.Name);
398-
if (IsCurrentDelegate)
406+
if (e is CodeTypeDelegate del)
399407
{
400-
CodeTypeDelegate del = (CodeTypeDelegate)e;
401408
ValidateTypeReference(del.ReturnType);
402409
ValidateParameters(del.Parameters);
403410
}
@@ -429,6 +436,11 @@ private void ValidateComment(CodeComment e)
429436

430437
private void ValidateStatement(CodeStatement e)
431438
{
439+
if (e == null)
440+
{
441+
throw new ArgumentNullException(nameof(e));
442+
}
443+
432444
ValidateCodeDirectives(e.StartDirectives);
433445
ValidateCodeDirectives(e.EndDirectives);
434446

@@ -684,7 +696,7 @@ private static void ValidateArity(CodeTypeReference e)
684696
// Check if we have zero type args for open types.
685697
if ((totalTypeArgs != e.TypeArguments.Count) && (e.TypeArguments.Count != 0))
686698
{
687-
throw new ArgumentException(SR.Format(SR.ArityDoesntMatch, baseType, e.TypeArguments.Count));
699+
throw new ArgumentException(SR.Format(SR.ArityDoesntMatch, baseType, e.TypeArguments.Count), nameof(e));
688700
}
689701
}
690702

@@ -693,7 +705,7 @@ private static void ValidateTypeName(object e, string propertyName, string typeN
693705
if (!CodeGenerator.IsValidLanguageIndependentTypeName(typeName))
694706
{
695707
string message = SR.Format(SR.InvalidTypeName, typeName, propertyName, e.GetType().FullName);
696-
throw new ArgumentException(message, nameof(typeName));
708+
throw new ArgumentException(message, nameof(e));
697709
}
698710
}
699711

@@ -702,7 +714,7 @@ private static void ValidateIdentifier(object e, string propertyName, string ide
702714
if (!CodeGenerator.IsValidLanguageIndependentIdentifier(identifier))
703715
{
704716
string message = SR.Format(SR.InvalidLanguageIdentifier, identifier, propertyName, e.GetType().FullName);
705-
throw new ArgumentException(message, nameof(identifier));
717+
throw new ArgumentException(message, nameof(e));
706718
}
707719
}
708720

@@ -1014,19 +1026,17 @@ private static void ValidateCodeDirective(CodeDirective e)
10141026
private static void ValidateChecksumPragma(CodeChecksumPragma e)
10151027
{
10161028
if (e.FileName.IndexOfAny(Path.GetInvalidPathChars()) != -1)
1017-
throw new ArgumentException(SR.Format(SR.InvalidPathCharsInChecksum, e.FileName));
1029+
throw new ArgumentException(SR.Format(SR.InvalidPathCharsInChecksum, e.FileName), nameof(e));
10181030
}
10191031

10201032
private static void ValidateRegionDirective(CodeRegionDirective e)
10211033
{
10221034
if (e.RegionText.IndexOfAny(s_newLineChars) != -1)
1023-
throw new ArgumentException(SR.Format(SR.InvalidRegion, e.RegionText));
1035+
throw new ArgumentException(SR.Format(SR.InvalidRegion, e.RegionText), nameof(e));
10241036
}
10251037

10261038
private bool IsCurrentInterface => _currentClass != null && !(_currentClass is CodeTypeDelegate) ? _currentClass.IsInterface : false;
10271039

10281040
private bool IsCurrentEnum => _currentClass != null && !(_currentClass is CodeTypeDelegate) ? _currentClass.IsEnum : false;
1029-
1030-
private bool IsCurrentDelegate => _currentClass != null && _currentClass is CodeTypeDelegate;
10311041
}
10321042
}

src/System.CodeDom/tests/System.CodeDom.Tests.csproj

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
<Compile Include="System\CodeDom\CodeTypeParameterCollectionTests.cs" />
2121
<Compile Include="System\CodeDom\CodeTypeReferenceCollectionTests.cs" />
2222
<Compile Include="System\CodeDom\Compiler\ExecutorTests.cs" />
23+
<Compile Include="System\CodeDom\Compiler\CodeCompilerTests.cs" />
2324
<Compile Include="System\CodeDom\Compiler\CodeGeneratorTests.cs" />
24-
<Compile Include="System\CodeDom\Compiler\CompilerInfoTests.cs" />
2525
<Compile Include="System\CodeDom\Compiler\CodeGeneratorOptionsTests.cs" />
26+
<Compile Include="System\CodeDom\Compiler\CodeValidatorTests.cs" />
2627
<Compile Include="System\CodeDom\Compiler\CompilerErrorCollectionTests.cs" />
27-
<Compile Include="System\CodeDom\Compiler\CompilerParametersTests.cs" />
2828
<Compile Include="System\CodeDom\Compiler\CompilerErrorTests.cs" />
29+
<Compile Include="System\CodeDom\Compiler\CompilerInfoTests.cs" />
30+
<Compile Include="System\CodeDom\Compiler\CompilerParametersTests.cs" />
2931
<Compile Include="System\CodeDom\Compiler\CompilerResultsTests.cs" />
30-
<Compile Include="System\CodeDom\Compiler\CodeCompilerTests.cs" />
3132
<Compile Include="System\CodeDom\TempFileCollectionTests.cs" />
3233
<Compile Include="System\CodeDom\CodeObjectCreateExpressionTests.cs" />
3334
<Compile Include="System\CodeDom\CodeParameterDeclarationExpressionTests.cs" />
@@ -111,5 +112,8 @@
111112
<Compile Include="$(CommonTestPath)\System\IO\TempFile.cs">
112113
<Link>Common\System\IO\TempFile.cs</Link>
113114
</Compile>
115+
<Compile Include="$(CommonTestPath)\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs">
116+
<Link>Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs</Link>
117+
</Compile>
114118
</ItemGroup>
115119
</Project>

src/System.CodeDom/tests/System/CodeDom/Compiler/CodeDomProviderTests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using System.Reflection;
9+
using System.Runtime.Serialization.Formatters.Tests;
810
using Microsoft.CSharp;
911
using Microsoft.VisualBasic;
1012
using Xunit;
@@ -134,6 +136,15 @@ public void CreateProvider_NoSuchLanguage_ThrowsConfigurationErrorsException(str
134136
AssertIsConfigurationErrorsException(ex2);
135137
}
136138

139+
[Fact]
140+
public void CreateProvider_SerializeConfigurationErrorsException_ThrowsPlatformNotSupportedException()
141+
{
142+
Exception ex = Assert.ThrowsAny<Exception>(() => CodeDomProvider.CreateProvider(string.Empty));
143+
AssertIsConfigurationErrorsException(ex);
144+
145+
BinaryFormatterHelpers.AssertExceptionDeserializationFails(ex.GetType());
146+
}
147+
137148
[Theory]
138149
[InlineData(" cs ", true)]
139150
[InlineData("cs", true)]

0 commit comments

Comments
 (0)