public void When_reading_beyond_end_of_stream_it_should_throw(string dataToParse, int successfulReadsCount) { // Arrange _parser = CreateSubject(dataToParse); if (!string.IsNullOrEmpty(dataToParse)) { _parser.AtEndOfStream().Should().BeFalse(); } // Act Func <byte> readByte = () => _parser.ReadByte(); for (int i = 0; i < successfulReadsCount; i++) { readByte.Should().NotThrow(); } // Assert _parser.AtEndOfStream() .Should() .BeTrue("all hex data was read"); readByte .Should() .Throw <ProtocolReaderException>() .Which.Message .Should().StartWith("Reached end of stream"); }
public void When_reading_non_padded_hexadecimal_string_it_should_throw(string dataToParse, params byte[] expectedData) { // Arrange _parser = CreateSubject(dataToParse); int position = 0; // Act Func <byte> readByte = () => _parser.ReadByte(); foreach (byte expectedByte in expectedData) { position += 2; readByte.Should() .NotThrow("two or more characters are still on the stream") .Which.Should() .Be(expectedByte); } // Assert _parser.AtEndOfStream() .Should() .BeFalse("the last char is not read yet"); ProtocolReaderException ex = readByte .Should() .Throw <ProtocolReaderException>("the stream does not have enough hex chars left") .Which; ex.Message.Should().StartWith("Reached end of stream"); ex.Position.Should().Be(position); ex.Length.Should().Be(dataToParse.Length); _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_to_end_should_return_correct_string(string dataToParse, params object[] expectedValues) { // Arrange _parser = CreateSubject(dataToParse); // Act & Assert _parser.ReadByte().Should().Be((byte)expectedValues[0]); _parser.ReadByte().Should().Be((byte)expectedValues[1]); _parser.ReadToEnd().Should().Be((string)expectedValues[2]); _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_datetime_should_read_correct_value(string dataToParse, string expectedDateStr) { // Arrange _parser = CreateSubject(dataToParse); DateTime expectedDate = XmlConvert.ToDateTime(expectedDateStr, XmlDateTimeSerializationMode.Unspecified); // Act DateTime actual = _parser.ReadDateTime(); // Assert actual.Should().Be(expectedDate); _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_encapsulated_text_should_return_correct_data(string dataToParse, char delimiter, params string[] expectedValues) { // Arrange _parser = CreateSubject(dataToParse); // Act foreach (string expectedValue in expectedValues) { string actual = _parser.ReadEncapsulatedString(delimiter); actual.Should().Be(expectedValue); } // Assert _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_ushort_should_read_correct_value(string dataToParse, params ushort[] expectedValues) { // Arrange _parser = CreateSubject(dataToParse); // Act foreach (ushort expectedValue in expectedValues) { ushort actual = _parser.ReadUInt16(); // Assert actual.Should().Be(expectedValue); } _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_byte_should_read_correct_value(string dataToParse, params byte[] expectedValues) { // Arrange _parser = CreateSubject(dataToParse); // Act foreach (byte expectedValue in expectedValues) { byte actual = _parser.ReadByte(); // Assert actual.Should().Be(expectedValue); } _parser.AtEndOfStream().Should().BeTrue(); }
public void When_reading_byte_from_data_that_is_not_hexadecimal_it_should_throw() { // Arrange _parser = CreateSubject("Zs"); // Act Action readByte = () => _parser.ReadByte(); // Assert ProtocolReaderException ex = readByte.Should() .Throw <ProtocolReaderException>() .Which; ex.InnerException.Should().BeOfType <FormatException>() .Which.Message.Should().Be("Could not find any recognizable digits."); ex.Position.Should().Be(0); ex.Length.Should().Be(2); _parser.AtEndOfStream().Should().BeTrue(); }