< Summary

Class:Tests.SearchHandlerShould
Assembly:BuilderProjectsPanelTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Tests/SearchHandlerShould.cs
Covered lines:61
Uncovered lines:6
Coverable lines:67
Total lines:193
Line coverage:91% (61 of 67)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SearchCorrectly()0%110100%
SortCorrectly()0%330100%
ApplyFilterCorrectly()0%110100%
ApplySortCorrectly()0%110100%
SearchItem_Mock(...)0%2100%
ToString()0%110100%
SortItem_Mock(...)0%2100%
Compare(...)0%220100%
ToString()0%2100%
ItemForHandler_Mock(...)0%110100%
Compare(...)0%16.676033.33%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuilderProjectsPanel/Tests/SearchHandlerShould.cs

#LineLine coverage
 1using NUnit.Framework;
 2using System;
 3using System.Collections.Generic;
 4
 5namespace Tests
 6{
 7    public class SearchHandlerShould
 8    {
 9        [Test]
 10        public void SearchCorrectly()
 11        {
 112            List<SearchItem_Mock> list = new List<SearchItem_Mock>
 13            {
 14                new SearchItem_Mock(new[] { "temptation", "good" }),
 15                new SearchItem_Mock(new[] { "temporal", "variable" }),
 16                new SearchItem_Mock(new[] { "20 degrees", "temperature" }),
 17                new SearchItem_Mock(new[] { "I dont", "know" }),
 18                new SearchItem_Mock(new[] { "empty" })
 19            };
 20
 21            const string searchText = "temp";
 122            var result = SearchHelper.Search(searchText, list);
 23
 124            Assert.IsTrue(result.Contains(list[0]), $"keywords {list[0]} should appear while searching {searchText}");
 125            Assert.IsTrue(result.Contains(list[1]), $"keywords {list[1]} should appear while searching {searchText}");
 126            Assert.IsTrue(result.Contains(list[2]), $"keywords {list[2]} should appear while searching {searchText}");
 127            Assert.IsFalse(result.Contains(list[3]), $"keywords {list[3]} shouldn't appear while searching {searchText}"
 128            Assert.IsFalse(result.Contains(list[4]), $"keywords {list[4]} shouldn't appear while searching {searchText}"
 129        }
 30
 31        [Test]
 32        public void SortCorrectly()
 33        {
 134            List<SortItem_Mock> list = new List<SortItem_Mock>
 35            {
 36                new SortItem_Mock(5),
 37                new SortItem_Mock(23),
 38                new SortItem_Mock(-1),
 39                new SortItem_Mock(0),
 40                new SortItem_Mock(-23),
 41                new SortItem_Mock(5),
 42                new SortItem_Mock(2)
 43            };
 44
 145            SearchHelper.Sort("", list, true);
 46
 147            var prev = list[0].value;
 1448            for (int i = 1; i < list.Count; i++)
 49            {
 650                Assert.LessOrEqual(list[i].value, prev);
 651                prev = list[i].value;
 52            }
 53
 154            SearchHelper.Sort("", list, false);
 55
 156            prev = list[0].value;
 1457            for (int i = 1; i < list.Count; i++)
 58            {
 659                Assert.GreaterOrEqual(list[i].value, prev);
 660                prev = list[i].value;
 61            }
 162        }
 63
 64        [Test]
 65        public void ApplyFilterCorrectly()
 66        {
 167            List<ItemForHandler_Mock> list = new List<ItemForHandler_Mock>
 68            {
 69                new ItemForHandler_Mock("ABC", -1),
 70                new ItemForHandler_Mock("DEF", -10),
 71                new ItemForHandler_Mock("GHI", 30),
 72                new ItemForHandler_Mock("JKL", 5),
 73            };
 74
 175            bool filterByOddValue = false;
 76
 177            var searchHandler = new SearchHandler<ItemForHandler_Mock>(
 78                sortingTypes: new [] { "NAME", "VALUE" },
 79                filterPredicate: (item =>
 80                {
 881                    int expected = filterByOddValue ? 0 : 1;
 882                    return item.value % 2 == expected;
 83                }));
 84
 185            searchHandler.SetSearchableList(list);
 86
 187            bool filteredByOddCalled = false;
 88
 89            void ResultFilterOdd(List<ItemForHandler_Mock> oddFiltered)
 90            {
 191                filteredByOddCalled = true;
 692                foreach (var item in oddFiltered)
 93                {
 294                    Assert.AreEqual(0, item.value % 2);
 95                }
 196            }
 97
 198            searchHandler.OnSearchChanged += ResultFilterOdd;
 199            filterByOddValue = true;
 1100            searchHandler.NotifyFilterChanged();
 1101            Assert.IsTrue(filteredByOddCalled);
 1102        }
 103
 104        [Test]
 105        public void ApplySortCorrectly()
 106        {
 1107            List<ItemForHandler_Mock> list = new List<ItemForHandler_Mock>
 108            {
 109                new ItemForHandler_Mock("ABC", -1),
 110                new ItemForHandler_Mock("DEF", -10),
 111                new ItemForHandler_Mock("GHI", 30),
 112                new ItemForHandler_Mock("JKL", 5),
 113            };
 114
 1115            var searchHandler = new SearchHandler<ItemForHandler_Mock>(
 116                sortingTypes: new [] { "NAME", "VALUE" }, filterPredicate: null);
 117
 1118            searchHandler.SetSearchableList(list);
 119
 1120            bool sortCalled = false;
 121
 122            void ResultFilterOdd(List<ItemForHandler_Mock> sortedList)
 123            {
 1124                sortCalled = true;
 1125                Assert.AreEqual("JKL", sortedList[0].name);
 1126                Assert.AreEqual("GHI", sortedList[1].name);
 1127                Assert.AreEqual("DEF", sortedList[2].name);
 1128                Assert.AreEqual("ABC", sortedList[3].name);
 1129            }
 130
 1131            searchHandler.OnSearchChanged += ResultFilterOdd;
 1132            searchHandler.NotifySortOrderChanged(false);
 1133            Assert.IsTrue(sortCalled);
 1134        }
 135
 136        class SearchItem_Mock : ISearchable
 137        {
 5138            public string[] keywords { get; }
 139
 0140            public SearchItem_Mock(string[] keywords) { this.keywords = keywords; }
 141
 5142            public override string ToString() { return String.Join(",", keywords); }
 143        }
 144
 145        class SortItem_Mock : ISortable<SortItem_Mock>
 146        {
 147            public int value;
 148
 0149            public SortItem_Mock(int someValue) { value = someValue; }
 150
 151            public int Compare(string sortType, bool isDescendingOrder, SortItem_Mock other)
 152            {
 34153                if (isDescendingOrder)
 154                {
 13155                    return other.value - value;
 156                }
 157
 21158                return value - other.value;
 159            }
 160
 0161            public override string ToString() { return value.ToString(); }
 162        }
 163
 164        class ItemForHandler_Mock : ISearchable, ISortable<ItemForHandler_Mock>
 165        {
 0166            public string[] keywords { get; }
 167            public int value;
 168            public string name;
 169
 8170            public ItemForHandler_Mock(string name, int someValue)
 171            {
 8172                keywords = new[] { name };
 8173                this.value = someValue;
 8174                this.name = name;
 8175            }
 176
 177            public int Compare(string sortType, bool isDescendingOrder, ItemForHandler_Mock other)
 178            {
 179                switch (sortType)
 180                {
 181                    case "NAME":
 9182                        return isDescendingOrder
 183                            ? String.Compare(keywords[0], other.keywords[0], StringComparison.Ordinal)
 184                            : String.Compare(other.keywords[0], keywords[0], StringComparison.Ordinal);
 185                    case "VALUE":
 0186                        return isDescendingOrder ? value.CompareTo(other.value) : other.value.CompareTo(other);
 187                }
 188
 0189                return 0;
 190            }
 191        }
 192    }
 193}