-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from SimplePEG/feature-pytest-integration
Feature pytest integration
- Loading branch information
Showing
71 changed files
with
209 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[run] | ||
omit = tests/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
language: python | ||
python: | ||
- "2.7" | ||
# command to install dependencies | ||
install: | ||
- pip install . | ||
- pip install coveralls | ||
# command to run tests | ||
script: | ||
coverage run --omit=*/tests/* --source=simplepeg setup.py test | ||
after_success: | ||
coveralls | ||
install: pip install tox-travis | ||
script: tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
#!/usr/bin/env python | ||
import sys | ||
|
||
from setuptools import setup | ||
from setuptools.command.test import test as TestCommand | ||
|
||
|
||
def readme(): | ||
|
@@ -11,6 +15,26 @@ def license_text(): | |
return f.read() | ||
|
||
|
||
class Tox(TestCommand): | ||
def initialize_options(self): | ||
TestCommand.initialize_options(self) | ||
self.tox_args = None | ||
|
||
def finalize_options(self): | ||
TestCommand.finalize_options(self) | ||
self.test_args = [] | ||
self.test_suite = True | ||
|
||
def run_tests(self): | ||
import tox | ||
import shlex | ||
args = self.tox_args | ||
if args: | ||
args = shlex.split(self.tox_args) | ||
errno = tox.cmdline(args) | ||
sys.exit(errno) | ||
|
||
|
||
setup(name='simplepeg', | ||
version='1.0.3', | ||
description='Python version of SimplePEG', | ||
|
@@ -26,8 +50,8 @@ def license_text(): | |
author_email='[email protected]', | ||
keywords='peg parser grammar', | ||
license=license_text(), | ||
test_suite='nose.collector', | ||
tests_require=['nose'], | ||
tests_require=['pytest', 'tox'], | ||
cmdclass={'test': Tox}, | ||
packages=['simplepeg'], | ||
include_package_data=True, | ||
zip_safe=False) |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from simplepeg import ( | ||
rd_parser as rd, | ||
speg_visitor as sv) | ||
|
||
|
||
def space(): | ||
return rd.regex_char('[\\s]') | ||
|
||
|
||
def multiplicative(): | ||
return rd.string('*') | ||
|
||
|
||
def additive(): | ||
return rd.string('+') | ||
|
||
|
||
def factor(): | ||
return rd.ordered_choice([ | ||
rd.sequence([ | ||
rd.string('('), | ||
rd.rec(exp), | ||
rd.string(')') | ||
]), | ||
rd.regex_char('[0-9]') | ||
]) | ||
|
||
|
||
def term(): | ||
return rd.sequence([ | ||
factor(), | ||
rd.zero_or_more(rd.sequence([ | ||
space(), | ||
multiplicative(), | ||
space(), | ||
factor() | ||
])) | ||
]) | ||
|
||
|
||
def exp(): | ||
return rd.sequence([ | ||
term(), | ||
rd.zero_or_more(rd.sequence([ | ||
space(), | ||
additive(), | ||
space(), | ||
term() | ||
])) | ||
]) | ||
|
||
|
||
def math(): | ||
return rd.sequence([ | ||
exp(), | ||
rd.end_of_file() | ||
]) | ||
|
||
|
||
def test_simple_math_grammar(): | ||
parser = math() | ||
ast = parser(rd.State(text='1 + 2')) | ||
assert ast.match == '1 + 2' | ||
visitor = sv.PegJsVisitor() | ||
pegjs_ast = visitor.visit(ast) | ||
assert pegjs_ast == [[[['1'], []], [[' ', '+', ' ', [['2'], []]]]], None] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
|
||
from simplepeg.speg import SPEG | ||
from simplepeg.exceptions import GrammarParseError, TextParseError | ||
|
||
|
||
def test_grammar_parser_exception(): | ||
speg = SPEG() | ||
with pytest.raises(GrammarParseError): | ||
speg.parse_grammar('!!!') | ||
|
||
|
||
def test_text_parser_exception(): | ||
speg = SPEG() | ||
speg.parse_grammar('GRAMMAR test a -> "A";') | ||
with pytest.raises(TextParseError): | ||
speg.parse_text('B') | ||
|
||
|
||
def test_grammar_before_text(): | ||
speg = SPEG() | ||
with pytest.raises(Exception): | ||
speg.parse_text('B') | ||
|
||
|
||
def test_speg_parse_grammar_parser_exception(): | ||
speg = SPEG() | ||
with pytest.raises(GrammarParseError): | ||
speg.parse('!!!', 'B') | ||
|
||
|
||
def test_speg_parse_text_parser_exception(): | ||
speg = SPEG() | ||
with pytest.raises(TextParseError): | ||
speg.parse('GRAMMAR test a -> "A";', 'B') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from os import path as op | ||
import json | ||
|
||
import pytest | ||
|
||
from simplepeg.speg import SPEG | ||
from utils import collect_files, read_file | ||
|
||
FIXTURES_ROOT = op.join(op.dirname(__file__), 'speg_fixtures') | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'grammar_file', | ||
collect_files(FIXTURES_ROOT, lambda f: f.endswith('.peg'))) | ||
def test_speg(grammar_file): | ||
grammar, text, result = [ | ||
read_file(grammar_file + postfix) | ||
for postfix in ('', '.txt', '.json')] | ||
speg = SPEG() | ||
ast = speg.parse(grammar, text) | ||
assert json.loads(ast.to_json()) == json.loads(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import print_function | ||
from os import path as op | ||
|
||
import pytest | ||
|
||
from simplepeg import speg_parser as sp | ||
from utils import read_file, collect_files | ||
|
||
FIXTURES_ROOT = op.join(op.dirname(__file__), 'speg_grammar_fixtures') | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'grammar_file', | ||
collect_files(op.join(FIXTURES_ROOT, 'valid'))) | ||
def test_speg_parser_valid(grammar_file): | ||
content = read_file(grammar_file) | ||
speg = sp.SimplePegParser() | ||
ast = speg.parse(content) | ||
last_error = speg.get_last_error() | ||
if last_error: | ||
print(last_error) | ||
assert ast | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'grammar_file', | ||
collect_files(op.join(FIXTURES_ROOT, 'invalid'))) | ||
def test_speg_parser_invalid(grammar_file): | ||
content = read_file(grammar_file) | ||
speg = sp.SimplePegParser() | ||
ast = speg.parse(content) | ||
assert not ast |
Oops, something went wrong.