public void ParseTest() { foreach (string file in Directory.EnumerateFiles("..\\..\\..\\TikzParserUnitTests\\testcases", "*.tex")) { Console.Out.WriteLine("Processing file " + file + " ..."); string code = File.ReadAllText(file); Tikz_ParseTree actual; try { actual = TikzParser.Parse(code); Console.Out.WriteLine("... has " + actual.Children.Count + " child" + (actual.Children.Count == 1?"":"ren") + ". OK."); } catch (Exception ex) { Assert.Fail("Exception in file: " + file + Environment.NewLine + ex.Message); } } }
public void ParseTest4() { string code = @"%some text \begin{tikzpicture}[scale=2] \draw (1,2) node (v) {bla} -- (3,3); \end{tikzpicture} "; Tikz_ParseTree actual = TikzParser.Parse(code); Tikz_Picture tp = actual.GetTikzPicture(); Tikz_Node n = tp.GetNodeByName("v"); System.Windows.Point p; bool ret = n.GetAbsPos(out p); Assert.IsTrue(ret); Assert.AreEqual(2, p.X); Assert.AreEqual(4, p.Y); Assert.AreEqual("bla", n.label); }
public void ParseTestStyleDetection() { string code = @" \tikzstyle{test 1}=[draw] %!TE% \tikzset{ test 2 /.style={draw}} \begin{tikzpicture}[test 3/.style={draw}] \tikzstyle{test 4}=[draw] \tikzset{ test 5/.style={draw}} \end{tikzpicture} "; Tikz_ParseTree T = TikzParser.Parse(code); Assert.AreEqual <int>(5, T.styles.Count); // check whether style names are stored with whitespace removed for (int i = 1; i <= 5; i++) { Assert.IsTrue(T.styles.ContainsKey("test " + i)); } }
public void ParseTest2() { string code = @"\begin{tikzpicture} \draw (1,2) -- (3,3); \end{tikzpicture} "; Tikz_ParseTree actual = TikzParser.Parse(code); Tikz_Picture tp = actual.GetTikzPicture(); Assert.AreEqual(0, tp.StartPosition()); Tikz_Path tpa = tp.Children.Find(tpi => tpi is Tikz_Path) as Tikz_Path; Assert.AreEqual(21, tpa.StartPosition()); Tikz_Coord tc = tpa.Children.Find(tpi => tpi is Tikz_Coord) as Tikz_Coord; Assert.AreEqual(1, tc.uX.number); Assert.AreEqual(2, tc.uY.number); Assert.AreEqual(27, tc.StartPosition()); }
void AsyncParser_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { AsyncParserResultType Result = new AsyncParserResultType(); //make sure that double typed numbers are converted with decimal point (not comma!) to string System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); try { AsyncParserJob job = e.Argument as AsyncParserJob; Result.DocumentID = job.DocumentID; Tikz_ParseTree tp = TikzParser.Parse(job.code); Result.ParseTree = tp; //include any styles from include files via \input cmd string inputfile = ""; try { //find input files using Regex MatchCollection files = InputsRegex.Matches(job.code); foreach (Match file in files) { //open, read, parse, and close each included file. inputfile = file.Groups["file"].ToString(); if (File.Exists(inputfile)) { StreamReader sr = new StreamReader(inputfile); string inputcode = sr.ReadToEnd(); sr.Close(); Tikz_ParseTree tp2 = TikzParser.ParseInputFile(inputcode); //if tp2 == null there probably was nothing useful included. if (tp2 != null) { //every style that was found in included file, add it to parse tree of main file. foreach (KeyValuePair <string, Tikz_Option> style in tp2.styles) { if (!Result.ParseTree.styles.ContainsKey(style.Key)) { Result.ParseTree.styles.Add(style.Key, style.Value); } else { ParserException pe = new ParserException(""); TexOutputParser.TexError te = new TexOutputParser.TexError(); te.Message = "Style [" + style.Key + "] is defined multiple times. Check position " + style.Value.StartPosition() + " in " + inputfile + " and this definition."; te.pos = Result.ParseTree.styles[style.Key].StartPosition(); te.severity = Severity.WARNING; pe.e = te; throw pe; } } } } } } catch (Exception ex) { Result.Warning = ex; Result.WarningSource = inputfile; } } catch (Exception ex) { //never set e.Cancel = true; //if you do, you cannot access e.Result from AsyncParser_RunWorkerCompleted. Result.Error = ex; Result.WarningSource = ShortFileName; } finally { e.Result = Result; } }