Пример #1
0
        public void GetFlags_ReturnsSetFieldsInOrderOfDefinition()
        {
            NoZeroFlags nzf = NoZeroFlags.One | NoZeroFlags.Three;

            Assert.That(nzf.GetFlags(true), Is.EquivalentTo(new[] { NoZeroFlags.One, NoZeroFlags.Three }));
            Assert.That(nzf.GetFlags(false), Is.EquivalentTo(new[] { NoZeroFlags.One, NoZeroFlags.Three }));

            nzf = NoZeroFlags.Four | NoZeroFlags.One;
            Assert.That(nzf.GetFlags(true), Is.EquivalentTo(new[] { NoZeroFlags.One, NoZeroFlags.Four }));
            Assert.That(nzf.GetFlags(false), Is.EquivalentTo(new[] { NoZeroFlags.One, NoZeroFlags.Four }));

            ZeroFlags zf = ZeroFlags.One | ZeroFlags.Three;

            Assert.That(zf.GetFlags(true), Is.EquivalentTo(new[] { ZeroFlags.One, ZeroFlags.Three }));
            Assert.That(zf.GetFlags(false), Is.EquivalentTo(new[] { ZeroFlags.Zero, ZeroFlags.One, ZeroFlags.Three }));
            Assert.That(zf.GetFlags(), Is.EquivalentTo(new[] { ZeroFlags.Zero, ZeroFlags.One, ZeroFlags.Three }));

            zf = ZeroFlags.Four | ZeroFlags.One;
            Assert.That(zf.GetFlags(true), Is.EquivalentTo(new[] { ZeroFlags.One, ZeroFlags.Four }));
            Assert.That(zf.GetFlags(false), Is.EquivalentTo(new[] { ZeroFlags.Zero, ZeroFlags.One, ZeroFlags.Four }));
            Assert.That(zf.GetFlags(), Is.EquivalentTo(new[] { ZeroFlags.Zero, ZeroFlags.One, ZeroFlags.Four }));
        }
Пример #2
0
        public void GetFlags_DoesNotReturnUnsetFields()
        {
            NoZeroFlags nzf = NoZeroFlags.One | NoZeroFlags.Three;

            Assert.That(nzf.GetFlags(),
                        Has.No.Member(NoZeroFlags.Two)
                        .And.No.Member(NoZeroFlags.Four));

            ZeroFlags zf = ZeroFlags.One | ZeroFlags.Three;

            Assert.That(zf.GetFlags(), Has
                        .No.Member(ZeroFlags.Two)
                        .And.No.Member(ZeroFlags.Four)
                        .And.Member(ZeroFlags.Zero));
        }
Пример #3
0
        public void Explore()
        {
            Assert.That(Enumeration.IsEnum <ActionTargets>(), Is.True);
            Assert.That(Enumeration.IsEnum <int>(), Is.False);
            Assert.That(Enumeration.AssertEnum <ActionTargets>, Throws.Nothing);
            Assert.That(Enumeration.AssertEnum <int>, Throws
                        .InstanceOf <ArgumentException>()
                        .With.Message.Contains("Int32"));
            Assert.That(Enumeration.IsFlags <ZeroFlags>(), Is.True);
            Assert.That(Enumeration.IsFlags <IntEnum>(), Is.False);
            Assert.That(Enumeration.AssertFlags <ZeroFlags>, Throws.Nothing);
            Assert.That(Enumeration.AssertFlags <IntEnum>, Throws
                        .ArgumentException
                        .With.Message.Contains("IntEnum"));

            Assert.That(Enumeration.IsDefined(StringComparison.Ordinal), Is.True);
            Assert.That(Enumeration.IsDefined((StringComparison)100), Is.False);
            Assert.That(Enumeration.IsDefined <LongEnum>(1L), Is.True);
            Assert.That(Enumeration.IsDefined <StringComparison>("undefined"), Is.False);
            Assert.That(Enumeration.IsDefined <StringComparison>("oRDinaL", ignoreCase: true), Is.True);

            Assert.That(() => Enumeration.AssertDefined(StringComparison.Ordinal), Throws.Nothing);
            Assert.That(() => Enumeration.AssertDefined <StringComparison>("ordinal"), Throws
                        .InstanceOf <InvalidEnumArgumentException>()
                        .With.Message.Contains("ordinal")
                        .And.With.Message.Contains("StringComparison"));

            Assert.That(Enumeration.GetName(StringComparison.Ordinal), Is.EqualTo("Ordinal"));
            Assert.That(Enumeration.GetName <ULongEnum>(1UL), Is.EqualTo("Two"));
            Assert.That(() => Enumeration.GetName <IntEnum>(100), Throws.InstanceOf <InvalidEnumArgumentException>());

            Assert.That(Enumeration.GetValues <StringSplitOptions>(), Is.EquivalentTo(
                            new[] { StringSplitOptions.None, StringSplitOptions.RemoveEmptyEntries }));
            Assert.That(Enumeration.GetValue <ULongEnum, ulong>(ULongEnum.Two), Is.EqualTo(1UL));

            Assert.That(Enumeration.Cast <ByteEnum>((byte)1), Is.EqualTo(ByteEnum.Two));
            StringComparison?value;

            Assert.That(Enumeration.TryCast(100, out value), Is.False);

            Assert.That(Enumeration.Parse <ActionTargets>("Suite"), Is.EqualTo(ActionTargets.Suite));
            Assert.That(() => Enumeration.Parse <ActionTargets>("SUIte", ignoreCase: false),
                        Throws.InstanceOf <InvalidEnumArgumentException>());
            Assert.That(Enumeration.Parse <StringComparison>("4"), Is.EqualTo(StringComparison.Ordinal));
            ActionTargets?parsed;

            Assert.That(() => Enumeration.TryParse("100", out parsed), Is.False);
            Assert.That(Enumeration.TryParse("suiTE", true, out parsed), Is.True);

            var         fourNotSet = NoZeroFlags.Three;
            NoZeroFlags fourSet    = fourNotSet.SetFlag(NoZeroFlags.Four);

            Assert.That(fourSet.HasFlag(NoZeroFlags.Four), Is.True);

            NoZeroFlags fourUnset = fourSet.UnsetFlag(NoZeroFlags.Four);

            Assert.That(fourUnset.HasFlag(NoZeroFlags.Four), Is.False);

            fourSet = fourUnset.ToggleFlag(NoZeroFlags.Four);
            Assert.That(fourSet.HasFlag(NoZeroFlags.Four), Is.True);
            fourNotSet = fourSet.ToggleFlag(NoZeroFlags.Four);
            Assert.That(fourNotSet.HasFlag(NoZeroFlags.Four), Is.False);

            Assert.That(fourSet.GetFlags(), Is.EquivalentTo(new[] { NoZeroFlags.Three, NoZeroFlags.Four }));
        }