Skip to content

Commit

Permalink
Merge pull request #9 from SimplePEG/feature-pytest-integration
Browse files Browse the repository at this point in the history
Feature pytest integration
  • Loading branch information
obenjiro authored Jun 28, 2016
2 parents 2e97d51 + 409f9b2 commit ad72736
Show file tree
Hide file tree
Showing 71 changed files with 209 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
11 changes: 2 additions & 9 deletions .travis.yml
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
28 changes: 26 additions & 2 deletions setup.py
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():
Expand All @@ -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',
Expand All @@ -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 removed simplepeg/tests/__init__.py
Empty file.
62 changes: 0 additions & 62 deletions simplepeg/tests/test_rd_parser_combo.py

This file was deleted.

41 changes: 0 additions & 41 deletions simplepeg/tests/test_speg_exceptions.py

This file was deleted.

29 changes: 0 additions & 29 deletions simplepeg/tests/test_speg_fixtures.py

This file was deleted.

34 changes: 0 additions & 34 deletions simplepeg/tests/test_speg_parser_fixtures.py

This file was deleted.

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.
66 changes: 66 additions & 0 deletions tests/test_rd_parser_combo.py
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]
35 changes: 35 additions & 0 deletions tests/test_speg_exceptions.py
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')
21 changes: 21 additions & 0 deletions tests/test_speg_fixtures.py
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)
32 changes: 32 additions & 0 deletions tests/test_speg_parser_fixtures.py
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
Loading

0 comments on commit ad72736

Please sign in to comment.