fix ByteStringBuilder.addAll (#903)

* fix ByteStringBuilder.addAll

* sort out tests for different scala versions

* rework tests

* rework unmanaged source dirs
This commit is contained in:
PJ Fanning 2024-01-04 12:21:13 +01:00 committed by GitHub
parent 16e72eb396
commit d5381958c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 2 deletions

View file

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pekko.util
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
/**
* Extra tests to run for ByteStringBuilder when building with Scala 2.13+
*/
class ByteStringBuilderScala213PlusSpec extends AnyWordSpec with Matchers {
"ByteStringBuilder" should {
"handle addAll with LinearSeq" in {
val result: ByteString = ByteString.newBuilder.addAll(List[Byte]('a')).result()
result shouldEqual ByteString("a")
}
"handle addAll with IndexedSeq" in {
val result: ByteString = ByteString.newBuilder.addAll(Vector[Byte]('a')).result()
result shouldEqual ByteString("a")
}
"handle addAll with ByteString" in {
val result: ByteString = ByteString.newBuilder.addAll(ByteString("a")).result()
result shouldEqual ByteString("a")
}
}
}

View file

@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pekko.util
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
class ByteStringBuilderSpec extends AnyWordSpec with Matchers {
"ByteStringBuilder" should {
"handle ++= with LinearSeq" in {
val result: ByteString = ByteString.newBuilder.++=(List[Byte]('a')).result()
result shouldEqual ByteString("a")
}
"handle ++= with IndexedSeq" in {
val result: ByteString = ByteString.newBuilder.++=(Vector[Byte]('a')).result()
result shouldEqual ByteString("a")
}
"handle ++= with ByteString" in {
val result: ByteString = ByteString.newBuilder.++=(ByteString("a")).result()
result shouldEqual ByteString("a")
}
}
}

View file

@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] {
_length += seq.length
}
case _ =>
super.++=(xs)
super.addAll(xs)
}
this
}

View file

@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] {
_length += seq.length
}
case _ =>
super.++=(xs)
super.addAll(xs)
}
this
}

View file

@ -166,6 +166,16 @@ object PekkoBuild {
case _ => Nil
}
},
// Adds a `src/test/scala-2.13+` source directory for code shared
// between Scala 2.13 and Scala 3
Test / unmanagedSourceDirectories ++= {
val sourceDir = (Test / sourceDirectory).value
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, n)) => Seq(sourceDir / "scala-2.13+")
case Some((2, n)) if n >= 13 => Seq(sourceDir / "scala-2.13+")
case _ => Nil
}
},
ThisBuild / ivyLoggingLevel := UpdateLogging.Quiet,
licenses := Seq(("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))),
homepage := Some(url("https://pekko.apache.org/")),