|
3 | 3 | using System.Collections; |
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.Collections.Immutable; |
| 6 | +using System.Linq; |
6 | 7 | using NFluent; |
7 | 8 | using NiceTry; |
8 | 9 | using NUnit.Framework; |
@@ -471,19 +472,6 @@ public void ShouldExtenedListByFirstMethod() |
471 | 472 | Check.That(first).IsEqualTo("A"); |
472 | 473 | } |
473 | 474 |
|
474 | | - [Test] |
475 | | - public void ShouldReturnNullFirstElementOfEmptyCollection() |
476 | | - { |
477 | | - // given |
478 | | - ICollection<int?> collection = Lists.EmptyList<int?>(); |
479 | | - |
480 | | - // when |
481 | | - var first = collection.First(); |
482 | | - |
483 | | - // then |
484 | | - Check.That(first).IsNull(); |
485 | | - } |
486 | | - |
487 | 475 | [Test] |
488 | 476 | public void ShouldExtenedLinkedListByLastMethod() |
489 | 477 | { |
@@ -512,19 +500,6 @@ public void ShouldExtenedListByLastMethod() |
512 | 500 | Check.That(first).IsEqualTo("C"); |
513 | 501 | } |
514 | 502 |
|
515 | | - [Test] |
516 | | - public void ShouldReturnNullLastElementOfEmptyCollection() |
517 | | - { |
518 | | - // given |
519 | | - ICollection<int?> collection = Lists.EmptyList<int?>(); |
520 | | - |
521 | | - // when |
522 | | - var first = collection.Last(); |
523 | | - |
524 | | - // then |
525 | | - Check.That(first).IsNull(); |
526 | | - } |
527 | | - |
528 | 503 | [Test] |
529 | 504 | public void ShouldExtendLinkedListByRemoveFirstMethod() |
530 | 505 | { |
@@ -727,5 +702,221 @@ public void ShouldExtenedEnumerableByDistinctElementCountMethod() |
727 | 702 | Check.That(setDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 1, "C", 1)); |
728 | 703 | Check.That(listDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 3, "C", 2, "D", 4)); |
729 | 704 | } |
| 705 | + |
| 706 | + [Test] |
| 707 | + [TestCaseSource("TopSliceCases")] |
| 708 | + public IList<string> ShouldReturnTopSliceOfList(IList<string> list, int size) |
| 709 | + { |
| 710 | + // when |
| 711 | + var result = list.TopSlice(size); |
| 712 | + |
| 713 | + // then |
| 714 | + return result; |
| 715 | + } |
| 716 | + |
| 717 | + private static IEnumerable<ITestCaseData> TopSliceCases() |
| 718 | + { |
| 719 | + var givenList = Lists.AsList("A", "B", "C"); |
| 720 | + var givenSize = 3; |
| 721 | + var expectedSlice = Lists.AsList("A", "B", "C"); |
| 722 | + yield return new TestCaseData(givenList, givenSize) |
| 723 | + .SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 724 | + .Returns(expectedSlice); |
| 725 | + |
| 726 | + givenList = Lists.AsList("A", "B", "C"); |
| 727 | + givenSize = 2; |
| 728 | + expectedSlice = Lists.AsList("B", "C"); |
| 729 | + yield return new TestCaseData(givenList, givenSize) |
| 730 | + .SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 731 | + .Returns(expectedSlice); |
| 732 | + |
| 733 | + givenList = Lists.AsList("A", "B", "C"); |
| 734 | + givenSize = 1; |
| 735 | + expectedSlice = Lists.AsList("C"); |
| 736 | + yield return new TestCaseData(givenList, givenSize) |
| 737 | + .SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 738 | + .Returns(expectedSlice); |
| 739 | + |
| 740 | + givenList = Lists.AsList("A", "B", "C"); |
| 741 | + givenSize = 5; |
| 742 | + expectedSlice = Lists.AsList("A", "B", "C"); |
| 743 | + yield return new TestCaseData(givenList, givenSize) |
| 744 | + .SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 745 | + .Returns(expectedSlice); |
| 746 | + |
| 747 | + givenList = Lists.AsList("A", "B", "C"); |
| 748 | + givenSize = 0; |
| 749 | + expectedSlice = Lists.EmptyList<string>(); |
| 750 | + yield return new TestCaseData(givenList, givenSize) |
| 751 | + .SetName(Strings.Format("Top {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 752 | + .Returns(expectedSlice); |
| 753 | + } |
| 754 | + |
| 755 | + [Test] |
| 756 | + [TestCaseSource("BottomSliceCases")] |
| 757 | + public IList<string> ShouldTrimCollectionRight(IList<string> list, int size) |
| 758 | + { |
| 759 | + // when |
| 760 | + var result = list.BottomSlice(size); |
| 761 | + |
| 762 | + // then |
| 763 | + return result; |
| 764 | + } |
| 765 | + |
| 766 | + private static IEnumerable<ITestCaseData> BottomSliceCases() |
| 767 | + { |
| 768 | + var givenList = Lists.AsList("A", "B", "C"); |
| 769 | + var givenSize = 3; |
| 770 | + var expectedSlice = Lists.AsList("A", "B", "C"); |
| 771 | + yield return new TestCaseData(givenList, givenSize) |
| 772 | + .SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 773 | + .Returns(expectedSlice); |
| 774 | + |
| 775 | + givenList = Lists.AsList("A", "B", "C"); |
| 776 | + givenSize = 2; |
| 777 | + expectedSlice = Lists.AsList("A", "B"); |
| 778 | + yield return new TestCaseData(givenList, givenSize) |
| 779 | + .SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 780 | + .Returns(expectedSlice); |
| 781 | + |
| 782 | + givenList = Lists.AsList("A", "B", "C"); |
| 783 | + givenSize = 1; |
| 784 | + expectedSlice = Lists.AsList("A"); |
| 785 | + yield return new TestCaseData(givenList, givenSize) |
| 786 | + .SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 787 | + .Returns(expectedSlice); |
| 788 | + |
| 789 | + givenList = Lists.AsList("A", "B", "C"); |
| 790 | + givenSize = 5; |
| 791 | + expectedSlice = Lists.AsList("A", "B", "C"); |
| 792 | + yield return new TestCaseData(givenList, givenSize) |
| 793 | + .SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 794 | + .Returns(expectedSlice); |
| 795 | + |
| 796 | + givenList = Lists.AsList("A", "B", "C"); |
| 797 | + givenSize = 0; |
| 798 | + expectedSlice = Lists.EmptyList<string>(); |
| 799 | + yield return new TestCaseData(givenList, givenSize) |
| 800 | + .SetName(Strings.Format("Bottom {0} slice of {1} should return {2}", givenSize, givenList, expectedSlice)) |
| 801 | + .Returns(expectedSlice); |
| 802 | + } |
| 803 | + |
| 804 | + [Test] |
| 805 | + [TestCaseSource("SetLastListElementCases")] |
| 806 | + public IList<string> ShouldReplaceLastElementList(IList<string> elements, string element) |
| 807 | + { |
| 808 | + // given |
| 809 | + var list = new List<string>(elements); |
| 810 | + |
| 811 | + // when |
| 812 | + list.SetLast(element); |
| 813 | + |
| 814 | + // then |
| 815 | + return list; |
| 816 | + } |
| 817 | + |
| 818 | + private static IEnumerable<ITestCaseData> SetLastListElementCases() |
| 819 | + { |
| 820 | + const string element = "X"; |
| 821 | + var givenList = Lists.AsList("A", "B", "C"); |
| 822 | + var expected = Lists.AsList("A", "B", element); |
| 823 | + yield return new TestCaseData(givenList, element) |
| 824 | + .SetName(Strings.Format("Replace last item of {1} by {0} results {2}", element, givenList, expected)) |
| 825 | + .Returns(expected); |
| 826 | + |
| 827 | + givenList = Lists.EmptyList<string>(); |
| 828 | + expected = Lists.AsList(element); |
| 829 | + yield return new TestCaseData(givenList, element) |
| 830 | + .SetName(Strings.Format("Replace last item of {1} by {0} results {2}", element, givenList, expected)) |
| 831 | + .Returns(expected); |
| 832 | + } |
| 833 | + |
| 834 | + [Test] |
| 835 | + [TestCaseSource("SetFirstListElementCases")] |
| 836 | + public IList<string> ShouldReplaceFirstElementList(IList<string> elements, string element) |
| 837 | + { |
| 838 | + // given |
| 839 | + var list = new List<string>(elements); |
| 840 | + |
| 841 | + // when |
| 842 | + list.SetFirst(element); |
| 843 | + |
| 844 | + // then |
| 845 | + return list; |
| 846 | + } |
| 847 | + |
| 848 | + private static IEnumerable<ITestCaseData> SetFirstListElementCases() |
| 849 | + { |
| 850 | + const string element = "X"; |
| 851 | + var givenList = Lists.AsList("A", "B", "C"); |
| 852 | + var expected = Lists.AsList(element, "B", "C"); |
| 853 | + yield return new TestCaseData(givenList, element) |
| 854 | + .SetName(Strings.Format("Replace first item of {1} by {0} results {2}", element, givenList, expected)) |
| 855 | + .Returns(expected); |
| 856 | + |
| 857 | + givenList = Lists.EmptyList<string>(); |
| 858 | + expected = Lists.AsList(element); |
| 859 | + yield return new TestCaseData(givenList, element) |
| 860 | + .SetName(Strings.Format("Replace first item of {1} by {0} results {2}", element, givenList, expected)) |
| 861 | + .Returns(expected); |
| 862 | + } |
| 863 | + |
| 864 | + [Test] |
| 865 | + public void ShouldConvertEnumerableToComparable() |
| 866 | + { |
| 867 | + // given |
| 868 | + IEnumerable<string> enumerable = new[] {"A", "B", "C"}; |
| 869 | + |
| 870 | + // when |
| 871 | + var comparable = enumerable.ToComparable(); |
| 872 | + |
| 873 | + // then |
| 874 | + Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C")); |
| 875 | + Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode()); |
| 876 | + Check.That(comparable.ToString()).IsEqualTo("[A, B, C]"); |
| 877 | + } |
| 878 | + |
| 879 | + [Test] |
| 880 | + public void ShouldConvertListToComparable() |
| 881 | + { |
| 882 | + // given |
| 883 | + IList<string> enumerable = new[] { "A", "B", "C" }; |
| 884 | + |
| 885 | + // when |
| 886 | + var comparable = enumerable.ToComparable(); |
| 887 | + |
| 888 | + // then |
| 889 | + Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C")); |
| 890 | + Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode()); |
| 891 | + Check.That(comparable.ToString()).IsEqualTo("[A, B, C]"); |
| 892 | + } |
| 893 | + |
| 894 | + [Test] |
| 895 | + public void ShouldConvertCollectionToComparable() |
| 896 | + { |
| 897 | + // given |
| 898 | + ICollection<string> enumerable = new[] { "A", "B", "C" }; |
| 899 | + |
| 900 | + // when |
| 901 | + var comparable = enumerable.ToComparable(); |
| 902 | + |
| 903 | + // then |
| 904 | + Check.That(comparable).IsEqualTo(Lists.AsList("A", "B", "C")); |
| 905 | + Check.That(comparable.GetHashCode()).IsEqualTo(enumerable.ToComparable().GetHashCode()); |
| 906 | + Check.That(comparable.ToString()).IsEqualTo("[A, B, C]"); |
| 907 | + } |
| 908 | + |
| 909 | + [Test] |
| 910 | + public void ShouldConvertSetToComparable() |
| 911 | + { |
| 912 | + // given |
| 913 | + ISet<string> enumerable = new HashSet<string>(new [] {"A", "B", "C"}); |
| 914 | + |
| 915 | + // when |
| 916 | + var comparable = enumerable.ToComparable(); |
| 917 | + |
| 918 | + // then |
| 919 | + Check.That(comparable).IsEqualTo(Lists.AsList("C", "B", "A")); |
| 920 | + } |
730 | 921 | } |
731 | 922 | } |
0 commit comments