예제 #1
0
        public void IndexGet_CorrectIndex_ReturnsExpectedCharacter()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello");

            // Act + Assert
            Assert.Equal('o', myStr[4]);
        }
예제 #2
0
        public void SubString_IncorrectRange_ThrowsException()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello World");

            // Act + Assert
            Assert.Throws <Exception>(() => myStr.SubString(7, 5));
        }
예제 #3
0
        public void IndexGet_IncorrectIndex_ThrowsException()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello");

            // Act + Assert
            Assert.Throws <IndexOutOfRangeException>(() => myStr[-1]);
            Assert.Throws <IndexOutOfRangeException>(() => myStr[5]);
        }
예제 #4
0
        public void Constructor_NoArgs_CreatesEmptyMyString()
        {
            // Arrange + Act
            var myStr = new MyString.MyString();

            // Assert
            Assert.Equal(0, myStr.Length);
            Assert.Equal(new char[0], myStr);
        }
예제 #5
0
        public void Assignment_SomeClassInstance_NewInstanceIsSame()
        {
            // Arrange
            var a = new MyString.MyString("hey");

            // Act
            var b = a; // Assignment reference

            // Act
            Assert.Same(a, b);
        }
예제 #6
0
        public void IndexSet_CorrectIndex_CharAtIndexIsChanged()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello")
            {
                [3] = 'a'
            };

            // Assert
            Assert.Equal('a', myStr[3]);
        }
예제 #7
0
        public void GetStringData_MyStringWithSomeData_ReturnsCharEnumerablePlusNullSymbol()
        {
            // Arrange
            const string someString = "Hello";

            // Act
            var myStr = new MyString.MyString(someString);

            // Assert
            Assert.Equal(someString + "\0", myStr.GetStringData);
        }
예제 #8
0
        public void Constructor_CharArrayWithNullCharacterAtCenter_CreatesMyStringWithSameCharacters()
        {
            // Arrange
            var chars = new[] { 'H', 'e', 'l', 'l', 'o', '\0', 'W', 'o', 'r', 'l', 'd' };

            // Act
            var myStr = new MyString.MyString(chars);

            // Assert
            Assert.Equal(chars.Length, myStr.Length);
            Assert.Equal("Hello\0World", myStr);
        }
예제 #9
0
        public void SubString_CorrectRange_ReturnsExpectedSubString()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello World");

            // Act
            var newStr = myStr.SubString(6, 5);

            // Assert
            Assert.Equal(5, newStr.Length);
            Assert.Equal("World", newStr);
        }
예제 #10
0
        public void Constructor_OtherMyStringInstance_CreatesNewInstanceOfMyStringWithSameCharacters()
        {
            // Arrange + Act
            var myStr   = new MyString.MyString(new[] { 'H', 'e', 'l', 'l', 'o' });
            var copyStr = new MyString.MyString(myStr);

            // Assert
            Assert.NotSame(myStr, copyStr);

            Assert.Equal(myStr.Length, copyStr.Length);
            Assert.Equal(copyStr, myStr);
        }
예제 #11
0
        public void Clear_NonEmptyString_StringIsEmptyNow()
        {
            // Arrange
            var myStr = new MyString.MyString("Hello");

            // Act
            myStr.Clear();

            // Assert
            Assert.Equal(0, myStr.Length);
            Assert.Equal(string.Empty, myStr);
        }
예제 #12
0
        public void Constructor_RegularString_CreatesMyStringWithSameCharacters()
        {
            // Arrange
            const string someString = "Hello";

            // Act
            var myStr = new MyString.MyString(someString);

            // Assert
            Assert.Equal(someString.Length, myStr.Length);
            Assert.Equal(new[] { 'H', 'e', 'l', 'l', 'o' }, myStr);
        }
예제 #13
0
        public void Concat_CharArrayWithMyString_ReturnsCombinedMyString()
        {
            // Arrange
            var a = new[] { 'h', 'e', 'y', ' ' };
            var b = new MyString.MyString("world");

            // Act
            var c = a + b;

            // Assert
            Assert.Equal(a.Length + b.Length, c.Length);
            Assert.Equal("hey world", c);
        }
예제 #14
0
        public void Concat_StringWithMyString_ReturnsCombinedMyString()
        {
            // Arrange
            const string a = "hey";
            var          b = new MyString.MyString(" world");

            // Act
            var c = a + b;

            // Assert
            Assert.Equal(a.Length + b.Length, c.Length);
            Assert.Equal("hey world", c);
        }
예제 #15
0
        public void Constructor_CharArray_CreatesMyStringWithSameCharacters()
        {
            // Arrange
            var chars = new[] { 'H', 'e', 'l', 'l', 'o' };

            // Act
            var myStr = new MyString.MyString(chars);

            // Assert
            Assert.Equal(chars.Length, myStr.Length);
            Assert.Equal(chars, myStr);
            Assert.Equal("Hello", myStr);
        }
예제 #16
0
        public void Concat_MyStringWithMyString_ReturnsCombinedMyString()
        {
            // Arrange
            var a = new MyString.MyString("hey ");
            var b = new MyString.MyString(new[] { 'w', 'o', 'r', 'l', 'd' });

            // Act
            var c = a + b;

            // Assert
            Assert.Equal(a.Length + b.Length, c.Length);
            Assert.Equal("hey world", c);
        }
예제 #17
0
        public void Constructor_CharArrayWithLength_CreatesMyStringWithFirstNChars()
        {
            // Arrange
            const int length = 4;
            var       chars  = new[] { 'H', 'e', 'l', 'l', 'o' };

            // Act
            var myStr = new MyString.MyString(chars, length);

            // Assert
            Assert.Equal(length, myStr.Length);
            Assert.Equal(new[] { 'H', 'e', 'l', 'l' }, myStr);
        }
예제 #18
0
        public void Comparison_TwoDifferentStrings_ExpectedOperatorsBehaviour()
        {
            // Arrange
            var a = new MyString.MyString("Hello");
            var b = new MyString.MyString("Hell");

            // Act + Assert
            Assert.False(a == b);
            Assert.True(a != b);

            Assert.True(a >= b);
            Assert.False(a <= b);

            Assert.True(a > b);
            Assert.False(a < b);
        }
예제 #19
0
        public void WriteToStream_StringWithSomeTextToFileStream_WritesSameTextToTheFile()
        {
            // Arrange
            const string filePath = "temp.txt";
            const string fileText = "Hello";
            var          myString = new MyString.MyString("Hello");

            // Act
            using (var fsa = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                myString.WriteToStream(fsa);
            }

            var actualFileText = File.ReadAllText(filePath);

            File.Delete(filePath);

            // Assert
            Assert.Equal(fileText, actualFileText);
        }
예제 #20
0
        public void ReadFromStream_FileStreamWithText_StringIsFilledWithTheText()
        {
            // Arrange
            const string filePath = "temp.txt";
            const string fileText = "Hello world and everyone here!";

            File.WriteAllText(filePath, fileText);
            var myString = new MyString.MyString();

            // Act
            using (var fsa = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                myString.ReadFromStream(fsa);
            }

            File.Delete(filePath);

            // Assert
            Assert.Equal(fileText, myString);
        }
예제 #21
0
        public void ReadFromStream_FileStreamWithWhiteSpaces_StringIsFilledWithWhiteSpaces()
        {
            // Arrange
            const string filePath = "temp.txt";
            const string fileText = "            ";

            File.WriteAllText(filePath, fileText);
            var myString = new MyString.MyString("Hello");

            // Act
            using (var fsa = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                myString.ReadFromStream(fsa);
            }

            File.Delete(filePath);

            // Assert
            Assert.Equal(fileText.Length, myString.Length);
            Assert.Equal(fileText, myString);
        }