Skip to content

Commit

Permalink
fix ByteStringBuilder.addAll (#903)
Browse files Browse the repository at this point in the history
* fix ByteStringBuilder.addAll

* sort out tests for different scala versions

* rework tests

* rework unmanaged source dirs
  • Loading branch information
pjfanning authored Jan 4, 2024
1 parent 16e72eb commit d538195
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] {
_length += seq.length
}
case _ =>
super.++=(xs)
super.addAll(xs)
}
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] {
_length += seq.length
}
case _ =>
super.++=(xs)
super.addAll(xs)
}
this
}
Expand Down
10 changes: 10 additions & 0 deletions project/PekkoBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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/")),
Expand Down

0 comments on commit d538195

Please sign in to comment.