public void ShouldAlwaysCreateNewInstanceOfDefaultFormat()
        {
            var firstFormat = CollectionFormat.CreateDefault()
                              .AddOptions(IncludeIndex)
                              .SetIndexValuePairPrefixAndSuffix("<", ">");

            var firstToString = firstFormat.Compile();

            var secondFormat = CollectionFormat.CreateDefault();

            var secondToString = secondFormat.Compile();

            Assert.That(secondFormat, Is.Not.SameAs(firstFormat));
            Assert.That(secondToString, Is.Not.SameAs(firstToString));

            Assert.That(
                secondToString(new object[] { "John", "007", null, "Bond" }),
                Is.EqualTo("['John', '007', '', 'Bond']")
                );
        }
        public void ShouldCreateDefaultFormat()
        {
            ValueTuple <string, string> defaultPrefixAndSuffix = (null, null);

            var format = CollectionFormat.CreateDefault();

            var toString = format.Compile();

            Assert.That(toString, Is.Not.Null);

            Assert.That(format.IndexValueSeparator, Is.Null);
            Assert.That(format.IndexValuePairSeparator, Is.EqualTo(", "));
            Assert.That(format.ValuePrefixAndSuffix, Is.EqualTo(("'", "'")));
            Assert.That(format.IndexPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix));
            Assert.That(format.IndexValuePairPrefixAndSuffix, Is.EqualTo(defaultPrefixAndSuffix));
            Assert.That(format.CollectionPrefixAndSuffix, Is.EqualTo(("[", "]")));
            Assert.That(format.Options, Is.EqualTo(IncludeNullValues));

            Assert.That(
                toString(new object[] { "John", "007", null, "Bond" }),
                Is.EqualTo("['John', '007', '', 'Bond']")
                );
        }
Exemplo n.º 3
0
 /// <summary>
 /// Use <see cref="CollectionFormat"/> for converting members of <see cref="ICollection"/> type
 /// to string. Only affects members which declared types are assignable to <see cref="ICollection"/>.
 /// </summary>
 /// <param name="setup">
 /// Function accepting default <see cref="CollectionFormat"/> and returning modified version or brand new
 /// instance to be used by <see cref="object.ToString"/> function being built. When it returns null than
 /// default <see cref="CollectionFormat"/> (the one passed to it) will be used.
 /// <see cref="CollectionFormat.CreateDefault"/> will be used when function is omitted (or null passed).
 /// </param>
 /// <returns>Updated <see cref="ToStringBuilder{TTarget}"/> instance.</returns>
 public ToStringBuilder <TTarget> UseCollectionFormat(
     Func <CollectionFormat, CollectionFormat> setup = null
     )
 {
     return(Use <ICollection>(SetupFormat(CollectionFormat.CreateDefault(), setup).Compile()));
 }