<commit_before>﻿using System;
using Newtonsoft.Json.Schema;
using System.IO;
using System.Text;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#elif DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Tests
{
    public class DollarQuoteStringTest
    {
        [Test]
        public void ReadDollarQuoteString()
        {
            string json = @"{""NameOfStore"":$$Forest's Bakery And Cafe$$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteStringWithTag()
        {
            string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuotePropertyWithTag()
        {
            string json = @"{$pp$Name'Of""Store$pp$:$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteString2()
        {
            string json = @"{""NameOfStore"":``Forest's Bakery And Cafe``}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteStringWithTag2()
        {
            string json = @"{""NameOfStore"":`tag`Forest's Bakery And Cafe`tag`}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuotePropertyWithTag2()
        {
            string json = @"{`pp`Name'Of""Store`pp`:`tag`Forest's Bakery And Cafe`tag`}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void WriteDollarQuoteJson()
        {
            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T'om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$T'om$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T\"om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$T\"om$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$To$m$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$'$To$$m$'$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$$``m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$'$To$$``m$'$}");
                }
            }

            {
                Assert.Catch(delegate ()
                {
                    dynamic d = new Json.Linq.JObject();
                    d.name = "To``$$m";
                    JsonSerializer js = new JsonSerializer();
                    StringBuilder sb = new StringBuilder();
                    using (var stringW = new StringWriter(sb))
                    using (var jw = new JsonTextWriter(stringW))
                    {
                        jw.DollarType = '$';
                        jw.DollarTag = "$";
                        js.Serialize(jw, d);
                        var str = sb.ToString();
                        Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                    }
                }
                , "Invalid DollarTag or DollarType. Can't be same.");
            }

        }

        [Test]
        public void WriteDollarQuoteJson2()
        {
            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T'om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``T'om``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T\"om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``T\"om``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To`m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``To`m``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To``$$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                }
            }

            {
                Assert.Catch(delegate ()
                {
                    dynamic d = new Json.Linq.JObject();
                    d.name = "To``$$m";
                    JsonSerializer js = new JsonSerializer();
                    StringBuilder sb = new StringBuilder();
                    using (var stringW = new StringWriter(sb))
                    using (var jw = new JsonTextWriter(stringW))
                    {
                        jw.DollarTag = "`";
                        js.Serialize(jw, d);
                        var str = sb.ToString();
                        Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                    }
                }
                , "Invalid DollarTag or DollarType. Can't be same.");
            }
        }        

        [Test]
        public void ReadAndWriteDollarQuoteJson()
        { 

            dynamic d;
            using (var jsonFile = System.IO.File.OpenText("myTest.pjson"))
            using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
            {
#if DEBUG
                jsonTextReader.SetCharBuffer(new char[2]);<commit_msg>Add some test case.<commit_after>﻿using System;
using Newtonsoft.Json.Schema;
using System.IO;
using System.Text;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#elif DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Tests
{
    public class DollarQuoteStringTest
    {
        [Test]
        public void ReadDollarQuoteString()
        {
            string json = @"{""NameOfStore"":$$Forest's Bakery And Cafe$$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteStringWithTag()
        {
            string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuotePropertyWithTag()
        {
            string json = @"{$pp$Name'Of""Store$pp$:$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteString2()
        {
            string json = @"{""NameOfStore"":``Forest's Bakery And Cafe``}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuoteStringWithTag2()
        {
            string json = @"{""NameOfStore"":`tag`Forest's Bakery And Cafe`tag`}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }

        [Test]
        public void ReadDollarQuotePropertyWithTag2()
        {
            string json = @"{`pp`Name'Of""Store`pp`:`tag`Forest's Bakery And Cafe`tag`}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            Assert.AreEqual(@"Name'Of""Store", jsonTextReader.Value);
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }


        [Test]
        public void ReadSomeDollarQuoteString()
        {
            {               
                string str = "{``name``:`'`Tom`'`}";
                dynamic d = JsonConvert.DeserializeObject(str);
                Assert.AreEqual(d.name.Value, "Tom");
            }
            {
                string str = "{``name``:`'`T`om`'`}";
                dynamic d = JsonConvert.DeserializeObject(str);
                Assert.AreEqual(d.name.Value, "T`om");
            }
            {
                string str = "{``name``:`'`T``om`'`}";
                dynamic d = JsonConvert.DeserializeObject(str);
                Assert.AreEqual(d.name.Value, "T``om");
            }
            {
                string str = "{``name``:`''`T`'`om`''`}";
                dynamic d = JsonConvert.DeserializeObject(str);
                Assert.AreEqual(d.name.Value, "T`'`om");
            }
        }

        [Test]
        public void WriteDollarQuoteJson()
        {
            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T'om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$T'om$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T\"om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$T\"om$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$To$m$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$'$To$$m$'$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$$``m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$'$To$$``m$'$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To$'$`'`m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$$To$'$`'`m$$}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T$$o$'$`'`m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarType = '$';
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":$''$T$$o$'$`'`m$''$}");
                }
            }

            {
                Assert.Catch(delegate ()
                {
                    dynamic d = new Json.Linq.JObject();
                    d.name = "To``$$m";
                    JsonSerializer js = new JsonSerializer();
                    StringBuilder sb = new StringBuilder();
                    using (var stringW = new StringWriter(sb))
                    using (var jw = new JsonTextWriter(stringW))
                    {
                        jw.DollarType = '$';
                        jw.DollarTag = "$";
                        js.Serialize(jw, d);
                        var str = sb.ToString();
                        Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                    }
                }
                , "Invalid DollarTag or DollarType. Can't be same.");
            }

        }

        [Test]
        public void WriteDollarQuoteJson2()
        {
            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T'om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``T'om``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T\"om";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``T\"om``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To`m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``To`m``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To``$$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "To`'`$'$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":``To`'`$'$m``}");
                }
            }

            {
                dynamic d = new Json.Linq.JObject();
                d.name = "T``o`'`$'$m";
                JsonSerializer js = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                using (var stringW = new StringWriter(sb))
                using (var jw = new JsonTextWriter(stringW))
                {
                    jw.DollarTag = "";
                    js.Serialize(jw, d);
                    var str = sb.ToString();
                    Assert.AreEqual(str, "{\"name\":`''`T``o`'`$'$m`''`}");
                }
            }

            {
                Assert.Catch(delegate ()
                {
                    dynamic d = new Json.Linq.JObject();
                    d.name = "To``$$m";
                    JsonSerializer js = new JsonSerializer();
                    StringBuilder sb = new StringBuilder();
                    using (var stringW = new StringWriter(sb))
                    using (var jw = new JsonTextWriter(stringW))
                    {
                        jw.DollarTag = "`";
                        js.Serialize(jw, d);
                        var str = sb.ToString();
                        Assert.AreEqual(str, "{\"name\":`'`To``$$m`'`}");
                    }
                }
                , "Invalid DollarTag or DollarType. Can't be same.");
            }
        }        

        [Test]
        public void ReadAndWriteDollarQuoteJson()
        { 

            dynamic d;
            using (var jsonFile = System.IO.File.OpenText("myTest.pjson"))
            using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
            {
#if DEBUG
                jsonTextReader.SetCharBuffer(new char[2]);
{'fraction_non_alphanumeric': 0.12118684461391802, 'fraction_numerical': 0.0005560851604702892, 'mean_word_length': 1.285078961699038, 'pattern_counts': {'":': 34, '<': 3, '<?xml version=': 0, '>': 3, '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': '2006851', 'n_tokens_mistral': 6762, 'n_tokens_neox': 6288, 'n_words': 1328}