Skip to content

Commit 0ecd14f

Browse files
committed
Start running shared tests
1 parent 9a3d307 commit 0ecd14f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3116
-20817
lines changed

gen-tests.gradle.kts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -649,61 +649,6 @@ val generateMoshiTests by tasks.creating {
649649
)
650650
}
651651

652-
// Shared Json tests.
653-
654-
val generateSharedJsonCompactJavaTests by tasks.creating(JavaExec::class) {
655-
group = "Generate Shared JSON Tests"
656-
description = "Generates compat Java classes for shared JSON tests"
657-
classpath = wire
658-
mainClass.set("com.squareup.wire.WireCompiler")
659-
args = listOf(
660-
"--proto_path=wire-tests/src/commonTest/shared/proto/proto2",
661-
"--proto_path=wire-tests/src/commonTest/shared/proto/proto3",
662-
"--java_out=wire-tests/src/jvmJsonJavaTest/proto-java",
663-
"--compact",
664-
"all32.proto",
665-
"all64.proto",
666-
"all_types_proto2.proto",
667-
"all_types_proto3_test_proto3_optional.proto",
668-
"all_structs.proto",
669-
"all_wrappers.proto",
670-
"camel_case.proto",
671-
"map_types.proto",
672-
"pizza.proto"
673-
)
674-
}
675-
676-
val generateSharedJsonKotlinTests by tasks.creating(JavaExec::class) {
677-
group = "Generate Shared JSON Tests"
678-
description = "Generates Kotlin classes for shared JSON tests"
679-
classpath = wire
680-
mainClass.set("com.squareup.wire.WireCompiler")
681-
args = listOf(
682-
"--proto_path=wire-tests/src/commonTest/shared/proto/proto2",
683-
"--proto_path=wire-tests/src/commonTest/shared/proto/proto3",
684-
"--kotlin_out=wire-tests/src/jvmJsonKotlinTest/proto-kotlin",
685-
"--java_interop",
686-
"all32.proto",
687-
"all64.proto",
688-
"all_types_proto2.proto",
689-
"all_types_proto3_test_proto3_optional.proto",
690-
"all_structs.proto",
691-
"all_wrappers.proto",
692-
"camel_case.proto",
693-
"map_types.proto",
694-
"pizza.proto"
695-
)
696-
}
697-
698-
val generateSharedJson by tasks.creating {
699-
group = "Generate Tests"
700-
description = "Generates Java and Kotlin classes for shared JSON tests"
701-
dependsOn(
702-
generateSharedJsonCompactJavaTests,
703-
generateSharedJsonKotlinTests
704-
)
705-
}
706-
707652
val generateTests by tasks.creating {
708653
group = "Generate Tests"
709654
description = "Generates all test classes"
@@ -731,7 +676,6 @@ val generateTests by tasks.creating {
731676
generateProto3KotlinTests,
732677
generateProto3JavaTests,
733678
generateProtoReader32KotlinTests,
734-
generateSharedJson,
735679
generateOpaqueTypeTests,
736680
)
737681
}

settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ include(":wire-schema-tests")
6060
include(":wire-swift-generator")
6161
include(":wire-test-utils")
6262
include(":wire-tests")
63+
include(":wire-tests-2:jvm-java-kotlin")
64+
include(":wire-tests-2:jvm-json-java")
65+
include(":wire-tests-2:jvm-json-kotlin")
6366
if (startParameter.projectProperties.get("swift") != "false") {
6467
include(":wire-runtime-swift")
6568
include(":wire-tests-swift")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
kotlin("jvm")
3+
id("com.squareup.wire")
4+
}
5+
6+
dependencies {
7+
implementation(libs.assertk)
8+
implementation(libs.jimfs)
9+
implementation(libs.kotlin.test.junit)
10+
implementation(libs.truth)
11+
implementation(projects.wireGsonSupport)
12+
implementation(projects.wireMoshiAdapter)
13+
implementation(projects.wireTestUtils)
14+
}
15+
16+
wire {
17+
sourcePath {
18+
srcDir("../../wire-tests/src/commonTest/proto/java")
19+
}
20+
21+
java {
22+
}
23+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) 2016 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import assertk.assertThat
19+
import assertk.assertions.isEmpty
20+
import assertk.assertions.isEqualTo
21+
import assertk.assertions.isNotNull
22+
import com.squareup.wire.internal.createRuntimeMessageAdapter
23+
import com.squareup.wire.map.Mappy
24+
import com.squareup.wire.map.Thing
25+
import java.io.IOException
26+
import okio.ByteString
27+
import okio.ByteString.Companion.decodeHex
28+
import org.junit.Test
29+
import org.junit.runner.RunWith
30+
import org.junit.runners.Parameterized
31+
import org.junit.runners.Parameterized.Parameter
32+
import org.junit.runners.Parameterized.Parameters
33+
34+
@RunWith(Parameterized::class)
35+
class MapTest {
36+
@Parameter(0)
37+
lateinit var name: String
38+
39+
@Parameter(1)
40+
lateinit var adapter: ProtoAdapter<Mappy>
41+
42+
@Test
43+
fun serialize() {
44+
assertThat(ByteString.of(*adapter.encode(THREE))).isEqualTo(BYTES)
45+
46+
assertThat(adapter.encode(EMPTY)).isEmpty()
47+
}
48+
49+
@Test
50+
@Throws(IOException::class)
51+
fun deserialize() {
52+
assertThat(adapter.decode(BYTES)).isEqualTo(THREE)
53+
54+
val empty = adapter.decode(ByteArray(0))
55+
assertThat(empty.things).isNotNull()
56+
}
57+
58+
companion object {
59+
private val BYTES =
60+
"0a0c0a036f6e6512050a034f6e650a0c0a0374776f12050a0354776f0a100a05746872656512070a055468726565"
61+
.decodeHex()
62+
private val EMPTY = Mappy.Builder()
63+
.build()
64+
private val THREE = Mappy.Builder()
65+
.things(mapOf("one" to Thing("One"), "two" to Thing("Two"), "three" to Thing("Three")))
66+
.build()
67+
68+
@Parameters(name = "{0}")
69+
@JvmStatic
70+
fun parameters() = listOf(
71+
arrayOf("Generated", Mappy.ADAPTER),
72+
arrayOf(
73+
"Runtime",
74+
createRuntimeMessageAdapter(
75+
Mappy::class.java,
76+
"square.github.io/wire/unknown",
77+
Syntax.PROTO_2,
78+
Mappy::class.java.classLoader,
79+
),
80+
),
81+
)
82+
}
83+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2020 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import assertk.assertThat
19+
import assertk.assertions.isEqualTo
20+
import com.squareup.moshi.Moshi
21+
import com.squareup.wire.protos.redacted.NotRedacted
22+
import com.squareup.wire.protos.redacted.RedactedChild
23+
import com.squareup.wire.protos.redacted.RedactedFields
24+
import org.junit.Test
25+
26+
class MoshiRedactedTest {
27+
@Test fun nonRedacted() {
28+
val redacted = RedactedFields.Builder().a("a").b("b").c("c").build()
29+
val jsonAdapter = moshi.adapter(RedactedFields::class.java)
30+
assertThat(jsonAdapter.toJson(redacted)).isEqualTo("""{"a":"a","b":"b","c":"c"}""")
31+
}
32+
33+
@Test fun redacted() {
34+
val redacted = RedactedFields.Builder().a("a").b("b").c("c").build()
35+
val jsonAdapter = moshi.adapter(RedactedFields::class.java).redacting()
36+
assertThat(jsonAdapter.toJson(redacted))
37+
.isEqualTo("""{"b":"b","c":"c","__redacted_fields":["a"]}""")
38+
}
39+
40+
@Test fun redactedButSkipped() {
41+
val redacted = RedactedFields.Builder().b("b").c("c").build()
42+
val jsonAdapter = moshi.adapter(RedactedFields::class.java).redacting()
43+
assertThat(jsonAdapter.toJson(redacted)).isEqualTo("""{"b":"b","c":"c"}""")
44+
}
45+
46+
@Test fun redactedChild() {
47+
val redacted = RedactedChild.Builder()
48+
.a("a")
49+
.b(
50+
RedactedFields.Builder()
51+
.a("a")
52+
.b("b")
53+
.c("c")
54+
.build(),
55+
).c(
56+
NotRedacted.Builder()
57+
.a("a")
58+
.b("b")
59+
.build(),
60+
).build()
61+
62+
val jsonAdapter = moshi.adapter(RedactedChild::class.java).redacting()
63+
assertThat(jsonAdapter.toJson(redacted)).isEqualTo(
64+
"""{"a":"a","b":{"b":"b","c":"c","__redacted_fields":["a"]},"c":{"a":"a","b":"b"}}""",
65+
)
66+
}
67+
68+
companion object {
69+
private val moshi = Moshi.Builder()
70+
.add(WireJsonAdapterFactory())
71+
.build()
72+
}
73+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2015 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static org.junit.Assert.fail;
20+
21+
import com.squareup.wire.protos.oneof.OneOfMessage;
22+
import java.io.IOException;
23+
import org.junit.Test;
24+
25+
public class OneOfTest {
26+
27+
private static final byte[] INITIAL_BYTES = {};
28+
// (Tag #1 << 3 | VARINT) = 8.
29+
private static final byte[] FOO_BYTES = {8, 17};
30+
// (Tag #3 << 3 | LENGTH_DELIMITED) = 26, string length = 6.
31+
private static final byte[] BAR_BYTES = {26, 6, 'b', 'a', 'r', 'b', 'a', 'r'};
32+
33+
private final ProtoAdapter<OneOfMessage> adapter = OneOfMessage.ADAPTER;
34+
35+
@Test
36+
public void testOneOf() throws Exception {
37+
OneOfMessage.Builder builder = new OneOfMessage.Builder();
38+
validate(builder, null, null, INITIAL_BYTES);
39+
40+
builder.foo(17);
41+
validate(builder, 17, null, FOO_BYTES);
42+
43+
builder.bar("barbar");
44+
validate(builder, null, "barbar", BAR_BYTES);
45+
46+
builder.bar(null);
47+
validate(builder, null, null, INITIAL_BYTES);
48+
49+
builder.bar("barbar");
50+
validate(builder, null, "barbar", BAR_BYTES);
51+
52+
builder.foo(17);
53+
validate(builder, 17, null, FOO_BYTES);
54+
55+
builder.foo(null);
56+
validate(builder, null, null, INITIAL_BYTES);
57+
}
58+
59+
@Test
60+
public void buildFailsWhenBothFieldsAreNonNull() throws Exception {
61+
OneOfMessage.Builder builder = new OneOfMessage.Builder();
62+
builder.foo = 1;
63+
builder.bar = "two";
64+
try {
65+
builder.build();
66+
fail();
67+
} catch (IllegalArgumentException expected) {
68+
assertThat(expected)
69+
.hasMessageThat()
70+
.contains("at most one of foo, bar, baz may be non-null");
71+
}
72+
}
73+
74+
@Test
75+
public void constructorFailsWhenBothFieldsAreNonNull() throws Exception {
76+
try {
77+
new OneOfMessage(1, "two", null);
78+
fail();
79+
} catch (IllegalArgumentException expected) {
80+
assertThat(expected)
81+
.hasMessageThat()
82+
.contains("at most one of foo, bar, baz may be non-null");
83+
}
84+
}
85+
86+
private void validate(
87+
OneOfMessage.Builder builder, Integer expectedFoo, String expectedBar, byte[] expectedBytes)
88+
throws IOException {
89+
// Check builder fields
90+
assertThat(builder.foo).isEqualTo(expectedFoo);
91+
assertThat(builder.bar).isEqualTo(expectedBar);
92+
93+
// Check message fields.
94+
OneOfMessage message = builder.build();
95+
assertThat(message.foo).isEqualTo(expectedFoo);
96+
assertThat(message.bar).isEqualTo(expectedBar);
97+
98+
// Check serialized bytes.
99+
byte[] bytes = adapter.encode(message);
100+
assertThat(expectedBytes).isEqualTo(bytes);
101+
102+
// Check result of deserialization.
103+
OneOfMessage newMessage = adapter.decode(bytes);
104+
assertThat(newMessage.foo).isEqualTo(expectedFoo);
105+
assertThat(newMessage.bar).isEqualTo(expectedBar);
106+
}
107+
}

0 commit comments

Comments
 (0)