public void AppendFormatted_ValueTypes_CreateProviderFlowed(bool useScratch) { void Test <T>(T t) { var provider = new CultureInfo("en-US"); DefaultInterpolatedStringHandler handler = useScratch ? new DefaultInterpolatedStringHandler(1, 2, provider, stackalloc char[16]) : new DefaultInterpolatedStringHandler(1, 2, provider); handler.AppendFormatted(t); Assert.Same(provider, ((IHasToStringState)t).ToStringState.LastProvider); handler.AppendFormatted(t, 1); Assert.Same(provider, ((IHasToStringState)t).ToStringState.LastProvider); handler.AppendFormatted(t, "X2"); Assert.Same(provider, ((IHasToStringState)t).ToStringState.LastProvider); handler.AppendFormatted(t, 1, "X2"); Assert.Same(provider, ((IHasToStringState)t).ToStringState.LastProvider); } Test(new FormattableInt32Wrapper(42)); Test(new SpanFormattableInt32Wrapper(84)); Test((FormattableInt32Wrapper?)new FormattableInt32Wrapper(42)); Test((SpanFormattableInt32Wrapper?)new SpanFormattableInt32Wrapper(84)); }
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()); }
private static void FormatArgument <T>(ref DefaultInterpolatedStringHandler builder, T arg, int?alignment, string?format) { if (arg is null) { builder.AppendLiteral(NullValue); } else { if (alignment.HasValue && format is not null) { builder.AppendFormatted <T>(arg, alignment.GetValueOrDefault(), format); } else if (alignment.HasValue) { builder.AppendFormatted <T>(arg, alignment.GetValueOrDefault()); } else if (format is not null) { builder.AppendFormatted <T>(arg, format); } else { builder.AppendFormatted <T>(arg); } } }
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 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 void AppendFormatted_ReferenceTypes_CreateProviderFlowed(bool useScratch) { var provider = new CultureInfo("en-US"); DefaultInterpolatedStringHandler handler = useScratch ? new DefaultInterpolatedStringHandler(1, 2, provider, stackalloc char[16]) : new DefaultInterpolatedStringHandler(1, 2, provider); foreach (IHasToStringState tss in new IHasToStringState[] { new FormattableStringWrapper("hello"), new SpanFormattableStringWrapper("hello") }) { handler.AppendFormatted(tss); Assert.Same(provider, tss.ToStringState.LastProvider); handler.AppendFormatted(tss, 1); Assert.Same(provider, tss.ToStringState.LastProvider); handler.AppendFormatted(tss, "X2"); Assert.Same(provider, tss.ToStringState.LastProvider); handler.AppendFormatted(tss, 1, "X2"); Assert.Same(provider, tss.ToStringState.LastProvider); } }
private void AppendParameterReference() { var parameterCount = ++_parameterCount; if (parameterCount < Parameters.Length) { _innerHandler.AppendLiteral(Parameters[parameterCount]); } else { // Fallback if we have more than 9 parameters _innerHandler.AppendLiteral("$"); _innerHandler.AppendFormatted(++_parameterCount); } }
public void AppendFormatted <T>(T value) { _innerHandler.AppendFormatted(value); }