Пример #1
0
        public async Task Get_first_value_that_match_a_predicate()
        {
            var numberGenerator = new NumberGenerator(10, 13);

            var number = await numberGenerator.GenerateNumbers(100).FirstAsync(x => x == 12);

            number.Should().Be(12);

            var secondNumber = await numberGenerator.GenerateNumbers(100).FirstOrDefaultAsync(x => x == 12);

            secondNumber.Should().Be(12);
        }
Пример #2
0
        public async Task Get_the_first_element_of_an_async_enumerable()
        {
            var numberGenerator = new NumberGenerator(0, 0);

            var number = await numberGenerator.GenerateNumbers(10).FirstAsync();

            number.Should().Be(0);

            var secondNumber = await numberGenerator.GenerateNumbers(10).FirstOrDefaultAsync();

            secondNumber.Should().Be(0);
        }
        public void GenerateNumbers_WhenCalled_Return100Numbers()
        {
            var result    = NumberGenerator.GenerateNumbers();
            var assertion = Enumerable.Range(1, 100);

            Assert.That(result.Count, Is.EqualTo(assertion.Count()));
        }
Пример #4
0
        public async Task Enumerate_all_elements_of_an_async_enumerable(int count)
        {
            var numberGenerator = new NumberGenerator();

            var numbers = await numberGenerator.GenerateNumbers(count).EnumerateAsync();

            numbers.Should().HaveCount(count);
        }
        public void GenerateNumbers_WhenCalled_ReturnCorrectList()
        {
            var result    = NumberGenerator.GenerateNumbers();
            var assertion = Enumerable.Range(1, 100);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.EqualTo(assertion));
        }
Пример #6
0
        public async Task Get_default_value_when_first_or_default_on_empty_collection()
        {
            var numberGenerator = new NumberGenerator(10, 100);

            var number = await numberGenerator.GenerateNumbers(0).FirstOrDefaultAsync();

            number.Should().Be(0);
        }
Пример #7
0
        public void UnenumeratedDoesNoWork()
        {
            var generator = new NumberGenerator();

            generator.GenerateNumbers();

            Assert.AreEqual(0, generator.NumbersGenerated);
        }
        public void GenerateNumbers_ShouldReturnNumbersFrom1To100()
        {
            var expected = Enumerable.Range(1, 100);

            var actual = _target.GenerateNumbers();

            expected.Should().BeEquivalentTo(actual);
        }
Пример #9
0
        public void Throw_error_when_getting_the_first_element_but_no_element_found()
        {
            var numberGenerator = new NumberGenerator(0, 0);

            Func <Task> gettingFirstElementOfEmptyEnumerable = async() => await numberGenerator.GenerateNumbers(0).FirstAsync();

            gettingFirstElementOfEmptyEnumerable
            .Should()
            .Throw <InvalidOperationException>()
            .WithMessage("The sequence contains no element");
        }
Пример #10
0
        public async Task Can_chain_enumeration_with_asynchronous_filter(int maxValue)
        {
            const int count           = 10;
            var       numberGenerator = new NumberGenerator(1, maxValue);

            var numbers = await numberGenerator
                          .GenerateNumbers(count)
                          .WhereAsync(x => Task.FromResult(x < maxValue))
                          .EnumerateAsync();

            numbers.Should().HaveCount(count);
        }
Пример #11
0
        public async Task Can_chain_enumeration_with_asynchronous_projection(int maxValue)
        {
            const int count           = 10;
            var       numberGenerator = new NumberGenerator(1, maxValue);

            var numbers = await numberGenerator
                          .GenerateNumbers(count)
                          .SelectAsync(x => TaskUtils.WaitAndReturn(x * 20))
                          .EnumerateAsync();

            numbers.Should().Match(x => x.All(i => i < maxValue * 20));
        }
Пример #12
0
        public async Task Can_chain_enumeration_with_projection(int maxValue)
        {
            const int count           = 10;
            var       numberGenerator = new NumberGenerator(1, maxValue);

            var numbers = await numberGenerator
                          .GenerateNumbers(count)
                          .SelectAsync(x => x * 10)
                          .EnumerateAsync();

            numbers.Should().Match(x => x.All(i => i < maxValue * 10));
        }
Пример #13
0
        public void GenerateFiveNumbers()
        {
            var generator = new NumberGenerator();

            foreach (int number in generator.GenerateNumbers())
            {
                Console.WriteLine(number);

                if (number == 5)
                {
                    break;
                }
            }

            Assert.AreEqual(5, generator.NumbersGenerated);
        }
Пример #14
0
        private static void Main(string[] args)
        {
            NumberGenerator numberGenerator = new NumberGenerator();

            var tuples = new Tuple <int, string>[]
            {
                new Tuple <int, string>(3, "snap"),
                new Tuple <int, string>(5, "crackle"),
                new Tuple <int, string>(7, "pop")
            };

            IEnumerable <string> numbers = numberGenerator.GenerateNumbers(1, 1000, tuples);

            foreach (var number in numbers)
            {
                Console.WriteLine(number);
            }
        }
Пример #15
0
        public void ChainedNumberGeneratorsAreDependentOnEachOther()
        {
            var firstGenerator = new NumberGenerator();
            var lastGenerator  = new ChainedNumberGenerator();

            foreach (int number in lastGenerator.GenerateNumbers(firstGenerator.GenerateNumbers()))
            {
                Console.WriteLine(number);

                if (number == 5)
                {
                    break;
                }
            }

            Assert.AreEqual(5, lastGenerator.NumbersGenerated);
            Assert.AreEqual(5, firstGenerator.NumbersGenerated);
        }
        public void GenerateNumbers_WhenCalled_ReturnUniqueNumbers()
        {
            var result = NumberGenerator.GenerateNumbers();

            Assert.That(result, Is.Unique);
        }