#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2017 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support {
/// Unit Test for the strign segment class
[TestFixture]
internal class StringSegmentTest {
///
/// Tests whether the default constructor of the StringSegment class throws the
/// right exception when being passed 'null' instead of a string
///
[Test]
public void TestNullStringInSimpleConstructor() {
Assert.Throws(
delegate() { new StringSegment(null); }
);
}
///
/// Tests whether the simple constructor of the StringSegment class accepts
/// an empty string
///
[Test]
public void TestEmptyStringInSimpleConstructor() {
new StringSegment(string.Empty);
}
///
/// Tests whether the full constructor of the StringSegment class throws the
/// right exception when being passed 'null' instead of a string
///
[Test]
public void TestNullStringInFullConstructor() {
Assert.Throws(
delegate() { new StringSegment(null, 0, 0); }
);
}
///
/// Tests whether the full constructor of the StringSegment class accepts
/// an empty string
///
[Test]
public void TestEmptyStringInFullConstructor() {
new StringSegment(string.Empty, 0, 0);
}
///
/// Tests whether the full constructor of the StringSegment class throws the
/// right exception when being passed an invalid start offset
///
[Test]
public void TestInvalidOffsetInConstructor() {
Assert.Throws(
delegate() { new StringSegment(string.Empty, -1, 0); }
);
}
///
/// Tests whether the full constructor of the StringSegment class throws the
/// right exception when being passed an invalid string length
///
[Test]
public void TestInvalidLengthInConstructor() {
Assert.Throws(
delegate() { new StringSegment(string.Empty, 0, -1); }
);
}
///
/// Tests whether the full constructor of the StringSegment class throws the
/// right exception when being passed a string length that's too large
///
[Test]
public void TestExcessiveLengthInConstructor() {
Assert.Throws(
delegate() { new StringSegment("hello", 3, 3); }
);
}
/// Tests whether the 'Text' property works as expected
[Test]
public void TestTextProperty() {
StringSegment testSegment = new StringSegment("hello", 1, 3);
Assert.AreEqual("hello", testSegment.Text);
}
/// Tests whether the 'Offset' property works as expected
[Test]
public void TestOffsetProperty() {
StringSegment testSegment = new StringSegment("hello", 1, 3);
Assert.AreEqual(1, testSegment.Offset);
}
/// Tests whether the 'Count' property works as expected
[Test]
public void TestCountProperty() {
StringSegment testSegment = new StringSegment("hello", 1, 3);
Assert.AreEqual(3, testSegment.Count);
}
///
/// Tests whether two differing instances produce different hash codes
///
[Test]
public void TestHashCodeOnDifferingInstances() {
StringSegment helloWorldSegment = new StringSegment("hello world", 2, 7);
StringSegment howAreYouSegment = new StringSegment("how are you", 1, 9);
Assert.AreNotEqual(
helloWorldSegment.GetHashCode(), howAreYouSegment.GetHashCode()
);
}
///
/// Tests whether two equivalent instances produce an identical hash code
///
[Test]
public void TestHashCodeOnEquivalentInstances() {
StringSegment helloWorld1Segment = new StringSegment("hello world", 2, 7);
StringSegment helloWorld2Segment = new StringSegment("hello world", 2, 7);
Assert.AreEqual(
helloWorld1Segment.GetHashCode(), helloWorld2Segment.GetHashCode()
);
}
/// Tests the equals method performing a comparison against null
[Test]
public void TestEqualsOnNull() {
StringSegment helloWorldSegment = new StringSegment("hello world", 2, 7);
Assert.IsFalse(
helloWorldSegment.Equals(null)
);
}
/// Tests the equality operator with differing instances
[Test]
public void TestEqualityOnDifferingInstances() {
StringSegment helloWorldSegment = new StringSegment("hello world", 2, 7);
StringSegment howAreYouSegment = new StringSegment("how are you", 1, 9);
Assert.IsFalse(helloWorldSegment == howAreYouSegment);
}
/// Tests the equality operator with equivalent instances
[Test]
public void TestEqualityOnEquivalentInstances() {
StringSegment helloWorld1Segment = new StringSegment("hello world", 2, 7);
StringSegment helloWorld2Segment = new StringSegment("hello world", 2, 7);
Assert.IsTrue(helloWorld1Segment == helloWorld2Segment);
}
/// Tests the inequality operator with differing instances
[Test]
public void TestInequalityOnDifferingInstances() {
StringSegment helloWorldSegment = new StringSegment("hello world", 2, 7);
StringSegment howAreYouSegment = new StringSegment("how are you", 1, 9);
Assert.IsTrue(helloWorldSegment != howAreYouSegment);
}
/// Tests the inequality operator with equivalent instances
[Test]
public void TestInequalityOnEquivalentInstances() {
StringSegment helloWorld1Segment = new StringSegment("hello world", 2, 7);
StringSegment helloWorld2Segment = new StringSegment("hello world", 2, 7);
Assert.IsFalse(helloWorld1Segment != helloWorld2Segment);
}
/// Tests the ToString() method of the string segment
[Test]
public void TestToString() {
StringSegment helloWorldSegment = new StringSegment("hello world", 4, 3);
Assert.AreEqual("o w", helloWorldSegment.ToString());
}
///
/// Tests the ToString() method of the string segment with an invalid string
///
[Test]
public void TestToStringWithInvalidString() {
Assert.Throws(
delegate() { new StringSegment(null, 4, 3); }
);
}
///
/// Tests the ToString() method of the string segment with an invalid offset
///
[Test]
public void TestToStringWithInvalidOffset() {
Assert.Throws(
delegate() { new StringSegment("hello world", -4, 3); }
);
}
///
/// Tests the ToString() method of the string segment with an invalid count
///
[Test]
public void TestToStringWithInvalidCount() {
Assert.Throws(
delegate() { new StringSegment("hello world", 4, -3); }
);
}
}
} // namespace Nuclex.Support
#endif // UNITTEST