# Path: pydsl/check.py
# def checker_factory(grammar):
#     from pydsl.grammar.BNF import BNFGrammar
#     from pydsl.grammar.PEG import Sequence, Choice, OneOrMore, ZeroOrMore
#     from pydsl.grammar.definition import PLYGrammar, RegularExpression, String, PythonGrammar, JsonSchema
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(grammar, str) and not isinstance(grammar, String):
#         raise TypeError(grammar)
#     if isinstance(grammar, BNFGrammar):
#         return BNFChecker(grammar)
#     elif isinstance(grammar, JsonSchema):
#         return JsonSchemaChecker(grammar)
#     elif isinstance(grammar, RegularExpression):
#         return RegularExpressionChecker(grammar)
#     elif isinstance(grammar, PythonGrammar) or isinstance(grammar, dict) and "matchFun" in grammar:
#         return PythonChecker(grammar)
#     elif isinstance(grammar, PLYGrammar):
#         return PLYChecker(grammar)
#     elif isinstance(grammar, Choice):
#         return ChoiceChecker(grammar)
#     elif isinstance(grammar, ParsleyGrammar):
#         return ParsleyChecker(grammar)
#     elif isinstance(grammar, String):
#         return StringChecker(grammar)
#     elif isinstance(grammar, Sequence):
#         return SequenceChecker(grammar)
#     elif isinstance(grammar, OneOrMore):
#         return OneOrMoreChecker(grammar)
#     elif isinstance(grammar, ZeroOrMore):
#         return ZeroOrMoreChecker(grammar)
#     elif isinstance(grammar, Iterable):
#         return ChoiceChecker(grammar)
#     else:
#         raise ValueError(grammar)
# 
# Path: pydsl/grammar/definition.py
# class String(Grammar, str):
#     def __init__(self, string):
#         if isinstance(string, list):
#             raise TypeError('Attempted to initialize a String with a list %s' % (string, ) )
#         Grammar.__init__(self, None)
# 
#     @property
#     def first(self):
#         return String(self[0])
# 
#     def enum(self):
#         yield self
# 
#     @property
#     def maxsize(self):
#         return len(self)
# 
#     @property
#     def minsize(self):
#         return len(self)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding
# 
# Path: pydsl/grammar/PEG.py
# class Sequence(Grammar, list):
#     def __init__(self, *args, **kwargs):
#         base_alphabet = kwargs.pop('base_alphabet', None)
#         Grammar.__init__(self, base_alphabet)
#         list.__init__(self, *args, **kwargs)
#         for x in self:
#             if not isinstance(x, Grammar):
#                 raise TypeError(x)
# 
#     def __hash__(self):
#         return hash(tuple(self))
# 
#     @classmethod
#     def from_string(cls, string):
#         from .definition import String
#         return cls([String(x) for x in string])


import unittest
import sys
from pydsl.check import checker_factory
from pydsl.grammar.definition import String
from pydsl.grammar.PEG import Sequence
        from pydsl.contrib.bnfgrammar import productionset0
        from pydsl.check import BNFChecker
        from pydsl.check import RegularExpressionChecker
        from pydsl.check import PLYChecker
        from pydsl.contrib.grammar import example_ply
        from pydsl.grammar.definition import PLYGrammar
        from pydsl.grammar.definition import JsonSchema
        from pydsl.check import JsonSchemaChecker
        from pydsl.check import ChoiceChecker
        from pydsl.grammar.PEG import Choice
        from pydsl.grammar import RegularExpression
        from pydsl.check import StringChecker
        from pydsl.grammar.PEG import Sequence
        from pydsl.check import SequenceChecker
        checker = JsonSchemaChecker(grammardef)
        self.assertFalse(checker.check("a"))
        self.assertTrue(checker.check({"foo":1}))
        self.assertFalse(checker.check({"foo":2}))
        self.assertTrue(checker.check({"foo":3}))
        self.assertFalse(checker.check([1, {"foo" : 2, "bar" : {"baz" : [1]}}, "quux"]))
        self.assertRaises(Exception, checker.check, [1, {"foo" : 2, "bar" : {"baz" : [1]}}, "quux"], raise_exceptions=True)
        number_three = checker_factory(String("3"))
        fc = {"number_three":number_three}
        grammardef = JsonSchema(schema)
        checker = JsonSchemaChecker(grammardef, fc) # Adds a format checker
        self.assertFalse(checker.check({"foo" : 1, "bar" : "123456"}))
        self.assertTrue(checker.check({"foo" : 1, "bar" : "3"}))





class TestChoiceChecker(unittest.TestCase):
    def testCheck(self):
        a = Choice([RegularExpression('^[0123456789]*$')])
        checker = ChoiceChecker(a)
        self.assertTrue(checker.check([x for x in '1234']))
        self.assertTrue(checker.check('1234'))
        self.assertFalse(checker.check('abc'))
        self.assertFalse(checker.check(''))

class TestStringChecker(unittest.TestCase):
    def testCheck(self):
        """Test checker instantiation and call"""

# Path: pydsl/check.py
# def checker_factory(grammar):
#     from pydsl.grammar.BNF import BNFGrammar
#     from pydsl.grammar.PEG import Sequence, Choice, OneOrMore, ZeroOrMore
#     from pydsl.grammar.definition import PLYGrammar, RegularExpression, String, PythonGrammar, JsonSchema
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(grammar, str) and not isinstance(grammar, String):
#         raise TypeError(grammar)
#     if isinstance(grammar, BNFGrammar):
#         return BNFChecker(grammar)
#     elif isinstance(grammar, JsonSchema):
#         return JsonSchemaChecker(grammar)
#     elif isinstance(grammar, RegularExpression):
#         return RegularExpressionChecker(grammar)
#     elif isinstance(grammar, PythonGrammar) or isinstance(grammar, dict) and "matchFun" in grammar:
#         return PythonChecker(grammar)
#     elif isinstance(grammar, PLYGrammar):
#         return PLYChecker(grammar)
#     elif isinstance(grammar, Choice):
#         return ChoiceChecker(grammar)
#     elif isinstance(grammar, ParsleyGrammar):
#         return ParsleyChecker(grammar)
#     elif isinstance(grammar, String):
#         return StringChecker(grammar)
#     elif isinstance(grammar, Sequence):
#         return SequenceChecker(grammar)
#     elif isinstance(grammar, OneOrMore):
#         return OneOrMoreChecker(grammar)
#     elif isinstance(grammar, ZeroOrMore):
#         return ZeroOrMoreChecker(grammar)
#     elif isinstance(grammar, Iterable):
#         return ChoiceChecker(grammar)
#     else:
#         raise ValueError(grammar)
# 
# Path: pydsl/grammar/definition.py
# class String(Grammar, str):
#     def __init__(self, string):
#         if isinstance(string, list):
#             raise TypeError('Attempted to initialize a String with a list %s' % (string, ) )
#         Grammar.__init__(self, None)
# 
#     @property
#     def first(self):
#         return String(self[0])
# 
#     def enum(self):
#         yield self
# 
#     @property
#     def maxsize(self):
#         return len(self)
# 
#     @property
#     def minsize(self):
#         return len(self)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding
# 
# Path: pydsl/grammar/PEG.py
# class Sequence(Grammar, list):
#     def __init__(self, *args, **kwargs):
#         base_alphabet = kwargs.pop('base_alphabet', None)
#         Grammar.__init__(self, base_alphabet)
#         list.__init__(self, *args, **kwargs)
#         for x in self:
#             if not isinstance(x, Grammar):
#                 raise TypeError(x)
# 
#     def __hash__(self):
#         return hash(tuple(self))
# 
#     @classmethod
#     def from_string(cls, string):
#         from .definition import String
#         return cls([String(x) for x in string])


# Path: tests/unit/test_Checker.py
import unittest
import sys
from pydsl.check import checker_factory
from pydsl.grammar.definition import String
from pydsl.grammar.PEG import Sequence
        from pydsl.contrib.bnfgrammar import productionset0
        from pydsl.check import BNFChecker
        from pydsl.check import RegularExpressionChecker
        from pydsl.check import PLYChecker
        from pydsl.contrib.grammar import example_ply
        from pydsl.grammar.definition import PLYGrammar
        from pydsl.grammar.definition import JsonSchema
        from pydsl.check import JsonSchemaChecker
        from pydsl.check import ChoiceChecker
        from pydsl.grammar.PEG import Choice
        from pydsl.grammar import RegularExpression
        from pydsl.check import StringChecker
        from pydsl.grammar.PEG import Sequence
        from pydsl.check import SequenceChecker

        checker = JsonSchemaChecker(grammardef)
        self.assertFalse(checker.check("a"))
        self.assertTrue(checker.check({"foo":1}))
        self.assertFalse(checker.check({"foo":2}))
        self.assertTrue(checker.check({"foo":3}))
        self.assertFalse(checker.check([1, {"foo" : 2, "bar" : {"baz" : [1]}}, "quux"]))
        self.assertRaises(Exception, checker.check, [1, {"foo" : 2, "bar" : {"baz" : [1]}}, "quux"], raise_exceptions=True)
        number_three = checker_factory(String("3"))
        fc = {"number_three":number_three}
        grammardef = JsonSchema(schema)
        checker = JsonSchemaChecker(grammardef, fc) # Adds a format checker
        self.assertFalse(checker.check({"foo" : 1, "bar" : "123456"}))
        self.assertTrue(checker.check({"foo" : 1, "bar" : "3"}))





class TestChoiceChecker(unittest.TestCase):
    def testCheck(self):
        a = Choice([RegularExpression('^[0123456789]*$')])
        checker = ChoiceChecker(a)
        self.assertTrue(checker.check([x for x in '1234']))
        self.assertTrue(checker.check('1234'))
        self.assertFalse(checker.check('abc'))
        self.assertFalse(checker.check(''))

class TestStringChecker(unittest.TestCase):
    def testCheck(self):
        """Test checker instantiation and call"""

        grammarchecker = StringChecker(String("3"))