<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoreLinq.Pull;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using System.Diagnostics;

namespace MoreLinq.Test.Pull
{
    using System.Globalization;
    using System.IO;

    [TestFixture]
    public class SideEffectsTest
    {
        #region ForEach
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ForEachNullSequence()
        {
            SideEffects.ForEach<int>(null, x => { throw new InvalidOperationException(); });
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ForEachNullAction()
        {
            SideEffects.ForEach(new[] { 1, 2, 3 }, null);
        }

        [Test]
        public void ForEachWithSequence()
        {
            List<int> results = new List<int>();
            SideEffects.ForEach(new[] { 1, 2, 3 }, results.Add);
            results.AssertSequenceEqual(1, 2, 3);
        }
        #endregion

        #region Pipe
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PipeNullSequence()
        {
            SideEffects.Pipe<int>(null, x => { throw new InvalidOperationException(); });
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PipeNullAction()
        {
            SideEffects.Pipe(new[] { 1, 2, 3 }, null);
        }

        [Test]
        public void PipeWithSequence()
        {
            List<int> results = new List<int>();
            var returned = SideEffects.Pipe(new[] { 1, 2, 3 }, results.Add);
            // Lazy - nothing has executed yet
            Assert.That(results, Is.Empty);
            returned.AssertSequenceEqual(1, 2, 3);
            // Now it has...
            results.AssertSequenceEqual(1, 2, 3);
        }

        [Test]
        public void PipeIsLazy()
        {
            new BreakingSequence<int>().Pipe(x => { });
        }

        [Test]
        public void PipeActionOccursBeforeYield()
        {
            var source = new[] { new StringBuilder(), new StringBuilder() };
            // The action will occur "in" the pipe, so by the time Where gets it, the
            // sequence will be empty.
            Assert.That(source.Pipe(sb => sb.Append("x"))
                              .Where(x => x.Length==0), 
                        Is.Empty);
        }
        #endregion

        #region Trace

        [Test]
        public void TraceSequence()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace("the quick brown fox".Split()).Consume(); 
            }));
            trace.AssertSequenceEqual("the", "quick", "brown", "fox");
        }

        [Test]
        public void TraceSequenceWithSomeNullElements()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace(new int?[] { 1, null, 2, null, 3 }).Consume();
            }));
            trace.AssertSequenceEqual("1", string.Empty, "2", string.Empty, "3");
        }

        [Test]
        public void TraceSequenceWithSomeNullReferences()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace(new[] { "the", null, "quick", null, "brown", null, "fox" }).Consume();
            }));

            trace.AssertSequenceEqual("the", string.Empty, "quick", string.Empty, "brown", string.Empty, "fox");
        }

        [Test]
        public void TraceSequenceWithFormatting()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                using (new CurrentThreadCultureScope(CultureInfo.InvariantCulture))
                    SideEffects.Trace(new[] { 1234, 5678 }, "{0:N0}").Consume();
            }));

            trace.AssertSequenceEqual("1,234", "5,678");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TraceSequenceWithNullFormatter()
        {
            SideEffects.Trace(new object[0], (Func<object, string>) null);
        }

        [Test]
        public void TraceSequenceWithFormatter()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace(new int?[] { 1234, null, 5678 }, 
                    n => n.HasValue ? n.Value.ToString("N0") : "#NULL").Consume();
            }));
<commit_msg>Fixed culture-dependent formatting bug in `TraceSequenceWithFormatter` test.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoreLinq.Pull;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using System.Diagnostics;

namespace MoreLinq.Test.Pull
{
    using System.Globalization;
    using System.IO;

    [TestFixture]
    public class SideEffectsTest
    {
        #region ForEach
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ForEachNullSequence()
        {
            SideEffects.ForEach<int>(null, x => { throw new InvalidOperationException(); });
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ForEachNullAction()
        {
            SideEffects.ForEach(new[] { 1, 2, 3 }, null);
        }

        [Test]
        public void ForEachWithSequence()
        {
            List<int> results = new List<int>();
            SideEffects.ForEach(new[] { 1, 2, 3 }, results.Add);
            results.AssertSequenceEqual(1, 2, 3);
        }
        #endregion

        #region Pipe
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PipeNullSequence()
        {
            SideEffects.Pipe<int>(null, x => { throw new InvalidOperationException(); });
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PipeNullAction()
        {
            SideEffects.Pipe(new[] { 1, 2, 3 }, null);
        }

        [Test]
        public void PipeWithSequence()
        {
            List<int> results = new List<int>();
            var returned = SideEffects.Pipe(new[] { 1, 2, 3 }, results.Add);
            // Lazy - nothing has executed yet
            Assert.That(results, Is.Empty);
            returned.AssertSequenceEqual(1, 2, 3);
            // Now it has...
            results.AssertSequenceEqual(1, 2, 3);
        }

        [Test]
        public void PipeIsLazy()
        {
            new BreakingSequence<int>().Pipe(x => { });
        }

        [Test]
        public void PipeActionOccursBeforeYield()
        {
            var source = new[] { new StringBuilder(), new StringBuilder() };
            // The action will occur "in" the pipe, so by the time Where gets it, the
            // sequence will be empty.
            Assert.That(source.Pipe(sb => sb.Append("x"))
                              .Where(x => x.Length==0), 
                        Is.Empty);
        }
        #endregion

        #region Trace

        [Test]
        public void TraceSequence()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace("the quick brown fox".Split()).Consume(); 
            }));
            trace.AssertSequenceEqual("the", "quick", "brown", "fox");
        }

        [Test]
        public void TraceSequenceWithSomeNullElements()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace(new int?[] { 1, null, 2, null, 3 }).Consume();
            }));
            trace.AssertSequenceEqual("1", string.Empty, "2", string.Empty, "3");
        }

        [Test]
        public void TraceSequenceWithSomeNullReferences()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                SideEffects.Trace(new[] { "the", null, "quick", null, "brown", null, "fox" }).Consume();
            }));

            trace.AssertSequenceEqual("the", string.Empty, "quick", string.Empty, "brown", string.Empty, "fox");
        }

        [Test]
        public void TraceSequenceWithFormatting()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                using (new CurrentThreadCultureScope(CultureInfo.InvariantCulture))
                    SideEffects.Trace(new[] { 1234, 5678 }, "{0:N0}").Consume();
            }));

            trace.AssertSequenceEqual("1,234", "5,678");
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TraceSequenceWithNullFormatter()
        {
            SideEffects.Trace(new object[0], (Func<object, string>) null);
        }

        [Test]
        public void TraceSequenceWithFormatter()
        {
            var trace = Lines(CaptureTrace(delegate
            {
                var formatter = System.Globalization.CultureInfo.InvariantCulture;
                SideEffects.Trace(new int?[] { 1234, null, 5678 }, 
                    n => n.HasValue ? n.Value.ToString("N0", formatter) : "#NULL").Consume();
            }));

{'fraction_non_alphanumeric': 0.12101980523033154, 'fraction_numerical': 0.012255170149906992, 'mean_word_length': 1.7942525221644756, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '3502543', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2390, 'n_words': 534}