Exemple #1
0
        public void Should_return_default_value(PropertyInfo property, object value, object defaultValue, bool isSetting)
        {
            // Arrange
            object root = null;

            if (isSetting)
            {
                root = property.GetValue(null);

                property = property.PropertyType
                           .GetProperty(nameof(ISetting <string> .Value));
            }

            using TempFileCollection tempFiles = new();
            string filePath = tempFiles.AddExtension(".settings");

            tempFiles.AddFile(filePath + ".backup", keepFile: false);

            File.WriteAllText(filePath, SettingsFileContent);

            using GitExtSettingsCache cache = GitExtSettingsCache.Create(filePath);
            RepoDistSettings container   = new(null, cache, SettingLevel.Unknown);
            object           storedValue = null;

            // Act
            AppSettings.UsingContainer(container, () =>
            {
                storedValue = property.GetValue(root);
            });

            // Assert
            Assert.That(storedValue, Is.EqualTo(defaultValue));
        }
        public void Should_return_default_value(PropertyInfo property, object value, object defaultValue, bool isSetting)
        {
            // Arrange
            object root = null;

            if (isSetting)
            {
                root = property.GetValue(null);

                property = property.PropertyType
                           .GetProperty(nameof(ISetting <string> .Value));
            }

            var filePath = Path.GetTempFileName();

            File.WriteAllText(filePath, SettingsFileContent);

            var    container   = new RepoDistSettings(null, GitExtSettingsCache.Create(filePath), SettingLevel.Unknown);
            object storedValue = null;

            // Act
            AppSettings.UsingContainer(container, () =>
            {
                storedValue = property.GetValue(root);
            });

            // Assert
            Assert.That(storedValue, Is.EqualTo(defaultValue));
        }
Exemple #3
0
        public void Should_save_value(PropertyInfo property, object value, object defaultValue, bool isSetting)
        {
            // Arrange
            object root = null;

            if (isSetting)
            {
                root = property.GetValue(null);

                property = property.PropertyType
                           .GetProperty(nameof(ISetting <string> .Value));
            }

            using TempFileCollection tempFiles = new();
            string filePath = tempFiles.AddExtension(".settings");

            tempFiles.AddFile(filePath + ".backup", keepFile: false);

            File.WriteAllText(filePath, SettingsFileContent);

            using GitExtSettingsCache cache = GitExtSettingsCache.Create(filePath);
            RepoDistSettings container   = new(null, cache, SettingLevel.Unknown);
            object           storedValue = null;

            // Act
            AppSettings.UsingContainer(container, () =>
            {
                property.SetValue(root, value);

                storedValue = property.GetValue(root);
            });

            // Assert
            if (Type.GetTypeCode(property.PropertyType) == TypeCode.String)
            {
                if (isSetting)
                {
                    Assert.That(storedValue, Is.EqualTo(value ?? string.Empty));
                }
                else
                {
                    Assert.That(storedValue, Is.EqualTo(value ?? defaultValue));
                }
            }
            else if (Type.GetTypeCode(property.PropertyType) == TypeCode.DateTime)
            {
                // We keep only the date
                Assert.That(storedValue, Is.EqualTo(((DateTime)value).Date));
            }
            else
            {
                Assert.That(storedValue, Is.EqualTo(value));
            }
        }