-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
483ca82
commit 236b331
Showing
2 changed files
with
125 additions
and
125 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,125 @@ | ||
#pragma warning disable IL2026 | ||
|
||
[TestFixture] | ||
public class SanityChecks | ||
{ | ||
[Test] | ||
public void NoPublicTypes() | ||
{ | ||
var visibleTypes = typeof(SanityChecks).Assembly | ||
.GetExportedTypes() | ||
.Where(type => type.Namespace?.StartsWith("System") == true); | ||
#if PolyPublic | ||
#if !NET7_0_OR_GREATER | ||
Assert.That(visibleTypes, Is.Not.Empty); | ||
#endif | ||
#else | ||
Assert.That(visibleTypes, Is.Empty); | ||
#endif | ||
} | ||
#if DEBUG | ||
[Test] | ||
public void CodeChecks() | ||
{ | ||
var dir = Path.Combine(SolutionDirectoryFinder.Find(), "Polyfill"); | ||
var errors = new List<string>(); | ||
foreach (var file in Directory.EnumerateFiles(dir, "*.cs", SearchOption.AllDirectories)) | ||
{ | ||
var directoryName = Path.GetDirectoryName(file)!; | ||
if (directoryName.Contains("bin") || directoryName.Contains("obj")) | ||
{ | ||
continue; | ||
} | ||
|
||
var content = File.ReadAllText(file); | ||
var requiredText = new[] | ||
{ | ||
"// <auto-generated />", | ||
"#pragma warning disable" | ||
}; | ||
foreach (var required in requiredText) | ||
{ | ||
if (!content.Contains(required)) | ||
{ | ||
errors.Add($"{file} must contain '{required}'"); | ||
} | ||
} | ||
} | ||
|
||
if (errors.Count > 0) | ||
{ | ||
throw new(string.Join("\n", errors)); | ||
} | ||
} | ||
#endif | ||
[Test] | ||
public void ReflectionChecks() | ||
{ | ||
var errors = new List<string>(); | ||
foreach (var type in typeof(SanityChecks).Assembly.GetTypes()) | ||
{ | ||
if (type.Namespace != null && !type.Namespace.StartsWith("System")) | ||
{ | ||
continue; | ||
} | ||
|
||
if (type.IsNested) | ||
{ | ||
continue; | ||
} | ||
|
||
if (HasAttribute<TestFixtureAttribute>(type)) | ||
{ | ||
continue; | ||
} | ||
|
||
var name = type.Name; | ||
if (name.EndsWith("Usage") || | ||
name.Contains("<") || | ||
name.Contains("Sample") || | ||
name == "SolutionDirectoryFinder" || | ||
name == "AutoGeneratedProgram" || | ||
name == "IsReadOnlyAttribute" || | ||
name == "IsByRefLikeAttribute" || | ||
name == "NullableAttribute" || | ||
name == "NullableContextAttribute" || | ||
name == "ScopedRefAttribute" || | ||
name == "RefSafetyRulesAttribute" || | ||
name == "ParamCollectionAttribute" || | ||
name == "FileUtil") | ||
{ | ||
continue; | ||
} | ||
|
||
if (type is {IsInterface: false, IsEnum: false}) | ||
{ | ||
if (!HasAttribute<ExcludeFromCodeCoverageAttribute>(type)) | ||
{ | ||
errors.Add($"{name} must have ExcludeFromCodeCoverageAttribute"); | ||
} | ||
|
||
if (!HasAttribute<DebuggerNonUserCodeAttribute>(type)) | ||
{ | ||
errors.Add($"{name} must have DebuggerNonUserCodeAttribute"); | ||
} | ||
} | ||
} | ||
|
||
if (errors.Count > 0) | ||
{ | ||
throw new(string.Join("\n", errors)); | ||
} | ||
} | ||
|
||
static bool HasAttribute<T>(Type type) | ||
{ | ||
try | ||
{ | ||
return type.GetCustomAttribute(typeof(T)) != null; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new($"Failed to get {typeof(T).Name} from {type.Name}", e); | ||
} | ||
} | ||
} |