From f35e2e4b87d517f2b06c7bf7acf42745c41fcce7 Mon Sep 17 00:00:00 2001 From: riiswa Date: Thu, 25 Mar 2021 22:23:08 +0100 Subject: [PATCH 1/2] add `readWith` method to StdIn --- src/main/scala/scala/io/next/package.scala | 31 ++++++++++++ .../scala/scala/io/TestStdInExtensions.scala | 47 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/main/scala/scala/io/next/package.scala create mode 100644 src/test/scala/scala/io/TestStdInExtensions.scala diff --git a/src/main/scala/scala/io/next/package.scala b/src/main/scala/scala/io/next/package.scala new file mode 100644 index 0000000..20f0369 --- /dev/null +++ b/src/main/scala/scala/io/next/package.scala @@ -0,0 +1,31 @@ +/* + * Scala (https://www.scala-lang.org) + * + * Copyright EPFL and Lightbend, Inc. + * + * Licensed under Apache License 2.0 + * (http://www.apache.org/licenses/LICENSE-2.0). + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + +package scala.io + +package object next { + implicit class NextStdInExtensions(si: StdIn) { + /** Reads and applying a function on an entire line of the default input . + * + * @return the Byte that was read + * @throws java.io.EOFException if the end of the + * input stream has been reached. + */ + def readWith[A](f: String => A): A = { + val s = si.readLine() + if (s == null) + throw new java.io.EOFException("Console has reached end of input") + else + f(s) + } + } +} diff --git a/src/test/scala/scala/io/TestStdInExtensions.scala b/src/test/scala/scala/io/TestStdInExtensions.scala new file mode 100644 index 0000000..2dac71e --- /dev/null +++ b/src/test/scala/scala/io/TestStdInExtensions.scala @@ -0,0 +1,47 @@ +/* + * Scala (https://www.scala-lang.org) + * + * Copyright EPFL and Lightbend, Inc. + * + * Licensed under Apache License 2.0 + * (http://www.apache.org/licenses/LICENSE-2.0). + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + +package scala.io + +import next._ + +import org.junit.Assert._ +import org.junit.Test + +import java.io.ByteArrayInputStream +import java.io.InputStream + +class TestStdInExtensions { + @Test + def readArray(): Unit = { + val in = new ByteArrayInputStream("1 2 3 4".getBytes) + Console.withIn(in) { + assertArrayEquals( + StdIn.readWith(input => input.split(" ").map(_.toInt)), + Array(1, 2, 3, 4)) + } + } + + @Test + def readClass(): Unit = { + case class Person(name: String, age: Int) + val in = new ByteArrayInputStream("John 34".getBytes) + Console.withIn(in) { + assertEquals( + StdIn.readWith(input => { + val Array(name, age) = input.split(" ") + Person(name, age.toInt) + }), + Person("John", 34)) + } + } +} From c954ca54b090577c4cf44cc8f463219601ffe95a Mon Sep 17 00:00:00 2001 From: riiswa Date: Thu, 25 Mar 2021 22:31:51 +0100 Subject: [PATCH 2/2] Fix scaladoc --- src/main/scala/scala/io/next/package.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/scala/io/next/package.scala b/src/main/scala/scala/io/next/package.scala index 20f0369..119580e 100644 --- a/src/main/scala/scala/io/next/package.scala +++ b/src/main/scala/scala/io/next/package.scala @@ -16,7 +16,7 @@ package object next { implicit class NextStdInExtensions(si: StdIn) { /** Reads and applying a function on an entire line of the default input . * - * @return the Byte that was read + * @return the object A that was read * @throws java.io.EOFException if the end of the * input stream has been reached. */