Zack Williams | 28f1e49 | 2019-02-01 10:02:56 -0700 | [diff] [blame^] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from __future__ import print_function |
| 16 | import unittest |
| 17 | |
| 18 | import ply.lex as lex |
| 19 | import ply.yacc as yacc |
| 20 | from plyxproto.logicparser import FOLLexer, FOLParser |
| 21 | |
| 22 | |
| 23 | class TestFOL(unittest.TestCase): |
| 24 | |
| 25 | @classmethod |
| 26 | def setUpClass(self): |
| 27 | |
| 28 | self.DEBUG = 0 |
| 29 | self.lexer = lex.lex(module=FOLLexer(), debug=self.DEBUG) |
| 30 | self.parser = yacc.yacc(module=FOLParser(), start='goal', debug=self.DEBUG) |
| 31 | |
| 32 | def test_true(self): |
| 33 | '''verify basic truth statement''' |
| 34 | |
| 35 | t_in = "<true>" |
| 36 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 37 | self.assertEqual(p_out, "true") |
| 38 | |
| 39 | def test_or(self): |
| 40 | '''verify or statement''' |
| 41 | |
| 42 | t_in = "<a | b>" |
| 43 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 44 | self.assertEqual(p_out, {'|': ['a', 'b']}) |
| 45 | |
| 46 | def test_exists(self): |
| 47 | '''verify exists statement''' |
| 48 | |
| 49 | t_in = "<exists a: x=y>" |
| 50 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 51 | self.assertEqual(p_out, {'exists': ['a', {'=': ('x', 'y')}]}) |
| 52 | |
| 53 | def test_forall(self): |
| 54 | '''verify forall statement''' |
| 55 | |
| 56 | t_in = "<forall a: exists b: x.b=y.b>" |
| 57 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 58 | self.assertEqual(p_out, {'forall': ['a', {'exists': ['b', {'=': ('x.b', 'y.b')}]}]}) |
| 59 | |
| 60 | def test_endswith(self): |
| 61 | '''verify endswith statement''' |
| 62 | |
| 63 | t_in = "<forall a: {{ a.endswith('good') }}>" |
| 64 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 65 | self.assertEqual(p_out, {'forall': ['a', {'python': "a.endswith('good')"}]}) |
| 66 | |
| 67 | def test_function(self): |
| 68 | '''verify policy function calls''' |
| 69 | |
| 70 | t_in = "< *doit(foo) >" |
| 71 | p_out = self.parser.parse(t_in, lexer=self.lexer, debug=self.DEBUG) |
| 72 | self.assertEqual(p_out, {'policy': ['doit', 'foo']}) |