Skip to content

Commit

Permalink
Full coverage (#2)
Browse files Browse the repository at this point in the history
* Parser full coverage

* Syntax tree full coverage
  • Loading branch information
Ahhhhmed committed Mar 8, 2018
1 parent d473389 commit 7d1a972
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
13 changes: 11 additions & 2 deletions homotopy/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
import ply.yacc as yacc
from homotopy.syntax_tree import SimpleSnippet, CompositeSnippet

# Exceptions


class IllegalCharacter(Exception):
pass

# Lexer

# List of token names.


tokens = (
'SNIPPET',
'LEFT_OPERATOR',
Expand All @@ -23,10 +31,11 @@

# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
raise IllegalCharacter(t.value[0])

# Build the lexer


lexer = lex.lex()

# Parser
Expand Down
5 changes: 4 additions & 1 deletion test/testParser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from unittest import TestCase

from homotopy.parser import parser
from homotopy.parser import parser, IllegalCharacter
from homotopy.syntax_tree import SimpleSnippet, CompositeSnippet


class TestParser(TestCase):
def test_basic(self):
self.assertEqual(parser.parse('asd'), SimpleSnippet('asd'))

with self.assertRaises(IllegalCharacter):
parser.parse("☼")

def test_left_associativity(self):
left = '!@#'
for l in left:
Expand Down
11 changes: 8 additions & 3 deletions test/testSyntax_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@


class TestSyntaxTree(TestCase):
def setUp(self):
self.snippet = st.CompositeSnippet(st.SimpleSnippet('if'), '$', st.SimpleSnippet('i==1'))

def test_repr(self):
self.snippet = st.CompositeSnippet(st.SimpleSnippet('if'), '$', st.SimpleSnippet('i==1'))
self.assertEqual(str(self.snippet), 'if$i==1')

def test_eq(self):
# should not be equal to any instance of any other type
self.assertTrue(st.SimpleSnippet('if') != 123)
self.assertTrue(st.CompositeSnippet(None, None, None) != 123)

self.assertEqual(st.SimpleSnippet('if'),
st.SimpleSnippet('if'))

Expand All @@ -19,7 +21,10 @@ def test_eq(self):

self.assertFalse(st.SimpleSnippet('if') != st.SimpleSnippet('if'))


def test_compile(self):
self.assertIsNone(st.Snippet().compile())

self.assertEqual(
st.CompositeSnippet(
st.CompositeSnippet(st.SimpleSnippet('for'), '#', st.SimpleSnippet('i')),
Expand Down

0 comments on commit 7d1a972

Please sign in to comment.