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()); }
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()); }
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(); }
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."); }
public void Replace_Simple() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.Replace("/*a*/", "123"); Assert.AreEqual("123", fr.ToString()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
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()); }
/// <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; }