<commit_before>﻿using System;
using System.Linq;
using System.Text;

namespace Atata
{
    public static class IUIComponentValueProviderExtensions
    {
        public static TOwner Get<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, out TValue value)
            where TOwner : PageObject<TOwner>
        {
            value = provider.Get();
            return provider.Owner;
        }

        public static TOwner Verify<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            StringBuilder logMessageBuilder = new StringBuilder();
            logMessageBuilder.AppendFormat("{0} {1}", provider.ComponentFullName, provider.ProviderName);

            if (!string.IsNullOrWhiteSpace(verificationMessage))
                logMessageBuilder.Append(" ").AppendFormat(verificationMessage, verificationMessageArgs);

            ATContext.Current.Log.StartVerificationSection(logMessageBuilder.ToString());

            assertAction();

            ATContext.Current.Log.EndSection();

            return provider.Owner;
        }

        public static TOwner Verify<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action<TValue, string> assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                () =>
                {
                    TValue actual = provider.Get();

                    string errorMessage = BuildErrorMessage(provider);
                    assertAction(actual, errorMessage);
                },
                verificationMessage,
                verificationMessageArgs);
        }

        public static TOwner VerifyEquals<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, TValue expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.AreEqual(expected, actual, message),
                "equals \"{0}\"",
                provider.ConvertValueToString(expected));
        }

        public static TOwner VerifyDoesNotEqual<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, TValue unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.AreNotEqual(unexpected, actual, message),
                "does not equal \"{0}\"",
                provider.ConvertValueToString(unexpected));
        }

        public static TOwner VerifyContains<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.Contains(expected, actual, message),
                "contains \"{0}\"",
                expected);
        }

        public static TOwner VerifyStartsWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.StartsWith(expected, actual, message),
                "starts with \"{0}\"",
                expected);
        }

        public static TOwner VerifyEndsWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.EndsWith(expected, actual, message),
                "ends with \"{0}\"",
                expected);
        }

        public static TOwner VerifyMatches<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string pattern)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsMatch(pattern, actual, message),
                "matches pattern \"{0}\"",
                pattern);
        }

        public static TOwner VerifyDoesNotContain<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotContain(unexpected, actual, message),
                "does not contain \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotStartWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotStartWith(unexpected, actual, message),
                "does not start with \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotEndWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotEndWith(unexpected, actual, message),
                "does not end with \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotMatch<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string pattern)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotMatch(pattern, actual, message),
                "does not match pattern \"{0}\"",
                pattern);
        }

        public static TOwner VerifyIsTrue<TOwner>(this IUIComponentValueProvider<bool, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsTrue(actual, message),
                "is {0}",
                bool.TrueString);
        }

        public static TOwner VerifyIsTrue<TOwner>(this IUIComponentValueProvider<bool?, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsTrue(actual, message),
                "is {0}",
                bool.TrueString);
        }

        public static TOwner VerifyIsFalse<TOwner>(this IUIComponentValueProvider<bool, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsFalse(actual, message),
                "is {0}",
                bool.FalseString);
        }

        public static TOwner VerifyIsFalse<TOwner>(this IUIComponentValueProvider<bool?, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsFalse(actual, message),
                "is {0}",
                bool.FalseString);
        }

        public static TOwner VerifyUntilMatchesAny<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, TermMatch match, params string[] expectedValues)
            where TOwner : PageObject<TOwner>
        {
            if (expectedValues == null)
                throw new ArgumentNullException("expectedValues");
            if (expectedValues.Length == 0)
                throw ExceptionFactory.CreateForArgumentEmptyCollection("expectedValues");

            string matchAsString = match.ToString(TermFormat.LowerCase);
            string expectedValuesAsString = expectedValues.ToQuotedValuesListOfString(true);

            return provider.Verify(
                () =>
                {
                    string actualText = null;

                    bool containsText = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actualText = provider.Get();
                        return match.IsMatch(actualText, expectedValues);
                    });

                    if (!containsText)
                    {
                        string errorMessage = DefaultAsserter.BuildAssertionErrorMessage(
                            "String that {0} {1}\"{2}\"".FormatWith(matchAsString, expectedValues.Length > 1 ? "any of " : null, expectedValuesAsString),
                            string.Format("\"{0}\"", actualText),
                            "{0} {1} doesn't match criteria", provider.ComponentFullName, provider.ProviderName);

                        ATAssert.IsTrue(containsText, errorMessage);
                    }
                },
                "{0} {1}{2}",
                matchAsString,
                expectedValues.Length > 1 ? "any of " : null,
                expectedValuesAsString);
        }

        public static TOwner VerifyUntilContains<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, params string[] expectedValues)
            where TOwner : PageObject<TOwner>
        {
            if (expectedValues == null)
                throw new ArgumentNullException("expectedValues");

            expectedValues = expectedValues.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            if (expectedValues.Length == 0)
                throw ExceptionFactory.CreateForArgumentEmptyCollection("expectedValues");

            string matchAsString = TermMatch.Contains.ToString(TermFormat.LowerCase);
            string expectedValuesAsString = expectedValues.ToQuotedValuesListOfString(true);

            return provider.Verify(
                () =>
                {
                    string actualText = null;
                    string notFoundValue = null;

                    bool containsText = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actualText = provider.Get();
                        notFoundValue = expectedValues.FirstOrDefault(value => !actualText.Contains(value));
                        return notFoundValue == null;
                    });

                    if (!containsText)
                    {
                        string errorMessage = DefaultAsserter.BuildAssertionErrorMessage(
                            "String that {0} \"{1}\"".FormatWith(matchAsString, notFoundValue),
                            string.Format("\"{0}\"", actualText),
                            "{0} {1} doesn't match criteria", provider.ComponentFullName, provider.ProviderName);

                        ATAssert.IsTrue(containsText, errorMessage);
                    }
                },
                "{0} {1}",
                matchAsString,
                expectedValuesAsString);
        }

        public static TOwner VerifyUntil<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action<TValue, string> assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                () =>
                {
                    string errorMessage = BuildErrorMessage(provider);
                    TValue actual = default(TValue);

                    bool isSuccess = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actual = provider.Get();
                        try
                        {
                            assertAction(actual, errorMessage);
                            return true;
                        }
                        catch
                        {
                            return false;
                        }
                    });

                    if (!isSuccess)
                        assertAction(actual, errorMessage);
                },
                verificationMessage,
                verificationMessageArgs);
        }

        internal static string BuildErrorMessage<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return string.Format("Invalid {0} {1}", provider.ComponentFullName, provider.ProviderName);
        }
    }
}
<commit_msg>Add VerifyIsNull and VerifyIsNotNull extension methods<commit_after>﻿using System;
using System.Linq;
using System.Text;

namespace Atata
{
    public static class IUIComponentValueProviderExtensions
    {
        public static TOwner Get<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, out TValue value)
            where TOwner : PageObject<TOwner>
        {
            value = provider.Get();
            return provider.Owner;
        }

        public static TOwner Verify<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            StringBuilder logMessageBuilder = new StringBuilder();
            logMessageBuilder.AppendFormat("{0} {1}", provider.ComponentFullName, provider.ProviderName);

            if (!string.IsNullOrWhiteSpace(verificationMessage))
                logMessageBuilder.Append(" ").AppendFormat(verificationMessage, verificationMessageArgs);

            ATContext.Current.Log.StartVerificationSection(logMessageBuilder.ToString());

            assertAction();

            ATContext.Current.Log.EndSection();

            return provider.Owner;
        }

        public static TOwner Verify<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action<TValue, string> assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                () =>
                {
                    TValue actual = provider.Get();

                    string errorMessage = BuildErrorMessage(provider);
                    assertAction(actual, errorMessage);
                },
                verificationMessage,
                verificationMessageArgs);
        }

        public static TOwner VerifyIsTrue<TOwner>(this IUIComponentValueProvider<bool, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsTrue(actual, message),
                "is {0}",
                bool.TrueString);
        }

        public static TOwner VerifyIsTrue<TOwner>(this IUIComponentValueProvider<bool?, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsTrue(actual, message),
                "is {0}",
                bool.TrueString);
        }

        public static TOwner VerifyIsFalse<TOwner>(this IUIComponentValueProvider<bool, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsFalse(actual, message),
                "is {0}",
                bool.FalseString);
        }

        public static TOwner VerifyIsFalse<TOwner>(this IUIComponentValueProvider<bool?, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsFalse(actual, message),
                "is {0}",
                bool.FalseString);
        }

        public static TOwner VerifyIsNull<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsNull(actual, message),
                "is null");
        }

        public static TOwner VerifyIsNotNull<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsNotNull(actual, message),
                "is not null");
        }

        public static TOwner VerifyEquals<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, TValue expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.AreEqual(expected, actual, message),
                "equals \"{0}\"",
                provider.ConvertValueToString(expected));
        }

        public static TOwner VerifyDoesNotEqual<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, TValue unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.AreNotEqual(unexpected, actual, message),
                "does not equal \"{0}\"",
                provider.ConvertValueToString(unexpected));
        }

        public static TOwner VerifyContains<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.Contains(expected, actual, message),
                "contains \"{0}\"",
                expected);
        }

        public static TOwner VerifyStartsWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.StartsWith(expected, actual, message),
                "starts with \"{0}\"",
                expected);
        }

        public static TOwner VerifyEndsWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string expected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.EndsWith(expected, actual, message),
                "ends with \"{0}\"",
                expected);
        }

        public static TOwner VerifyMatches<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string pattern)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.IsMatch(pattern, actual, message),
                "matches pattern \"{0}\"",
                pattern);
        }

        public static TOwner VerifyDoesNotContain<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotContain(unexpected, actual, message),
                "does not contain \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotStartWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotStartWith(unexpected, actual, message),
                "does not start with \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotEndWith<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string unexpected)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotEndWith(unexpected, actual, message),
                "does not end with \"{0}\"",
                unexpected);
        }

        public static TOwner VerifyDoesNotMatch<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, string pattern)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                (actual, message) => ATAssert.DoesNotMatch(pattern, actual, message),
                "does not match pattern \"{0}\"",
                pattern);
        }

        public static TOwner VerifyUntilMatchesAny<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, TermMatch match, params string[] expectedValues)
            where TOwner : PageObject<TOwner>
        {
            if (expectedValues == null)
                throw new ArgumentNullException("expectedValues");
            if (expectedValues.Length == 0)
                throw ExceptionFactory.CreateForArgumentEmptyCollection("expectedValues");

            string matchAsString = match.ToString(TermFormat.LowerCase);
            string expectedValuesAsString = expectedValues.ToQuotedValuesListOfString(true);

            return provider.Verify(
                () =>
                {
                    string actualText = null;

                    bool containsText = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actualText = provider.Get();
                        return match.IsMatch(actualText, expectedValues);
                    });

                    if (!containsText)
                    {
                        string errorMessage = DefaultAsserter.BuildAssertionErrorMessage(
                            "String that {0} {1}\"{2}\"".FormatWith(matchAsString, expectedValues.Length > 1 ? "any of " : null, expectedValuesAsString),
                            string.Format("\"{0}\"", actualText),
                            "{0} {1} doesn't match criteria", provider.ComponentFullName, provider.ProviderName);

                        ATAssert.IsTrue(containsText, errorMessage);
                    }
                },
                "{0} {1}{2}",
                matchAsString,
                expectedValues.Length > 1 ? "any of " : null,
                expectedValuesAsString);
        }

        public static TOwner VerifyUntilContains<TOwner>(this IUIComponentValueProvider<string, TOwner> provider, params string[] expectedValues)
            where TOwner : PageObject<TOwner>
        {
            if (expectedValues == null)
                throw new ArgumentNullException("expectedValues");

            expectedValues = expectedValues.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            if (expectedValues.Length == 0)
                throw ExceptionFactory.CreateForArgumentEmptyCollection("expectedValues");

            string matchAsString = TermMatch.Contains.ToString(TermFormat.LowerCase);
            string expectedValuesAsString = expectedValues.ToQuotedValuesListOfString(true);

            return provider.Verify(
                () =>
                {
                    string actualText = null;
                    string notFoundValue = null;

                    bool containsText = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actualText = provider.Get();
                        notFoundValue = expectedValues.FirstOrDefault(value => !actualText.Contains(value));
                        return notFoundValue == null;
                    });

                    if (!containsText)
                    {
                        string errorMessage = DefaultAsserter.BuildAssertionErrorMessage(
                            "String that {0} \"{1}\"".FormatWith(matchAsString, notFoundValue),
                            string.Format("\"{0}\"", actualText),
                            "{0} {1} doesn't match criteria", provider.ComponentFullName, provider.ProviderName);

                        ATAssert.IsTrue(containsText, errorMessage);
                    }
                },
                "{0} {1}",
                matchAsString,
                expectedValuesAsString);
        }

        public static TOwner VerifyUntil<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider, Action<TValue, string> assertAction, string verificationMessage, params object[] verificationMessageArgs)
            where TOwner : PageObject<TOwner>
        {
            return provider.Verify(
                () =>
                {
                    string errorMessage = BuildErrorMessage(provider);
                    TValue actual = default(TValue);

                    bool isSuccess = ATContext.Current.Driver.Try().Until(_ =>
                    {
                        actual = provider.Get();
                        try
                        {
                            assertAction(actual, errorMessage);
                            return true;
                        }
                        catch
                        {
                            return false;
                        }
                    });

                    if (!isSuccess)
                        assertAction(actual, errorMessage);
                },
                verificationMessage,
                verificationMessageArgs);
        }

        internal static string BuildErrorMessage<TValue, TOwner>(this IUIComponentValueProvider<TValue, TOwner> provider)
            where TOwner : PageObject<TOwner>
        {
            return string.Format("Invalid {0} {1}", provider.ComponentFullName, provider.ProviderName);
        }
    }
}

{'fraction_non_alphanumeric': 0.08716145732720318, 'fraction_numerical': 0.0029362902291079087, 'mean_word_length': 1.692603765733902, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 191, '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': '6034395', 'n_tokens_mistral': 6348, 'n_tokens_neox': 6089, 'n_words': 1306}