Esempio n. 1
0
        public void GetValue_Returns_Correct_Type()
        {
            TypeParameterTest(Utilities.SettingNames.TEST_SETTING_NAME, typeof(string));
            TypeParameterTest(Utilities.SettingNames.TEST_CHARACTER_NAME, typeof(char));
            TypeParameterTest(Utilities.SettingNames.TEST_BOOLEAN_NAME, typeof(bool));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(byte));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(sbyte));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(decimal));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(double));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(float));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(int));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(uint));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(long));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(ulong));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(short));
            TypeParameterTest(Utilities.SettingNames.TEST_INTEGER_NAME, typeof(ushort));

            GenericTypeTest <string>(Utilities.SettingNames.TEST_SETTING_NAME);
            GenericTypeTest <char>(Utilities.SettingNames.TEST_CHARACTER_NAME);
            GenericTypeTest <bool>(Utilities.SettingNames.TEST_BOOLEAN_NAME);
            GenericTypeTest <byte>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <sbyte>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <decimal>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <double>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <float>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <int>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <uint>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <long>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <ulong>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <short>(Utilities.SettingNames.TEST_INTEGER_NAME);
            GenericTypeTest <ushort>(Utilities.SettingNames.TEST_INTEGER_NAME);

            void TypeParameterTest(string setting, Type type)
            {
                try
                {
                    IValueCache cache = new ValueCache();
                    cache.RegisterValue(setting, type);
                    Assert.That(cache.GetValue(setting), Is.TypeOf(type), $"Failed for {type.Name}");
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"Failed for {type.Name}", ex);
                }
            }

            void GenericTypeTest <T>(string setting)
            {
                try
                {
                    IValueCache cache = new ValueCache();
                    cache.RegisterValue(setting, typeof(T));
                    Assert.That(cache.GetValue <T>(setting), Is.TypeOf <T>(), $"Failed for {typeof(T).Name}");
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"Failed for {typeof(T).Name}", ex);
                }
            }
        }
Esempio n. 2
0
        public void GetValue_From_Template_Type_Returns_Not_Null_Or_Empty()
        {
            IValueCache cache = new ValueCache();

            cache.RegisterValue(Utilities.SettingNames.TEST_SETTING_NAME);
            Assert.That(cache.GetValue <string>(Utilities.SettingNames.TEST_SETTING_NAME), Is.Not.Null.And.Not.Empty);
        }
Esempio n. 3
0
        public void GetValue_Throws_Exception_When_Custom_Type_Cast_Not_Provided()
        {
            IValueCache cache = new ValueCache();

            cache.RegisterValue(Utilities.SettingNames.TEST_SETTING_NAME, typeof(TestCustomClass), true, null);
            Assert.That(() => cache.GetValue(Utilities.SettingNames.TEST_SETTING_NAME), Throws.Exception);
        }
Esempio n. 4
0
        public void GetValue_Throws_ConfigurationErrorsException_When_Setting_Not_Found_And_Required()
        {
            IValueCache cache = new ValueCache();

            cache.RegisterValue(Utilities.SettingNames.SETTING_THAT_NOT_EXISTS, typeof(string));
            Assert.That(() => cache.GetValue(Utilities.SettingNames.SETTING_THAT_NOT_EXISTS), Throws.TypeOf <ConfigurationErrorsException>());
        }
Esempio n. 5
0
        public void GetValue_Supports_Custom_Type_Casting()
        {
            IValueCache cache = new ValueCache();

            cache.RegisterValue(Utilities.SettingNames.TEST_SETTING_NAME, typeof(TestCustomClass), true,
                                setting => new TestCustomClass
            {
                TestStringProperty = setting
            }
                                );
            Assert.That(cache.GetValue <TestCustomClass>(Utilities.SettingNames.TEST_SETTING_NAME)?.TestStringProperty, Is.Not.Null.And.EqualTo(ApplicationSettings.AsString(Utilities.SettingNames.TEST_SETTING_NAME)));
        }
Esempio n. 6
0
        public void GetValue_From_Template_Type_Throws_When_Not_Registered()
        {
            IValueCache cache = new ValueCache();

            Assert.That(() => cache.GetValue <string>(Utilities.SettingNames.TEST_SETTING_NAME), Throws.Exception);
        }