summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2014-12-06 03:11:46 -0800
committerSom Snytt <som.snytt@gmail.com>2014-12-12 18:29:10 -0800
commite320d81f4eecbec139b1bfc4010f54136bd7a308 (patch)
tree451d0f4d2242b758a160b0251d4dec2ae2da5a94
parentd9f623db0ff1d20040939fbb9e15d4d4e5887c75 (diff)
downloadscala-e320d81f4eecbec139b1bfc4010f54136bd7a308.tar.gz
scala-e320d81f4eecbec139b1bfc4010f54136bd7a308.tar.bz2
scala-e320d81f4eecbec139b1bfc4010f54136bd7a308.zip
SI-8538 Test or example for Source.report
It's possible to supply an arbitrary `Positioner` to a custom `Source` that wants to override `report`. This obviates the need to expose the deprecated `Position` class. The default report method consumes the underlying iterator, which is not desirable and produces unexpected results. The expected outcome is that no one uses or extends the legacy position handling code. In 2.12, use a Reporter instead, perhaps. The current API is used by scala-xml's MarkupParser.
-rw-r--r--test/junit/scala/io/SourceTest.scala56
1 files changed, 56 insertions, 0 deletions
diff --git a/test/junit/scala/io/SourceTest.scala b/test/junit/scala/io/SourceTest.scala
new file mode 100644
index 0000000000..efe081f844
--- /dev/null
+++ b/test/junit/scala/io/SourceTest.scala
@@ -0,0 +1,56 @@
+
+package scala.io
+
+import org.junit.Test
+import org.junit.Assert._
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+import scala.tools.testing.AssertUtil._
+
+import java.io.{ Console => _, _ }
+
+@RunWith(classOf[JUnit4])
+class SourceTest {
+
+ private implicit val `our codec` = Codec.UTF8
+ private val charSet = Codec.UTF8.charSet.name
+
+ private def sampler = """
+ |Big-endian and little-endian approaches aren't
+ |readily interchangeable in general, because the
+ |laws of arithmetic send signals leftward from
+ |the bits that are "least significant."
+ |""".stripMargin.trim
+
+ private def in = new ByteArrayInputStream(sampler.getBytes)
+
+ @Test def canIterateLines() = {
+ assertEquals(sampler.lines.size, (Source fromString sampler).getLines.size)
+ }
+ @Test def canCustomizeReporting() = {
+ class CapitalReporting(is: InputStream) extends BufferedSource(is) {
+ override def report(pos: Int, msg: String, out: PrintStream): Unit = {
+ out print f"$pos%04x: ${msg.toUpperCase}"
+ }
+ class OffsetPositioner extends Positioner(null) {
+ override def next(): Char = {
+ ch = iter.next()
+ pos = pos + 1
+ ch
+ }
+ }
+ withPositioning(new OffsetPositioner)
+ }
+ val s = new CapitalReporting(in)
+ // skip to next line and report an error
+ do {
+ val c = s.next()
+ } while (s.ch != '\n')
+ s.next()
+ val out = new ByteArrayOutputStream
+ val ps = new PrintStream(out, true, charSet)
+ s.reportError(s.pos, "That doesn't sound right.", ps)
+ assertEquals("0030: THAT DOESN'T SOUND RIGHT.", out.toString(charSet))
+ }
+}