New utilities and tests
This commit is contained in:
Родитель
3ad46aa9dd
Коммит
ead6b54b39
|
@ -175,7 +175,6 @@ namespace NSubstitute.Elevated.Weaver
|
|||
|
||||
type.Methods.Add(ctor);
|
||||
}
|
||||
|
||||
void Patch(MethodDefinition method)
|
||||
{
|
||||
if (method.IsCompilerControlled || method.IsConstructor || method.IsAbstract)
|
||||
|
|
|
@ -82,6 +82,43 @@ namespace Unity.Core.Tests
|
|||
Should.Throw<Exception>(() => "abc".Right(-1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_ThatDoesNotShorten_ReturnsSameInstance()
|
||||
{
|
||||
const string text = "abc def";
|
||||
ReferenceEquals(text, text.Truncate(100)).ShouldBeTrue();
|
||||
ReferenceEquals(text, text.Truncate(10)).ShouldBeTrue();
|
||||
ReferenceEquals(text, text.Truncate(text.Length)).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_ThatShortens_TruncatesAndAddsTrailer()
|
||||
{
|
||||
"abc def".Truncate(6).ShouldBe("abc...");
|
||||
"abc def".Truncate(5).ShouldBe("ab...");
|
||||
"abc def".Truncate(4).ShouldBe("a...");
|
||||
"abc def".Truncate(3).ShouldBe("...");
|
||||
|
||||
"abc def".Truncate(6, "ghi").ShouldBe("abcghi");
|
||||
"abc def".Truncate(5, "ghi").ShouldBe("abghi");
|
||||
"abc def".Truncate(4, "ghi").ShouldBe("aghi");
|
||||
"abc def".Truncate(3, "ghi").ShouldBe("ghi");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_WithTooBigTrailer_ShouldThrow()
|
||||
{
|
||||
Should.Throw<ArgumentException>(() => "abc def".Truncate(2));
|
||||
Should.Throw<ArgumentException>(() => "abc def".Truncate(5, "123456"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Truncate_WithUnderflow_ShouldThrow()
|
||||
{
|
||||
Should.Throw<ArgumentException>(() => "abc def".Truncate(-2));
|
||||
Should.Throw<ArgumentException>(() => "abc def".Truncate(0, "ghi"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringJoin_WithEmpty_ReturnsEmptyString()
|
||||
{
|
||||
|
@ -144,7 +181,7 @@ namespace Unity.Core.Tests
|
|||
}
|
||||
|
||||
[Test]
|
||||
public void ExpandTabs_WithNoTabs_Returns_IdenticalString() // i.e. no allocs
|
||||
public void ExpandTabs_WithNoTabs_ReturnsSameInstance() // i.e. no allocs
|
||||
{
|
||||
const string text = "abc def ghijkl";
|
||||
ReferenceEquals(text, text.ExpandTabs(4)).ShouldBeTrue();
|
||||
|
|
|
@ -43,5 +43,8 @@ namespace Unity.Core
|
|||
|
||||
public static bool IsNullOrEmpty<T>([CanBeNull] this IEnumerable<T> @this)
|
||||
=> @this == null || !@this.Any();
|
||||
|
||||
public static IEnumerable<T> SelectMany<T>([NotNull] this IEnumerable<IEnumerable<T>> @this)
|
||||
=> @this.SelectMany(_ => _);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,9 +21,7 @@ namespace Unity.Core
|
|||
|
||||
[NotNull]
|
||||
public static string Left([NotNull] this string @this, int maxChars)
|
||||
{
|
||||
return @this.Substring(0, Math.Min(maxChars, @this.Length));
|
||||
}
|
||||
=> @this.Substring(0, Math.Min(maxChars, @this.Length));
|
||||
|
||||
[NotNull]
|
||||
public static string Mid([NotNull] this string @this, int offset, int maxChars = -1)
|
||||
|
@ -46,6 +44,15 @@ namespace Unity.Core
|
|||
return @this.Substring(@this.Length - safeMaxChars, safeMaxChars);
|
||||
}
|
||||
|
||||
[NotNull]
|
||||
public static string Truncate([NotNull] this string @this, int maxChars, string trailer = "...")
|
||||
{
|
||||
if (@this.Length <= maxChars)
|
||||
return @this;
|
||||
|
||||
return @this.Left(maxChars - trailer.Length) + trailer;
|
||||
}
|
||||
|
||||
[NotNull]
|
||||
public static string StringJoin([NotNull] this IEnumerable @this, [NotNull] string separator)
|
||||
=> string.Join(separator, @this.Cast<object>());
|
||||
|
|
Загрузка…
Ссылка в новой задаче