Пример #1
0
        public void WhenReadingEndOfSource_ExpectMinusOne()
        {
            // Given that the text source has been initialized with an string
            // When Read is called
            // Then -1 is returned
            var sut  = new StringTextSource("");
            var read = sut.Read();

            Assert.Equal(-1, read);
        }
Пример #2
0
        public void WhenReadWithEmptyBuffer_ExpectArgumentNullException()
        {
            // Given that the text source has been initialized with a non-empty string
            // When Read is called with a buffer that is a null reference
            // Then an ArgumentNullException is thrown
            var sut = new StringTextSource(Sentence);
            int read;

            Assert.Throws <ArgumentNullException>(() => read = sut.Read(null, 0, Sentence.Length));
        }
Пример #3
0
        public void WhenReadWithCountBeyondBufferLength_ExpectArgumentOutOfRangeException()
        {
            // Given that the text source has been initialized with a non-empty string
            // When Read is called with a count that exceeds the buffer length
            // Then an ArgumentOutOfRangeException is thrown
            var sut    = new StringTextSource(Sentence);
            var buffer = new char[Sentence.Length];
            int read;

            Assert.Throws <ArgumentOutOfRangeException>(() => read = sut.Read(buffer, 0, buffer.Length + 1));
        }
Пример #4
0
        public void WhenReadWithNegativeOffset_ExpectArgumentOutOfRangeException()
        {
            // Given that the text source has been initialized with a non-empty string
            // When Read is called with a negative offset
            // Then an ArgumentOutOfRangeException is thrown
            var sut    = new StringTextSource(Sentence);
            var buffer = new char[Sentence.Length];
            int read;

            Assert.Throws <ArgumentOutOfRangeException>(() => read = sut.Read(buffer, -1, buffer.Length));
        }
Пример #5
0
 public void SeekWithoutRecording()
 {
     // Given that there is a text source
     //  and the text length is 10
     // When reading 1 character
     //  and seeking to offset 0
     // Then an exception is thrown
     using (var sut = new StringTextSource("1234567890"))
     {
         sut.Read(new char[2], 0, 2);
         Assert.Throws <ArgumentOutOfRangeException>(() => sut.Seek(0));
     }
 }
Пример #6
0
        public void WhenRead_ExpectCharacterIsReturnedAndConsumed()
        {
            // Given that there is text to be read
            // When Read is called
            // Then the next available character is returned
            //  And the character is consumed
            using (var sut = new StringTextSource("abc"))
            {
                var read = (char)sut.Read();
                Assert.Equal('a', read);

                // Verify behavior by peeking at the next character
                var peek = (char)sut.Peek();
                Assert.NotEqual('a', peek);
            }
        }
Пример #7
0
        public void WhenReadWithBuffer_ExpectTextIsReturnedAndConsumed()
        {
            // Given that there is text to be read
            // When Read is called
            // Then the buffer contains the text
            //  And the text is consumed
            using (var sut = new StringTextSource(Sentence))
            {
                var buffer = new char[Sentence.Length];
                var len    = sut.Read(buffer, 0, buffer.Length);
                Assert.Equal(buffer.Length, len);
                Assert.Equal(Sentence.ToCharArray(), buffer);

                // Verify behavior by peeking at the next character
                Assert.Equal(-1, sut.Peek());
            }
        }
Пример #8
0
        public void WhenReadWithBufferBiggerThanTextSource_ExpectResultLessThanBufferSize()
        {
            // Given that there is text to be read
            // When Read is called with a buffer
            //  And the buffer length is larger than the length of the text
            // Then the buffer contains the text
            //  And the returned length is less than the length of the buffer
            //  And the text is consumed
            using (var sut = new StringTextSource(Sentence))
            {
                var buffer = new char[Sentence.Length * 2];
                var len    = sut.Read(buffer, 0, buffer.Length);
                Assert.Equal(Sentence.Length, len);
                Assert.Equal(Sentence.ToCharArray(), buffer.Take(len));

                // Verify behavior by peeking at the next character
                Assert.Equal(-1, sut.Peek());
            }
        }