Skip to content

Commit

Permalink
Merge pull request #327 from fandango-fuzzer/andreas
Browse files Browse the repository at this point in the history
Andreas
  • Loading branch information
joszamama authored Feb 10, 2025
2 parents 6ca84fe + e036924 commit 240b91e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/coverage-demo.fan
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<start> ::= <html>
<html> ::= <text> | <element>
<element> ::= <open_tag> <html> <close_tag> | <self_closing_tag>
where <open_tag>.<tag_name> == <close_tag>.<tag_name>

<open_tag> ::= r'\x3c' <tag_name> r'\x3e'
<close_tag> ::= r'\x3c/' <tag_name> r'\x3e'
<self_closing_tag> ::= r'\x3c' <tag_name> r'/\x3e'
<tag_name> ::= 'html' | 'head' | 'body' | 'title' | 'p' | 'h1' | 'h2' | 'h3'
<text> ::= 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(<start>)) >= 10
42 changes: 42 additions & 0 deletions docs/coverage.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 2 additions & 0 deletions tests/resources/entry.fan
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<start> ::= <bit>{8}
<bit> ::= 0 | 1
8 changes: 8 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/abcd.txt --validate")
out, err, code = self.run_command(command)
self.assertEqual("", err)
self.assertEqual("", out)
self.assertEqual(0, code)

0 comments on commit 240b91e

Please sign in to comment.