-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SequenceParser and UnionParser to allow chaining of several par…
…sers via certain rules
- Loading branch information
Showing
4 changed files
with
111 additions
and
4 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
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
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,31 @@ | ||
from lmformatenforcer import UnionParser, SequenceParser, StringParser | ||
from .common import assert_parser_with_string | ||
from pydantic import BaseModel | ||
from lmformatenforcer import JsonSchemaParser | ||
|
||
|
||
def test_string_choice(): | ||
parser = UnionParser([StringParser('aa'), StringParser('bb')]) | ||
assert_parser_with_string('aa', parser, True) | ||
assert_parser_with_string('bb', parser, True) | ||
assert_parser_with_string('ab', parser, False) | ||
assert_parser_with_string('aabb', parser, False) | ||
|
||
|
||
def test_string_sequence(): | ||
parser = SequenceParser([StringParser('aa'), StringParser('bb')]) | ||
assert_parser_with_string('aa', parser, False) | ||
assert_parser_with_string('bb', parser, False) | ||
assert_parser_with_string('ab', parser, False) | ||
assert_parser_with_string('aabb', parser, True) | ||
assert_parser_with_string('bbaa', parser, False) | ||
|
||
|
||
def test_json_markdown_sequence(): | ||
class TestModel(BaseModel): | ||
a: str | ||
json_parser = JsonSchemaParser(TestModel.schema()) | ||
parser = SequenceParser([StringParser("```json\n"), json_parser, StringParser('\n```')]) | ||
assert_parser_with_string('```json\n{"a": "b"}\n```', parser, True) | ||
assert_parser_with_string('{"a": "b"}', parser, False) | ||
|
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