forked from btlines/pbdirect
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protoc comparison test #32
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
syntax = "proto3"; | ||
|
||
message MessageOne { | ||
double dbl = 1; | ||
bool boolean = 2; | ||
} | ||
|
||
message MessageTwo { | ||
int64 long = 1; | ||
MessageOne messageOne = 2; | ||
MessageOne messageOneOption = 3; | ||
oneof coproduct { | ||
MessageOne a = 4; | ||
MessageTwo b = 5; // recursion! | ||
int32 c = 6; | ||
} | ||
} | ||
|
||
message MessageThree { | ||
int32 int = 2; | ||
repeated int32 packedInts = 3; | ||
repeated int32 unpackedInts = 4 [packed=false]; | ||
int32 intOption = 5; | ||
map<string, string> stringStringMap = 6; | ||
string _string = 7; | ||
bytes _bytes = 8; | ||
MessageOne messageOne = 9; | ||
MessageOne messageOneOption = 10; | ||
MessageTwo messageTwo = 11; | ||
map<int32, MessageTwo> intMessageTwoMap = 12; | ||
} |
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,189 @@ | ||
package pbdirect | ||
|
||
import org.scalatest.flatspec._ | ||
import org.scalatestplus.scalacheck.Checkers | ||
import org.scalacheck.ScalacheckShapeless._ | ||
import org.scalacheck.Prop._ | ||
import com.github.os72.protocjar._ | ||
import scala.sys.process._ | ||
import java.io._ | ||
import shapeless._ | ||
|
||
class ProtocComparisonSpec extends AnyFlatSpec with Checkers { | ||
import ProtocComparisonSpec._ | ||
|
||
implicit override val generatorDrivenConfig = | ||
PropertyCheckConfiguration(minSuccessful = 500) | ||
|
||
val protoc: File = Protoc.extractProtoc(ProtocVersion.PROTOC_VERSION, true) | ||
val workingDir: File = new File(".") | ||
val protoFile: File = new File("src/test/resources/proto/ProtocComparisonSpec.proto") | ||
val protocCommand = | ||
s"${protoc.getAbsolutePath} --proto_path=${workingDir.getAbsolutePath} --encode=MessageThree $protoFile" | ||
|
||
"pbdirect" should "write the same bytes as protoc does" in check { | ||
forAllNoShrink { (message: MessageThree) => | ||
val pbdirectOutputBytes = message.toPB.toList | ||
|
||
val textFormattedMessage = TextFormatEncoding.messageThree(message) | ||
val in = new ByteArrayInputStream(textFormattedMessage.getBytes) | ||
val out = new ByteArrayOutputStream() | ||
protocCommand.#<(in).#>(out).! | ||
val protocOutputBytes = out.toByteArray.toList | ||
|
||
val label = | ||
s"""|_bytes = ${message._bytes.toList} | ||
| | ||
|text formatted message = | ||
|$textFormattedMessage | ||
| | ||
|binary output of pbdirect = | ||
|$pbdirectOutputBytes | ||
| | ||
|binary output of protoc = | ||
|$protocOutputBytes""".stripMargin | ||
|
||
label |: pbdirectOutputBytes == protocOutputBytes | ||
} | ||
} | ||
} | ||
|
||
object ProtocComparisonSpec { | ||
|
||
case class MessageOne( | ||
dbl: Double, | ||
boolean: Boolean | ||
) | ||
|
||
case class MessageTwo( | ||
long: Long, | ||
messageOne: MessageOne, | ||
messageOneOption: Option[MessageOne], | ||
@pbIndex(4, 5, 6) coproduct: Option[MessageOne :+: MessageTwo :+: Int :+: CNil] | ||
) | ||
|
||
case class MessageThree( | ||
@pbIndex(2) int: Int, | ||
@pbIndex(3) packedInts: List[Int], | ||
@pbIndex(4) @pbUnpacked unpackedInts: List[Int], | ||
@pbIndex(5) intOption: Option[Int], | ||
@pbIndex(6) stringStringMap: Map[String, String], | ||
@pbIndex(7) _string: String, | ||
@pbIndex(8) _bytes: Array[Byte], | ||
@pbIndex(9) messageOne: MessageOne, | ||
@pbIndex(10) messageOneOption: Option[MessageOne], | ||
@pbIndex(11) messageTwo: MessageTwo, | ||
@pbIndex(12) intMessageTwoMap: Map[Int, MessageTwo] | ||
) | ||
|
||
object TextFormatEncoding { | ||
|
||
/* | ||
* Note: | ||
* we could define some kind of Encoder[A] type class | ||
* to encode a value of type A as protobuf text format. | ||
* But then we'd be basically reinventing the whole | ||
* library but for text instead of binary, and then using | ||
* that to test the behaviour of the library. Feels a bit | ||
* weird. | ||
* | ||
* So instead we keep things simple: write out the encoder | ||
* by hand and only support the exact types used in the test. | ||
*/ | ||
|
||
def indent(string: String): String = | ||
string.split('\n').map(line => s" $line").mkString("\n") | ||
|
||
def messageOne(m: MessageOne): String = | ||
s"""|dbl: ${m.dbl} | ||
|boolean: ${m.boolean}""".stripMargin | ||
|
||
def embeddedMessageOne(m: MessageOne): String = | ||
s"""|{ | ||
|${indent(messageOne(m))} | ||
|}""".stripMargin | ||
|
||
def option[A](fieldName: String, opt: Option[A])(f: A => String): String = | ||
opt.fold("")(a => s"$fieldName: ${f(a)}") | ||
|
||
def coproduct(cop: MessageOne :+: MessageTwo :+: Int :+: CNil): String = { | ||
cop match { | ||
case Inl(messageOne) => s"a: ${embeddedMessageOne(messageOne)}" | ||
case Inr(Inl(messageTwo)) => s"b: ${embeddedMessageTwo(messageTwo)}" | ||
case Inr(Inr(Inl(int))) => s"c: $int" | ||
case _ => "" | ||
} | ||
} | ||
|
||
def messageTwo(m: MessageTwo): String = | ||
s"""|long: ${m.long} | ||
|messageOne: ${embeddedMessageOne(m.messageOne)} | ||
|${option("messageOneOption", m.messageOneOption)(embeddedMessageOne)} | ||
|${m.coproduct.fold("")(coproduct)} | ||
|""".stripMargin | ||
|
||
def embeddedMessageTwo(m: MessageTwo): String = | ||
s"""|{ | ||
|${indent(messageTwo(m))} | ||
|}""".stripMargin | ||
|
||
def bytes(xs: Array[Byte]): String = | ||
xs.map(b => s"\\${(b.toInt & 0xFF).toOctalString}").mkString | ||
|
||
def string(x: String): String = { | ||
val escaped = x | ||
.replaceAllLiterally("""\""", """\\""") // escape backslashes | ||
.replaceAllLiterally("\n", "\\n") // escape newlines and other control characters | ||
.replaceAllLiterally("\r", "\\r") | ||
.replaceAllLiterally("\b", "\\b") | ||
.replaceAllLiterally("\f", "\\f") | ||
.replaceAllLiterally("\t", "\\t") | ||
.replaceAllLiterally("\u0000", "\\0") | ||
.replaceAllLiterally(""""""", """\"""") // esape double quotes | ||
s""""$escaped"""" // wrap in double quotes | ||
} | ||
|
||
def stringStringMap(map: Map[String, String]): String = { | ||
map | ||
.map { | ||
case (key, value) => | ||
s"""|stringStringMap: { | ||
| key: ${string(key)} | ||
| value: ${string(value)} | ||
|}""".stripMargin | ||
} | ||
.mkString("\n") | ||
} | ||
|
||
def intMessageTwoMap(map: Map[Int, MessageTwo]): String = { | ||
map | ||
.map { | ||
case (key, value) => | ||
s"""|intMessageTwoMap: { | ||
| key: ${key} | ||
| value: { | ||
|${indent(indent(messageTwo(value)))} | ||
| } | ||
|}""".stripMargin | ||
} | ||
.mkString("\n") | ||
} | ||
|
||
def messageThree(m: MessageThree): String = { | ||
s"""|int: ${m.int} | ||
|packedInts: [${m.packedInts.mkString(", ")}] | ||
|unpackedInts: [${m.unpackedInts.mkString(", ")}] | ||
|${option("intOption", m.intOption)(_.toString)} | ||
|${stringStringMap(m.stringStringMap)} | ||
|_string: ${string(m._string)} | ||
|_bytes: "${bytes(m._bytes)}" | ||
|messageOne: ${embeddedMessageOne(m.messageOne)} | ||
|${option("messageOneOption", m.messageOneOption)(embeddedMessageOne)} | ||
|messageTwo: ${embeddedMessageTwo(m.messageTwo)} | ||
|${intMessageTwoMap(m.intMessageTwoMap)} | ||
|""".stripMargin | ||
} | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😁