public void ToStringAndClear_Clears() { DefaultInterpolatedStringHandler handler = new DefaultInterpolatedStringHandler(0, 0); handler.AppendLiteral("hi"); Assert.Equal("hi", handler.ToStringAndClear()); Assert.Equal(string.Empty, handler.ToStringAndClear()); }
public void AppendFormatted_ReadOnlySpanChar() { var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0); foreach (string s in new[] { "", "a", "bc", "def", "this is a longer string", "!" }) { // span expected.Append(s); actual.AppendFormatted((ReadOnlySpan <char>)s); // span, format expected.AppendFormat("{0:X2}", s); actual.AppendFormatted((ReadOnlySpan <char>)s, format: "X2"); foreach (int alignment in new[] { 0, 3, -3 }) { // span, alignment expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + "}", s); actual.AppendFormatted((ReadOnlySpan <char>)s, alignment); // span, alignment, format expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + ":X2}", s); actual.AppendFormatted((ReadOnlySpan <char>)s, alignment, "X2"); } } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); }
public void AppendFormatted_String_ICustomFormatter() { var provider = new ConcatFormatter(); var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0, provider); foreach (string s in new[] { null, "", "a" }) { // string expected.AppendFormat(provider, "{0}", s); actual.AppendFormatted(s); // string, format expected.AppendFormat(provider, "{0:X2}", s); actual.AppendFormatted(s, "X2"); // string, alignment expected.AppendFormat(provider, "{0,3}", s); actual.AppendFormatted(s, 3); // string, alignment, format expected.AppendFormat(provider, "{0,-3:X2}", s); actual.AppendFormatted(s, -3, "X2"); } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); }
public void AppendFormatted_ReferenceTypes() { var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0); foreach (string rawInput in new[] { null, "", "a", "bc", "def", "this is a longer string", "!" }) { foreach (object o in new object[] { rawInput, // raw string directly; ToString will return itself new StringWrapper(rawInput), // wrapper object that returns string from ToString new FormattableStringWrapper(rawInput), // IFormattable wrapper around string new SpanFormattableStringWrapper(rawInput) // ISpanFormattable wrapper around string }) { // object expected.AppendFormat("{0}", o); actual.AppendFormatted(o); if (o is IHasToStringState tss1) { Assert.True(string.IsNullOrEmpty(tss1.ToStringState.LastFormat)); AssertModeMatchesType(tss1); } // object, format expected.AppendFormat("{0:X2}", o); actual.AppendFormatted(o, "X2"); if (o is IHasToStringState tss2) { Assert.Equal("X2", tss2.ToStringState.LastFormat); AssertModeMatchesType(tss2); } foreach (int alignment in new[] { 0, 3, -3 }) { // object, alignment expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + "}", o); actual.AppendFormatted(o, alignment); if (o is IHasToStringState tss3) { Assert.True(string.IsNullOrEmpty(tss3.ToStringState.LastFormat)); AssertModeMatchesType(tss3); } // object, alignment, format expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + ":X2}", o); actual.AppendFormatted(o, alignment, "X2"); if (o is IHasToStringState tss4) { Assert.Equal("X2", tss4.ToStringState.LastFormat); AssertModeMatchesType(tss4); } } } } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); }
static void TestManualDefaultStringInterpolationHandler() { Console.WriteLine("TestManualDefaultStringInterpolationHandler:"); C c = new C(42); DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 1); defaultInterpolatedStringHandler.AppendFormatted(c); M2(Space(), defaultInterpolatedStringHandler.ToStringAndClear()); }
public void ToString_DoesntClear() { DefaultInterpolatedStringHandler handler = new DefaultInterpolatedStringHandler(0, 0); handler.AppendLiteral("hi"); for (int i = 0; i < 3; i++) { Assert.Equal("hi", handler.ToString()); } Assert.Equal("hi", handler.ToStringAndClear()); }
public void AppendLiteral() { var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0); foreach (string s in new[] { "", "a", "bc", "def", "this is a long string", "!" }) { expected.Append(s); actual.AppendLiteral(s); } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); }
public void Grow_Large(bool useScratch) { var expected = new StringBuilder(); DefaultInterpolatedStringHandler handler = useScratch ? new DefaultInterpolatedStringHandler(3, 1000, null, stackalloc char[16]) : new DefaultInterpolatedStringHandler(3, 1000); for (int i = 0; i < 1000; i++) { handler.AppendFormatted(i); expected.Append(i); handler.AppendFormatted(i, 3); expected.AppendFormat("{0,3}", i); } Assert.Equal(expected.ToString(), handler.ToStringAndClear()); }
public void AppendFormatted_ValueTypes_ICustomFormatter() { var provider = new ConcatFormatter(); void Test <T>(T t) { void AssertTss(T tss, string format) { Assert.Equal(format, ((IHasToStringState)tss).ToStringState.LastFormat); Assert.Same(provider, ((IHasToStringState)tss).ToStringState.LastProvider); Assert.Equal(ToStringMode.ICustomFormatterFormat, ((IHasToStringState)tss).ToStringState.ToStringMode); } var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0, provider); // struct expected.AppendFormat(provider, "{0}", t); actual.AppendFormatted(t); AssertTss(t, null); // struct, format expected.AppendFormat(provider, "{0:X2}", t); actual.AppendFormatted(t, "X2"); AssertTss(t, "X2"); // struct, alignment expected.AppendFormat(provider, "{0,3}", t); actual.AppendFormatted(t, 3); AssertTss(t, null); // struct, alignment, format expected.AppendFormat(provider, "{0,-3:X2}", t); actual.AppendFormatted(t, -3, "X2"); AssertTss(t, "X2"); Assert.Equal(expected.ToString(), actual.ToStringAndClear()); } Test(new FormattableInt32Wrapper(42)); Test(new SpanFormattableInt32Wrapper(84)); Test((FormattableInt32Wrapper?)new FormattableInt32Wrapper(42)); Test((SpanFormattableInt32Wrapper?)new SpanFormattableInt32Wrapper(84)); }
public void AppendFormatted_ValueTypes() { void Test <T>(T t) { var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0); // struct expected.AppendFormat("{0}", t); actual.AppendFormatted(t); Assert.True(string.IsNullOrEmpty(((IHasToStringState)t).ToStringState.LastFormat)); AssertModeMatchesType(((IHasToStringState)t)); // struct, format expected.AppendFormat("{0:X2}", t); actual.AppendFormatted(t, "X2"); Assert.Equal("X2", ((IHasToStringState)t).ToStringState.LastFormat); AssertModeMatchesType(((IHasToStringState)t)); foreach (int alignment in new[] { 0, 3, -3 }) { // struct, alignment expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + "}", t); actual.AppendFormatted(t, alignment); Assert.True(string.IsNullOrEmpty(((IHasToStringState)t).ToStringState.LastFormat)); AssertModeMatchesType(((IHasToStringState)t)); // struct, alignment, format expected.AppendFormat("{0," + alignment.ToString(CultureInfo.InvariantCulture) + ":X2}", t); actual.AppendFormatted(t, alignment, "X2"); Assert.Equal("X2", ((IHasToStringState)t).ToStringState.LastFormat); AssertModeMatchesType(((IHasToStringState)t)); } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); } Test(new FormattableInt32Wrapper(42)); Test(new SpanFormattableInt32Wrapper(84)); Test((FormattableInt32Wrapper?)new FormattableInt32Wrapper(42)); Test((SpanFormattableInt32Wrapper?)new SpanFormattableInt32Wrapper(84)); }
public void AppendFormatted_ReferenceTypes_ICustomFormatter() { var provider = new ConcatFormatter(); var expected = new StringBuilder(); DefaultInterpolatedStringHandler actual = new DefaultInterpolatedStringHandler(0, 0, provider); foreach (string s in new[] { null, "", "a" }) { foreach (IHasToStringState tss in new IHasToStringState[] { new FormattableStringWrapper(s), new SpanFormattableStringWrapper(s) }) { void AssertTss(IHasToStringState tss, string format) { Assert.Equal(format, tss.ToStringState.LastFormat); Assert.Same(provider, tss.ToStringState.LastProvider); Assert.Equal(ToStringMode.ICustomFormatterFormat, tss.ToStringState.ToStringMode); } // object expected.AppendFormat(provider, "{0}", tss); actual.AppendFormatted(tss); AssertTss(tss, null); // object, format expected.AppendFormat(provider, "{0:X2}", tss); actual.AppendFormatted(tss, "X2"); AssertTss(tss, "X2"); // object, alignment expected.AppendFormat(provider, "{0,3}", tss); actual.AppendFormatted(tss, 3); AssertTss(tss, null); // object, alignment, format expected.AppendFormat(provider, "{0,-3:X2}", tss); actual.AppendFormatted(tss, -3, "X2"); AssertTss(tss, "X2"); } } Assert.Equal(expected.ToString(), actual.ToStringAndClear()); }
public string ToStringAndClear() => _innerHandler.ToStringAndClear();
/// <summary>Creates a new string by using the specified provider to control the formatting of the specified interpolated string.</summary> /// <param name="provider">An object that supplies culture-specific formatting information.</param> /// <param name="initialBuffer">The initial buffer that may be used as temporary space as part of the formatting operation. The contents of this buffer may be overwritten.</param> /// <param name="handler">The interpolated string.</param> /// <returns>The string that results for formatting the interpolated string using the specified format provider.</returns> public static string Create(IFormatProvider?provider, Span <char> initialBuffer, [InterpolatedStringHandlerArgument("provider", "initialBuffer")] ref DefaultInterpolatedStringHandler handler) => handler.ToStringAndClear();
/// <summary>Creates a new string by using the specified provider to control the formatting of the specified interpolated string.</summary> /// <param name="provider">An object that supplies culture-specific formatting information.</param> /// <param name="handler">The interpolated string.</param> /// <returns>The string that results for formatting the interpolated string using the specified format provider.</returns> public static string Create(IFormatProvider?provider, [InterpolatedStringHandlerArgument(nameof(provider))] ref DefaultInterpolatedStringHandler handler) => handler.ToStringAndClear();
public string ToStringAndClear() { return(_innerHandler.ToStringAndClear()); }