using System; using Blacklight.Core.Keyboard; using UnitTestSharp; namespace Blacklight.Core.UnitTests.Keyboard { public class KeyStateTests : TestFixture { public class EqualityTests : EqualityTestFixture { public EqualityTests() : base(new KeyState(KeyCode.A, KeyCode.Numpad0)) { } } public class EqualityOperatorTests : TestFixture { public void EqualStates_AreEqual() { Check(new KeyState(KeyCode.A) == new KeyState(KeyCode.A)); } public void DifferentStates_AreNotEqual() { Check(new KeyState(KeyCode.A) != new KeyState(KeyCode.B)); } public void EqualStates_HaveEqualHashCodes() { CheckEqual(new KeyState(KeyCode.A).GetHashCode(), new KeyState(KeyCode.A).GetHashCode()); } } public class CtorTests : TestFixture { public void SingleKey_SetsIt() { Check(new KeyState(KeyCode.A).IsSet(KeyCode.A)); } public void MultipleKeys_SetsThemAll() { KeyState state = new KeyState(KeyCode.A, KeyCode.R, KeyCode.Y, KeyCode.Period); Check(state.IsSet(KeyCode.A)); Check(state.IsSet(KeyCode.R)); Check(state.IsSet(KeyCode.Y)); Check(state.IsSet(KeyCode.Period)); } public void NoKeys_IsEmpty() { CheckEqual(default(KeyState), new KeyState()); } public void SingleKeyOverloadAndParamsOverload_AgreeForOneKey() { CheckEqual(new KeyState(KeyCode.A), new KeyState(new[] { KeyCode.A })); } public void LowAndHighOverload_SetsThemDirectly() { KeyState state = new KeyState(5UL, 9UL); CheckEqual(5UL, state.Low); CheckEqual(9UL, state.High); } } public class LowTests : TestFixture { public void KeyBelow64_SetsTheMatchingBit() { CheckEqual(1UL, new KeyState(KeyCode.A).Low); } public void KeyAt64OrAbove_LeavesItZero() { CheckEqual(0UL, new KeyState(KeyCode.NumpadEnter).Low); } } public class HighTests : TestFixture { public void KeyAt64OrAbove_SetsTheMatchingBit() { CheckNotEqual(0UL, new KeyState(KeyCode.NumpadEnter).High); } public void KeyBelow64_LeavesItZero() { CheckEqual(0UL, new KeyState(KeyCode.A).High); } } public class IsSetTests : TestFixture { public void EveryKeyCode_IsSetOnlyForItself() { foreach (KeyCode expected in Enum.GetValues(typeof(KeyCode))) { KeyState state = new KeyState(expected); foreach (KeyCode other in Enum.GetValues(typeof(KeyCode))) { CheckEqual(other == expected, state.IsSet(other)); } } } } public class WithSetTests : TestFixture { public void SetsTheKey() { Check(default(KeyState).WithSet(KeyCode.A).IsSet(KeyCode.A)); } public void LeavesOtherKeysAlone() { KeyState state = new KeyState(KeyCode.A).WithSet(KeyCode.B); Check(state.IsSet(KeyCode.A)); } public void DoesNotMutateTheOriginal() { KeyState original = default(KeyState); original.WithSet(KeyCode.A); CheckFalse(original.IsSet(KeyCode.A)); } } public class WithClearedTests : TestFixture { public void ClearsAKeyThatWasSet() { CheckFalse(new KeyState(KeyCode.A).WithCleared(KeyCode.A).IsSet(KeyCode.A)); } public void KeyThatWasNeverSet_StaysUnset() { CheckFalse(default(KeyState).WithCleared(KeyCode.A).IsSet(KeyCode.A)); } public void LeavesOtherKeysAlone() { KeyState state = new KeyState(KeyCode.A, KeyCode.B).WithCleared(KeyCode.B); Check(state.IsSet(KeyCode.A)); } public void DoesNotMutateTheOriginal() { KeyState original = new KeyState(KeyCode.A); original.WithCleared(KeyCode.A); Check(original.IsSet(KeyCode.A)); } } public class ContainsTests : TestFixture { public void AllChordKeysSet_IsTrue() { KeyState state = new KeyState(KeyCode.A, KeyCode.R, KeyCode.Y, KeyCode.Period); KeyState chord = new KeyState(KeyCode.A, KeyCode.R); Check(state.Contains(chord)); } public void OneChordKeyMissing_IsFalse() { KeyState state = new KeyState(KeyCode.A, KeyCode.R); KeyState chord = new KeyState(KeyCode.A, KeyCode.R, KeyCode.Y); CheckFalse(state.Contains(chord)); } public void EmptyChord_IsAlwaysTrue() { Check(new KeyState(KeyCode.A).Contains(default(KeyState))); Check(default(KeyState).Contains(default(KeyState))); } } public class ContainsAnyTests : TestFixture { public void OneOfTheKeysSet_IsTrue() { Check(new KeyState(KeyCode.ShiftRight).ContainsAny(KeyState.Shift)); } public void NoneOfTheKeysSet_IsFalse() { CheckFalse(new KeyState(KeyCode.A).ContainsAny(KeyState.Shift)); } public void EmptySet_IsAlwaysFalse() { CheckFalse(new KeyState(KeyCode.A).ContainsAny(default(KeyState))); } } public class AndOperatorTests : TestFixture { public void KeepsOnlyKeysInBoth() { KeyState a = new KeyState(KeyCode.A, KeyCode.B); KeyState b = new KeyState(KeyCode.B, KeyCode.C); CheckEqual(new KeyState(KeyCode.B), a & b); } public void NeitherSet_IsEmpty() { CheckEqual(default(KeyState), new KeyState(KeyCode.A) & new KeyState(KeyCode.B)); } } public class OrOperatorTests : TestFixture { public void UnionsBothSets() { KeyState a = new KeyState(KeyCode.A); KeyState b = new KeyState(KeyCode.B); CheckEqual(new KeyState(KeyCode.A, KeyCode.B), a | b); } } public class XorOperatorTests : TestFixture { public void KeepsOnlyKeysInExactlyOneSet() { KeyState a = new KeyState(KeyCode.A, KeyCode.B); KeyState b = new KeyState(KeyCode.B, KeyCode.C); CheckEqual(new KeyState(KeyCode.A, KeyCode.C), a ^ b); } } public class NotOperatorTests : TestFixture { public void FlipsASetKeyToUnset() { CheckFalse((~new KeyState(KeyCode.A)).IsSet(KeyCode.A)); } public void FlipsAnUnsetKeyToSet() { Check((~new KeyState(KeyCode.A)).IsSet(KeyCode.B)); } } public class EdgeDetectionTests : TestFixture { public void RisingEdge_FindsOnlyTheNewlySetKey() { KeyState previous = new KeyState(KeyCode.A); KeyState current = new KeyState(KeyCode.A, KeyCode.B); KeyState pressed = current & ~previous; Check(pressed.IsSet(KeyCode.B)); CheckFalse(pressed.IsSet(KeyCode.A)); } public void FallingEdge_FindsOnlyTheClearedKey() { KeyState previous = new KeyState(KeyCode.A, KeyCode.B); KeyState current = new KeyState(KeyCode.A); KeyState released = previous & ~current; Check(released.IsSet(KeyCode.B)); CheckFalse(released.IsSet(KeyCode.A)); } } public class AggregatesTests : TestFixture { public void Shift_IsEitherShiftKey() { Check(new KeyState(KeyCode.ShiftLeft).ContainsAny(KeyState.Shift)); Check(new KeyState(KeyCode.ShiftRight).ContainsAny(KeyState.Shift)); CheckFalse(new KeyState(KeyCode.A).ContainsAny(KeyState.Shift)); } public void Control_IsEitherControlKey() { Check(new KeyState(KeyCode.ControlLeft).ContainsAny(KeyState.Control)); Check(new KeyState(KeyCode.ControlRight).ContainsAny(KeyState.Control)); } public void Alt_IsEitherAltKey() { Check(new KeyState(KeyCode.AltLeft).ContainsAny(KeyState.Alt)); Check(new KeyState(KeyCode.AltRight).ContainsAny(KeyState.Alt)); } public void Enter_IsEitherEnterKey() { Check(new KeyState(KeyCode.MainEnter).ContainsAny(KeyState.Enter)); Check(new KeyState(KeyCode.NumpadEnter).ContainsAny(KeyState.Enter)); } public void AggregatesDoNotOverlapEachOther() { CheckEqual(default(KeyState), KeyState.Shift & KeyState.Control); CheckEqual(default(KeyState), KeyState.Control & KeyState.Alt); CheckEqual(default(KeyState), KeyState.Alt & KeyState.Enter); } } } }