Esempio n. 1
0
        public void TestAllFlagsUnset()
        {
            var ex = Assert.Throws <ArgumentException>(() => EnumCode.FlagUnset(FileAccess.Read, "arg00", FileAccess.ReadWrite));

            Assert.That(ex.Message, Does.Contain("arg00"));
            Assert.That(ex.Message, Does.Contain("The value of the arg00 argument "));
            Assert.That(ex.Message, Does.Contain(" should not include any flag from "));

            Assert.DoesNotThrow(() => EnumCode.FlagUnset(FileAccess.Read, "arg00", FileAccess.Write));
        }
Esempio n. 2
0
        public void TestEnumUseCase()
        {
            var permissions = PermittedActions.None;
            var readOrOwner = PermittedActions.Read | PermittedActions.SetOwner;

            Assert.AreEqual(permissions, PermittedActions.None);

            permissions = permissions.SetFlag(PermittedActions.Read);
            Assert.AreEqual(permissions, PermittedActions.Read);

            permissions = permissions.SetFlag(PermittedActions.Write);
            Assert.AreEqual(permissions, PermittedActions.ReadWrite);

            permissions = permissions.ClearFlag(PermittedActions.Write);
            Assert.AreEqual(permissions, PermittedActions.Read);

            // conditional set or clear
            permissions = permissions.SetFlag(PermittedActions.ReadContent, enabled: false);
            Assert.AreEqual(permissions, PermittedActions.ReadMetadata);

            permissions = permissions.SetFlag(PermittedActions.ReadWrite, enabled: true);
            Assert.AreEqual(permissions, PermittedActions.ReadWrite);

            // Checks that entire bit combination is set
            Assert.IsFalse(permissions.IsFlagSet(readOrOwner));
            Assert.IsTrue(permissions.IsFlagSet(PermittedActions.Read));
            Assert.IsFalse(permissions.IsFlagSet(PermittedActions.SetOwner));

            // Checks that any bit is NOT set
            Assert.IsTrue(permissions.IsAnyFlagUnset(readOrOwner));
            Assert.IsFalse(permissions.IsAnyFlagUnset(PermittedActions.Read));
            Assert.IsTrue(permissions.IsAnyFlagUnset(PermittedActions.SetOwner));

            // Checks that any bit is set
            Assert.IsTrue(permissions.IsAnyFlagSet(readOrOwner));
            Assert.IsTrue(permissions.IsAnyFlagSet(PermittedActions.Read));
            Assert.IsFalse(permissions.IsAnyFlagSet(PermittedActions.SetOwner));

            // Checks that entire bit combination is NOT set
            Assert.IsFalse(permissions.IsFlagUnset(readOrOwner));
            Assert.IsFalse(permissions.IsFlagUnset(PermittedActions.Read));
            Assert.IsTrue(permissions.IsFlagUnset(PermittedActions.SetOwner));

            // Assertions
            Assert.DoesNotThrow(() => EnumCode.FlagSet(permissions, nameof(permissions), PermittedActions.Read));
            Assert.DoesNotThrow(() => EnumCode.AnyFlagUnset(permissions, nameof(permissions), readOrOwner));
            Assert.DoesNotThrow(() => EnumCode.AnyFlagSet(permissions, nameof(permissions), PermittedActions.Read));
            Assert.DoesNotThrow(() => EnumCode.FlagUnset(permissions, nameof(permissions), PermittedActions.SetOwner));
        }
Esempio n. 3
0
        public void TestFlagUnset()
        {
            // Arg assertion
            Exception ex = Assert.Throws <ArgumentException>(
                () => EnumCode.FlagUnset(FileAccess.Read, "arg00", FileAccess.ReadWrite));

            Assert.That(ex.Message, Does.Contain("arg00"));
            Assert.That(ex.Message, Does.Contain("The value of the arg00 argument "));
            Assert.That(ex.Message, Does.Contain(" should not include any flag from "));

            Assert.DoesNotThrow(() => EnumCode.FlagUnset(FileAccess.Read, "arg00", FileAccess.Write));

            // State assertion
            ex = Assert.Throws <InvalidOperationException>(
                () => EnumCode.StateFlagUnset(FileAccess.Read, FileAccess.ReadWrite, "someUniqueMessage"));
            Assert.AreEqual(ex.Message, "someUniqueMessage");

            ex = Assert.Throws <InvalidOperationException>(
                () => EnumCode.StateFlagUnset(FileAccess.Read, FileAccess.ReadWrite, "someUniqueMessage {0}", "someUniqueFormatArg"));
            Assert.AreEqual(ex.Message, "someUniqueMessage someUniqueFormatArg");

            Assert.DoesNotThrow(() => EnumCode.StateFlagUnset(FileAccess.Read, FileAccess.Write, "someUniqueMessage"));
        }