def make_test_scopes(): scope0 = Scope(None) scope0.add_definition( Definition(string_to_signature('Fake sentence for testing.'), 0)) scope0.add_definition( Definition(string_to_signature('Beginning Middle. end.'), 1)) return [scope0, Scope(scope0)]
def make_test_scope(self): scope = Scope() scope.add_definition(Definition(string_to_signature('Unit.'), 0)) scope.add_definition(Definition( string_to_signature('Something with Sub sentence. to parse.'), 1)) scope.add_definition(Definition( string_to_signature('Define New thing. to be a new type.'), 2)) return scope
def test_parse_expression(self): scope = self.make_test_scope() parser = fake_parser(tokenify_list(['Something', 'with', 'Unit', '.', 'to', 'parse', '.'])) exp = parser.parse_expression(scope) self.assertEqual(exp, string_to_signature('Something with Unit. to parse.'))
def test_parse_definition(self): scope = self.make_test_scope() parser = fake_parser(tokenify_list(['Define', 'Sig', 'sentence', '.', 'to', 'be', 'a', 'new', 'type', '.'])) dfn = parser.parse_definition(scope) self.assertEqual(dfn, string_to_signature('Define Sig sentence. to be a new type.'))
def test_has_define_function(self): scope = create_built_in_scope() ptr = scope.new_matcher() ptr.next(FirstToken('Define')) ptr.next() ptr.next(WordToken('to')) ptr.next(WordToken('be')) ptr.next() self.assertTrue(ptr.has_end()) self.assertIsInstance( scope.match_sentence( string_to_signature('Define Abc. to be Xyz. .')), Definition)
def test_new_define_scope(self): outer_scope = Scope() outer_scope.add_definition( Definition(string_to_signature('Define Head. to be a built in.'), None)) inner_scope = outer_scope.new_define_scope( string_to_signature( 'Check that The Sentences. given. are Defined. .')) # raises on missing sentence. inner_scope.match_sentence( string_to_signature('Check that Things. are Something. .')) inner_scope.match_sentence(string_to_signature('The Things. given.')) inner_scope.match_sentence(string_to_signature('Defined.')) with self.assertRaises(NoDefinitionError): inner_scope.match_sentence(string_to_signature('Sentences.'))
def test_nested_signature(self): result = string_to_signature('Define Head. to be Body. .') self.assertEqual(FirstToken('Head'), result[1][0]) self.assertEqual(PeriodToken(), result[1][1]) self.assertEqual(WordToken('to'), result[2])
def test_flat_signature(self): result = string_to_signature('Frames per second.') self.assertEqual(FirstToken('Frames'), result[0]) self.assertEqual(WordToken('per'), result[1]) self.assertEqual(WordToken('second'), result[2]) self.assertEqual(PeriodToken(), result[3])
def add_text(text, code, type=None): scope.add_definition(Definition(string_to_signature(text), code, type))
def type_def(text, code, type=_type_type): return Definition(string_to_signature(text), code, type)
def test_define_identity(self): action = define_function(Scope(), string_to_signature('Identity Id. .'), string_to_signature('Id.')) self.assertIsNone(action.definition.code(Scope(), None))