223 lines
15 KiB
C#
223 lines
15 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using KernelPanic.Combat;
|
|
using KernelPanic.Core;
|
|
using KernelPanic.Data;
|
|
using KernelPanic.Run;
|
|
using NUnit.Framework;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class RepositoryExpansionTests
|
|
{
|
|
const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
|
|
GameObject owner;
|
|
CombatManager combat;
|
|
RunManager run;
|
|
CardDatabase cards;
|
|
UnityEngine.Random.State random;
|
|
CombatantState Player => combat.PlayerState;
|
|
CombatantState Target => combat.Enemies[0].State;
|
|
static T Asset<T>(string path) where T : UnityEngine.Object => AssetDatabase.LoadAssetAtPath<T>(path);
|
|
static void Set(object obj, string field, object value) => obj.GetType().GetField(field, Private).SetValue(obj, value);
|
|
static object Call(object obj, string method, params object[] args) => obj.GetType().GetMethod(method, Private).Invoke(obj, args);
|
|
|
|
[SetUp] public void Setup()
|
|
{
|
|
random = UnityEngine.Random.state;
|
|
cards = Asset<CardDatabase>("Assets/Data/Cards/CardDatabase.asset");
|
|
owner = new GameObject("Repository language expansion");
|
|
run = owner.AddComponent<RunManager>(); combat = owner.AddComponent<CombatManager>();
|
|
Call(combat,"OnDisable"); Call(combat,"Awake"); Call(combat,"OnEnable");
|
|
var distro = Asset<DistroDefinition>("Assets/Data/Distros/Ubuntu.asset");
|
|
var config = new RunConfig(distro,Language.C,Language.Python,distro.ExclusiveCards.Take(4).ToArray(),73,1);
|
|
run.StartRun(config); Set(combat,"_runConfig",config); Set(combat,"_runManager",run);
|
|
Set(combat,"_playerState",new CombatantState(100,32,100) { ForceMaxRolls=true });
|
|
Set(combat,"_handController",new HandController(32));
|
|
Set(combat,"currentPhase",TurnPhase.Execute); Set(combat,"_turnNumberThisWave",1);
|
|
((List<EnemyInstance>)combat.Enemies).Add(new EnemyInstance("first",10000,Array.Empty<EnemyIntent>()));
|
|
((List<EnemyInstance>)combat.Enemies).Add(new EnemyInstance("second",10000,Array.Empty<EnemyIntent>()));
|
|
combat.Python.BeginPhase(1);
|
|
}
|
|
[TearDown] public void Cleanup()
|
|
{ Call(combat,"OnDisable"); UnityEngine.Object.DestroyImmediate(owner); UnityEngine.Random.state=random; }
|
|
CardInstance Card(string id) => new(cards.FindById(id) ?? throw new Exception(id));
|
|
CardInstance Play(string id, int magnitude=0)
|
|
{
|
|
var card=Card(id); card.ApplyMagnitudeUpgrade(magnitude);
|
|
Assert.That(combat.HandController.Add(card),Is.True); combat.SelectEnemy(0);
|
|
Assert.That(combat.PlayCard(card),Is.True,id); return card;
|
|
}
|
|
void Queue() => Finish((IEnumerator)Call(combat,"InterpretQueueSequenced"));
|
|
static void Finish(IEnumerator routine)
|
|
{ int limit=2000; while(routine.MoveNext()) { Assert.That(--limit,Is.GreaterThan(0)); if(routine.Current is IEnumerator child) Finish(child); } }
|
|
void DrawPile(int count=10) => combat.DeckController.InitializeInDrawOrder(Enumerable.Range(0,count).Select(_=>Card("lang_rs_borrow")));
|
|
|
|
[Test] public void ShopAssetsHaveLanguagePrefixedNamesAndImportedDescriptions()
|
|
{
|
|
foreach(var definition in cards.AllCards.Where(card=>card.IsRunOnly))
|
|
{
|
|
string filename=System.IO.Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(definition));
|
|
Assert.That(filename,Does.StartWith(definition.Language.ToString()),definition.Id);
|
|
Assert.That(definition.name,Is.EqualTo(filename),definition.Id);
|
|
Assert.That(string.IsNullOrWhiteSpace(definition.Description),Is.False,definition.Id);
|
|
}
|
|
}
|
|
[Test] public void KernelExploitImportsItsFullDescriptionAndMetadata()
|
|
{
|
|
var definition=cards.FindById("shop_c_kernel_exploit");
|
|
Assert.That(definition.Description,Does.Contain("36 ignoring Shield").And.Contain("50% of overkill")
|
|
.And.Contain("50% segfault: uptime -8").And.Contain("corrupt a random card in hand"));
|
|
Assert.That((definition.Language,definition.CycleCost,definition.Rarity,definition.ResolutionTrack),
|
|
Is.EqualTo((Language.C,3,Rarity.Rare,ResolutionTrack.Native)));
|
|
Assert.That(AssetDatabase.GetAssetPath(definition),Is.EqualTo("Assets/Data/Cards/Shop/CKernelExploit.asset"));
|
|
Assert.That(CardEffectFactory.GetRulesText(new CardInstance(definition)),Does.Contain("36 ignoring Shield"));
|
|
}
|
|
|
|
[TestCase("shop_c_memcpy",1,0,0)] [TestCase("shop_c_memset",1,0,0)] [TestCase("shop_c_realloc",2,1,0)]
|
|
[TestCase("shop_py_islice",0,1,0)] [TestCase("shop_py_deque",1,0,0)] [TestCase("shop_py_sum",2,1,1)]
|
|
[TestCase("shop_py_suppress",1,0,1)] [TestCase("shop_py_map",1,0,1)]
|
|
[TestCase("shop_js_array_map",1,0,0)] [TestCase("shop_js_promise_resolve",0,0,0)] [TestCase("shop_js_try_catch",1,0,0)]
|
|
[TestCase("shop_js_json_parse",1,0,0)] [TestCase("shop_js_eval",2,1,0)]
|
|
[TestCase("shop_rs_vec_push",1,0,0)] [TestCase("shop_rs_result_ok",1,0,0)] [TestCase("shop_rs_option_take",0,0,0)]
|
|
[TestCase("shop_rs_mutex_lock",1,0,0)] [TestCase("shop_rs_iterator_collect",1,0,0)] [TestCase("shop_rs_match",1,1,0)]
|
|
[TestCase("shop_rs_arc_clone",1,1,0)] [TestCase("shop_rs_mem_drop",2,1,0)]
|
|
[TestCase("shop_java_arraylist_add",1,0,0)] [TestCase("shop_java_stringbuilder_append",1,0,0)]
|
|
[TestCase("shop_java_require_non_null",0,0,0)] [TestCase("shop_java_try_finally",1,0,0)]
|
|
[TestCase("shop_java_stream_map",2,0,0)] [TestCase("shop_java_class_for_name",1,1,0)]
|
|
[TestCase("shop_java_system_gc",1,1,0)] [TestCase("shop_java_jit_compile",2,1,0)]
|
|
public void NewAssetsHaveValidMetadataEffectsAndUpgradeDescriptions(string id,int cost,int rarity,int track)
|
|
{
|
|
var card=Card(id); var d=card.Definition;
|
|
Assert.That((d.CycleCost,(int)d.Rarity,(int)d.ResolutionTrack),Is.EqualTo((cost,rarity,track)));
|
|
Assert.That(d.IsRunOnly,Is.True); Assert.That(d.DistroExclusive || d.IsToken,Is.False);
|
|
var expected=id.StartsWith("shop_c_") ? Language.C : id.StartsWith("shop_py_") ? Language.Python
|
|
: id.StartsWith("shop_js_") ? Language.JavaScript : id.StartsWith("shop_rs_") ? Language.Rust : Language.Java;
|
|
Assert.That(d.Language,Is.EqualTo(expected));
|
|
Assert.That(AssetDatabase.GetAssetPath(d),Does.StartWith("Assets/Data/Cards/Shop/"));
|
|
Assert.That(CardEffectFactory.CreateEffects(d).All(e=>e is not NoOpCardEffect),Is.True);
|
|
card.ApplyMagnitudeUpgrade(2);
|
|
Assert.That(CardEffectFactory.GetRulesText(card),Does.Not.Contain("{amount").And.Not.Contain("{shield"));
|
|
Assert.That(HandController.GetRamCost(card),Is.EqualTo(expected==Language.Java ? 2 : 1));
|
|
}
|
|
[TestCase(Language.C)] [TestCase(Language.Rust)] [TestCase(Language.Python)] [TestCase(Language.JavaScript)]
|
|
[TestCase(Language.Java)] [TestCase(Language.Go)] [TestCase(Language.Haskell)]
|
|
public void EveryPlayableLanguageHasAComparableShopPoolAndUnchangedStarterSize(Language language)
|
|
{
|
|
int expected=language==Language.Haskell ? 10 : 8;
|
|
Assert.That(cards.AllCards.Count(c=>c.IsRunOnly && c.Language==language),Is.EqualTo(expected));
|
|
var decks=Asset<LanguageDeckDatabase>("Assets/Data/Cards/Languages/LanguageDeckDatabase.asset");
|
|
Assert.That(decks.FindByLanguage(language).Entries.Sum(e=>e.Count),Is.EqualTo(4));
|
|
var distro=Asset<DistroDefinition>("Assets/Data/Distros/Ubuntu.asset");
|
|
foreach(bool first in new[]{true,false})
|
|
{
|
|
var other=language==Language.C ? Language.Rust : Language.C;
|
|
run.StartRun(new RunConfig(distro,first?language:other,first?other:language,distro.ExclusiveCards.Take(4).ToArray(),73,1));
|
|
var pool=(List<CardDefinition>)Call(run,"BuildCardOfferPool",cards,decks);
|
|
Assert.That(pool.Count(c=>c.IsRunOnly && c.Language==language),Is.EqualTo(expected));
|
|
Assert.That(pool.All(c=>c.Language==language || c.Language==other),Is.True);
|
|
}
|
|
}
|
|
[TestCase("shop_c_memcpy",6,false)] [TestCase("shop_c_memset",4,true)] [TestCase("shop_c_realloc",12,false)]
|
|
[TestCase("shop_js_array_map",5,true)] [TestCase("shop_js_promise_resolve",4,false)]
|
|
[TestCase("shop_js_json_parse",8,false)] [TestCase("shop_js_eval",30,false)]
|
|
[TestCase("shop_rs_vec_push",7,false)] [TestCase("shop_rs_result_ok",4,false)] [TestCase("shop_rs_option_take",3,false)]
|
|
[TestCase("shop_rs_match",5,false)] [TestCase("shop_rs_mem_drop",14,false)]
|
|
[TestCase("shop_java_arraylist_add",5,false)] [TestCase("shop_java_stringbuilder_append",5,false)]
|
|
[TestCase("shop_java_stream_map",4,true)] [TestCase("shop_java_jit_compile",8,false)]
|
|
public void NativeAttacksResolveMatchingPreviewAndOneMagnitudeBonus(string id,int damage,bool area)
|
|
{
|
|
var card=Card(id); card.ApplyMagnitudeUpgrade(2); combat.HandController.Add(card);
|
|
var preview=CardPreviewService.Build(combat,card,Target);
|
|
Assert.That(preview.MinimumDamage,Is.EqualTo(damage+2));
|
|
combat.SelectEnemy(0); Assert.That(combat.PlayCard(card),Is.True);
|
|
Assert.That(Target.CurrentUptime,Is.EqualTo(10000-damage-2));
|
|
Assert.That(combat.Enemies[1].State.CurrentUptime,Is.EqualTo(area?10000-damage-2:10000));
|
|
}
|
|
[TestCase("shop_js_array_map",2,5)] [TestCase("shop_js_promise_resolve",1,4)]
|
|
[TestCase("shop_js_json_parse",3,8)] [TestCase("shop_js_eval",0,30)]
|
|
public void JavaScriptShowsItsFullNormalRangeWithoutRolling(string id,int minimum,int maximum)
|
|
{
|
|
Player.ForceMaxRolls=false; int draws=RandomRoll.DrawCount;
|
|
var preview=CardPreviewService.Build(combat,Card(id),Target);
|
|
Assert.That(preview.MinimumDamage,Is.EqualTo(minimum)); Assert.That(preview.NormalMaximumDamage,Is.EqualTo(maximum));
|
|
Assert.That(RandomRoll.DrawCount,Is.EqualTo(draws));
|
|
}
|
|
[TestCase("shop_js_try_catch",5)] [TestCase("shop_rs_mutex_lock",7)] [TestCase("shop_rs_result_ok",3)]
|
|
[TestCase("shop_java_require_non_null",3)] [TestCase("shop_java_try_finally",5)] [TestCase("shop_java_system_gc",4)]
|
|
[TestCase("shop_py_deque",3)]
|
|
public void ShieldEffectsApplyMagnitudeOnce(string id,int shield)
|
|
{ Play(id,2); Assert.That(Player.Shield,Is.EqualTo(shield+2)); }
|
|
[TestCase("shop_c_memcpy",1)] [TestCase("shop_js_json_parse",1)] [TestCase("shop_rs_iterator_collect",2)]
|
|
[TestCase("shop_java_arraylist_add",1)] [TestCase("shop_java_try_finally",1)] [TestCase("shop_java_class_for_name",2)]
|
|
[TestCase("shop_py_deque",1)]
|
|
public void FixedDrawDoesNotGrowWithMagnitude(string id,int draw)
|
|
{ DrawPile(); Play(id,10); Assert.That(combat.HandController.Cards.Count,Is.EqualTo(draw)); }
|
|
[TestCase("shop_rs_match","lang_rs_borrow")] [TestCase("shop_java_stringbuilder_append","shop_java_require_non_null")]
|
|
public void SameLanguageCombosUseThePreviousAcceptedPlay(string id,string setup)
|
|
{ Play(setup); int before=Target.CurrentUptime; Play(id); Assert.That(before-Target.CurrentUptime,Is.EqualTo(9)); }
|
|
[TestCase("shop_c_realloc")] [TestCase("shop_java_system_gc")]
|
|
public void RepairCardsFixExactlyTwoCorruptedCards(string id)
|
|
{
|
|
var broken=Enumerable.Range(0,3).Select(_=>Card("lang_rs_borrow")).ToArray();
|
|
foreach(var card in broken) { card.IsBroken=true; combat.HandController.Add(card); }
|
|
Play(id); Assert.That(broken.Count(c=>c.IsBroken),Is.EqualTo(1));
|
|
}
|
|
[Test] public void TryCatchCleansesAndArcCloneCopiesAnIndependentCard()
|
|
{
|
|
new StatusEffectController().Apply(Player,StatusType.MemoryLeak,2,2);
|
|
Play("shop_js_try_catch"); Assert.That(Player.Statuses,Is.Empty);
|
|
var original=Card("shop_rs_option_take"); original.ApplyMagnitudeUpgrade(2); combat.HandController.Add(original);
|
|
Play("shop_rs_arc_clone"); var copy=combat.HandController.Cards.Single(c=>c!=original);
|
|
Assert.That(copy.Definition,Is.SameAs(original.Definition)); Assert.That(copy.InstanceId,Is.Not.EqualTo(original.InstanceId));
|
|
Assert.That(copy.MagnitudeBonus,Is.EqualTo(2)); copy.ApplyMagnitudeUpgrade(1); Assert.That(original.MagnitudeBonus,Is.EqualTo(2));
|
|
}
|
|
[TestCase("shop_rs_vec_push",4,0)] [TestCase("shop_java_arraylist_add",0,2)]
|
|
public void RustOverkillAndJavaSpillKeepTheirLanguageRules(string id,int shield,int spill)
|
|
{
|
|
var other=combat.Enemies[1].State;
|
|
Target.CurrentUptime=3; Play(id); Assert.That(Player.Shield,Is.EqualTo(shield));
|
|
Assert.That(other.CurrentUptime,Is.EqualTo(10000-spill));
|
|
}
|
|
[Test] public void JavaSupportWarmsJitAndCompileAddsOnlyATurnDiscount()
|
|
{
|
|
var attack=Card("shop_java_stream_map"); Assert.That(combat.GetDisplayedPlayCost(attack),Is.EqualTo(2));
|
|
Play("shop_java_require_non_null"); Play("shop_java_try_finally");
|
|
Assert.That(combat.GetDisplayedPlayCost(attack),Is.EqualTo(1));
|
|
Play("shop_java_jit_compile"); Assert.That(combat.GetDisplayedPlayCost(attack),Is.Zero);
|
|
Set(combat,"_javaCardsDiscountThisTurn",0); Assert.That(combat.GetDisplayedPlayCost(attack),Is.EqualTo(1));
|
|
}
|
|
[Test] public void IsliceSelectsACardWithoutReorderingTheOthers()
|
|
{
|
|
var top=new[]{Card("lang_rs_borrow"),Card("lang_c_malloc"),Card("lang_py_print")};
|
|
combat.DeckController.InitializeInDrawOrder(top); Play("shop_py_islice");
|
|
Assert.That(combat.Python.Choices.Count,Is.EqualTo(3)); Assert.That(combat.Python.ChoiceTitle,Does.StartWith("itertools.islice()"));
|
|
Assert.That(combat.Python.Choose(1),Is.True); Assert.That(combat.HandController.Cards.Single(),Is.SameAs(top[1]));
|
|
Assert.That(combat.DeckController.PeekTop(2),Is.EqualTo(new[]{top[0],top[2]}));
|
|
}
|
|
[TestCase("shop_py_sum",10)] [TestCase("shop_py_map",3)]
|
|
public void NewPayloadsUseLoopAndZipWhileAutomationCapturesOnlyBaseDamage(string id,int damage)
|
|
{
|
|
DrawPile(); Play("shop_py_async_def"); Play("lang_py_for_loop"); Play("shop_py_zip");
|
|
var card=Play(id); int hand=combat.HandController.Cards.Count;
|
|
Assert.That(CardPreviewService.Build(combat,card,Target).MinimumDamage,Is.EqualTo(2*damage));
|
|
Queue(); Assert.That(Target.CurrentUptime,Is.EqualTo(10000-2-2*damage));
|
|
Assert.That(combat.Enemies[1].State.CurrentUptime,Is.EqualTo(10000-2*damage));
|
|
int draws=hand+(id=="shop_py_map"?1:0); Assert.That(combat.HandController.Cards.Count,Is.EqualTo(draws));
|
|
combat.Python.BeginPhase(2); Assert.That(Target.CurrentUptime,Is.EqualTo(10000-2-3*damage));
|
|
Assert.That(combat.Enemies[1].State.CurrentUptime,Is.EqualTo(10000-2*damage));
|
|
Assert.That(combat.HandController.Cards.Count,Is.EqualTo(draws),"Automation must not replay draw.");
|
|
}
|
|
[Test] public void ShieldPayloadRepeatsAndReplaysWithoutConsumingZipOrMarkingDamage()
|
|
{
|
|
Play("shop_py_async_def"); Play("lang_py_for_loop"); Play("shop_py_zip"); var shield=Play("shop_py_suppress",2);
|
|
Assert.That(CardPreviewService.Build(combat,shield,Target).Shield,Is.EqualTo(18));
|
|
Queue(); Assert.That(Player.Shield,Is.EqualTo(20)); Assert.That(combat.Python.AreaCount,Is.EqualTo(1));
|
|
combat.Python.BeginPhase(2); Assert.That(Player.Shield,Is.EqualTo(29)); Assert.That(combat.Python.DamageThisPhase,Is.False);
|
|
var cinnamon=Card("mint_cinnamon"); Assert.That(CardPreviewService.Build(combat,cinnamon).Shield,Is.EqualTo(3));
|
|
}
|
|
}
|