Path: redb/redb.Tests.Integration/Tests/Unit/NoDynamicTypeTests.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/NoDynamicTypeTests.cs
----
using System.Text;
using System.Text.RegularExpressions;
namespace redb.Tests.Integration.Tests.Unit;
///
/// The repository does not use the C# type dynamic: values are strongly typed. The test
/// reads every C# source file of the tree it runs from (the whole repository on a development tree, the redb module on
/// the public mirror) and fails with the file and line of each use. Comments, string and character literals and
/// preprocessor lines are left out, so the word in prose or in a message does not count.
///
public class NoDynamicTypeTests
{
private static readonly Regex DynamicKeyword = new(@"(? !IsBuildOutput(root!, f))
.ToList();
files.Should().Contain(f => f.EndsWith("IRedbService.cs", StringComparison.Ordinal), "the scan reaches the core sources");
var uses = new List();
foreach (var file in files)
{
var code = CodeOnly(File.ReadAllText(file));
foreach (Match m in DynamicKeyword.Matches(code))
{
var line = 1 + code.AsSpan(0, m.Index).Count('\n');
uses.Add($"{Path.GetRelativePath(root!, file)}:{line}");
}
}
uses.Should().BeEmpty("the type dynamic is not used in this repository; use a strong type instead");
}
[Theory]
[InlineData("dynamic x = 1;", 1)]
[InlineData("var list = new List();", 1)]
[InlineData("var y = (dynamic)x;", 1)]
[InlineData("// a dynamic value\nint a = 1;", 0)]
[InlineData("/* dynamic */ int a = 1;", 0)]
[InlineData("var s = \"a dynamic node\";", 0)]
[InlineData("var s = @\"a \"\"dynamic\"\" node\";", 0)]
[InlineData("var s = $\"{(ok ? \"dynamic\" : \"static\")} node\";", 0)]
[InlineData("var s = $\"{Make()} node\";", 1)]
[InlineData("var s = \"\"\"\n dynamic \"text\"\n \"\"\";", 0)]
[InlineData("#region dynamic part\nint a = 1;", 0)]
[InlineData("var c = '\"'; dynamic d = c;", 1)]
[InlineData("bool _dynamic = true; var s = x.dynamicName;", 0)]
public void CodeOnly_KeepsCode_AndDropsCommentsAndLiterals(string source, int expected)
{
DynamicKeyword.Matches(CodeOnly(source)).Count.Should().Be(expected);
}
private static bool IsBuildOutput(string root, string file)
{
var parts = Path.GetRelativePath(root, file).Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return parts.Any(p => p is "bin" or "obj" or ".git" or "node_modules");
}
// The root is the repository (the folder that holds redb.DB/src/redb.Core) or, on the public mirror, the folder that
// holds redb.Core next to the tests.
private static string? FindSourceRoot()
{
for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir != null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "redb.DB", "src", "redb.Core", "IRedbService.cs"))
|| File.Exists(Path.Combine(dir.FullName, "redb.Core", "IRedbService.cs")))
return dir.FullName;
}
return null;
}
///
/// The text with comments, string and character literals and preprocessor lines replaced by blanks. Line breaks are
/// kept, so positions map to the same line. The holes of an interpolated string are code and are kept.
///
internal static string CodeOnly(string text)
{
var sb = new StringBuilder(text.Length);
var i = 0;
Scan(text, ref i, sb, stopAtBrace: false);
return sb.ToString();
}
private static void Scan(string t, ref int i, StringBuilder sb, bool stopAtBrace)
{
var depth = 0;
var lineStart = true;
while (i < t.Length)
{
var c = t[i];
var next = i + 1 < t.Length ? t[i + 1] : '\0';
if (lineStart && c == '#')
{
while (i < t.Length && t[i] != '\n') i++;
continue;
}
if (c is not (' ' or '\t' or '\r')) lineStart = c == '\n';
if (c == '/' && next == '/')
{
while (i < t.Length && t[i] != '\n') i++;
continue;
}
if (c == '/' && next == '*')
{
var end = t.IndexOf("*/", i + 2, StringComparison.Ordinal);
var stop = end < 0 ? t.Length : end + 2;
Blank(t, i, stop, sb);
i = stop;
continue;
}
if (c == '"' || ((c == '@' || c == '$') && (next == '"' || next == '@' || next == '$')))
{
SkipString(t, ref i, sb);
continue;
}
if (c == '\'')
{
var j = i + 1;
if (j < t.Length && t[j] == '\\') j += 2;
while (j < t.Length && t[j] != '\'' && t[j] != '\n') j++;
i = Math.Min(j + 1, t.Length);
sb.Append(' ');
continue;
}
if (stopAtBrace)
{
if (c == '{') depth++;
else if (c == '}' && depth-- == 0) return;
}
sb.Append(c);
i++;
}
}
private static void SkipString(string t, ref int i, StringBuilder sb)
{
var dollars = 0;
var verbatim = false;
while (i < t.Length && (t[i] == '$' || t[i] == '@'))
{
if (t[i] == '$') dollars++; else verbatim = true;
i++;
}
var quotes = 0;
while (i + quotes < t.Length && t[i + quotes] == '"') quotes++;
if (quotes >= 3)
{
// Raw string literal: ends at the next run of as many quotes; holes take as many braces as there are dollars.
i += quotes;
var close = new string('"', quotes);
var open = new string('{', Math.Max(dollars, 1));
while (i < t.Length && string.CompareOrdinal(t, i, close, 0, quotes) != 0)
{
if (dollars > 0 && string.CompareOrdinal(t, i, open, 0, open.Length) == 0 && (i + open.Length >= t.Length || t[i + open.Length] != '{'))
{
i += open.Length;
Scan(t, ref i, sb, stopAtBrace: true);
i += Math.Min(dollars, t.Length - i);
continue;
}
if (t[i] == '\n') sb.Append('\n');
i++;
}
i = Math.Min(i + quotes, t.Length);
sb.Append(' ');
return;
}
i++; // opening quote
while (i < t.Length)
{
var c = t[i];
if (verbatim && c == '"' && i + 1 < t.Length && t[i + 1] == '"') { i += 2; continue; }
if (!verbatim && c == '\\') { i += 2; continue; }
if (c == '"') { i++; break; }
if (!verbatim && c == '\n') break;
if (dollars > 0 && c == '{')
{
if (i + 1 < t.Length && t[i + 1] == '{') { i += 2; continue; }
i++;
Scan(t, ref i, sb, stopAtBrace: true);
i++; // closing brace
continue;
}
if (c == '\n') sb.Append('\n');
i++;
}
sb.Append(' ');
}
private static void Blank(string t, int from, int to, StringBuilder sb)
{
for (var k = from; k < to; k++)
if (t[k] == '\n') sb.Append('\n');
sb.Append(' ');
}
}