public void AddTextWithWrongId() { var diagnostics = LGParser.ParseText("[import](xx.lg) \r\n # t \n - hi", "a.lg").Diagnostics; Assert.AreEqual(1, diagnostics.Count); Assert.IsTrue(diagnostics[0].Message.Contains("Could not find file")); }
/// <summary> /// Use to evaluate an inline template str. /// </summary> /// <param name="lgFile">lg file.</param> /// <param name="inlineStr">inline string which will be evaluated.</param> /// <param name="scope">scope object or JToken.</param> /// <returns>Evaluate result.</returns> public static object Evaluate(this LGFile lgFile, string inlineStr, object scope = null) { if (inlineStr == null) { throw new ArgumentException("inline string is null."); } CheckErrors(lgFile.AllDiagnostics); // wrap inline string with "# name and -" to align the evaluation process var fakeTemplateId = Guid.NewGuid().ToString(); var multiLineMark = "```"; inlineStr = !inlineStr.Trim().StartsWith(multiLineMark) && inlineStr.Contains('\n') ? $"{multiLineMark}{inlineStr}{multiLineMark}" : inlineStr; var newContent = $"# {fakeTemplateId} \r\n - {inlineStr}"; var newLgFile = LGParser.ParseText(newContent, lgFile.Id, lgFile.ImportResolver); var allTemplates = lgFile.AllTemplates.Union(newLgFile.AllTemplates).ToList(); var evaluator = new Evaluator(allTemplates, lgFile.ExpressionEngine); return(evaluator.EvaluateTemplate(fakeTemplateId, scope)); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class. /// </summary> /// <param name="lgText">lg template text.</param> /// <param name="id">optional label for the source of the templates (used for labeling source of template errors).</param> /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param> public TemplateEngineLanguageGenerator(string lgText, string id, Dictionary <string, IList <IResource> > resourceMapping) { this.Id = id ?? DEFAULTLABEL; var(_, locale) = LGResourceLoader.ParseLGFileName(id); var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping); this.lgFile = LGParser.ParseText(lgText ?? string.Empty, Id, importResolver); }
public void TestImportLgFiles() { var lgFile = LGParser.ParseFile(GetExampleFilePath("import.lg")); // Assert 6.lg is imported only once when there are several relative paths which point to the same file. // Assert import cycle loop is handled well as expected when a file imports itself. Assert.AreEqual(14, lgFile.AllTemplates.Count()); string evaled = lgFile.EvaluateTemplate("basicTemplate", null).ToString(); Assert.IsTrue(evaled == "Hi" || evaled == "Hello"); evaled = lgFile.EvaluateTemplate("welcome", null).ToString(); Assert.IsTrue(evaled == "Hi DongLei :)" || evaled == "Hey DongLei :)" || evaled == "Hello DongLei :)"); evaled = lgFile.EvaluateTemplate("template3", null).ToString(); Assert.IsTrue(evaled == "Hi 2" || evaled == "Hello 2"); evaled = lgFile.EvaluateTemplate("welcome", new { userName = "******" }).ToString(); Assert.IsTrue(evaled == "Hi DL :)" || evaled == "Hey DL :)" || evaled == "Hello DL :)"); evaled = lgFile.EvaluateTemplate("basicTemplate2", null).ToString(); Assert.IsTrue(evaled == "Hi 2" || evaled == "Hello 2"); // Assert 6.lg of relative path is imported from text. lgFile = LGParser.ParseText("# basicTemplate\r\n- Hi\r\n- Hello\r\n[import](./6.lg)", GetExampleFilePath("xx.lg")); Assert.AreEqual(8, lgFile.AllTemplates.Count()); evaled = lgFile.EvaluateTemplate("basicTemplate", null).ToString(); Assert.IsTrue(evaled == "Hi" || evaled == "Hello"); evaled = lgFile.EvaluateTemplate("welcome", null).ToString(); Assert.IsTrue(evaled == "Hi DongLei :)" || evaled == "Hey DongLei :)" || evaled == "Hello DongLei :)"); evaled = lgFile.EvaluateTemplate("welcome", new { userName = "******" }).ToString(); Assert.IsTrue(evaled == "Hi DL :)" || evaled == "Hey DL :)" || evaled == "Hello DL :)"); }