Skip to content

Commit

Permalink
Merge pull request #46 from ferraith/feature/grammar-equality
Browse files Browse the repository at this point in the history
Added __eq__ and __hash__ methods to AntlrGrammar
  • Loading branch information
ferraith committed Oct 6, 2018
2 parents 770614c + 063da42 commit 6fcfdde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
7 changes: 7 additions & 0 deletions setuptools_antlr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def __init__(self, path: pathlib.Path):
self.path = path
self.dependencies = []

def __eq__(self, other):
return (isinstance(other, AntlrGrammar) and hash(other) == hash(self) and
self.dependencies == other.dependencies)

def __hash__(self):
return hash((self.name, self.path))

def read_imports(self) -> typing.List[str]:
"""Reads all imported grammars out of grammar file.
Expand Down
53 changes: 23 additions & 30 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,40 +119,33 @@ def test_find_antlr_log(self, tmpdir, command, available_antlr_logs, expected_an

def test_find_grammars_empty(self, tmpdir, command):
dsl_dir = tmpdir.mkdir('dsl')
g = command._find_grammars(pathlib.Path(str(dsl_dir)))
grammars = command._find_grammars(pathlib.Path(str(dsl_dir)))

assert len(g) == 0
assert len(grammars) == 0

def test_find_grammars_standalone(self, command):
g = command._find_grammars(pathlib.Path('standalone'))
grammars = command._find_grammars(pathlib.Path('standalone'))

assert len(g) == 1
assert g[0].name == 'SomeGrammar'
some_grammar = AntlrGrammar(pathlib.Path('standalone/SomeGrammar.g4'))

assert len(grammars) == 1
assert some_grammar in grammars

def test_find_grammars_distributed(self, command):
g = command._find_grammars(pathlib.Path('distributed'))

assert len(g) == 3

assert g[0].name == 'CommonTerminals'
d = g[0].dependencies
assert len(d) == 0

assert g[1].name == 'SharedRules'
d = g[1].dependencies
assert len(d) == 1
assert d[0].name == 'CommonTerminals'

assert g[2].name == 'SomeGrammar'
d = g[2].dependencies
assert len(d) == 2
assert d[0].name == 'CommonTerminals'
assert d[1].name == 'SharedRules'
dd = g[2].dependencies[0].dependencies
assert len(dd) == 0
dd = g[2].dependencies[1].dependencies
assert len(dd) == 1
assert dd[0].name == 'CommonTerminals'
grammars = command._find_grammars(pathlib.Path('distributed'))

common_terminals = AntlrGrammar(pathlib.Path('distributed/CommonTerminals.g4'))
shared_rules = AntlrGrammar(pathlib.Path('distributed/SharedRules.g4'))
some_grammar = AntlrGrammar(pathlib.Path('distributed/SomeGrammar.g4'))

shared_rules.dependencies.append(common_terminals)
some_grammar.dependencies.append(common_terminals)
some_grammar.dependencies.append(shared_rules)

assert len(grammars) == 3
assert common_terminals in grammars
assert shared_rules in grammars
assert some_grammar in grammars

def test_find_grammars_incomplete(self, command):
# check if DistutilsFileError was thrown
Expand Down Expand Up @@ -294,7 +287,7 @@ def test_run_java_not_found(self, mock_find_java, command):
@unittest.mock.patch('subprocess.run')
@unittest.mock.patch.object(AntlrCommand, '_find_grammars')
def test_run_antlr_found(self, mock_find_grammars, mock_run, mock_find_antlr, mock_find_java,
tmpdir, command):
tmpdir, command):
java_exe = pathlib.Path('c:/path/to/java/bin/java.exe')
antlr_jar = pathlib.Path('antlr-4.5.3-complete.jar')

Expand Down Expand Up @@ -709,7 +702,7 @@ def test_run_x_force_atn_disabled(self, mock_run, configured_command):
@unittest.mock.patch.object(AntlrCommand, '_find_antlr_log')
@unittest.mock.patch('shutil.move')
def test_run_x_log_enabled(self, mock_move, mock_find_antlr_log, mock_run, capsys,
configured_command):
configured_command):
log_file = 'antlr-2016-12-19-16.01.43.log'
mock_run.return_value = unittest.mock.Mock(returncode=0)
mock_find_antlr_log.return_value = pathlib.Path(log_file)
Expand Down

0 comments on commit 6fcfdde

Please sign in to comment.