# Path: pydsl/grammar/definition.py
# class RegularExpression(Grammar):
#     def __init__(self, regexp, flags = 0):
#         Grammar.__init__(self, None)
#         import re
#         retype = type(re.compile('hello, world'))
#         if isinstance(regexp, retype):
#             self.regexp = regexp
#             self.regexpstr = regexp.pattern
#             self.flags = regexp.flags
#         elif isinstance(regexp, str):
#             self.regexpstr = regexp
#             self.flags = flags
#             self.regexp = re.compile(regexp, flags)
#         else:
#             raise TypeError
# 
#     def __hash__(self):
#         return hash(self.regexpstr)
# 
#     def __eq__(self, other):
#         try:
#             return self.regexpstr == other.regexpstr and self.flags == other.flags
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return self.regexpstr
# 
#     @property
#     def first(self):
#         i = 0
#         while True:
#             if self.regexpstr[i] == "^":
#                 i+=1
#                 continue
#             if self.regexpstr[i] == "[":
#                 from .PEG import Choice
#                 return Choice([String(x) for x in self.regexpstr[i+1:self.regexpstr.find("]")]])
#             return String(self.regexpstr[i])
# 
#     def __getattr__(self, attr):
#         return getattr(self.regexp, attr)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding


import unittest
import re
from pydsl.grammar.definition import RegularExpression
        from pydsl.grammar.definition import String
        from pydsl.encoding import ascii_encoding
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with pydsl.  If not, see <http://www.gnu.org/licenses/>.


__author__ = "Nestor Arocha"
__copyright__ = "Copyright 2008-2014, Nestor Arocha"
__email__ = "nesaro@gmail.com"


class TestRegularExpression(unittest.TestCase):
    """Regular expression method tests"""
    def testInstantiation(self):
        re1 = RegularExpression('^a$')
        re2 = RegularExpression(re.compile('^a$'))
        self.assertEqual(str(re1), str(re2)) #FIXME python3 default flag value is 32

    def testEnumerate(self):
        re1 = RegularExpression(re.compile('^a$'))
        self.assertRaises(NotImplementedError, re1.enum)

    def testFirst(self):
        re1 = RegularExpression(re.compile('^a$'))
        self.assertEqual(len(re1.first),1)
        self.assertIn(String('a'), re1.first)

    def testMin(self):

# Path: pydsl/grammar/definition.py
# class RegularExpression(Grammar):
#     def __init__(self, regexp, flags = 0):
#         Grammar.__init__(self, None)
#         import re
#         retype = type(re.compile('hello, world'))
#         if isinstance(regexp, retype):
#             self.regexp = regexp
#             self.regexpstr = regexp.pattern
#             self.flags = regexp.flags
#         elif isinstance(regexp, str):
#             self.regexpstr = regexp
#             self.flags = flags
#             self.regexp = re.compile(regexp, flags)
#         else:
#             raise TypeError
# 
#     def __hash__(self):
#         return hash(self.regexpstr)
# 
#     def __eq__(self, other):
#         try:
#             return self.regexpstr == other.regexpstr and self.flags == other.flags
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return self.regexpstr
# 
#     @property
#     def first(self):
#         i = 0
#         while True:
#             if self.regexpstr[i] == "^":
#                 i+=1
#                 continue
#             if self.regexpstr[i] == "[":
#                 from .PEG import Choice
#                 return Choice([String(x) for x in self.regexpstr[i+1:self.regexpstr.find("]")]])
#             return String(self.regexpstr[i])
# 
#     def __getattr__(self, attr):
#         return getattr(self.regexp, attr)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding


# Path: tests/unit/test_RegularExpression.py
import unittest
import re
from pydsl.grammar.definition import RegularExpression
        from pydsl.grammar.definition import String
        from pydsl.encoding import ascii_encoding

#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with pydsl.  If not, see <http://www.gnu.org/licenses/>.


__author__ = "Nestor Arocha"
__copyright__ = "Copyright 2008-2014, Nestor Arocha"
__email__ = "nesaro@gmail.com"


class TestRegularExpression(unittest.TestCase):
    """Regular expression method tests"""
    def testInstantiation(self):
        re1 = RegularExpression('^a$')
        re2 = RegularExpression(re.compile('^a$'))
        self.assertEqual(str(re1), str(re2)) #FIXME python3 default flag value is 32

    def testEnumerate(self):
        re1 = RegularExpression(re.compile('^a$'))
        self.assertRaises(NotImplementedError, re1.enum)

    def testFirst(self):
        re1 = RegularExpression(re.compile('^a$'))
        self.assertEqual(len(re1.first),1)
        self.assertIn(String('a'), re1.first)

    def testMin(self):

        re1 = RegularExpression(re.compile('^a$'))