示例#1
0
        public void FormatFlags_ReturnsValidString_WhenUsingValidValueWithCustomDelimiter()
        {
            AssertTryFormatFlags(ColorFlagEnum.Black, " | ", "Black");
            AssertTryFormatFlags(ColorFlagEnum.Red, " | ", "Red");
            AssertTryFormatFlags(ColorFlagEnum.Green, " | ", "Green");
            AssertTryFormatFlags(ColorFlagEnum.Blue, " | ", "Blue");
            AssertTryFormatFlags(ColorFlagEnum.Red | ColorFlagEnum.Green, " | ", "Red | Green");
            AssertTryFormatFlags(ColorFlagEnum.Red | ColorFlagEnum.Blue, " | ", "Red | Blue");
            AssertTryFormatFlags(ColorFlagEnum.Green | ColorFlagEnum.Blue, " | ", "Green | Blue");
            AssertTryFormatFlags(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, " | ", "Red | Green | Blue");
            AssertTryFormatFlags(ColorFlagEnum.UltraViolet, " | ", "UltraViolet");
            AssertTryFormatFlags(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, " | ", "All");

            void AssertTryFormatFlags(ColorFlagEnum value, string delimiter, string expected)
            {
                Assert.AreEqual(expected, FlagEnums.FormatFlags(value, delimiter));
#if SPAN
                var dest = new char[expected.Length];
                Assert.True(FlagEnums.TryFormatFlags(value, dest, out var charsWritten, delimiter));
                Assert.AreEqual(expected.Length, charsWritten);
                Assert.AreEqual(expected, new string(dest));

                dest = new char[expected.Length - 1];
                Assert.False(FlagEnums.TryFormatFlags(value, dest, out charsWritten, delimiter));
                Assert.AreEqual(0, charsWritten);
                CollectionAssert.AreEqual(new char[dest.Length], dest);
#endif
            }
        }
示例#2
0
        public void FormatFlags_ReturnsValidString_WhenUsingInvalidValue()
        {
            AssertTryFormatFlags((ColorFlagEnum)16, "16");

            void AssertTryFormatFlags(ColorFlagEnum value, string expected)
            {
                Assert.AreEqual(expected, FlagEnums.FormatFlags(value));
#if SPAN
                var dest = new char[expected.Length];
                Assert.True(FlagEnums.TryFormatFlags(value, dest, out var charsWritten));
                Assert.AreEqual(expected.Length, charsWritten);
                Assert.AreEqual(expected, new string(dest));

                dest = new char[expected.Length - 1];
                Assert.False(FlagEnums.TryFormatFlags(value, dest, out charsWritten));
                Assert.AreEqual(0, charsWritten);
                CollectionAssert.AreEqual(new char[dest.Length], dest);
#endif
            }
        }
示例#3
0
        public void FormatFlags_UsesDefaultDelimiter_WhenUsingValidValueWithEmptyDelimiter()
        {
            var value = ColorFlagEnum.Red | ColorFlagEnum.Green;

            AssertTryFormatFlags(value, FlagEnums.FormatFlags(value));

            void AssertTryFormatFlags(ColorFlagEnum value, string expected)
            {
                Assert.AreEqual(expected, FlagEnums.FormatFlags(value, string.Empty));
#if SPAN
                var dest = new char[expected.Length];
                Assert.True(FlagEnums.TryFormatFlags(value, dest, out var charsWritten, string.Empty));
                Assert.AreEqual(expected.Length, charsWritten);
                Assert.AreEqual(expected, new string(dest));

                dest = new char[expected.Length - 1];
                Assert.False(FlagEnums.TryFormatFlags(value, dest, out charsWritten, string.Empty));
                Assert.AreEqual(0, charsWritten);
                CollectionAssert.AreEqual(new char[dest.Length], dest);
#endif
            }
        }