@@ -1507,13 +1507,6 @@ private void checkTextMessage(String from, String... lines) throws Exception {
1507
1507
assertThat (result ).isEqualTo (expect );
1508
1508
}
1509
1509
1510
- @ Test
1511
- public void testSimpleTextMessagesBooleanFields () throws Exception {
1512
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1513
- checkTextMessage ("struct(name=True).to_proto()" , "name: true" );
1514
- checkTextMessage ("struct(name=False).to_proto()" , "name: false" );
1515
- }
1516
-
1517
1510
@ Test
1518
1511
public void testStructRestrictedOverrides () throws Exception {
1519
1512
setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
@@ -1526,37 +1519,45 @@ public void testStructRestrictedOverrides() throws Exception {
1526
1519
1527
1520
@ Test
1528
1521
public void testSimpleTextMessages () throws Exception {
1529
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1530
- checkTextMessage ("struct(name='value').to_proto()" , "name: \" value\" " );
1531
- checkTextMessage ("struct(name=[]).to_proto()" ); // empty lines
1532
- checkTextMessage ("struct(name=['a', 'b']).to_proto()" , "name: \" a\" " , "name: \" b\" " );
1533
- checkTextMessage ("struct(name=123).to_proto()" , "name: 123" );
1522
+ checkTextMessage ("proto.encode_text(struct(name='value'))" , "name: \" value\" " );
1523
+ checkTextMessage ("proto.encode_text(struct(name=[]))" ); // empty lines
1524
+ checkTextMessage ("proto.encode_text(struct(name=['a', 'b']))" , "name: \" a\" " , "name: \" b\" " );
1525
+ checkTextMessage ("proto.encode_text(struct(name=123))" , "name: 123" );
1534
1526
checkTextMessage (
1535
- "struct(a=1.2e34, b=float('nan'), c=float('-inf'), d=float('+inf')).to_proto( )" ,
1527
+ "proto.encode_text( struct(a=1.2e34, b=float('nan'), c=float('-inf'), d=float('+inf')))" ,
1536
1528
"a: 1.2e+34" ,
1537
1529
"b: nan" ,
1538
1530
"c: -inf" ,
1539
1531
// Caution! textproto requires +inf be encoded as "inf" rather than "+inf"
1540
1532
"d: inf" );
1541
- checkTextMessage ("struct(name=123).to_proto( )" , "name: 123" );
1542
- checkTextMessage ("struct(name=[1, 2, 3]).to_proto( )" , "name: 1" , "name: 2" , "name: 3" );
1543
- checkTextMessage ("struct(a=struct(b='b')).to_proto( )" , "a {" , " b: \" b\" " , "}" );
1533
+ checkTextMessage ("proto.encode_text( struct(name=123))" , "name: 123" );
1534
+ checkTextMessage ("proto.encode_text( struct(name=[1, 2, 3]))" , "name: 1" , "name: 2" , "name: 3" );
1535
+ checkTextMessage ("proto.encode_text( struct(a=struct(b='b')))" , "a {" , " b: \" b\" " , "}" );
1544
1536
checkTextMessage (
1545
- "struct(a=[struct(b='x'), struct(b='y')]).to_proto( )" ,
1537
+ "proto.encode_text( struct(a=[struct(b='x'), struct(b='y')]))" ,
1546
1538
"a {" ,
1547
1539
" b: \" x\" " ,
1548
1540
"}" ,
1549
1541
"a {" ,
1550
1542
" b: \" y\" " ,
1551
1543
"}" );
1552
1544
checkTextMessage (
1553
- "struct(a=struct(b=struct(c='c'))).to_proto()" , "a {" , " b {" , " c: \" c\" " , " }" , "}" );
1545
+ "proto.encode_text(struct(a=struct(b=struct(c='c'))))" ,
1546
+ "a {" ,
1547
+ " b {" ,
1548
+ " c: \" c\" " ,
1549
+ " }" ,
1550
+ "}" );
1554
1551
// dict to_proto tests
1555
- checkTextMessage ("struct(name={}).to_proto( )" ); // empty lines
1552
+ checkTextMessage ("proto.encode_text( struct(name={}))" ); // empty lines
1556
1553
checkTextMessage (
1557
- "struct(name={'a': 'b'}).to_proto()" , "name {" , " key: \" a\" " , " value: \" b\" " , "}" );
1554
+ "proto.encode_text(struct(name={'a': 'b'}))" ,
1555
+ "name {" ,
1556
+ " key: \" a\" " ,
1557
+ " value: \" b\" " ,
1558
+ "}" );
1558
1559
checkTextMessage (
1559
- "struct(name={'c': 'd', 'a': 'b'}).to_proto( )" ,
1560
+ "proto.encode_text( struct(name={'c': 'd', 'a': 'b'}))" ,
1560
1561
"name {" ,
1561
1562
" key: \" c\" " ,
1562
1563
" value: \" d\" " ,
@@ -1566,15 +1567,15 @@ public void testSimpleTextMessages() throws Exception {
1566
1567
" value: \" b\" " ,
1567
1568
"}" );
1568
1569
checkTextMessage (
1569
- "struct(x=struct(y={'a': 1})).to_proto( )" ,
1570
+ "proto.encode_text( struct(x=struct(y={'a': 1})))" ,
1570
1571
"x {" ,
1571
1572
" y {" ,
1572
1573
" key: \" a\" " ,
1573
1574
" value: 1" ,
1574
1575
" }" ,
1575
1576
"}" );
1576
1577
checkTextMessage (
1577
- "struct(name={'a': struct(b=1, c=2)}).to_proto( )" ,
1578
+ "proto.encode_text( struct(name={'a': struct(b=1, c=2)}))" ,
1578
1579
"name {" ,
1579
1580
" key: \" a\" " ,
1580
1581
" value {" ,
@@ -1583,7 +1584,7 @@ public void testSimpleTextMessages() throws Exception {
1583
1584
" }" ,
1584
1585
"}" );
1585
1586
checkTextMessage (
1586
- "struct(name={'a': struct(b={4: 'z', 3: 'y'}, c=2)}).to_proto( )" ,
1587
+ "proto.encode_text( struct(name={'a': struct(b={4: 'z', 3: 'y'}, c=2)}))" ,
1587
1588
"name {" ,
1588
1589
" key: \" a\" " ,
1589
1590
" value {" ,
@@ -1612,50 +1613,48 @@ public void testNoneStructValue() throws Exception {
1612
1613
1613
1614
@ Test
1614
1615
public void testProtoFieldsOrder () throws Exception {
1615
- setBuildLanguageOptions ( "--incompatible_struct_has_no_methods=false" );
1616
- checkTextMessage ( " struct(d=4, b=2, c=3, a=1).to_proto( )" , "a: 1" , "b: 2" , "c: 3" , "d: 4" );
1616
+ checkTextMessage (
1617
+ "proto.encode_text( struct(d=4, b=2, c=3, a=1))" , "a: 1" , "b: 2" , "c: 3" , "d: 4" );
1617
1618
}
1618
1619
1619
1620
@ Test
1620
1621
public void testTextMessageEscapes () throws Exception {
1621
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1622
- checkTextMessage ("struct(name='a\" b').to_proto()" , "name: \" a\\ \" b\" " );
1623
- checkTextMessage ("struct(name='a\\ 'b').to_proto()" , "name: \" a'b\" " );
1624
- checkTextMessage ("struct(name='a\\ nb').to_proto()" , "name: \" a\\ nb\" " );
1622
+ checkTextMessage ("proto.encode_text(struct(name='a\" b'))" , "name: \" a\\ \" b\" " );
1623
+ checkTextMessage ("proto.encode_text(struct(name='a\\ 'b'))" , "name: \" a'b\" " );
1624
+ checkTextMessage ("proto.encode_text(struct(name='a\\ nb'))" , "name: \" a\\ nb\" " );
1625
1625
1626
1626
// struct(name="a\\\"b") -> name: "a\\\"b"
1627
- checkTextMessage ("struct(name='a\\ \\ \\ \" b').to_proto( )" , "name: \" a\\ \\ \\ \" b\" " );
1627
+ checkTextMessage ("proto.encode_text( struct(name='a\\ \\ \\ \" b'))" , "name: \" a\\ \\ \\ \" b\" " );
1628
1628
}
1629
1629
1630
1630
@ Test
1631
1631
public void testTextMessageInvalidStructure () throws Exception {
1632
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1633
1632
// list in list
1634
1633
ev .checkEvalErrorContains (
1635
1634
"in struct field .a: at list index 0: got list, want string, int, float, bool, or struct" ,
1636
- "struct(a=[['b']]).to_proto( )" );
1635
+ "proto.encode_text( struct(a=[['b']]))" );
1637
1636
1638
1637
// dict in list
1639
1638
ev .checkEvalErrorContains (
1640
1639
"in struct field .a: at list index 0: got dict, want string, int, float, bool, or struct" ,
1641
- "struct(a=[{'b': 1}]).to_proto( )" );
1640
+ "proto.encode_text( struct(a=[{'b': 1}]))" );
1642
1641
1643
1642
// tuple as dict key
1644
1643
ev .checkEvalErrorContains (
1645
1644
"in struct field .a: invalid dict key: got tuple, want int or string" ,
1646
- "struct(a={(1, 2): 3}).to_proto( )" );
1645
+ "proto.encode_text( struct(a={(1, 2): 3}))" );
1647
1646
1648
1647
// dict in dict
1649
1648
ev .checkEvalErrorContains (
1650
1649
"in struct field .name: in value for dict key \" a\" : got dict, want string, int, float,"
1651
1650
+ " bool, or struct" ,
1652
- "struct(name={'a': {'b': [1, 2]}}).to_proto( )" );
1651
+ "proto.encode_text( struct(name={'a': {'b': [1, 2]}}))" );
1653
1652
1654
1653
// callable in field
1655
1654
ev .checkEvalErrorContains (
1656
1655
"in struct field .a: got builtin_function_or_method, want string, int, float, bool, or"
1657
1656
+ " struct" ,
1658
- "struct(a=rule).to_proto( )" );
1657
+ "proto.encode_text( struct(a=rule))" );
1659
1658
}
1660
1659
1661
1660
private void checkJson (String from , String expected ) throws Exception {
@@ -1679,65 +1678,58 @@ public void testStarlarkJsonModule() throws Exception {
1679
1678
1680
1679
@ Test
1681
1680
public void testJsonBooleanFields () throws Exception {
1682
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1683
- checkJson ("struct(name=True).to_json()" , "{\" name\" :true}" );
1684
- checkJson ("struct(name=False).to_json()" , "{\" name\" :false}" );
1681
+ checkJson ("json.encode(struct(name=True))" , "{\" name\" :true}" );
1682
+ checkJson ("json.encode(struct(name=False))" , "{\" name\" :false}" );
1685
1683
}
1686
1684
1687
1685
@ Test
1688
1686
public void testJsonDictFields () throws Exception {
1689
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1690
- checkJson ("struct(config={}).to_json()" , "{\" config\" :{}}" );
1691
- checkJson ("struct(config={'key': 'value'}).to_json()" , "{\" config\" :{\" key\" :\" value\" }}" );
1687
+ checkJson ("json.encode(struct(config={}))" , "{\" config\" :{}}" );
1688
+ checkJson ("json.encode(struct(config={'key': 'value'}))" , "{\" config\" :{\" key\" :\" value\" }}" );
1692
1689
ev .checkEvalErrorContains (
1693
- "Keys must be a string but got a int for struct field 'config' " ,
1694
- "struct(config={1:2}).to_json( )" );
1690
+ "in struct field .config: dict has int key, want string " ,
1691
+ "json.encode( struct(config={1:2}))" );
1695
1692
ev .checkEvalErrorContains (
1696
- "Keys must be a string but got a int for dict value 'foo' " ,
1697
- "struct(config={'foo':{1:2}}).to_json( )" );
1693
+ "in struct field .config: in dict key \" foo \" : dict has int key, want string " ,
1694
+ "json.encode( struct(config={'foo':{1:2}}))" );
1698
1695
ev .checkEvalErrorContains (
1699
- "Keys must be a string but got a bool for struct field 'config' " ,
1700
- "struct(config={True: False}).to_json( )" );
1696
+ "in struct field .config: dict has bool key, want string " ,
1697
+ "json.encode( struct(config={True: False}))" );
1701
1698
}
1702
1699
1703
1700
@ Test
1704
1701
public void testJsonEncoding () throws Exception {
1705
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1706
- checkJson ("struct(name='value').to_json()" , "{\" name\" :\" value\" }" );
1707
- checkJson ("struct(name=['a', 'b']).to_json()" , "{\" name\" :[\" a\" ,\" b\" ]}" );
1708
- checkJson ("struct(name=123).to_json()" , "{\" name\" :123}" );
1709
- checkJson ("struct(name=[1, 2, 3]).to_json()" , "{\" name\" :[1,2,3]}" );
1710
- checkJson ("struct(a=struct(b='b')).to_json()" , "{\" a\" :{\" b\" :\" b\" }}" );
1702
+ checkJson ("json.encode(struct(name='value'))" , "{\" name\" :\" value\" }" );
1703
+ checkJson ("json.encode(struct(name=['a', 'b']))" , "{\" name\" :[\" a\" ,\" b\" ]}" );
1704
+ checkJson ("json.encode(struct(name=123))" , "{\" name\" :123}" );
1705
+ checkJson ("json.encode(struct(name=[1, 2, 3]))" , "{\" name\" :[1,2,3]}" );
1706
+ checkJson ("json.encode(struct(a=struct(b='b')))" , "{\" a\" :{\" b\" :\" b\" }}" );
1711
1707
checkJson (
1712
- "struct(a=[struct(b='x'), struct(b='y')]).to_json( )" ,
1708
+ "json.encode( struct(a=[struct(b='x'), struct(b='y')]))" ,
1713
1709
"{\" a\" :[{\" b\" :\" x\" },{\" b\" :\" y\" }]}" );
1714
- checkJson ("struct(a=struct(b=struct(c='c'))).to_json( )" , "{\" a\" :{\" b\" :{\" c\" :\" c\" }}}" );
1710
+ checkJson ("json.encode( struct(a=struct(b=struct(c='c'))))" , "{\" a\" :{\" b\" :{\" c\" :\" c\" }}}" );
1715
1711
}
1716
1712
1717
1713
@ Test
1718
1714
public void testJsonEscapes () throws Exception {
1719
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1720
- checkJson ("struct(name='a\" b').to_json()" , "{\" name\" :\" a\\ \" b\" }" );
1721
- checkJson ("struct(name='a\\ 'b').to_json()" , "{\" name\" :\" a'b\" }" );
1722
- checkJson ("struct(name='a\\ \\ b').to_json()" , "{\" name\" :\" a\\ \\ b\" }" );
1723
- checkJson ("struct(name='a\\ nb').to_json()" , "{\" name\" :\" a\\ nb\" }" );
1724
- checkJson ("struct(name='a\\ rb').to_json()" , "{\" name\" :\" a\\ rb\" }" );
1725
- checkJson ("struct(name='a\\ tb').to_json()" , "{\" name\" :\" a\\ tb\" }" );
1715
+ checkJson ("json.encode(struct(name='a\" b'))" , "{\" name\" :\" a\\ \" b\" }" );
1716
+ checkJson ("json.encode(struct(name='a\\ 'b'))" , "{\" name\" :\" a'b\" }" );
1717
+ checkJson ("json.encode(struct(name='a\\ \\ b'))" , "{\" name\" :\" a\\ \\ b\" }" );
1718
+ checkJson ("json.encode(struct(name='a\\ nb'))" , "{\" name\" :\" a\\ nb\" }" );
1719
+ checkJson ("json.encode(struct(name='a\\ rb'))" , "{\" name\" :\" a\\ rb\" }" );
1720
+ checkJson ("json.encode(struct(name='a\\ tb'))" , "{\" name\" :\" a\\ tb\" }" );
1726
1721
}
1727
1722
1728
1723
@ Test
1729
1724
public void testJsonNestedListStructure () throws Exception {
1730
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1731
- checkJson ("struct(a=[['b']]).to_json()" , "{\" a\" :[[\" b\" ]]}" );
1725
+ checkJson ("json.encode(struct(a=[['b']]))" , "{\" a\" :[[\" b\" ]]}" );
1732
1726
}
1733
1727
1734
1728
@ Test
1735
1729
public void testJsonInvalidStructure () throws Exception {
1736
- setBuildLanguageOptions ("--incompatible_struct_has_no_methods=false" );
1737
1730
ev .checkEvalErrorContains (
1738
- "Invalid text format, expected a struct, a string, a bool, or an int but got a "
1739
- + "builtin_function_or_method for struct field 'a'" ,
1740
- "struct(a=rule).to_json()" );
1731
+ "in struct field .a: cannot encode builtin_function_or_method as JSON" ,
1732
+ "json.encode(struct(a=rule))" );
1741
1733
}
1742
1734
1743
1735
@ Test
0 commit comments