Files
Chronos/tests/Chronos.Testing/Assert.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

263 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace Chronos.Testing;
/// <summary>Assertions with messages that state expected and actual values.</summary>
public static class Assert
{
public static void True([System.Diagnostics.CodeAnalysis.DoesNotReturnIf(false)] bool condition, string? message = null, [CallerArgumentExpression(nameof(condition))] string? expression = null)
{
if (!condition) Fail(message ?? $"Expected true: {expression}");
}
public static void False([System.Diagnostics.CodeAnalysis.DoesNotReturnIf(true)] bool condition, string? message = null, [CallerArgumentExpression(nameof(condition))] string? expression = null)
{
if (condition) Fail(message ?? $"Expected false: {expression}");
}
public static void Null(object? value, [CallerArgumentExpression(nameof(value))] string? expression = null)
{
if (value is not null) Fail($"Expected null: {expression} was {Format(value)}");
}
public static T NotNull<T>([System.Diagnostics.CodeAnalysis.NotNull] T? value, [CallerArgumentExpression(nameof(value))] string? expression = null) where T : class
{
if (value is null) Fail($"Expected non-null: {expression}");
return value!;
}
public static T NotNull<T>([System.Diagnostics.CodeAnalysis.NotNull] T? value, [CallerArgumentExpression(nameof(value))] string? expression = null) where T : struct
{
if (value is null) Fail($"Expected non-null: {expression}");
return value!.Value;
}
public static void Equal<T>(T expected, T actual, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
Fail(Prefix(message) + $"{expression}: expected {Format(expected)}, actual {Format(actual)}");
}
public static void NotEqual<T>(T unexpected, T actual, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (EqualityComparer<T>.Default.Equals(unexpected, actual))
Fail(Prefix(message) + $"{expression}: did not expect {Format(unexpected)}");
}
public static void Same(object? expected, object? actual, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!ReferenceEquals(expected, actual)) Fail($"{expression}: expected the same instance");
}
/// <summary>|expected − actual| ≤ tolerance.</summary>
public static void Approximately(double expected, double actual, double tolerance, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (double.IsNaN(actual) || Math.Abs(expected - actual) > tolerance)
Fail(Prefix(message) + $"{expression}: expected {F(expected)} ± {F(tolerance)}, actual {F(actual)} (diff {F(Math.Abs(expected - actual))})");
}
/// <summary>Distance between two points ≤ <paramref name="pixels"/>.</summary>
public static void Within(double expectedX, double expectedY, double actualX, double actualY, double pixels, string? message = null)
{
var d = Math.Sqrt((expectedX - actualX) * (expectedX - actualX) + (expectedY - actualY) * (expectedY - actualY));
if (!(d <= pixels))
Fail(Prefix(message) + $"expected ({F(expectedX)}, {F(expectedY)}) within {F(pixels)} px, actual ({F(actualX)}, {F(actualY)}) at {F(d)} px");
}
public static void InRange(double actual, double min, double max, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!(actual >= min && actual <= max))
Fail(Prefix(message) + $"{expression}: expected in [{F(min)}, {F(max)}], actual {F(actual)}");
}
public static void Greater(double actual, double bound, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!(actual > bound)) Fail(Prefix(message) + $"{expression}: expected > {F(bound)}, actual {F(actual)}");
}
public static void GreaterOrEqual(double actual, double bound, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!(actual >= bound)) Fail(Prefix(message) + $"{expression}: expected ≥ {F(bound)}, actual {F(actual)}");
}
public static void Less(double actual, double bound, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!(actual < bound)) Fail(Prefix(message) + $"{expression}: expected < {F(bound)}, actual {F(actual)}");
}
public static void LessOrEqual(double actual, double bound, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (!(actual <= bound)) Fail(Prefix(message) + $"{expression}: expected ≤ {F(bound)}, actual {F(actual)}");
}
public static void SequenceEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string? message = null, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
var e = expected.ToList();
var a = actual.ToList();
var n = Math.Min(e.Count, a.Count);
for (var i = 0; i < n; i++)
{
if (!EqualityComparer<T>.Default.Equals(e[i], a[i]))
Fail(Prefix(message) + $"{expression}: first difference at [{i}]: expected {Format(e[i])}, actual {Format(a[i])}");
}
if (e.Count != a.Count)
Fail(Prefix(message) + $"{expression}: expected {e.Count} items, actual {a.Count}");
}
public static void BytesEqual(ReadOnlySpan<byte> expected, ReadOnlySpan<byte> actual, string? message = null)
{
var n = Math.Min(expected.Length, actual.Length);
for (var i = 0; i < n; i++)
{
if (expected[i] != actual[i])
Fail(Prefix(message) + $"bytes differ at offset {i}: expected 0x{expected[i]:X2}, actual 0x{actual[i]:X2}");
}
if (expected.Length != actual.Length)
Fail(Prefix(message) + $"expected {expected.Length} bytes, actual {actual.Length}");
}
/// <summary>Every element is ≥ (or &gt; when strict) the previous one.</summary>
public static void Monotonic(IEnumerable<double> values, bool strict = false, bool decreasing = false, string? message = null)
{
var i = 0;
double? previous = null;
foreach (var v in values)
{
if (previous is { } p)
{
var ok = decreasing
? (strict ? v < p : v <= p)
: (strict ? v > p : v >= p);
if (!ok) Fail(Prefix(message) + $"not monotonic at [{i}]: {F(p)} then {F(v)}");
}
previous = v;
i++;
}
}
public static void Contains<T>(T expected, IEnumerable<T> collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
{
if (!collection.Contains(expected)) Fail($"{expression}: does not contain {Format(expected)}");
}
/// <summary>At least one element satisfies <paramref name="predicate"/>.</summary>
public static T Contains<T>(IEnumerable<T> collection, Func<T, bool> predicate, [CallerArgumentExpression(nameof(predicate))] string? expression = null)
{
foreach (var item in collection)
{
if (predicate(item)) return item;
}
Fail($"No element matches {expression}");
return default!;
}
public static void DoesNotContain<T>(T unexpected, IEnumerable<T> collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
{
if (collection.Contains(unexpected)) Fail($"{expression}: unexpectedly contains {Format(unexpected)}");
}
public static void Contains(string expected, string? actual, [CallerArgumentExpression(nameof(actual))] string? expression = null)
{
if (actual is null || !actual.Contains(expected, StringComparison.Ordinal))
Fail($"{expression}: expected to contain \"{expected}\", actual {Format(actual)}");
}
public static void Empty(IEnumerable collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
{
foreach (var item in collection) Fail($"{expression}: expected empty, first item {Format(item)}");
}
public static void NotEmpty(IEnumerable collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
{
foreach (var _ in collection) return;
Fail($"{expression}: expected at least one item");
}
public static void Count(int expected, IEnumerable collection, [CallerArgumentExpression(nameof(collection))] string? expression = null)
{
var n = 0;
foreach (var _ in collection) n++;
if (n != expected) Fail($"{expression}: expected {expected} items, actual {n}");
}
public static TException Throws<TException>(Action action, [CallerArgumentExpression(nameof(action))] string? expression = null) where TException : Exception
{
try
{
action();
}
catch (TException ex)
{
return ex;
}
catch (Exception ex)
{
Fail($"{expression}: expected {typeof(TException).Name}, got {ex.GetType().Name}: {ex.Message}");
}
Fail($"{expression}: expected {typeof(TException).Name}, nothing was thrown");
return null!;
}
public static async Task<TException> ThrowsAsync<TException>(Func<Task> action, [CallerArgumentExpression(nameof(action))] string? expression = null) where TException : Exception
{
try
{
await action().ConfigureAwait(false);
}
catch (TException ex)
{
return ex;
}
catch (Exception ex)
{
Fail($"{expression}: expected {typeof(TException).Name}, got {ex.GetType().Name}: {ex.Message}");
}
Fail($"{expression}: expected {typeof(TException).Name}, nothing was thrown");
return null!;
}
public static void DoesNotThrow(Action action, [CallerArgumentExpression(nameof(action))] string? expression = null)
{
try
{
action();
}
catch (Exception ex)
{
Fail($"{expression}: unexpected {ex.GetType().Name}: {ex.Message}");
}
}
public static void FileExists(string path)
{
if (!File.Exists(path)) Fail($"File not found: {path}");
}
public static void FileMissing(string path)
{
if (File.Exists(path)) Fail($"File should not exist: {path}");
}
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
public static void Fail(string message) => throw new AssertionException(message);
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
public static void Inconclusive(string reason) => throw new SkipException(reason);
private static string Prefix(string? message) => string.IsNullOrEmpty(message) ? string.Empty : message + ": ";
private static string F(double v) => v.ToString("G6", CultureInfo.InvariantCulture);
internal static string Format(object? value) => value switch
{
null => "null",
string s => "\"" + s + "\"",
double d => F(d),
float f => F(f),
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
_ => value.ToString() ?? value.GetType().Name,
};
}