using System.Collections.Generic; using UnitTestSharp; namespace Clave.Glass.UnitTests { public class SavedKeyBindingsTests : TestFixture { public class RoundTripTests : TestFixture { public void KeepsEveryChangedAction() { var setup = new KeyBindingsEditorTests.Setup(); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, Chord.OnPress(KeyCode.W).WithAnyHeld(KeyState.Control)); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); var loaded = new KeyBindingsEditorTests.Setup(); loaded.Editor.Load(KeyBindingsEditorTests.RoundTripped(setup.Editor.Save())); CheckEqual(Chord.OnPress(KeyCode.W).WithAnyHeld(KeyState.Control).Formula, loaded.Bindings.ChordsOf(loaded.Game.Jump)[0].Formula); CheckEqual(0, loaded.Bindings.ChordsOf(loaded.Game.Fire).Count); } } public class FollowsTests : TestFixture { public void ChordNamedForItsAction_Follows() { Chord chord = Chord.OnPress(KeyCode.K); SavedKeyBindings saved = Jumping(chord); saved.FollowsKind["game"] = new Dictionary> { { "jump", new List { Chord.OnPress(KeyCode.K) } }, }; CheckTrue(saved.Follows("game", "jump", chord)); } public void ChordNotNamed_DoesNotFollow() { CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Follows("game", "jump", Chord.OnPress(KeyCode.K))); } public void ChordNamedForAnotherAction_DoesNotFollow() { SavedKeyBindings saved = Jumping(Chord.OnPress(KeyCode.K)); saved.FollowsKind["game"] = new Dictionary> { { "fire", new List { Chord.OnPress(KeyCode.K) } }, }; CheckFalse(saved.Follows("game", "jump", Chord.OnPress(KeyCode.K))); } public void MissingTables_MeanNoChordFollows() { SavedKeyBindings saved = Jumping(Chord.OnPress(KeyCode.K)); saved.FollowsKind = null; CheckFalse(saved.Follows("game", "jump", Chord.OnPress(KeyCode.K))); saved.FollowsKind = new Dictionary>> { { "game", null } }; CheckFalse(saved.Follows("game", "jump", Chord.OnPress(KeyCode.K))); saved.FollowsKind["game"] = new Dictionary> { { "jump", null } }; CheckFalse(saved.Follows("game", "jump", Chord.OnPress(KeyCode.K))); } } public static SavedKeyBindings Jumping(params Chord[] chords) { var returnMe = new SavedKeyBindings(); returnMe.Sources["game"] = new Dictionary> { { "jump", new List(chords) } }; return returnMe; } public class MatchesTests : TestFixture { public void DistinctChordsOnTheSameKeys_Match() { CheckTrue(Jumping(Chord.OnPress(KeyCode.K)).Matches(Jumping(Chord.OnPress(KeyCode.K)))); } public void NoBindings_MatchNoBindings() { CheckTrue(new SavedKeyBindings().Matches(new SavedKeyBindings())); } public void ADifferentKey_DoesNotMatch() { CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(Jumping(Chord.OnPress(KeyCode.L)))); } public void TheSameChordsInAnotherOrder_DoNotMatch() { SavedKeyBindings first = Jumping(Chord.OnPress(KeyCode.K), Chord.OnPress(KeyCode.L)); CheckFalse(first.Matches(Jumping(Chord.OnPress(KeyCode.L), Chord.OnPress(KeyCode.K)))); } public void AnotherChordCount_DoesNotMatch() { SavedKeyBindings first = Jumping(Chord.OnPress(KeyCode.K)); CheckFalse(first.Matches(Jumping(Chord.OnPress(KeyCode.K), Chord.OnPress(KeyCode.L)))); } public void AnotherAction_DoesNotMatch() { SavedKeyBindings other = Jumping(Chord.OnPress(KeyCode.K)); other.Sources["game"] = new Dictionary> { { "fire", new List { Chord.OnPress(KeyCode.K) } }, }; CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(other)); } public void AnExtraAction_DoesNotMatch() { SavedKeyBindings other = Jumping(Chord.OnPress(KeyCode.K)); other.Sources["game"]["fire"] = new List(); CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(other)); } public void AnotherSource_DoesNotMatch() { SavedKeyBindings other = new SavedKeyBindings(); other.Sources["menu"] = Jumping(Chord.OnPress(KeyCode.K)).Sources["game"]; CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(other)); } public void AnExtraSource_DoesNotMatch() { SavedKeyBindings other = Jumping(Chord.OnPress(KeyCode.K)); other.Sources["menu"] = new Dictionary>(); CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(other)); } public void AChordFollowingTheKindInOnlyOne_DoesNotMatch() { Chord chord = Chord.OnPress(KeyCode.K); SavedKeyBindings following = Jumping(chord); following.FollowsKind["game"] = new Dictionary> { { "jump", new List { chord } }, }; CheckFalse(following.Matches(Jumping(Chord.OnPress(KeyCode.K)))); CheckFalse(Jumping(Chord.OnPress(KeyCode.K)).Matches(following)); } public void Null_DoesNotMatch() { CheckFalse(new SavedKeyBindings().Matches(null)); } } public class ToTextTests : TestFixture { public void ReadsBackThroughTryFromText() { var saved = new SavedKeyBindings(); saved.Sources["game"] = new Dictionary> { { "jump", new List { Chord.OnPress(KeyCode.K) } }, }; CheckTrue(SavedKeyBindings.TryFromText(saved.ToText(), out SavedKeyBindings loaded)); CheckEqual(Chord.OnPress(KeyCode.K).Formula, loaded.Sources["game"]["jump"][0].Formula); } public void NamesTheChordsInWords() { var saved = new SavedKeyBindings(); saved.Sources["game"] = new Dictionary> { { "jump", new List { Chord.OnPress(KeyCode.K) } }, }; CheckTrue(saved.ToText().Contains("Pressed(K)")); } } public class TryFromTextTests : TestFixture { public void TextThatIsNoSave_IsRefused() { CheckFalse(SavedKeyBindings.TryFromText("this is not lua {", out SavedKeyBindings saved)); CheckNull(saved); } public void SaveWithoutBindings_IsRefused() { CheckFalse(SavedKeyBindings.TryFromText("return { other = \"x\" }", out SavedKeyBindings saved)); } public void SaveWhoseBindingsAreNoTable_IsRefused() { CheckFalse(SavedKeyBindings.TryFromText("return { keyBindings = \"x\" }", out SavedKeyBindings saved)); CheckNull(saved); } public void Null_IsRefused() { CheckFalse(SavedKeyBindings.TryFromText(null, out SavedKeyBindings saved)); } public void TextWithNoFollowingChords_LoadsWithNoneFollowing() { string text = "return { keyBindings = { Sources = { entries = { game = { entries = { " + "jump = { { \"Pressed(K)\" } } } } } } } }"; CheckTrue(SavedKeyBindings.TryFromText(text, out SavedKeyBindings saved)); CheckFalse(saved.Follows("game", "jump", Chord.OnPress(KeyCode.K))); } public void ChordThatFollowsTheKind_ReadsBackFollowing() { Chord chord = Chord.OnPress(KeyCode.K); SavedKeyBindings original = Jumping(chord); original.FollowsKind["game"] = new Dictionary> { { "jump", new List { chord } }, }; CheckTrue(SavedKeyBindings.TryFromText(original.ToText(), out SavedKeyBindings loaded)); CheckTrue(loaded.Follows("game", "jump", loaded.Sources["game"]["jump"][0])); } public void ChordThatDoesNotLoad_IsLeftOut() { string text = "return { keyBindings = { Sources = { entries = { game = { entries = { " + "jump = { { \"Nonsense(Q)\" }, { \"Pressed(K)\" } } } } } } } }"; CheckTrue(SavedKeyBindings.TryFromText(text, out SavedKeyBindings saved)); CheckEqual(1, saved.Sources["game"]["jump"].Count); CheckEqual(Chord.OnPress(KeyCode.K).Formula, saved.Sources["game"]["jump"][0].Formula); } } } }