XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using XFEEDToolBox.Core.Data;
using XFEEDToolBox.Core.Models;

namespace XFEEDToolBox.Test;

internal static class Check
{
    public static void That(bool value, string message = "Assertion failed") { if (!value) throw new InvalidOperationException(message); }
    public static void Equal<T>(T expected, T actual) { if (!EqualityComparer<T>.Default.Equals(expected, actual)) throw new InvalidOperationException($"Expected {expected}, actual {actual}"); }
    public static void Throws<T>(Action action) where T : Exception
    {
        try { action(); } catch (T) { return; }
        throw new InvalidOperationException("Expected " + typeof(T).Name);
    }
    public static JsonElement Json(string text) => JsonDocument.Parse(text).RootElement.Clone();
    public static JsonElement Event(object value) => JsonSerializer.SerializeToElement(value);
    public static Station Station(long id, double x = 0) => new() { MarketId = id, Type = "Coriolis", Name = "Station " + id, SystemName = "System " + id, Position = new(x, 0, 0), MaxLandingPad = LandingPad.Large, Security = SecurityLevel.High, DistanceToArrivalLs = 300, UpdatedAt = DateTimeOffset.UtcNow };
    public static CommodityQuote Quote(long station, string key, long buy, long sell, long supply = 100, long demand = 100) => new() { MarketId = station, Commodity = key, DisplayName = key, BuyPrice = buy, SellPrice = sell, Supply = supply, Demand = demand, Source = DataSource.Journal, CollectedAt = DateTimeOffset.UtcNow };
    public static PlannerRequest Request => new() { StartMarketId = 1, ActualShipType = "Type9", CurrentPosition = new(0,0,0), CurrentMarketId = 1, Credits = 10000, ReserveCredits = 1000, CargoCapacity = 10, MaxLegs = 2, Loop = false, CostPerJump = 0, MinimumProfitPerTon = 1 };
    public static MarketBook TwoStations => new([Station(1), Station(2, 10)], [Quote(1, "gold", 100, 90), Quote(2, "gold", 200, 180), Quote(1, "silver", 200, 180), Quote(2, "silver", 100, 90)]);
}

internal sealed class TemporaryDirectory : IDisposable
{
    public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "XFEEDToolBox-tests", Guid.NewGuid().ToString("N"));
    public TemporaryDirectory() => Directory.CreateDirectory(Path);
    public string File(string name) => System.IO.Path.Combine(Path, name);
    public MarketRepository Repository() { var r = new MarketRepository(File("markets.db")); r.Initialize(); return r; }
    public void Dispose()
    {
        Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
        try { Directory.Delete(Path, true); } catch (IOException) { }
    }
}