public void Configuration_GetOrDefault_ValueFactoryNull_ThrowsArgumentNull()
        {
            Configuration configuration = ConfigurationTests.CreateConfiguration(
                TestUtil.GetDefaultValidObjectForRequirement,
                out IConfigurationRequirement requirement);

            Assert.ThrowsException <ArgumentNullException>(() => configuration.GetOrDefault(requirement, null));
        }
        public void Configuration_TryGetOrDefault_RequirementNull_ThrowsArgumentNull()
        {
            Configuration configuration = ConfigurationTests.CreateConfiguration(
                TestUtil.GetDefaultValidObjectForRequirement,
                out IConfigurationRequirement requirement);

            Assert.ThrowsException <ArgumentNullException>(
                () => configuration.TryGetOrDefault <object>(null, () => null, out object dontCare));
        }
        public void Configuration_TryGetOrDefault_ValueNotPresent_UsesValueFactory()
        {
            ConfigurationRequirementType type = ConfigurationRequirementType.Uri;

            Configuration configuration = ConfigurationTests.CreateConfiguration(
                TestUtil.GetDefaultValidObjectForRequirement,
                out IConfigurationRequirement present);
            IConfigurationRequirement notPresent = TestUtil.CreateConfigurationRequirement(type: type);

            Uri expected = (Uri)TestUtil.GetDefaultValidObjectForRequirement(notPresent);

            Assert.IsFalse(configuration.TryGetOrDefault(notPresent, () => expected, out Uri actual));
            Assert.AreEqual(expected, actual);
        }
        public void Configuration_TryGetOrDefault_ValuePresentButWrongType_ConversionSucceeds()
        {
            const long expected    = 8675309L;
            const int  notExpected = 111111;

            ConfigurationRequirementType type = ConfigurationRequirementType.Int32;

            Configuration configuration = ConfigurationTests.CreateConfiguration(
                x => expected,
                out IConfigurationRequirement requirement,
                () => TestUtil.CreateConfigurationRequirement(type: type));

            Assert.IsTrue(configuration.TryGetOrDefault <long>(requirement, () => notExpected, out long actual));
            Assert.AreEqual(expected, actual);
        }
        public void Configuration_TryGetOrDefault_ValuePresentButWrongType_ConversionFails_UsesValueFactory()
        {
            const string expected = "Configuration_TryGetOrDefault";

            // `expected` must be of a different type than `type`
            ConfigurationRequirementType type = ConfigurationRequirementType.Int32;

            Configuration configuration = ConfigurationTests.CreateConfiguration(
                TestUtil.GetDefaultValidObjectForRequirement,
                out IConfigurationRequirement requirement,
                () => TestUtil.CreateConfigurationRequirement(type: type));

            Assert.IsFalse(configuration.TryGetOrDefault <string>(requirement, () => expected, out string actual));
            Assert.AreEqual(expected, actual);
        }
        public void Configuration_GetOrDefault_Succeeds()
        {
            object expected = null;
            Func <IConfigurationRequirement, object> valueFactory =
                x =>
            {
                expected = TestUtil.GetDefaultValidObjectForRequirement(x);
                return(expected);
            };

            Configuration configuration = ConfigurationTests.CreateConfiguration(
                valueFactory,
                out IConfigurationRequirement requirement);

            Assert.AreEqual(expected, configuration.GetOrDefault(requirement, () => null));
        }
        public void Configuration_TryGetOrDefault_Succeeds()
        {
            // The type here needs to match the expected generic type used later.
            ConfigurationRequirementType type = ConfigurationRequirementType.Bool;

            bool expected = false;

            Configuration configuration = ConfigurationTests.CreateConfiguration(
                x =>
            {
                // Use the inverse of the default value for our expected (just in case the default somehow slips in)
                expected = !(bool)TestUtil.GetDefaultValidObjectForRequirement(x);
                return(expected);
            },
                out IConfigurationRequirement requirement,
                () => TestUtil.CreateConfigurationRequirement(type: type));

            Assert.IsTrue(configuration.TryGetOrDefault <bool>(requirement, () => !expected, out bool actual));
            Assert.AreEqual(expected, actual);
        }