|
| 1 | +#region License and Terms |
| 2 | +// MoreLINQ - Extensions to LINQ to Objects |
| 3 | +// Copyright (c) 2024 Andy Romero (armorynode). All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +#endregion |
| 17 | + |
| 18 | +namespace MoreLinq.Test |
| 19 | +{ |
| 20 | + using System.Collections.Generic; |
| 21 | + using NUnit.Framework; |
| 22 | + |
| 23 | + [TestFixture] |
| 24 | + public class SkipLastWhileTest |
| 25 | + { |
| 26 | + [Test] |
| 27 | + public void IsLazy() |
| 28 | + { |
| 29 | + _ = new BreakingSequence<object>().SkipLastWhile(BreakingFunc.Of<object, bool>()); |
| 30 | + } |
| 31 | + |
| 32 | + [TestCase(SourceKind.Sequence)] |
| 33 | + [TestCase(SourceKind.BreakingList)] |
| 34 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 35 | + public void PredicateNeverFalse(SourceKind sourceKind) |
| 36 | + { |
| 37 | + using var sequence = TestingSequence.Of(0, 1, 2, 3, 4); |
| 38 | + |
| 39 | + Assert.That(sequence.ToSourceKind(sourceKind).SkipLastWhile(x => x < 5), Is.Empty); |
| 40 | + } |
| 41 | + |
| 42 | + [TestCase(SourceKind.Sequence)] |
| 43 | + [TestCase(SourceKind.BreakingList)] |
| 44 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 45 | + public void PredicateNeverTrue(SourceKind sourceKind) |
| 46 | + { |
| 47 | + using var sequence = TestingSequence.Of(0, 1, 2, 3, 4); |
| 48 | + |
| 49 | + sequence.ToSourceKind(sourceKind) |
| 50 | + .SkipLastWhile(x => x == 100) |
| 51 | + .AssertSequenceEqual(0, 1, 2, 3, 4); |
| 52 | + } |
| 53 | + |
| 54 | + [TestCase(SourceKind.Sequence)] |
| 55 | + [TestCase(SourceKind.BreakingList)] |
| 56 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 57 | + public void PredicateBecomesTruePartWay(SourceKind sourceKind) |
| 58 | + { |
| 59 | + using var sequence = TestingSequence.Of(0, 1, 2, 3, 4); |
| 60 | + |
| 61 | + sequence.ToSourceKind(sourceKind) |
| 62 | + .SkipLastWhile(x => x > 2) |
| 63 | + .AssertSequenceEqual(0, 1, 2); |
| 64 | + } |
| 65 | + |
| 66 | + [TestCase(SourceKind.Sequence)] |
| 67 | + [TestCase(SourceKind.BreakingList)] |
| 68 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 69 | + public void NeverEvaluatesPredicateWhenSourceIsEmpty(SourceKind sourceKind) |
| 70 | + { |
| 71 | + using var sequence = TestingSequence.Of<int>(); |
| 72 | + |
| 73 | + Assert.That(sequence.ToSourceKind(sourceKind) |
| 74 | + .SkipLastWhile(BreakingFunc.Of<int, bool>()), |
| 75 | + Is.Empty); |
| 76 | + } |
| 77 | + |
| 78 | + [TestCase(SourceKind.Sequence)] |
| 79 | + [TestCase(SourceKind.BreakingList)] |
| 80 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 81 | + public void UsesCollectionCountAtIterationTime(SourceKind sourceKind) |
| 82 | + { |
| 83 | + var list = new List<int> { 1, 2, 3, 4 }; |
| 84 | + var result = list.ToSourceKind(sourceKind).SkipLastWhile(x => x > 2); |
| 85 | + list.Add(5); |
| 86 | + result.AssertSequenceEqual(1, 2); |
| 87 | + } |
| 88 | + |
| 89 | + [TestCase(SourceKind.Sequence)] |
| 90 | + [TestCase(SourceKind.BreakingList)] |
| 91 | + [TestCase(SourceKind.BreakingReadOnlyList)] |
| 92 | + public void KeepsNonTrailingItemsThatMatchPredicate(SourceKind sourceKind) |
| 93 | + { |
| 94 | + using var sequence = TestingSequence.Of(1, 2, 0, 0, 3, 4, 0, 0); |
| 95 | + |
| 96 | + sequence.ToSourceKind(sourceKind) |
| 97 | + .SkipLastWhile(x => x == 0) |
| 98 | + .AssertSequenceEqual(1, 2, 0, 0, 3, 4); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments