Beispiel #1
0
def tester(testfile):
	"""Ajaa kaikki annetun testitiedoston testit, ja tulostaa virheilmon epäonnistuneista testeistä"""
	skipped_lines = 0
	total_lines = 0
	failed_tests = 0

	file = open(testfile, 'r')
	matcher = None
	regexstr = None
	for line in file:
		total_lines += 1
		arg = line[1:].strip()
		if line[0] == '=': # vaihdetaan käytettävää regexpiä
			(regexstr, expected_postfix) = arg.split('=')

			try:
				parser = Parser(regexstr)
				matcher = DfaMatcher(parser.parse())
			except Exception as e:
				print(str.format("Parsing /{0}/ failed unexpectedly: {1}", regexstr, str(e)))
				failed_tests += 1
				continue
			if expected_postfix != '' and parser.postfix != expected_postfix:
				print(str.format("Parsing regex '{0}' failed: expected postfix '{1}', got '{2}'", regexstr, expected_postfix, parser.postfix))
				failed_tests += 1
		elif line[0] == '+' or line[0] == '-': # koitetaan tunnistaa annettu merkkijono nykyisellä regexpillä
			expected = line[0] == '+'
			if expected != bool(matcher.match(arg)):
				print(str.format("Matching '{0}' against /{1}/ caused unexpected result '{2}' instead of '{3}'", arg, regexstr, expected, not expected))
				failed_tests += 1
		else:
			skipped_lines += 1
	total_tests = total_lines - skipped_lines
	print(str.format("{0}/{1} tests passed.", total_tests - failed_tests, total_tests))
Beispiel #2
0
import sys
from Parser import *
from Debug import *
from Tester import *
from DfaMatcher import *

if len(sys.argv) < 3:
	print(str.format("usage: {0} <regex> <file>\n       {0} -t <testfile>", sys.argv[0]))
	sys.exit(1)
if sys.argv[1] == '-t':
	tester(sys.argv[2])
else:
	parser = Parser(sys.argv[1])
	nfa = parser.parse()
	matcher = DfaMatcher(nfa)
	file = open(sys.argv[2])
	
	for line in file:
		line = line.strip()
		if matcher.match(line):
			print line