Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions S1API.Tests/Trash/TrashContainerApiCompatibilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Reflection;
using S1API.Trash;

namespace S1API.Tests.Trash;

public sealed class TrashContainerApiCompatibilityTests
{
[Fact]
public void WrapperSurfaceUsesManagedTypes()
{
Assert.True(typeof(TrashContainer).IsSealed);
Assert.Equal(typeof(int), GetProperty(nameof(TrashContainer.Capacity)).PropertyType);
Assert.Equal(typeof(int), GetProperty(nameof(TrashContainer.Level)).PropertyType);
Assert.Equal(typeof(float), GetProperty(nameof(TrashContainer.NormalizedLevel)).PropertyType);
Assert.Equal(
typeof(IReadOnlyList<TrashContentEntry>),
GetProperty(nameof(TrashContainer.Contents)).PropertyType);
Assert.Equal(typeof(bool), GetProperty(nameof(TrashContainer.CanBeBagged)).PropertyType);
Assert.Equal(typeof(Action<string>), GetEvent(nameof(TrashContainer.OnTrashAdded)).EventHandlerType);
Assert.Equal(typeof(Action), GetEvent(nameof(TrashContainer.OnTrashLevelChanged)).EventHandlerType);
Assert.Equal(typeof(bool), GetMethod(nameof(TrashContainer.TryBagTrash)).ReturnType);
}

[Fact]
public void ContentEntryIsAnImmutableManagedValue()
{
Assert.True(typeof(TrashContentEntry).IsValueType);
Assert.True(typeof(TrashContentEntry).IsDefined(typeof(System.Runtime.CompilerServices.IsReadOnlyAttribute)));

PropertyInfo[] properties = typeof(TrashContentEntry).GetProperties(BindingFlags.Instance | BindingFlags.Public);
Assert.Equal(4, properties.Length);
Assert.All(properties, property => Assert.Null(property.SetMethod));
Assert.DoesNotContain(
typeof(TrashContentEntry).GetFields(BindingFlags.Instance | BindingFlags.Public),
field => !field.IsInitOnly);
}

[Fact]
public void ConstructionDoesNotExposeNativeTypes()
{
Assert.Empty(typeof(TrashContainer).GetConstructors(BindingFlags.Instance | BindingFlags.Public));
Assert.Empty(typeof(TrashContentEntry).GetConstructors(BindingFlags.Instance | BindingFlags.Public));
}

private static PropertyInfo GetProperty(string name) =>
typeof(TrashContainer).GetProperty(name, BindingFlags.Instance | BindingFlags.Public)
?? throw new InvalidOperationException($"Missing public property {name}.");

private static EventInfo GetEvent(string name) =>
typeof(TrashContainer).GetEvent(name, BindingFlags.Instance | BindingFlags.Public)
?? throw new InvalidOperationException($"Missing public event {name}.");

private static MethodInfo GetMethod(string name) =>
typeof(TrashContainer).GetMethod(name, BindingFlags.Instance | BindingFlags.Public)
?? throw new InvalidOperationException($"Missing public method {name}.");
}
40 changes: 40 additions & 0 deletions S1API.Tests/Trash/TrashContainerApiCompileFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using S1API.Trash;
using UnityEngine;

namespace S1API.Tests.Trash;

internal static class TrashContainerApiCompileFixture
{
internal static void ReadAndSubscribe(GameObject gameObject, TrashContainer container)
{
TrashContainer? existing = TrashContainer.FromGameObject(gameObject);
TrashContainer[] containers = TrashContainer.FindInScene(includeInactive: true);
GameObject? owner = container.GameObject;
int capacity = container.Capacity;
int level = container.Level;
float normalizedLevel = container.NormalizedLevel;
IReadOnlyList<TrashContentEntry> contents = container.Contents;
bool canBeBagged = container.CanBeBagged;

Action<string> trashAdded = _ => { };
Action levelChanged = () => { };
container.OnTrashAdded += trashAdded;
container.OnTrashLevelChanged += levelChanged;
container.OnTrashAdded -= trashAdded;
container.OnTrashLevelChanged -= levelChanged;

bool bagged = container.TryBagTrash();

_ = existing;
_ = containers;
_ = owner;
_ = capacity;
_ = level;
_ = normalizedLevel;
_ = contents;
_ = canBeBagged;
_ = bagged;
}
}
Loading
Loading