Пример #1
0
 public void Append_Simple_OtherLanguages()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("abc");
     fr.Append(@" čćšđž;'[]\");
     Assert.AreEqual(@"abc čćšđž;'[]\", fr.ToString());
 }
Пример #2
0
 public void Append_Simple()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("a");
     fr.Append("b");
     fr.Append("c");
     Assert.AreEqual("abc", fr.ToString());
 }
Пример #3
0
 public void CaseSensitive()
 {
     var fr = new FastReplacer("{", "}");
     fr.Append("{a}");
     Assert.IsTrue(fr.Contains("{a}"));
     Assert.IsFalse(fr.Contains("{A}"));
     Assert.IsFalse(fr.Replace("{A}", "x"));
     Assert.AreEqual("{a}", fr.ToString());
 }
Пример #4
0
 public void CaseInsensitive_OtherLanguages()
 {
     var fr = new FastReplacer("{", "}", false);
     fr.Append("{č}");
     Assert.IsTrue(fr.Contains("{č}"));
     Assert.IsTrue(fr.Contains("{Č}"));
     Assert.IsTrue(fr.Replace("{Č}", "x"));
     Assert.AreEqual("x", fr.ToString());
 }
Пример #5
0
 private static void FailAppend(string text)
 {
     ShouldFail(delegate
     {
         var fr = new FastReplacer("/*", "*/");
         fr.Append(text);
     }, "Expected exception was not thrown while appending \"" + text + "\".");
 }
Пример #6
0
        public void Combination1()
        {
            var fr = new FastReplacer("/*", "*/");
            fr.Append("1");
            fr.Append("2/*a*/3");
            fr.Append("4");
            Assert.AreEqual("12/*a*/34", fr.ToString());

            fr.InsertBefore("/*a*/", "x/*a*/x");
            Assert.AreEqual("12x/*a*/x/*a*/34", fr.ToString());

            fr.InsertBefore("/*a*/", "-");
            Assert.AreEqual("12x-/*a*/x-/*a*/34", fr.ToString());
        }
Пример #7
0
 private static string DoReplacingWithFastReplacer(string initial, int replaceCount)
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append(initial);
     for (int i = 1; i <= replaceCount; i++)
     {
         string token = "/*" + i + "*/";
         string newText = "(/*" + (2 * i) + "*//*" + (2 * i + 1) + "*/)";
         fr.Replace(token, newText);
     }
     return fr.ToString();
 }
Пример #8
0
        public void ReturnValue()
        {
            var fr = new FastReplacer("/*", "*/");
            fr.Append("/*a*/");
            Assert.AreEqual(true, fr.InsertAfter("/*a*/", "1"));
            Assert.AreEqual(true, fr.InsertBefore("/*a*/", "2"));
            Assert.AreEqual(true, fr.Replace("/*a*/", "/*b*/"));
            Assert.AreEqual(false, fr.InsertAfter("/*a*/", "1"));
            Assert.AreEqual(false, fr.InsertBefore("/*a*/", "2"));
            Assert.AreEqual(false, fr.Replace("/*a*/", "/*c*/"));
            Assert.AreEqual("2/*b*/1", fr.ToString());

            Assert.AreEqual(true, fr.Replace("/*b*/", "/*a*/"));
            Assert.AreEqual(false, fr.Replace("/*x*/", "x1"));
            Assert.AreEqual(false, fr.Replace("/*b*/", "x4"));
            Assert.AreEqual(false, fr.Replace("/*c*/", "x5"));
            Assert.AreEqual(true, fr.Replace("/*a*/", "x6"));
            Assert.AreEqual("2x61", fr.ToString());
        }
Пример #9
0
 public void Replace_Simple()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("/*a*/");
     fr.Replace("/*a*/", "123");
     Assert.AreEqual("123", fr.ToString());
 }
Пример #10
0
 public void Replace_RemoveWhenReplaced()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("/*a*/");
     Assert.AreEqual(true, fr.Replace("/*a*/", "123"));
     Assert.AreEqual(false, fr.Replace("/*a*/", "456"));
     Assert.AreEqual("123", fr.ToString());
 }
Пример #11
0
        public void Combination_Order()
        {
            var fr = new FastReplacer("/*", "*/");
            fr.Append("/*a*//*b*/");

            fr.InsertBefore("/*b*/", "1");
            fr.InsertBefore("/*b*/", "2");
            fr.InsertAfter("/*a*/", "3");
            fr.InsertAfter("/*a*/", "4");
            fr.InsertBefore("/*b*/", "5");
            fr.InsertAfter("/*a*/", "6");
            Assert.AreEqual("/*a*/346125/*b*/", fr.ToString());

            fr.Replace("/*a*/", "7");
            fr.Replace("/*b*/", "8");
            Assert.AreEqual("73461258", fr.ToString());
        }
Пример #12
0
 public void IgnoreTokenMarksInMultilineText()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append(@"
     /*a
     */
     /*
     /*b*/
     */");
     Assert.IsTrue(fr.Contains("/*b*/"));
     Assert.IsFalse(fr.Contains("/*a*/"));
     fr.Replace("/*b*/", "123");
     Assert.AreEqual(@"
     /*a
     */
     /*
     123
     */", fr.ToString());
 }
Пример #13
0
 public void InsertBefore_ComplexAndOrder()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("/*a*//*b*/");
     fr.InsertBefore("/*b*/", "1");
     fr.InsertBefore("/*a*/", "2");
     fr.InsertBefore("/*b*/", "3");
     fr.InsertBefore("/*a*/", "4");
     Assert.AreEqual("24/*a*/13/*b*/", fr.ToString());
 }
Пример #14
0
 public void Contains_Test()
 {
     string a = "/*a*/";
     string b = "/*b*/";
     string c = "/*c*/";
     var fr = new FastReplacer("/*", "*/");
     Assert.AreEqual(false, fr.Contains(a));
     fr.Append(a);
     Assert.AreEqual(true, fr.Contains(a));
     fr.Replace(a, b);
     Assert.AreEqual(false, fr.Contains(a));
     Assert.AreEqual(true, fr.Contains(b));
     fr.Replace(b, b);
     Assert.AreEqual(true, fr.Contains(b));
     fr.Replace(b, c);
     Assert.AreEqual(false, fr.Contains(b));
     Assert.AreEqual(c, fr.ToString());
 }
Пример #15
0
        public void Combination_TokenReuse()
        {
            var fr = new FastReplacer("/*", "*/");
            fr.Append("/*a*/");
            fr.Replace("/*a*/", "/*a*/");
            Assert.AreEqual("/*a*/", fr.ToString());

            fr.InsertAfter("/*a*/", "/*a*/");
            Assert.AreEqual("/*a*//*a*/", fr.ToString());

            fr.InsertBefore("/*a*/", "/*a*/");
            Assert.AreEqual("/*a*//*a*//*a*//*a*/", fr.ToString());
        }
Пример #16
0
 public void Combination_OrderAdvanced()
 {
     var fr = new FastReplacer("/*", "*/");
     fr.Append("/*a*/");
     fr.InsertAfter("/*a*/", "1");
     fr.InsertAfter("/*a*/", "2");
     fr.Replace("/*a*/", "/*a*/");
     fr.InsertAfter("/*a*/", "3");
     Assert.AreEqual("/*a*/312", fr.ToString());
 }
Пример #17
0
 private static void FailUsingToken(string token)
 {
     ShouldFail(delegate
     {
         var fr = new FastReplacer("/*", "*/");
         fr.Append("/*a*/");
         fr.Contains(token);
     }, "Expected exception was not thrown while using token \"" + token + "\" in function Contains.");
     ShouldFail(delegate
     {
         var fr = new FastReplacer("/*", "*/");
         fr.Append("/*a*/");
         fr.Replace(token, "x");
     }, "Expected exception was not thrown while using token \"" + token + "\" in function Replace.");
     ShouldFail(delegate
     {
         var fr = new FastReplacer("/*", "*/");
         fr.Append("/*a*/");
         fr.InsertBefore(token, "x");
     },
                "Expected exception was not thrown while using token \"" + token + "\" in function InsertBefore.");
     ShouldFail(delegate
     {
         var fr = new FastReplacer("/*", "*/");
         fr.Append("/*a*/");
         fr.InsertAfter(token, "x");
     },
                "Expected exception was not thrown while using token \"" + token + "\" in function InsertAfter.");
 }
Пример #18
0
        /// <summary>
        /// Gera a AST em Javascript do individuo
        /// </summary>
        /// <param name="codigoIndividuo"></param>
        /// <returns></returns>
        public string GerarAst(string codigoIndividuo)
        {
            var sw = new Stopwatch();
            sw.Start();

            var fr = new FastReplacer("{[#", "#]}");
            fr.Append(@"var syntax = esprima.parse({[#CODIGO#]}, { raw: true, tokens: true, range: true, loc:true, comment: true });");
            fr.Replace("{[#CODIGO#]}", EncodeJsString(codigoIndividuo));
            var esprimaParse = fr.ToString();

            //var engine = _manager.GetEngine();
            var esprimaParseAntigo = string.Format(@"var syntax = esprima.parse({0}, {{ raw: true, tokens: true, range: true, loc:true, comment: true }});", EncodeJsString(codigoIndividuo));
            _engine.Execute(esprimaParse);
            _engine.Execute("javascriptHelper.JsonAst = JSON.stringify(syntax);");

            sw.Stop();

            //Log(string.Format("ast gerada com sucesso"));
            //Log(string.Format(" {0} ms", sw.Elapsed.TotalMilliseconds));

            return JsonAst;
        }
Пример #19
0
        public void PossiblyUnintuitiveBehaviour_IgnoreTokenIfNotFromSingleText()
        {
            // Behaviour is different from standard String.Replace function:
            // Token is ignored if it is composed by concatenation of two different strings.
            var fr = new FastReplacer("/*", "*/");
            fr.Append("/*a*//*b*/");
            fr.Replace("/*a*/", "/");
            fr.Replace("/*b*/", "*c*/");

            Assert.AreEqual("/*c*/", fr.ToString());
            Assert.IsFalse(fr.Contains("/*c*/"));
            bool replaced = fr.Replace("/*c*/", "-");
            Assert.IsFalse(replaced);
            Assert.AreEqual("/*c*/", fr.ToString());
        }
Пример #20
0
        public void Combination2()
        {
            var fr = new FastReplacer("/*", "*/");
            fr.Append("/*a*/");
            fr.InsertAfter("/*a*/", "/*c*/");
            fr.Replace("/*a*/", "/*b*/");
            fr.InsertAfter("/*b*/", "/*d*/");

            fr.Replace("/*a*/", "a");
            fr.Replace("/*b*/", "b");
            fr.Replace("/*c*/", "c");
            fr.Replace("/*d*/", "d");
            Assert.AreEqual("bdc", fr.ToString());
        }
Пример #21
0
 public void PossiblyUnintuitiveBehaviour_NotAComment()
 {
     // Behaviour is different from standard C# or SQL comments:
     // Tokens are recognised even if they are inside a string (token is not always a comment).
     var fr = new FastReplacer("/*", "*/");
     fr.Append("PRINT 'Token starts with /* sequence'");
 }
Пример #22
0
 public CodeBuilder(string tagOpen, string tagClose)
 {
     _code =  new FastReplacer(tagOpen, tagClose);
 }