Fixes for unit tests

This commit is contained in:
Deukhoofd 2025-06-07 11:34:37 +02:00
parent 273d26057a
commit af0126e413
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 26 additions and 5 deletions

View File

@ -55,7 +55,7 @@ public static class AbilityDataLoader
var effect = serialized.Effect; var effect = serialized.Effect;
var parameters = serialized.Parameters.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter()); var parameters = serialized.Parameters.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter());
StringKey? effectName = effect == null ? null! : new StringKey(effect); var effectName = string.IsNullOrWhiteSpace(effect) ? (StringKey?)null : new StringKey(effect);
var flags = serialized.Flags.Select(x => new StringKey(x)).ToImmutableHashSet(); var flags = serialized.Flags.Select(x => new StringKey(x)).ToImmutableHashSet();

View File

@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;
namespace PkmnLib.Static.Utils; namespace PkmnLib.Static.Utils;
/// <summary> /// <summary>
@ -26,8 +28,18 @@ public readonly record struct StringKey
/// <summary> /// <summary>
/// Converts a <see cref="string"/> to a <see cref="StringKey"/>. /// Converts a <see cref="string"/> to a <see cref="StringKey"/>.
/// </summary> /// </summary>
[return: NotNullIfNotNull("key")]
public static implicit operator StringKey?(string? key) =>
string.IsNullOrWhiteSpace(key) ? null! : new StringKey(key);
/// <summary>
/// Converts a <see cref="string"/> to a <see cref="StringKey"/>.
/// Throws an <see cref="ArgumentException"/> if the key is null or whitespace.
/// </summary>
public static implicit operator StringKey(string key) => public static implicit operator StringKey(string key) =>
string.IsNullOrWhiteSpace(key) ? default : new StringKey(key); string.IsNullOrWhiteSpace(key)
? throw new ArgumentException("Key cannot be null or whitespace.", nameof(key))
: new StringKey(key);
/// <inheritdoc cref="string.ToString()"/> /// <inheritdoc cref="string.ToString()"/>
public override string ToString() => _key.ToLowerInvariant(); public override string ToString() => _key.ToLowerInvariant();

View File

@ -24,7 +24,7 @@ public class AbilityDataTests
} }
} }
[Test, MethodDataSource(nameof(AllAbilitiesHaveValidScriptsData)), Explicit] [Test, MethodDataSource(nameof(AllAbilitiesHaveValidScriptsData))]
public async Task AllAbilitiesEffectsHaveValidScripts(TestCaseData test) public async Task AllAbilitiesEffectsHaveValidScripts(TestCaseData test)
{ {
var scriptName = test.Ability.Effect; var scriptName = test.Ability.Effect;
@ -39,7 +39,7 @@ public class AbilityDataTests
catch (Exception e) catch (Exception e)
{ {
// Helper method to find the line number of the effect in the JSON file // Helper method to find the line number of the effect in the JSON file
var file = Path.GetFullPath("../../../../Plugins/PkmnLib.Plugin.Gen7/Data/Abilities.json"); var file = Path.GetFullPath("../../../../Plugins/PkmnLib.Plugin.Gen7/Data/Abilities.jsonc");
var json = await File.ReadAllLinesAsync(file); var json = await File.ReadAllLinesAsync(file);
var moveLineNumber = json.Select((line, index) => new { line, index }) var moveLineNumber = json.Select((line, index) => new { line, index })
.FirstOrDefault(x => x.line.Contains($"\"name\": \"{test.Ability.Effect}\""))?.index + 1; .FirstOrDefault(x => x.line.Contains($"\"name\": \"{test.Ability.Effect}\""))?.index + 1;

View File

@ -72,7 +72,8 @@ public class MultiAttackTests
user.Library.Returns(dynamicLibrary); user.Library.Returns(dynamicLibrary);
dynamicLibrary.StaticLibrary.Returns(staticLibrary); dynamicLibrary.StaticLibrary.Returns(staticLibrary);
staticLibrary.Types.Returns(typeLibrary); staticLibrary.Types.Returns(typeLibrary);
item.Name.Returns((StringKey)test.ItemName!); if (test.ItemName != null)
item.Name.Returns(new StringKey(test.ItemName));
move.User.Returns(user); move.User.Returns(user);
if (test.ItemName != null) if (test.ItemName != null)

View File

@ -1,5 +1,13 @@
namespace PkmnLib.Plugin.Gen7.Scripts.Abilities; namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
/// <summary>
/// PreventCritical is a generic ability that prevents the user from being hit by critical hits.
/// This ability completely blocks any incoming critical hits from opposing Pokémon.
/// This ability is used by abilities like Battle Armor and Shell Armor.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Battle_Armor_(Ability)">Bulbapedia - Battle Armor</see>
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Shell_Armor_(Ability)">Bulbapedia - Shell Armor</see>
/// </summary>
[Script(ScriptCategory.Ability, "prevent_critical")] [Script(ScriptCategory.Ability, "prevent_critical")]
public class PreventCritical : Script public class PreventCritical : Script
{ {