def test_dfas_with_provided_strings(self): """ Uses the strings provided in the Yaml files. Errors are raised if an accept string is rejected or a reject string is accepted """ for test in self.tests: accept_strings = test.yaml["accept_strings"] reject_strings = test.yaml["reject_strings"] # Make sure there are actually strings defined self.assertTrue(len(accept_strings) > 0) self.assertTrue(len(reject_strings) > 0) for input_string in accept_strings: # Run them through the DFA, raise exception if it doesn't accept if not dfa_accepts(test.dfa, input_string): raise(UnexpectedRejection(test.yaml["description"], input_string, test.yaml["regex"])) for input_string in reject_strings: # Run them through the DFA, raise exception if it accepts if dfa_accepts(test.dfa, input_string): raise(UnexpectedAccept(test.yaml["description"], input_string, test.yaml["regex"]))
def main(args): description, dfa = load_dfa(args.dfa_yaml) print("Loaded DFA: {}".format(description)) print("Input string: {}".format(args.input_string)) validate_dfa(dfa) accepts = dfa_accepts(dfa, args.input_string) if accepts: print("DFA accepts string '{}'".format(args.input_string)) sys.exit(0) else: print("DFA rejects string '{}'".format(args.input_string)) sys.exit(1)
def test_dfas_with_generated_strings(self): """ This test uses the exrex module to generate strings from a regular expression Each DFA yaml file includes it's own regular expression that describes the language. """ # Skip this test if exrex isn't installed try: import exrex except ImportError: msg = "Skipping exrex based test because it's missing. Do 'pip install exrex'" print(msg) self.skipTest(msg) for test in self.tests: # Take at most MAX_GENERATED strings from the exrex generator for input_string in islice(exrex.generate(test.yaml["regex"]), self.MAX_GENERATED): # Run them through the DFA, raise exception if it doesn't accept if not dfa_accepts(test.dfa, input_string): raise(UnexpectedRejection(test.yaml["description"], input_string, test.yaml["regex"]))
def test_rejects_string_with_unrecognized_character(self): self.assertFalse(dfa_accepts(self.dfa, "ababaQ"))
def test_throws_error(self): with self.assertRaises(InvalidDFA) as e: dfa_accepts(self.invalid_dfa, "bbbbb") error = "Something went wrong when attempting transition from state '1' on input 'b'" self.assertTrue(error in str(e.exception))
def test_rejects_string(self): input_string = "abba" self.assertFalse(dfa_accepts(self.dfa, input_string))
def test_accepts_string(self): input_string = "abbaa" self.assertTrue(dfa_accepts(self.dfa, input_string))