diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-11-30 14:21:51 +0100 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2016-11-30 16:55:59 +0100 |
commit | 5c607cd402001b57721afef8153d9bec5e68c933 (patch) | |
tree | 9965e195914374899c523c9b375a374804bfd52f /library/test | |
parent | 3f7614ae60263c937f7b2d97f45ef6e7c803ec01 (diff) | |
download | dotty-5c607cd402001b57721afef8153d9bec5e68c933.tar.gz dotty-5c607cd402001b57721afef8153d9bec5e68c933.tar.bz2 dotty-5c607cd402001b57721afef8153d9bec5e68c933.zip |
Add basic Show capability
Diffstat (limited to 'library/test')
-rw-r--r-- | library/test/dotty/ShowTests.scala | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/library/test/dotty/ShowTests.scala b/library/test/dotty/ShowTests.scala new file mode 100644 index 000000000..ef259e5d8 --- /dev/null +++ b/library/test/dotty/ShowTests.scala @@ -0,0 +1,76 @@ +package dotty + +import org.junit.Test +import org.junit.Assert._ + +class ShowTests { + import Show._ + + @Test def showString = { + assertEquals("\"\\thello world!\"", "\thello world!".show) + assertEquals("\"\\nhello world!\"", "\nhello world!".show) + assertEquals("\"\\rhello world!\"", "\rhello world!".show) + assertEquals("\"\\b\\t\\n\\f\\r\\\'\\\"\"", "\b\t\n\f\r\'\"".show) + } + + @Test def showFloat = { + assertEquals("1.0f", 1.0f.show) + assertEquals("1.0f", 1.0F.show) + } + + @Test def showDouble = { + assertEquals("1.0", 1.0d.show) + assertEquals("1.0", 1.0.show) + } + + @Test def showChar = { + assertEquals("'\\b'", '\b'.show) + assertEquals("'\\t'", '\t'.show) + assertEquals("'\\n'", '\n'.show) + assertEquals("'\\f'", '\f'.show) + assertEquals("'\\r'", '\r'.show) + assertEquals("'\\''", '\''.show) + assertEquals("'\\\"'", '\"'.show) + } + + @Test def showCar = { + import Show.List._ + case class Car(model: String, manufacturer: String, year: Int) + implicit val showCar = new Show[Car] { + def show(c: Car) = + "Car(" + c.model.show + ", " + c.manufacturer.show + ", " + c.year.show + ")" + } + + case class Shop(xs: List[Car], name: String) + implicit val showShop = new Show[Shop] { + def show(sh: Shop) = + "Shop(" + sh.xs.show + ", " + sh.name.show + ")" + } + + assertEquals("Car(\"Mustang\", \"Ford\", 1967)", Car("Mustang", "Ford", 1967).show) + } + + @Test def showOptions = { + import Show.Option._ + // TODO: first case is not possible since there's no Show[Nothing] - yet + //assertEquals("None", None.show) + assertEquals("None", (None: Option[String]).show) + assertEquals("Some(\"hello opt\")", Some("hello opt").show) + } + + @Test def showMaps = { + import Show.Map._ + val mp = scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2") + assertEquals("Map(\"str1\" -> \"val1\", \"str2\" -> \"val2\")", mp.show) + } + + @Test def withoutShow = { + case class Car(model: String, manufacturer: String, year: Int) + + assertEquals("Car(Mustang,Ford,1967)", Car("Mustang", "Ford", 1967).show) + assertEquals( + "Map(str1 -> val1, str2 -> val2)", + scala.collection.immutable.Map("str1" -> "val1", "str2" -> "val2").show + ) + } +} |