public void SetValueSetsCorrectDataBits() { //arrange var enabled = new PinStub(1); var data = Substitute.For <IPinInterface>(); var shift = new PinStub(2); var output = new PinStub(3); var clear = new PinStub(4); var register = new ShiftRegister(enabled, data, shift, output, clear); //act register.SetValue(146); //assert var calls = data.ReceivedCalls().ToArray(); Assert.Equal(PowerValue.On, calls[0].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[1].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[2].GetArguments()[0]); Assert.Equal(PowerValue.On, calls[3].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[4].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[5].GetArguments()[0]); Assert.Equal(PowerValue.On, calls[6].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[7].GetArguments()[0]); }
public void SendByteArraySendsCorrectPowerValues() { //arrange var data = Substitute.For <IPinInterface>(); var clock = new PinStub(2); var input = new InputStub(data, clock); var bytesIn = Encoding.ASCII.GetBytes("Abc123"); //act input.Send(bytesIn); //assert var bit = data.ReceivedCalls() .Select(c => c.GetArguments()[0]).Cast <PowerValue>() .Select(p => p == PowerValue.On ? (byte)1 : (byte)0) .ToArray(); var bytesOut = new byte[6]; for (var byteNum = 0; byteNum < bytesOut.Length; byteNum++) { byte byteSum = 0; for (var bitNum = 0; bitNum < 8; bitNum++) { byteSum += (byte)(bit[8 * byteNum + bitNum] << (7 - bitNum)); } bytesOut[byteNum] = byteSum; } var chars = Encoding.ASCII.GetChars(bytesOut); Assert.Equal("Abc123", chars); }
private static PinStub[] StubPins() { const byte numPins = 16; var pins = new PinStub[numPins + 1]; for (byte x = 1; x <= numPins; x++) { pins[x] = new PinStub(x); } return(pins); }
public void CanSendPowerValue() { //arrange var data = Substitute.For <IPinInterface>(); var clock = new PinStub(2); var input = new InputStub(data, clock); //act input.Send(PowerValue.On); //assert data.Received().Power = PowerValue.On; }
public async Task TurnCounterclockwiseForSetsDirection() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise); //act await motor.TurnCounterclockwiseFor(TimeSpan.Zero); //assert Assert.Equal(Motor.Rotation.Counterclockwise, motor.Direction); }
public async Task RunForStopsWhenDone() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise); //act await motor.RunFor(TimeSpan.Zero); //assert Assert.Equal(PowerValue.Off, clockwise.Power); Assert.Equal(PowerValue.Off, counterclockwise.Power); }
public void OnDecreasePerformsActionWhenSet() { //arrange var increasePin = new PinStub(1); var decreasePin = new PinStub(2); var dial = new RotaryEncoder(increasePin, decreasePin); var called = false; dial.OnDecrease(() => called = true); //act decreasePin.Spike(); //assert Assert.True(called); }
public async Task CanFadeOutToBlack() { //arrange var red = new PinStub(1); var green = new PinStub(2); var blue = new PinStub(3); var led = new RGBLED(red, green, blue); //act await led.FadeOut(TimeSpan.Zero); //assert Assert.Equal(PowerValue.Off, red.Power); Assert.Equal(PowerValue.Off, green.Power); Assert.Equal(PowerValue.Off, blue.Power); }
public void TurnOffTurnsOffAllPins() { //arrange var red = new PinStub(1); var green = new PinStub(2); var blue = new PinStub(3); var led = new RGBLED(red, green, blue); //act led.TurnOff(); //assert Assert.Equal(PowerValue.Off, red.Power); Assert.Equal(PowerValue.Off, green.Power); Assert.Equal(PowerValue.Off, blue.Power); }
public async Task PulseFadesOutCompletely() { //arrange var red = new PinStub(1); var green = new PinStub(2); var blue = new PinStub(3); var led = new RGBLED(red, green, blue); //act await led.Pulse(Color.White, TimeSpan.Zero); //assert Assert.Equal(PowerValue.Off, red.Power); Assert.Equal(PowerValue.Off, green.Power); Assert.Equal(PowerValue.Off, blue.Power); }
public void TurnCounterclockwiseSetsDirection() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise) { Direction = Motor.Rotation.Clockwise }; //act motor.TurnCounterclockwise(); //assert Assert.Equal(Motor.Rotation.Counterclockwise, motor.Direction); }
public async Task CanFadeToColor() { //arrange var red = new PinStub(1); var green = new PinStub(2); var blue = new PinStub(3); var led = new RGBLED(red, green, blue); //act var ecoGreen = Color.FromArgb(83, 141, 67); await led.FadeTo(ecoGreen, TimeSpan.Zero); //assert Assert.Equal(32.5, red.Strength, 1); Assert.Equal(55.3, green.Strength, 1); Assert.Equal(26.3, blue.Strength, 1); }
public void CoastKeepsInputsOn() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise); motor.Start(); //act motor.Coast(); //assert Assert.Equal(PowerValue.Off, enabled.Power); Assert.Equal(PowerValue.On, clockwise.Power); Assert.Equal(PowerValue.Off, counterclockwise.Power); }
public void ClearSpikesOutputWhileClearIsOn() { //arrange var enabled = new PinStub(1); var data = new PinStub(2); var shift = new PinStub(3); var output = Substitute.For <IPinInterface>(); var clear = Substitute.For <IPinInterface>(); var register = new ShiftRegister(enabled, data, shift, output, clear); //act register.Clear(); //assert clear.Received().TurnOn(); output.Received().Spike(); clear.Received().TurnOff(); }
public void StopTurnsOffAllPins() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise); motor.Start(); //act motor.Stop(); //assert Assert.Equal(PowerValue.Off, enabled.Power); Assert.Equal(PowerValue.Off, clockwise.Power); Assert.Equal(PowerValue.Off, counterclockwise.Power); }
public void StartWhenCounterclockwiseSetsInputsCorrectly() { //arrange var enabled = new PinStub(1); var clockwise = new PinStub(2); var counterclockwise = new PinStub(3); var motor = new Motor(enabled, clockwise, counterclockwise) { Direction = Motor.Rotation.Counterclockwise }; //act motor.Start(); //assert Assert.Equal(PowerValue.On, enabled.Power); Assert.Equal(PowerValue.Off, clockwise.Power); Assert.Equal(PowerValue.On, counterclockwise.Power); }
public void SendPowerArraySendsCorrectPowerValues() { //arrange var data = Substitute.For <IPinInterface>(); var clock = new PinStub(2); var input = new InputStub(data, clock); var values = new[] { PowerValue.On, PowerValue.Off, PowerValue.Off, PowerValue.On }; //act input.Send(values); //assert var calls = data.ReceivedCalls().ToArray(); Assert.Equal(PowerValue.On, calls[0].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[1].GetArguments()[0]); Assert.Equal(PowerValue.Off, calls[2].GetArguments()[0]); Assert.Equal(PowerValue.On, calls[3].GetArguments()[0]); }
public void ColorsMatch(Color color, double r, double g, double b) { //arrange var red = new PinStub(1); var green = new PinStub(2); var blue = new PinStub(3); var led = new RGBLED(red, green, blue); //act led.SetColor(color); //assert Assert.Equal(r, red.Strength); Assert.Equal(g, green.Strength); Assert.Equal(b, blue.Strength); Assert.Equal(r > 0 ? PowerValue.On : PowerValue.Off, red.Power); Assert.Equal(g > 0 ? PowerValue.On : PowerValue.Off, green.Power); Assert.Equal(b > 0 ? PowerValue.On : PowerValue.Off, blue.Power); }
public void PinsAreMappedCorrectly() { //arrange var newPin = Substitute.For <Func <byte, IPinInterface> >(); newPin.Invoke(Arg.Any <byte>()).Returns(p => new PinStub(p.Arg <byte>())); var board = new BroadcomStub(newPin); var pins = new PinStub[28]; //act for (var x = 0; x < 28; x++) { pins[x] = (PinStub)board.GetType().GetProperty($"GPIO{x}")?.GetValue(board) !; } //assert for (var x = 0; x < 28; x++) { Assert.Equal(x, pins[x].Pin); } }