Skip to content

Commit a7c588c

Browse files
c-mitacopybara-github
authored andcommitted
Switch tests to use proto.encode_text and json.encode
PiperOrigin-RevId: 622843999 Change-Id: Ied0f5eb590a224875a68ac2498b8f92338009a7a
1 parent a0c17b0 commit a7c588c

File tree

1 file changed

+62
-70
lines changed

1 file changed

+62
-70
lines changed

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,13 +1507,6 @@ private void checkTextMessage(String from, String... lines) throws Exception {
15071507
assertThat(result).isEqualTo(expect);
15081508
}
15091509

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-
15171510
@Test
15181511
public void testStructRestrictedOverrides() throws Exception {
15191512
setBuildLanguageOptions("--incompatible_struct_has_no_methods=false");
@@ -1526,37 +1519,45 @@ public void testStructRestrictedOverrides() throws Exception {
15261519

15271520
@Test
15281521
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");
15341526
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')))",
15361528
"a: 1.2e+34",
15371529
"b: nan",
15381530
"c: -inf",
15391531
// Caution! textproto requires +inf be encoded as "inf" rather than "+inf"
15401532
"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\"", "}");
15441536
checkTextMessage(
1545-
"struct(a=[struct(b='x'), struct(b='y')]).to_proto()",
1537+
"proto.encode_text(struct(a=[struct(b='x'), struct(b='y')]))",
15461538
"a {",
15471539
" b: \"x\"",
15481540
"}",
15491541
"a {",
15501542
" b: \"y\"",
15511543
"}");
15521544
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+
"}");
15541551
// dict to_proto tests
1555-
checkTextMessage("struct(name={}).to_proto()"); // empty lines
1552+
checkTextMessage("proto.encode_text(struct(name={}))"); // empty lines
15561553
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+
"}");
15581559
checkTextMessage(
1559-
"struct(name={'c': 'd', 'a': 'b'}).to_proto()",
1560+
"proto.encode_text(struct(name={'c': 'd', 'a': 'b'}))",
15601561
"name {",
15611562
" key: \"c\"",
15621563
" value: \"d\"",
@@ -1566,15 +1567,15 @@ public void testSimpleTextMessages() throws Exception {
15661567
" value: \"b\"",
15671568
"}");
15681569
checkTextMessage(
1569-
"struct(x=struct(y={'a': 1})).to_proto()",
1570+
"proto.encode_text(struct(x=struct(y={'a': 1})))",
15701571
"x {",
15711572
" y {",
15721573
" key: \"a\"",
15731574
" value: 1",
15741575
" }",
15751576
"}");
15761577
checkTextMessage(
1577-
"struct(name={'a': struct(b=1, c=2)}).to_proto()",
1578+
"proto.encode_text(struct(name={'a': struct(b=1, c=2)}))",
15781579
"name {",
15791580
" key: \"a\"",
15801581
" value {",
@@ -1583,7 +1584,7 @@ public void testSimpleTextMessages() throws Exception {
15831584
" }",
15841585
"}");
15851586
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)}))",
15871588
"name {",
15881589
" key: \"a\"",
15891590
" value {",
@@ -1612,50 +1613,48 @@ public void testNoneStructValue() throws Exception {
16121613

16131614
@Test
16141615
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");
16171618
}
16181619

16191620
@Test
16201621
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\"");
16251625

16261626
// 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\"");
16281628
}
16291629

16301630
@Test
16311631
public void testTextMessageInvalidStructure() throws Exception {
1632-
setBuildLanguageOptions("--incompatible_struct_has_no_methods=false");
16331632
// list in list
16341633
ev.checkEvalErrorContains(
16351634
"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']]))");
16371636

16381637
// dict in list
16391638
ev.checkEvalErrorContains(
16401639
"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}]))");
16421641

16431642
// tuple as dict key
16441643
ev.checkEvalErrorContains(
16451644
"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}))");
16471646

16481647
// dict in dict
16491648
ev.checkEvalErrorContains(
16501649
"in struct field .name: in value for dict key \"a\": got dict, want string, int, float,"
16511650
+ " bool, or struct",
1652-
"struct(name={'a': {'b': [1, 2]}}).to_proto()");
1651+
"proto.encode_text(struct(name={'a': {'b': [1, 2]}}))");
16531652

16541653
// callable in field
16551654
ev.checkEvalErrorContains(
16561655
"in struct field .a: got builtin_function_or_method, want string, int, float, bool, or"
16571656
+ " struct",
1658-
"struct(a=rule).to_proto()");
1657+
"proto.encode_text(struct(a=rule))");
16591658
}
16601659

16611660
private void checkJson(String from, String expected) throws Exception {
@@ -1679,65 +1678,58 @@ public void testStarlarkJsonModule() throws Exception {
16791678

16801679
@Test
16811680
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}");
16851683
}
16861684

16871685
@Test
16881686
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\"}}");
16921689
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}))");
16951692
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}}))");
16981695
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}))");
17011698
}
17021699

17031700
@Test
17041701
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\"}}");
17111707
checkJson(
1712-
"struct(a=[struct(b='x'), struct(b='y')]).to_json()",
1708+
"json.encode(struct(a=[struct(b='x'), struct(b='y')]))",
17131709
"{\"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\"}}}");
17151711
}
17161712

17171713
@Test
17181714
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\"}");
17261721
}
17271722

17281723
@Test
17291724
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\"]]}");
17321726
}
17331727

17341728
@Test
17351729
public void testJsonInvalidStructure() throws Exception {
1736-
setBuildLanguageOptions("--incompatible_struct_has_no_methods=false");
17371730
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))");
17411733
}
17421734

17431735
@Test

0 commit comments

Comments
 (0)