@@ -1655,5 +1655,138 @@ def test_basic_structural_tag_utf8(stag_format: Dict[str, Any], instance: str, i
16551655 check_stag_with_instance (stag_format , instance , is_accepted )
16561656
16571657
1658+ basic_structural_tags_instance_is_accepted = [
1659+ # ConstStringFormat
1660+ (xgr .structural_tag .ConstStringFormat (value = "hello" ), "hello" , True ),
1661+ (xgr .structural_tag .ConstStringFormat (value = "hello" ), "hello world" , False ),
1662+ # JSONSchemaFormat
1663+ (xgr .structural_tag .JSONSchemaFormat (json_schema = {"type" : "object" }), '{"key": "value"}' , True ),
1664+ (xgr .structural_tag .JSONSchemaFormat (json_schema = {"type" : "string" }), '"abc"' , True ),
1665+ (xgr .structural_tag .JSONSchemaFormat (json_schema = {"type" : "integer" }), "123" , True ),
1666+ (xgr .structural_tag .JSONSchemaFormat (json_schema = {"type" : "integer" }), "abc" , False ),
1667+ # AnyTextFormat
1668+ (xgr .structural_tag .AnyTextFormat (), "" , True ),
1669+ (xgr .structural_tag .AnyTextFormat (), "any text here" , True ),
1670+ # SequenceFormat
1671+ (
1672+ xgr .structural_tag .SequenceFormat (
1673+ elements = [
1674+ xgr .structural_tag .ConstStringFormat (value = "A" ),
1675+ xgr .structural_tag .ConstStringFormat (value = "B" ),
1676+ ]
1677+ ),
1678+ "AB" ,
1679+ True ,
1680+ ),
1681+ (
1682+ xgr .structural_tag .SequenceFormat (
1683+ elements = [
1684+ xgr .structural_tag .ConstStringFormat (value = "A" ),
1685+ xgr .structural_tag .ConstStringFormat (value = "B" ),
1686+ ]
1687+ ),
1688+ "A" ,
1689+ False ,
1690+ ),
1691+ # OrFormat
1692+ (
1693+ xgr .structural_tag .OrFormat (
1694+ elements = [
1695+ xgr .structural_tag .ConstStringFormat (value = "A" ),
1696+ xgr .structural_tag .ConstStringFormat (value = "B" ),
1697+ ]
1698+ ),
1699+ "A" ,
1700+ True ,
1701+ ),
1702+ (
1703+ xgr .structural_tag .OrFormat (
1704+ elements = [
1705+ xgr .structural_tag .ConstStringFormat (value = "A" ),
1706+ xgr .structural_tag .ConstStringFormat (value = "B" ),
1707+ ]
1708+ ),
1709+ "B" ,
1710+ True ,
1711+ ),
1712+ (
1713+ xgr .structural_tag .OrFormat (
1714+ elements = [
1715+ xgr .structural_tag .ConstStringFormat (value = "A" ),
1716+ xgr .structural_tag .ConstStringFormat (value = "B" ),
1717+ ]
1718+ ),
1719+ "C" ,
1720+ False ,
1721+ ),
1722+ # TagFormat
1723+ (
1724+ xgr .structural_tag .TagFormat (
1725+ begin = "<b>" , content = xgr .structural_tag .AnyTextFormat (), end = "</b>"
1726+ ),
1727+ "<b>text</b>" ,
1728+ True ,
1729+ ),
1730+ (
1731+ xgr .structural_tag .TagFormat (
1732+ begin = "<b>" , content = xgr .structural_tag .AnyTextFormat (), end = "</b>"
1733+ ),
1734+ "<b>text</b" ,
1735+ False ,
1736+ ),
1737+ # TagsWithSeparatorFormat
1738+ (
1739+ xgr .structural_tag .TagsWithSeparatorFormat (
1740+ tags = [
1741+ xgr .structural_tag .TagFormat (
1742+ begin = "<b>" , content = xgr .structural_tag .AnyTextFormat (), end = "</b>"
1743+ )
1744+ ],
1745+ separator = "," ,
1746+ ),
1747+ '<b>"1"</b>,<b>"2"</b>' ,
1748+ True ,
1749+ ),
1750+ (
1751+ xgr .structural_tag .TagsWithSeparatorFormat (
1752+ tags = [
1753+ xgr .structural_tag .TagFormat (
1754+ begin = "<b>" , content = xgr .structural_tag .AnyTextFormat (), end = "</b>"
1755+ )
1756+ ],
1757+ separator = "," ,
1758+ ),
1759+ '<b>"1"</b><b>"2"</b>' ,
1760+ False ,
1761+ ),
1762+ # QwenXMLParameterFormat
1763+ (
1764+ xgr .structural_tag .QwenXMLParameterFormat (
1765+ json_schema = {"type" : "object" , "properties" : {"name" : {"type" : "string" }}}
1766+ ),
1767+ "<parameter=name>value</parameter>" ,
1768+ True ,
1769+ ),
1770+ (
1771+ xgr .structural_tag .QwenXMLParameterFormat (
1772+ json_schema = {"type" : "object" , "properties" : {"name" : {"type" : "string" }}}
1773+ ),
1774+ "<parameter=name>value</param>" ,
1775+ False ,
1776+ ),
1777+ ]
1778+
1779+
1780+ @pytest .mark .parametrize (
1781+ "stag_format, instance, is_accepted" , basic_structural_tags_instance_is_accepted
1782+ )
1783+ def test_from_structural_tag_with_structural_tag_instance (
1784+ stag_format : xgr .structural_tag .Format , instance : str , is_accepted : bool
1785+ ):
1786+ stag = xgr .structural_tag .StructuralTag (format = stag_format )
1787+ grammar = xgr .Grammar .from_structural_tag (stag )
1788+ assert _is_grammar_accept_string (grammar , instance ) == is_accepted
1789+
1790+
16581791if __name__ == "__main__" :
16591792 pytest .main (sys .argv )
0 commit comments