using System.Diagnostics.CodeAnalysis;
using System.Text;
using Xunit;

namespace Scalider.Text
{

    public class AsciiFoldingTextTransformerTests
    {

        [Fact]
        public void OnNullOrEmptyShouldBeEmpty()
        {
            var transformer = AsciiFoldingTextTransformer.Instance;

            Assert.Equal(0, transformer.Transform(null).Length);
            Assert.Equal(0, transformer.Transform("").Length);
            Assert.Equal(0, transformer.Transform(new char[0]).Length);
        }

        [Fact]
        public void FoldingTo4CharactersShouldNotFail()
        {
            var transformer = AsciiFoldingTextTransformer.Instance;
            var result = transformer.Transform("\u247E");

            Assert.Equal(4, result.Length);
            Assert.Equal("(11)", new string(result));
        }

        /// <summary>
        /// The data for this test comes from
        /// https://github.com/apache/lucenenet/blob/master/src/Lucene.Net.Tests.Analysis.Common/Analysis/Miscellaneous/TestASCIIFoldingFilter.cs
        /// </summary>=
        [Theory]
        [ClassData(typeof(TheoryData<string, string>))]
        [MemberData(nameof(GetLatin1AccentsData))]
        [MemberData(nameof(GetAllFoldingsData))]
        public void ShouldFoldNonAsciiCharacters(string input, string expected)
        {
            var transformer = AsciiFoldingTextTransformer.Instance;
            var actual = transformer.Transform(input);

            Assert.Equal(expected.Length, actual.Length);
            Assert.Equal(expected, new string(actual));
        }

        [Fact]
        public void SurrogateCharactersShouldFoldToNothing()
        {
            var transformer = AsciiFoldingTextTransformer.Instance;
            for (var i = 0xd800; i < 0xe000; i++)
            {
                var chr = (char) i;
                var transformed = transformer.Transform(new[] {chr});

                Assert.Equal(0, transformed.Length);
            }
        }

        [ExcludeFromCodeCoverage]
        public static TheoryData<string, string> GetLatin1AccentsData()
            => StreamHelper.ReadInputAndExpectedFromStream("latin1-accents", Encoding.UTF8);

        [ExcludeFromCodeCoverage]
        public static TheoryData<string, string> GetAllFoldingsData()
            => StreamHelper.ReadInputAndExpectedFromStream("all-foldings", Encoding.UTF8);

    }

}
