#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.IO;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers
{
    public class TextFormatTest
    {
        private static readonly string AllFieldsSetText = TestResources.text_format_unittest_data;
        private static readonly string AllExtensionsSetText = TestResources.text_format_unittest_extensions_data;
        /// <summary>
        /// Note that this is slightly different to the Java - 123.0 becomes 123, and 1.23E17 becomes 1.23E+17.
        /// Both of these differences can be parsed by the Java and the C++, and we can parse their output too.
        /// </summary>
        private const string ExoticText =
            "repeated_int32: -1\n" +
            "repeated_int32: -2147483648\n" +
            "repeated_int64: -1\n" +
            "repeated_int64: -9223372036854775808\n" +
            "repeated_uint32: 4294967295\n" +
            "repeated_uint32: 2147483648\n" +
            "repeated_uint64: 18446744073709551615\n" +
            "repeated_uint64: 9223372036854775808\n" +
            "repeated_double: 123\n" +
            "repeated_double: 123.5\n" +
            "repeated_double: 0.125\n" +
            "repeated_double: 1.23E+17\n" +
            "repeated_double: 1.235E+22\n" +
            "repeated_double: 1.235E-18\n" +
            "repeated_double: 123.456789\n" +
            "repeated_double: Infinity\n" +
            "repeated_double: -Infinity\n" +
            "repeated_double: NaN\n" +
            "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" +
            "\\341\\210\\264\"\n" +
            "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n";
        private const string MessageSetText =
            "[protobuf_unittest.TestMessageSetExtension1] {\n" +
            "  i: 123\n" +
            "}\n" +
            "[protobuf_unittest.TestMessageSetExtension2] {\n" +
            "  str: \"foo\"\n" +
            "}\n";
        /// <summary>
        /// Print TestAllTypes and compare with golden file. 
        /// </summary>
        [Test]
        public void PrintMessage()
        {
            TestUtil.TestInMultipleCultures(() =>
            {
                string text = TextFormat.PrintToString(TestUtil.GetAllSet());
                Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n").Trim(),
                                text.Replace("\r\n", "\n").Trim());
            });
        }
        /// <summary>
        /// Tests that a builder prints the same way as a message.
        /// </summary>
        [Test]
        public void PrintBuilder()
        {
            TestUtil.TestInMultipleCultures(() =>
            {
                string messageText = TextFormat.PrintToString(TestUtil.GetAllSet());
                string builderText = TextFormat.PrintToString(TestUtil.GetAllSet().ToBuilder());
                Assert.AreEqual(messageText, builderText);
            });
        }
        /// <summary>
        /// Print TestAllExtensions and compare with golden file.
        /// </summary>
        [Test]
        public void PrintExtensions()
        {
            string text = TextFormat.PrintToString(TestUtil.GetAllExtensionsSet());
            Assert.AreEqual(AllExtensionsSetText.Replace("\r\n", "\n").Trim(), text.Replace("\r\n", "\n").Trim());
        }
        /// <summary>
        /// Test printing of unknown fields in a message.
        /// </summary>
        [Test]
        public void PrintUnknownFields()
        {
            TestEmptyMessage message =
                TestEmptyMessage.CreateBuilder()
                    .SetUnknownFields(
                        UnknownFieldSet.CreateBuilder()
                            .AddField(5,
                                      UnknownField.CreateBuilder()
                                          .AddVarint(1)
                                          .AddFixed32(2)
                                          .AddFixed64(3)
                                          .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
                                          .AddGroup(
                                              UnknownFieldSet.CreateBuilder()
                                                  .AddField(10,
                                                            UnknownField.CreateBuilder()
                                                                .AddVarint(5)
                                                                .Build())
                                                  .Build())
                                          .Build())
                            .AddField(8,
                                      UnknownField.CreateBuilder()
                                          .AddVarint(1)
                                          .AddVarint(2)
                                          .AddVarint(3)
                                          .Build())
                            .AddField(15,
                                      UnknownField.CreateBuilder()
                                          .AddVarint(0xABCDEF1234567890L)
                                          .AddFixed32(0xABCD1234)
                                          .AddFixed64(0xABCDEF1234567890L)
                                          .Build())
                            .Build())
                    .Build();
            Assert.AreEqual(
                "5: 1\n" +
                "5: 0x00000002\n" +
                "5: 0x0000000000000003\n" +
                "5: \"4\"\n" +
                "5 {\n" +
                "  10: 5\n" +
                "}\n" +
                "8: 1\n" +
                "8: 2\n" +
                "8: 3\n" +
                "15: 12379813812177893520\n" +
                "15: 0xabcd1234\n" +
                "15: 0xabcdef1234567890\n",
                TextFormat.PrintToString(message));
        }
        /// <summary>
        /// Helper to construct a ByteString from a string containing only 8-bit
        /// characters. The characters are converted directly to bytes, *not*
        /// encoded using UTF-8.
        /// </summary>
        private static ByteString Bytes(string str)
        {
            byte[] bytes = new byte[str.Length];
            for (int i = 0; i < bytes.Length; i++)
                bytes[i] = (byte)str[i];
            return ByteString.CopyFrom(bytes);
        }
        [Test]
        public void PrintExotic()
        {
            IMessage message = TestAllTypes.CreateBuilder()
                // Signed vs. unsigned numbers.
                .AddRepeatedInt32(-1)
                .AddRepeatedUint32(uint.MaxValue)
                .AddRepeatedInt64(-1)
                .AddRepeatedUint64(ulong.MaxValue)
                .AddRepeatedInt32(1 << 31)
                .AddRepeatedUint32(1U << 31)
                .AddRepeatedInt64(1L << 63)
                .AddRepeatedUint64(1UL << 63)
                // Floats of various precisions and exponents.
                .AddRepeatedDouble(123)
                .AddRepeatedDouble(123.5)
                .AddRepeatedDouble(0.125)
                .AddRepeatedDouble(123e15)
                .AddRepeatedDouble(123.5e20)
                .AddRepeatedDouble(123.5e-20)
                .AddRepeatedDouble(123.456789)
                .AddRepeatedDouble(Double.PositiveInfinity)
                .AddRepeatedDouble(Double.NegativeInfinity)
                .AddRepeatedDouble(Double.NaN)
                // Strings and bytes that needing escaping.
                .AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
                .AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
                .Build();
            Assert.AreEqual(ExoticText, message.ToString());
        }
        [Test]
        public void PrintMessageSet()
        {
            TestMessageSet messageSet =
                TestMessageSet.CreateBuilder()
                    .SetExtension(
                        TestMessageSetExtension1.MessageSetExtension,
                        TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
                    .SetExtension(
                        TestMessageSetExtension2.MessageSetExtension,
                        TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
                    .Build();
            Assert.AreEqual(MessageSetText, messageSet.ToString());
        }
        // =================================================================
        [Test]
        public void Parse()
        {
            TestUtil.TestInMultipleCultures(() =>
                                                {
                                                    TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
                                                    TextFormat.Merge(AllFieldsSetText, builder);
                                                    TestUtil.AssertAllFieldsSet(builder.Build());
                                                });
        }
        [Test]
        public void ParseReader()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
            TestUtil.AssertAllFieldsSet(builder.Build());
        }
        [Test]
        public void ParseExtensions()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            TextFormat.Merge(AllExtensionsSetText,
                             TestUtil.CreateExtensionRegistry(),
                             builder);
            TestUtil.AssertAllExtensionsSet(builder.Build());
        }
        [Test]
        public void ParseCompatibility()
        {
            string original = "repeated_float: inf\n" +
                              "repeated_float: -inf\n" +
                              "repeated_float: nan\n" +
                              "repeated_float: inff\n" +
                              "repeated_float: -inff\n" +
                              "repeated_float: nanf\n" +
                              "repeated_float: 1.0f\n" +
                              "repeated_float: infinityf\n" +
                              "repeated_float: -Infinityf\n" +
                              "repeated_double: infinity\n" +
                              "repeated_double: -infinity\n" +
                              "repeated_double: nan\n";
            string canonical = "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_float: NaN\n" +
                               "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_float: NaN\n" +
                               "repeated_float: 1\n" + // Java has 1.0; this is fine
                               "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_double: Infinity\n" +
                               "repeated_double: -Infinity\n" +
                               "repeated_double: NaN\n";
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(original, builder);
            Assert.AreEqual(canonical, builder.Build().ToString());
        }
        [Test]
        public void ParseExotic()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(ExoticText, builder);
            // Too lazy to check things individually.  Don't try to debug this
            // if testPrintExotic() is Assert.Failing.
            Assert.AreEqual(ExoticText, builder.Build().ToString());
        }
        [Test]
        public void ParseMessageSet()
        {
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
            extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
            extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
            TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
            TextFormat.Merge(MessageSetText, extensionRegistry, builder);
            TestMessageSet messageSet = builder.Build();
            Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
            Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
            Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
            Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
        }
        [Test]
        public void ParseNumericEnum()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge("optional_nested_enum: 2", builder);
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
        }
        [Test]
        public void ParseAngleBrackets()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
            Assert.IsTrue(builder.HasOptionalGroup);
            Assert.AreEqual(1, builder.OptionalGroup.A);
        }
        [Test]
        public void ParseComment()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(
                "# this is a comment\n" +
                "optional_int32: 1  # another comment\n" +
                "optional_int64: 2\n" +
                "# EOF comment", builder);
            Assert.AreEqual(1, builder.OptionalInt32);
            Assert.AreEqual(2, builder.OptionalInt64);
        }
        private static void AssertParseError(string error, string text)
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            Exception exception = Assert.Throws<FormatException>(() => TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder));
            Assert.AreEqual(error, exception.Message);
        }
        [Test]
        public void ParseErrors()
        {
            AssertParseError(
                "1:16: Expected \":\".",
                "optional_int32 123");
            AssertParseError(
                "1:23: Expected identifier.",
                "optional_nested_enum: ?");
            AssertParseError(
                "1:18: Couldn't parse integer: Number must be positive: -1",
                "optional_uint32: -1");
            AssertParseError(
                "1:17: Couldn't parse integer: Number out of range for 32-bit signed " +
                "integer: 82301481290849012385230157",
                "optional_int32: 82301481290849012385230157");
            AssertParseError(
                "1:16: Expected \"true\" or \"false\".",
                "optional_bool: maybe");
            AssertParseError(
                "1:18: Expected string.",
                "optional_string: 123");
            AssertParseError(
                "1:18: String missing ending quote.",
                "optional_string: \"ueoauaoe");
            AssertParseError(
                "1:18: String missing ending quote.",
                "optional_string: \"ueoauaoe\n" +
                "optional_int32: 123");
            AssertParseError(
                "1:18: Invalid escape sequence: '\\z'",
                "optional_string: \"\\z\"");
            AssertParseError(
                "1:18: String missing ending quote.",
                "optional_string: \"ueoauaoe\n" +
                "optional_int32: 123");
            AssertParseError(
                "1:2: Extension \"nosuchext\" not found in the ExtensionRegistry.",
                "[nosuchext]: 123");
            AssertParseError(
                "1:20: Extension \"protobuf_unittest.optional_int32_extension\" " +
                "not found in the ExtensionRegistry.",
                "[protobuf_unittest.optional_int32_extension]: 123");
            AssertParseError(
                "1:1: Message type \"protobuf_unittest.TestAllTypes\" has no field " +
                "named \"nosuchfield\".",
                "nosuchfield: 123");
            AssertParseError(


Answer:                 "1:21: Expected \">\".",