<commit_before>﻿// Copyright (c) <NAME> 2014
// See License.txt file for more details

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ThinMvvm.Tests
{
    [TestClass]
    public sealed class SettingsBaseTests
    {
        public sealed class TestSettingsStorage : ISettingsStorage
        {
            public Dictionary<string, object> Values = new Dictionary<string, object>();

            public bool IsDefined( string key )
            {
                return Values.ContainsKey( key );
            }

            public T Get<T>( string key )
            {
                return (T) Values[key];
            }

            public void Set( string key, object value )
            {
                Values[key] = value;
            }
        }

        public sealed class TestSettings : SettingsBase<TestSettings>
        {
            public bool Bool
            {
                get { return Get<bool>(); }
                set { Set( value ); }
            }

            public string String
            {
                get { return Get<string>(); }
                set { Set( value ); }
            }

            public int NoDefault
            {
                get { return Get<int>(); }
                set { Set( value ); }
            }

            public TestSettings( ISettingsStorage storage ) : base( storage ) { }

            protected override SettingsDefaultValues GetDefaultValues()
            {
                return new SettingsDefaultValues
                {
                    { x => x.Bool, () => true },
                    { x => x.String, () => "abc" }
                };
            }
        }

        [TestMethod]
        public void DefaultValueIsRespected()
        {
            var settings = new TestSettings( new TestSettingsStorage() );

            Assert.AreEqual( true, settings.Bool );
            Assert.AreEqual( "abc", settings.String );
        }

        [TestMethod]
        public void DefaultValueIsStored()
        {
            var storage = new TestSettingsStorage();
            var settings = new TestSettings( storage );

            GC.KeepAlive( settings.Bool );
            GC.KeepAlive( settings.String );

            Assert.IsTrue( storage.Values.ContainsKey( "ThinMvvm.Tests.SettingsBaseTests+TestSettings.Bool" ) );
            Assert.AreEqual( true, storage.Values["ThinMvvm.Tests.SettingsBaseTests+TestSettings.Bool"] );
            Assert.IsTrue( storage.Values.ContainsKey( "ThinMvvm.Tests.SettingsBaseTests+TestSettings.String" ) );
            Assert.AreEqual( "abc", storage.Values["ThinMvvm.Tests.SettingsBaseTests+TestSettings.String"] );
        }

        [TestMethod]
        [ExpectedException( typeof( InvalidOperationException ) )]
        public void ExceptionThrownWhenNoDefault()
        {
            var settings = new TestSettings( new TestSettingsStorage() );
            GC.KeepAlive( settings.NoDefault );
        }

        [TestMethod]
        public void SetAndGetWorks()
        {
            var settings = new TestSettings( new TestSettingsStorage() );

            settings.String = "xyz";

            Assert.AreEqual( "xyz", settings.String );
        }
    }
}<commit_msg>More tests for SettingsBase (100% coverage).<commit_after>﻿// Copyright (c) <NAME> 2014
// See License.txt file for more details

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ThinMvvm.Tests
{
    [TestClass]
    public sealed class SettingsBaseTests
    {
        public sealed class TestSettingsStorage : ISettingsStorage
        {
            public Dictionary<string, object> Values = new Dictionary<string, object>();
            public Dictionary<string, int> SetCallsCounts = new Dictionary<string, int>();

            public bool IsDefined( string key )
            {
                return Values.ContainsKey( key );
            }

            public T Get<T>( string key )
            {
                return (T) Values[key];
            }

            public void Set( string key, object value )
            {
                if ( Values.ContainsKey( key ) )
                {
                    Values[key] = value;
                    SetCallsCounts[key]++;
                }
                else
                {
                    Values.Add( key, value );
                    SetCallsCounts.Add( key, 1 );
                }
            }
        }

        public sealed class TestObservableObject : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void FirePropertyChanged()
            {
                PropertyChanged( this, new PropertyChangedEventArgs( "" ) );
            }
        }

        // don't use ObservableCollection since it also implements INotifyPropertyChanged
        public sealed class TestObservableCollection : INotifyCollectionChanged
        {
            public event NotifyCollectionChangedEventHandler CollectionChanged;
            public void FireCollectionChanged()
            {
                CollectionChanged( this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Reset ) );
            }
        }

        public sealed class TestSettings : SettingsBase<TestSettings>
        {
            public bool Bool
            {
                get { return Get<bool>(); }
                set { Set( value ); }
            }

            public string String
            {
                get { return Get<string>(); }
                set { Set( value ); }
            }

            public TestObservableObject ObservableObject
            {
                get { return Get<TestObservableObject>(); }
                set { Set( value ); }
            }

            public TestObservableCollection ObservableCollection
            {
                get { return Get<TestObservableCollection>(); }
                set { Set( value ); }
            }

            public int NoDefault
            {
                get { return Get<int>(); }
                set { Set( value ); }
            }

            public TestSettings( ISettingsStorage storage ) : base( storage ) { }

            protected override SettingsDefaultValues GetDefaultValues()
            {
                return new SettingsDefaultValues
                {
                    { x => x.Bool, () => true },
                    { x => x.String, () => "abc" },
                    { x => x.ObservableObject, () => null },
                    { x => x.ObservableCollection, () => null }
                };
            }
        }


        [TestMethod]
        public void DefaultValueIsRespected()
        {
            var settings = new TestSettings( new TestSettingsStorage() );

            Assert.AreEqual( true, settings.Bool );
            Assert.AreEqual( "abc", settings.String );
        }

        [TestMethod]
        public void DefaultValueIsStored()
        {
            var storage = new TestSettingsStorage();
            var settings = new TestSettings( storage );

            bool b = settings.Bool;
            string s = settings.String;

            Assert.IsTrue( storage.Values.ContainsKey( "ThinMvvm.Tests.SettingsBaseTests+TestSettings.Bool" ) );
            Assert.AreEqual( true, storage.Values["ThinMvvm.Tests.SettingsBaseTests+TestSettings.Bool"] );
            Assert.IsTrue( storage.Values.ContainsKey( "ThinMvvm.Tests.SettingsBaseTests+TestSettings.String" ) );
            Assert.AreEqual( "abc", storage.Values["ThinMvvm.Tests.SettingsBaseTests+TestSettings.String"] );
        }

        [TestMethod]
        [ExpectedException( typeof( InvalidOperationException ) )]
        public void ExceptionThrownWhenNoDefault()
        {
            var settings = new TestSettings( new TestSettingsStorage() );
            int n = settings.NoDefault;
        }

        [TestMethod]
        public void SetAndGetWorks()
        {
            var settings = new TestSettings( new TestSettingsStorage() );

            settings.String = "xyz";

            Assert.AreEqual( "xyz", settings.String );
        }

        [TestMethod]
        public void SettingsAreSavedWhenNestedPropertyChanges()
        {
            var storage = new TestSettingsStorage();
            var settings = new TestSettings( storage );

            settings.ObservableObject = new TestObservableObject();
            settings.ObservableObject.FirePropertyChanged();

            Assert.AreEqual( 2, storage.SetCallsCounts["ThinMvvm.Tests.SettingsBaseTests+TestSettings.ObservableObject"] );
        }

        [TestMethod]
        public void SettingsAreSavedWhenObservableCollectionChanges()
        {

            var storage = new TestSettingsStorage();
            var settings = new TestSettings( storage );

            settings.ObservableCollection = new TestObservableCollection();
            settings.ObservableCollection.FireCollectionChanged();

            Assert.AreEqual( 2, storage.SetCallsCounts["ThinMvvm.Tests.SettingsBaseTests+TestSettings.ObservableCollection"] );
        }
    }
}
{'fraction_non_alphanumeric': 0.08848116382586038, 'fraction_numerical': 0.0015199218325914667, 'mean_word_length': 1.669371196754564, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1767286', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2128, 'n_words': 509}