From 77a82dd31e2eb9a1bbf93f6f7d5a5facd9060af9 Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 15:37:37 +0100 Subject: [PATCH 1/3] Added test for #318 --- tests/resources/entry.fan | 2 ++ tests/resources/test.txt | 1 + tests/test_parsing.py | 8 ++++++++ 3 files changed, 11 insertions(+) create mode 100644 tests/resources/entry.fan create mode 100644 tests/resources/test.txt diff --git a/tests/resources/entry.fan b/tests/resources/entry.fan new file mode 100644 index 0000000..2c9f1fb --- /dev/null +++ b/tests/resources/entry.fan @@ -0,0 +1,2 @@ + ::= {8} + ::= 0 | 1 diff --git a/tests/resources/test.txt b/tests/resources/test.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/tests/resources/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/tests/test_parsing.py b/tests/test_parsing.py index a09e20b..5465474 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -272,3 +272,11 @@ def test_gif(self): self.assertEqual("", err) self.assertEqual("", out) self.assertEqual(0, code) + +class TestEntryParsing(TestCLIParsing): + def test_entry(self): + command = shlex.split("fandango parse -f tests/resources/entry.fan tests/resources/test.txt --validate") + out, err, code = self.run_command(command) + self.assertEqual("", err) + self.assertEqual("", out) + self.assertEqual(0, code) From 7eb9529e958ed6e9c4fb2e3d55d820e6bd955d6f Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 15:39:17 +0100 Subject: [PATCH 2/3] New: simple coverage demo --- docs/coverage-demo.fan | 48 ++++++++++++++++++++++++++++++++++++++++++ docs/coverage.py | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 docs/coverage-demo.fan create mode 100755 docs/coverage.py diff --git a/docs/coverage-demo.fan b/docs/coverage-demo.fan new file mode 100644 index 0000000..b21af1e --- /dev/null +++ b/docs/coverage-demo.fan @@ -0,0 +1,48 @@ + ::= + ::= | + ::= | +where . == . + + ::= r'\x3c' r'\x3e' + ::= r'\x3c/' r'\x3e' + ::= r'\x3c' r'/\x3e' + ::= 'html' | 'head' | 'body' | 'title' | 'p' | 'h1' | 'h2' | 'h3' + ::= r'[^\x3c\x3e]+' := 'Some beautiful text' + + +import sys + +def coverage(fun, *args, **kwargs): + covered_lines = set() + + def traceit(frame, event, arg): + if event == 'line': + filename = frame.f_code.co_filename + lineno = frame.f_lineno + covered_lines.add((filename, lineno)) + return traceit + + sys.settrace(traceit) + ret = fun(*args, **kwargs) + sys.settrace(None) + + return len(covered_lines) + +def remove_html_markup(s): + tag = False + quote = False + out = "" + + for c in s: + if c == '<' and not quote: + tag = True + elif c == '>' and not quote: + tag = False + elif (c == '"' or c == "'") and tag: + quote = not quote + elif not tag: + out = out + c + + return out + +where coverage(remove_html_markup, str()) >= 10 \ No newline at end of file diff --git a/docs/coverage.py b/docs/coverage.py new file mode 100755 index 0000000..d0ade05 --- /dev/null +++ b/docs/coverage.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# Simple coverage measurement for Python + +import sys + +def coverage(fun, *args, **kwargs): + covered_lines = set() + + def traceit(frame, event, arg): + if event == 'line': + filename = frame.f_code.co_filename + lineno = frame.f_lineno + covered_lines.add((filename, lineno)) + return traceit + + sys.settrace(traceit) + ret = fun(*args, **kwargs) + sys.settrace(None) + + return ret, covered_lines + +if __name__ == '__main__': + def remove_html_markup(s): + tag = False + quote = False + out = "" + + for c in s: + if c == '<' and not quote: + tag = True + elif c == '>' and not quote: + tag = False + elif (c == '"' or c == "'") and tag: + quote = not quote + elif not tag: + out = out + c + + return out + + ret, covered_lines = coverage(remove_html_markup, "foo") + print('Return value:', ret) + print('Covered lines:', len(covered_lines)) From 5fe0b2e3483a063e7a51642b0c4cef5f28114f2d Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 15:40:45 +0100 Subject: [PATCH 3/3] Fix: use "abcd.txt" as input --- tests/resources/test.txt | 1 - tests/test_parsing.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 tests/resources/test.txt diff --git a/tests/resources/test.txt b/tests/resources/test.txt deleted file mode 100644 index 30d74d2..0000000 --- a/tests/resources/test.txt +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 5465474..1c3e4cf 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -275,7 +275,7 @@ def test_gif(self): class TestEntryParsing(TestCLIParsing): def test_entry(self): - command = shlex.split("fandango parse -f tests/resources/entry.fan tests/resources/test.txt --validate") + command = shlex.split("fandango parse -f tests/resources/entry.fan tests/resources/abcd.txt --validate") out, err, code = self.run_command(command) self.assertEqual("", err) self.assertEqual("", out)