<commit_before>﻿using Functional.PatternMatching;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatternMatcher.Tests
{
    public class PatternMatcherTests
    {<commit_msg>Add more tests to bring code coverage up to 100%<commit_after>﻿using Functional.PatternMatching;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PatternMatcherTests
{
    [TestFixture]
    public class PatternMatcherTests
    {
        [Test]
        public void TestMatch()
        {
            Dictionary<string, bool> matched = new Dictionary<string, bool>();

            Action resetMatched = () =>
            {
                matched.Clear();
            };

            Action<string> checkMatched = (m) =>
            {
                Assert.IsTrue(matched.ContainsKey(m));
                Assert.AreEqual(1, matched.Count);
            };

            var pm = PatternMatcher.Match<object>();

            pm = pm.With<int>(0, () => matched.Add("intZero", true))
                   .With<int>(x => matched.Add("int", true))
                   .With<string>(s => matched.Add("string", true))
                   .With<double>(1.0, () => {})
                   .With<_>(() => matched.Add("wildcard", true));

            pm.Return();
            checkMatched("wildcard");
            resetMatched();

            pm.Return(0.0);
            checkMatched("wildcard");
            resetMatched();

            pm = (new object()).Match();

            pm = pm.With<int>(0, () => matched.Add("intZero", true))
                   .With<int>(x => matched.Add("int", true))
                   .With<string>(s => matched.Add("string", true))
                   .With<_>(() => matched.Add("wildcard", true));

            pm.Return();
            checkMatched("wildcard");
            resetMatched();

            Action<object> matcher =
                PatternMatcher.Match()
                    .With<int>(0, () => matched.Add("intZero", true))
                    .With<int>(x => matched.Add("int", true))
                    .With<string>(s => matched.Add("string", true))
                    .With<_>(() => matched.Add("wildcard", true))
                    .Return;

            matcher(0);
            checkMatched("intZero");
            resetMatched();

            matcher(10);
            checkMatched("int");
            resetMatched();

            matcher("string");
            checkMatched("string");
            resetMatched();

            matcher(new { Unmatched = true });
            checkMatched("wildcard");
            resetMatched();
        }

        [Test]
        public void TestMatchWithResult()
        {
            var pm = PatternMatcher.Match<object, string>()
                .With<int>(0, () => "Zero")
                .With<int>(x => x.ToString())
                .With<string>(s => s)
                .With<double>(1.0, () => "One Point Zero")
                .With<_>(() => "Wildcard");

            Assert.AreEqual("Wildcard", pm.Return());
            Assert.AreEqual("Wildcard", pm.Return(0.0));

            pm = (new object()).Match<object, string>();
            pm = (new object()).MatchWithResult<string>();

            pm = pm.With<int>(0, () => "Zero")
                   .With<int>(x => x.ToString())
                   .With<string>(s => s)
                   .With<double>(1.0, () => "One Point Zero")
                   .With<_>(() => "Wildcard");

            Assert.AreEqual("Wildcard", pm.Return());
            Assert.AreEqual("Wildcard", pm.Return(0.0));

            Func<object, string> matcher =
                PatternMatcher.MatchWithResult<string>()
                    .With<int>(0, () => "Zero")
                    .With<int>(x => x.ToString())
                    .With<string>(s => s)
                    .With<_>(() => "Wildcard")
                    .Return;

            Assert.AreEqual("Zero", matcher(0));
            Assert.AreEqual("string", matcher("string"));
            Assert.AreEqual("10", matcher(10));
            Assert.AreEqual("Wildcard", matcher(new { Unmatched = true }));
        }

        [Test]
        public void TestExceptions()
        {
            var pm = PatternMatcher.Match()
                .With<int>(0, () => { })
                .With<int>(x => { })
                .With<string>(s => { })
                .With<_>(() => { });

            Assert.Throws(typeof(MatchFailureException),
                () => pm.With<int>(0, () => { }));

            Assert.Throws(typeof(MatchFailureException),
                () => pm.With<int>(i => { }));

            Assert.Throws(typeof(MatchFailureException),
                () => pm.With<_>(() => { }));

            pm = PatternMatcher.Match();

            Assert.Throws(typeof(MatchFailureException),
                () => pm.Return());

            Assert.Throws(typeof(MatchFailureException),
                () => pm.Return("any value"));

            var pm2 = PatternMatcher.MatchWithResult<string>()
                .With<int>(0, () => "Zero")
                .With<int>(x => x.ToString())
                .With<string>(s => s)
                .With<_>(() => "Wildcard");

            Assert.Throws(typeof(MatchFailureException),
                () => pm2.With<int>(0, () => "Zero"));

            Assert.Throws(typeof(MatchFailureException),
                () => pm2.With<int>(i => i.ToString()));

            Assert.Throws(typeof(MatchFailureException),
                () => pm2.With<_>(() => "Wildcard"));

            pm2 = PatternMatcher.MatchWithResult<string>();

            Assert.Throws(typeof(MatchFailureException),
                () => pm2.Return());

            Assert.Throws(typeof(MatchFailureException),
                () => pm2.Return("any value"));
        }

        [Test]
        public void TestStaticVars()
        {
            var wildcard = _.Instance;
        }
{'fraction_non_alphanumeric': 0.16316051018717906, 'fraction_numerical': 0.006791452708298824, 'mean_word_length': 1.6217976552323057, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 109, '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': '4257681', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1569, 'n_words': 268}