示例#1
0
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)]
示例#2
0
 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
示例#3
0
 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.'))
示例#4
0
 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.'))
示例#5
0
    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)
示例#6
0
 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.'))
示例#7
0
 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])
示例#8
0
 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])
示例#9
0
 def add_text(text, code, type=None):
     scope.add_definition(Definition(string_to_signature(text), code, type))
示例#10
0
def type_def(text, code, type=_type_type):
    return Definition(string_to_signature(text), code, type)
示例#11
0
 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))