<commit_before>﻿using System;
using System.Linq;
using NUnit.Framework;
using Sharpy;

namespace Tests.Integration {
    [TestFixture]
    public class Delegate {
        private class TestClas {
            public string Text { get; set; }
        }

        [Test]
        public void Bind_Number_Generators() {
            var doubledGeneration = new Generator(100)
                .ToDelegate(x => x.Integer(20))
                .GenerateSequence(20)
                .Select(x => x * 2);
            var gen = new Generator(100)
                .ToDelegate(x => x.Integer(20));
            var gen2 = new Generator(100)
                .ToDelegate(x => x.Integer(20));
            var addedGenerations = gen.Bind(i => gen2, (i, i1) => i + i1).GenerateSequence(20);

            Assert.IsTrue(addedGenerations.SequenceEqual(doubledGeneration));
        }

        [Test]
        public void Do_Set_String() {
            var gen = new Generator()
                .ToDelegate(g => new TestClas {Text = g.FirstName()})
                .Do(clas => clas.Text = "World")
                .Generate();
            Assert.AreEqual("World", gen.Text);
        }

        [Test]
        public void Filter_Integers_Is_Dividable_By_Two() {
            var numbers = new Generator()
                .ToDelegate(g => g.Integer(20))
                .Filter(x => x % 2 == 0)
                .GenerateSequence(20);
            Assert.IsTrue(numbers.All(x => x % 2 == 0));
        }

        [Test]
        public void Filter_String_Contains_Mail() {
            var mails = new Generator(new Configurement {MailDomains = new[] {"gmail.com", "foobar.com"}})
                .ToDelegate(x => x.MailAddress("bob", "doe"))
                .Filter(x => x.Contains("mail"))
                .GenerateSequence(20);

            Assert.IsTrue(mails.All(s => s.Contains("mail")));
        }

        [Test]
        public void Filter_Threshold_Reached() {
            var badPredicate = new Generator().ToDelegate(g => g.Bool()).Filter(x => false);
            Assert.Throws<ArgumentException>(() => badPredicate());
        }

        [Test]
        public void Map_String_Length() {
            var names = new Generator(20)
                .ToDelegate(x => x.FirstName())
                .GenerateSequence(20);
            var nameLenghts = new Generator(20)
                .ToDelegate(x => x.FirstName())<commit_msg>Add tests for generic generator<commit_after>﻿using System;
using System.Linq;
using NUnit.Framework;
using Sharpy;

namespace Tests.Integration {
    [TestFixture]
    public class Delegate {
        private class TestClas {
            public string Text { get; set; }
        }

        [Test]
        public void Bind_Flattens() {
            var generator = new Generator()
                .ToDelegate(g => g.ToDelegate(g2 => g2.Integer()))
                .Bind(g => g);
            Assert.IsInstanceOf<Generator<int>>(generator);
        }

        [Test]
        public void Bind_Flattens_And_Combines() {
            var doubledGeneration = new Generator(100)
                .ToDelegate(x => x.Integer(20))
                .GenerateSequence(20)
                .Select(x => x * 2);
            var gen = new Generator(100)
                .ToDelegate(x => x.Integer(20));
            var gen2 = new Generator(100)
                .ToDelegate(x => x.Integer(20));
            var combinedGenerators = gen.Bind(i => gen2, (i, i1) => i + i1);

            Assert.IsInstanceOf<Generator<int>>(combinedGenerators);
            Assert.IsTrue(combinedGenerators
                .GenerateSequence(20)
                .SequenceEqual(doubledGeneration)
            );
        }

        [Test]
        public void Do_Set_String() {
            var gen = new Generator()
                .ToDelegate(g => new TestClas {Text = g.FirstName()})
                .Do(clas => clas.Text = "World")
                .Generate();
            Assert.AreEqual("World", gen.Text);
        }

        [Test]
        public void Filter_Custom_Threshold_Reached() {
            var badPredicate = new Generator()
                .ToDelegate(g => g.Bool())
                .Filter(x => false, 1000);
            Assert.Throws<ArgumentException>(() => badPredicate());
        }

        [Test]
        public void Filter_Integers_Are_Dividable_By_Two() {
            var numbers = new Generator()
                .ToDelegate(g => g.Integer(20))
                .Filter(x => x % 2 == 0)
                .GenerateSequence(20);
            Assert.IsTrue(numbers.All(x => x % 2 == 0));
        }

        [Test]
        public void Filter_Integers_Are_Dividable_By_Two_CustomThreshold() {
            var numbers = new Generator()
                .ToDelegate(g => g.Integer(20))
                .Filter(x => x % 2 == 0, 1000)
                .GenerateSequence(20);
            Assert.IsTrue(numbers.All(x => x % 2 == 0));
        }

        [Test]
        public void Filter_String_Contains_Mail() {
            var mails = new Generator(new Configurement {MailDomains = new[] {"gmail.com", "foobar.com"}})
                .ToDelegate(x => x.MailAddress("bob", "doe"))
                .Filter(x => x.Contains("mail"))
                .GenerateSequence(20);

            Assert.IsTrue(mails.All(s => s.Contains("mail")));
        }

        [Test]
        public void Filter_String_Contains_Mail_Custom_ThreshHold() {
            var mails = new Generator(new Configurement {MailDomains = new[] {"gmail.com", "foobar.com"}})
                .ToDelegate(x => x.MailAddress("bob", "doe"))
                .Filter(x => x.Contains("mail"), 1000)
                .GenerateSequence(20);

            Assert.IsTrue(mails.All(s => s.Contains("mail")));
        }

        [Test]
        public void Filter_Threshold_Reached() {
            var badPredicate = new Generator()
                .ToDelegate(g => g.Bool())
                .Filter(x => false);
            Assert.Throws<ArgumentException>(() => badPredicate());
        }

        [Test]
        public void Map_String_Length() {
            var names = new Generator(20)
                .ToDelegate(x => x.FirstName())
                .GenerateSequence(20);
            var nameLenghts = new Generator(20)
                .ToDelegate(x => x.FirstName())
{'fraction_non_alphanumeric': 0.12784588441331, 'fraction_numerical': 0.016557872950167172, 'mean_word_length': 1.6846153846153846, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 60, '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': '96650', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1740, 'n_words': 369}