From a24248025e3df5dc3265a97ceb2ace107a6c84e5 Mon Sep 17 00:00:00 2001 From: Kirill Shumilov Date: Mon, 26 Nov 2018 17:10:28 +0300 Subject: [PATCH] feat(xod-tabtest): support comments in tabtests --- packages/xod-tabtest/src/TabData.re | 11 +++++++ packages/xod-tabtest/test/TabData_jest.re | 36 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/xod-tabtest/src/TabData.re b/packages/xod-tabtest/src/TabData.re index be5437ded..625f88c73 100644 --- a/packages/xod-tabtest/src/TabData.re +++ b/packages/xod-tabtest/src/TabData.re @@ -92,13 +92,24 @@ let map = List.map; let mapWithIndex = List.mapWithIndex; +let commentsRegEx = [%re + /* Finds comments started with "//" and ignores it inside quotes */ + {|/\/\/(.(?=([^"]*"[^"]*")*[^"]*$))+/gm|} +]; + +let emptyLinesRegEx = [%re {|/^\s+$/gm|}]; + let tabSplit = x => Js.String.split("\t", x) |. List.fromArray |. List.map(Js.String.trim); let parse = (tsvSource: string) : t => tsvSource + |> Js.String.replaceByRe([%re {|/\r/gm|}], "") + |> Js.String.replaceByRe(commentsRegEx, "") + |> Js.String.replaceByRe(emptyLinesRegEx, "") |> Js.String.split("\n") |> List.fromArray + |. List.keep(x => x != "") |> ( lines => switch (List.head(lines)) { diff --git a/packages/xod-tabtest/test/TabData_jest.re b/packages/xod-tabtest/test/TabData_jest.re index 27218f3b5..990c7f081 100644 --- a/packages/xod-tabtest/test/TabData_jest.re +++ b/packages/xod-tabtest/test/TabData_jest.re @@ -50,6 +50,42 @@ describe("TSV parser", () => { ]; expect(TabData.parse(tsv)) |> toEqual(expected); }); + test("skips empty lines and comments", () => { + let tsv = + "A\tB\tC\n" + ++ "// This comment should be ommited\n" + ++ "1\ttrue\t\"Hey\"\n" + ++ "\t\t\n" + ++ " \t \t \n" + ++ "\n" + ++ " \t \t //--This line and three above should be ommited too\n" + ++ "2\tfalse\t\"Hello\" // Comment should be ommited\n" + ++ "3\tfalse\t\"Slashes inside //String should not be ommited\" // This comment should be ommited\n" + ++ "4\ttrue\t\"\""; + let expected: TabData.t = [ + Map.String.fromArray([| + ("A", Number(1.0)), + ("B", Boolean(true)), + ("C", String("Hey")), + |]), + Map.String.fromArray([| + ("A", Number(2.0)), + ("B", Boolean(false)), + ("C", String("Hello")), + |]), + Map.String.fromArray([| + ("A", Number(3.0)), + ("B", Boolean(false)), + ("C", String("Slashes inside //String should not be ommited")), + |]), + Map.String.fromArray([| + ("A", Number(4.0)), + ("B", Boolean(true)), + ("C", String("")), + |]), + ]; + expect(TabData.parse(tsv)) |> toEqual(expected); + }); test("recognizes types", () => { let tsv = "Number\tBoolean\tByte\tString\tPulse\n"