<commit_before>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;

namespace osu.Framework.Tests.Bindables
{
    [TestFixture]
    public class BindableListTest
    {
        private BindableList<string> bindableStringList;

        [SetUp]
        public void SetUp()
        {
            bindableStringList = new BindableList<string>();
        }

        #region Constructor

        [Test]
        public void TestConstructorDoesNotAddItemsByDefault()
        {
            Assert.IsEmpty(bindableStringList);
        }

        [Test]
        public void TestConstructorWithItemsAddsItemsInternally()
        {
            string[] array =
            {
                "ok", "nope", "random", null, ""
            };

            var bindableList = new BindableList<string>(array);

            Assert.Multiple(() =>
            {
                foreach (string item in array)
                    Assert.Contains(item, bindableList);

                Assert.AreEqual(array.Length, bindableList.Count);
            });
        }

        #endregion

        #region BindTarget

        /// <summary>
        /// Tests binding via the various <see cref="BindableList{T}.BindTarget"/> methods.
        /// </summary>
        [Test]
        public void TestBindViaBindTarget()
        {
            BindableList<int> parentBindable = new BindableList<int>();

            BindableList<int> bindable1 = new BindableList<int>();
            IBindableList<int> bindable2 = new BindableList<int>();

            bindable1.BindTarget = parentBindable;
            bindable2.BindTarget = parentBindable;

            parentBindable.Add(5);

            Assert.That(bindable1[0], Is.EqualTo(5));
            Assert.That(bindable2[0], Is.EqualTo(5));
        }

        #endregion

        #region BindCollectionChanged

        [Test]
        public void TestBindCollectionChangedWithoutRunningImmediately()
        {
            var list = new BindableList<int> { 1 };

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);

            Assert.That(triggeredArgs, Is.Null);
        }

        [Test]
        public void TestBindCollectionChangedWithRunImmediately()
        {
            var list = new BindableList<int>();

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args, true);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(list));
        }

        [Test]
        public void TestBindCollectionChangedNotRunIfBoundToSequenceEqualList()
        {
            var list = new BindableList<int>(new[] { 1, 3, 5, 6 });
            var otherList = new BindableList<int>(new[] { 1, 3, 5, 6 });

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);
            list.BindTo(otherList);

            Assert.That(triggeredArgs, Is.Null);
        }

        [Test]
        public void TestBindCollectionChangedNotRunIfParsingSequenceEqualEnumerable()
        {
            var list = new BindableList<int>(new[] { 99, 100, 101, 102 });
            var enumerable = Enumerable.Range(99, 4);

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);
            list.Parse(enumerable);

            Assert.That(triggeredArgs, Is.Null);<commit_msg>Add positive test coverage<commit_after>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;

namespace osu.Framework.Tests.Bindables
{
    [TestFixture]
    public class BindableListTest
    {
        private BindableList<string> bindableStringList;

        [SetUp]
        public void SetUp()
        {
            bindableStringList = new BindableList<string>();
        }

        #region Constructor

        [Test]
        public void TestConstructorDoesNotAddItemsByDefault()
        {
            Assert.IsEmpty(bindableStringList);
        }

        [Test]
        public void TestConstructorWithItemsAddsItemsInternally()
        {
            string[] array =
            {
                "ok", "nope", "random", null, ""
            };

            var bindableList = new BindableList<string>(array);

            Assert.Multiple(() =>
            {
                foreach (string item in array)
                    Assert.Contains(item, bindableList);

                Assert.AreEqual(array.Length, bindableList.Count);
            });
        }

        #endregion

        #region BindTarget

        /// <summary>
        /// Tests binding via the various <see cref="BindableList{T}.BindTarget"/> methods.
        /// </summary>
        [Test]
        public void TestBindViaBindTarget()
        {
            BindableList<int> parentBindable = new BindableList<int>();

            BindableList<int> bindable1 = new BindableList<int>();
            IBindableList<int> bindable2 = new BindableList<int>();

            bindable1.BindTarget = parentBindable;
            bindable2.BindTarget = parentBindable;

            parentBindable.Add(5);

            Assert.That(bindable1[0], Is.EqualTo(5));
            Assert.That(bindable2[0], Is.EqualTo(5));
        }

        #endregion

        #region BindCollectionChanged

        [Test]
        public void TestBindCollectionChangedWithoutRunningImmediately()
        {
            var list = new BindableList<int> { 1 };

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);

            Assert.That(triggeredArgs, Is.Null);
        }

        [Test]
        public void TestBindCollectionChangedWithRunImmediately()
        {
            var list = new BindableList<int>();

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args, true);

            Assert.That(triggeredArgs.Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
            Assert.That(triggeredArgs.NewItems, Is.EquivalentTo(list));
        }

        [Test]
        public void TestBindCollectionChangedNotRunIfBoundToSequenceEqualList()
        {
            var list = new BindableList<int>(new[] { 1, 3, 5, 6 });
            var otherList = new BindableList<int>(new[] { 1, 3, 5, 6 });

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);
            list.BindTo(otherList);

            Assert.That(triggeredArgs, Is.Null);
        }

        [Test]
        public void TestBindCollectionChangedNotRunIfParsingSequenceEqualEnumerable()
        {
            var list = new BindableList<int>(new[] { 99, 100, 101, 102 });
            var enumerable = Enumerable.Range(99, 4);

            NotifyCollectionChangedEventArgs triggeredArgs = null;
            list.BindCollectionChanged((_, args) => triggeredArgs = args);
            list.Parse(enumerable);

            Assert.That(triggeredArgs, Is.Null);
        }

        [Test]
        public void TestBindCollectionChangedEventsRanIfBoundToDifferentList()
        {
            var list = new BindableList<string>(new[] { "first", "list", "here" });
            var otherList = new BindableList<string>(new[] { "other", "list" });

            var triggeredArgs = new List<NotifyCollectionChangedEventArgs>();
            list.BindCollectionChanged((_, args) => triggeredArgs.Add(args));
            list.BindTo(otherList);

            Assert.That(triggeredArgs, Has.Count.EqualTo(2));

            var removeEvent = triggeredArgs.SingleOrDefault(ev => ev.Action == NotifyCollectionChangedAction.Remove);
            Assert.That(removeEvent, Is.Not.Null);
            Assert.That(removeEvent.OldStartingIndex, Is.EqualTo(0));
            Assert.That(removeEvent.OldItems, Is.EquivalentTo(new[] { "first", "list", "here" }));

            var addEvent = triggeredArgs.SingleOrDefault(ev => ev.Action == NotifyCollectionChangedAction.Add);
            Assert.That(addEvent, Is.Not.Null);
            Assert.That(addEvent.NewStartingIndex, Is.EqualTo(0));
            Assert.That(addEvent.NewItems, Is.EquivalentTo(otherList));
{'fraction_non_alphanumeric': 0.10237619364867866, 'fraction_numerical': 0.007883633133466577, 'mean_word_length': 2.245765765765766, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 57, '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': '6123901', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2332, 'n_words': 494}