Beispiel #1
0
 def test_convert_conjunction(self):
     call = Compound('call', Variable('X'))
     term = Compound(',', Variable('X'), call)
     goal = convert_to_goal(term)
     self.assertEquals(',/2', goal.predicate_indicator())
     self.assertEquals(call, goal.value[1])
     self.assertEquals(call, goal.value[2])
Beispiel #2
0
 def test_deep_compound(self):
     px = Compound('p', Variable('X'))
     p1 = Compound('p', Atomic(1))
     foo = Compound('foo', px, p1)
     args = (Variable('A'),) * 2
     foo_variable = Compound('foo', *args)
     mgu = unify(foo, foo_variable)
     self.assertIsNotNone(mgu)
     self.assertEquals(Atomic(1), mgu['X'])
Beispiel #3
0
 def test_reduce_with_compound(self):
     s = Substitution()
     # variables ending with '#N' are considered as renamed
     s['A#0'] = Variable('I')
     s['B'] = Compound('p', Variable('A#0'))
     s['I'] = Atomic('q')
     result = Substitution()
     result['I'] = Atomic('q')
     result['B'] = Compound('p', Variable('A#0'))
     s.reduce()
     self.assertEquals(result, s)
Beispiel #4
0
 def test_file_name_property(self):
     goal = 'stream_property(S, file_name(F)).'
     self.assertTrue(self.engine.solve(goal))
     s = Compound('$stream', Atomic(1))
     self.assertEquals(s, self.engine.currsubst()['S'])
     self.assertEquals(Atomic('<stdout>'), self.engine.currsubst()['F'])
     self.assertTrue(self.engine.solve_next())
     s = Compound('$stream', Atomic(0))
     self.assertEquals(s, self.engine.currsubst()['S'])
     self.assertEquals(Atomic('<stdin>'), self.engine.currsubst()['F'])
     self.assertFalse(self.engine.solve_next())
Beispiel #5
0
 def test_end_of_stream(self):
     s = open('tests/resources/editor')
     stream = prologio.Stream(s)
     stream.stream_term = Compound('$stream', Atomic(999))
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("peek_code('$stream'(999), Code)."))
     self.assertEquals(Atomic(-1), self.engine.currsubst()['Code'])
     del prologio.stream_terms[str(stream.stream_term)]
Beispiel #6
0
 def test_stream_wrong_code(self):
     s = open('tests/resources/qwerty')
     stream = prologio.Stream(s)
     stream.stream_term = Compound('$stream', Atomic(999))
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertFalse(self.engine.solve("peek_code('$stream'(999), 0'p)."))
     self.assertEquals('qwerty', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Beispiel #7
0
 def test_stream_quote(self):
     s = open('tests/resources/quoted_qwerty')
     stream = prologio.Stream(s)
     stream.stream_term = Compound('$stream', Atomic(999))
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("peek_char('$stream'(999), Char)."))
     self.assertEquals(Atomic("'"), self.engine.currsubst()['Char'])
     self.assertEquals("'qwerty'", s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Beispiel #8
0
 def test_stream_code(self):
     s = open('tests/resources/qwerty')
     stream = prologio.Stream(s)
     stream.stream_term = Compound('$stream', Atomic(999))
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("get_code('$stream'(999), Code)."))
     self.assertEquals(Atomic(113), self.engine.currsubst()['Code'])
     self.assertEquals('werty', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Beispiel #9
0
 def test_end_of_stream(self):
     s = open('tests/resources/editor')
     stream = prologio.Stream(s)
     stream.stream_term = Compound('$stream', Atomic(999))
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("get_char('$stream'(999), Char)."))
     self.assertEquals(Atomic('end_of_file'),
                       self.engine.currsubst()['Char'])
     # The stream-position past-end-of-stream is not supported
     del prologio.stream_terms[str(stream.stream_term)]
Beispiel #10
0
 def test_code_as_string(self):
     goal = "put_code(my_file, 'ty')."
     caught = self.assertRaises(PrologError, self.engine.solve, goal)
     error = Compound('type_error', Atomic('integer'), Atomic('ty'))
     self.assertEquals(error, caught.error_term())
Beispiel #11
0
 def test_input_stream(self):
     goal = 'nl(user_input).'
     caught = self.assertRaises(PrologError, self.engine.solve, goal)
     args = (Atomic('output'), Atomic('stream'), Atomic('user_input'))
     error = Compound('permission_error', *args)
     self.assertEquals(error, caught.error_term())
Beispiel #12
0
 def test_compound(self):
     args = (Variable('A'),) * 3
     foo_variable = Compound('foo', *args)
     foo_ground = Compound('foo', Atomic(1), Atomic(2), Atomic(3))
     self.assertIsNone(unify(foo_variable, foo_ground))
Beispiel #13
0
 def test_convert_disjunction(self):
     term = Compound(';', Variable('X'), Atomic('p'))
     goal = convert_to_goal(term)
     self.assertEquals(';/2', goal.predicate_indicator())
     self.assertEquals(Compound('call', Variable('X')), goal.value[1])
     self.assertEquals(Atomic('p'), goal.value[2])
Beispiel #14
0
 def test_convert_call(self):
     term = Compound('call', Variable('X'))
     self.assertEquals(term, convert_to_goal(term))
Beispiel #15
0
 def test_convert_variable(self):
     term = Variable('X')
     goal = Compound('call', term)
     self.assertEquals(goal, convert_to_goal(term))
Beispiel #16
0
 def test_flatten_with_compound(self):
     s = Substitution()
     s['A'] = Variable('I')
     s['B'] = Compound('p', Variable('A'))
     s.flatten()
     self.assertEquals(Compound('p', Variable('I')), s['B'])
Beispiel #17
0
 def test_output_property(self):
     goal = 'stream_property(S, output).'
     self.assertTrue(self.engine.solve(goal))
     s = Compound('$stream', Atomic(1))
     self.assertEquals(s, self.engine.currsubst()['S'])
     self.assertFalse(self.engine.solve_next())
Beispiel #18
0
 def test_convert_compound(self):
     term = Compound('f', Variable('X'), Atomic('g'))
     self.assertEquals(term, convert_to_goal(term))