From c535aec63f6e30b0b689ad60b1dd2f1b78c66039 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 21 Mar 2012 14:12:25 -0700 Subject: An illustrative delayedInit test. --- test/files/run/delay-bad.check | 47 +++++++++++++++++++++++++ test/files/run/delay-bad.scala | 77 +++++++++++++++++++++++++++++++++++++++++ test/files/run/delay-good.check | 41 ++++++++++++++++++++++ test/files/run/delay-good.scala | 77 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 test/files/run/delay-bad.check create mode 100644 test/files/run/delay-bad.scala create mode 100644 test/files/run/delay-good.check create mode 100644 test/files/run/delay-good.scala (limited to 'test/files/run') diff --git a/test/files/run/delay-bad.check b/test/files/run/delay-bad.check new file mode 100644 index 0000000000..9d9c828a03 --- /dev/null +++ b/test/files/run/delay-bad.check @@ -0,0 +1,47 @@ + + +// new C { } +-A -B -C + +// new C { 5 } +-A -B -C + A+ B+ C+ + +// new D() +-A -B -C -D + A+ B+ C+ D+ + +// new D() { } +-A -B -C -D + A+ B+ C+ D+ + +// new D() { val x = 5 } +-A -B -C -D + A+ B+ C+ D+ + A+ B+ C+ D+ + +// new { val x = 5 } with D() +-A -B -C -D + A+ B+ C+ D+ + +// new E() { val x = 5 } +-A -B -C -D + A+ B+ C+ D+ E+ -E + A+ B+ C+ D+ E+ + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() +-A -B -C -D + A+ B+ C+ D+ E+ -E + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() { } +-A -B -C -D + A+ B+ C+ D+ E+ -E + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() { 5 } +-A -B -C -D + A+ B+ C+ D+ E+ -E + A+ B+ C+ D+ E+ + A+ B+ C+ D+ E+ diff --git a/test/files/run/delay-bad.scala b/test/files/run/delay-bad.scala new file mode 100644 index 0000000000..43acc1ea3d --- /dev/null +++ b/test/files/run/delay-bad.scala @@ -0,0 +1,77 @@ +trait A extends DelayedInit +{ + print("-A") + + def delayedInit(body: => Unit) = { + body + postConstructionCode + } + def postConstructionCode: Unit = { + print("\n A+") + } +} +trait B extends A { + print(" -B") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" B+") + } +} + +trait C extends B { + print(" -C") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" C+") + } +} + +class D() extends C { + print(" -D") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" D+") + } +} +class E() extends D() { + print(" -E") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" E+") + } +} + +object Test { + def p(msg: String) = println("\n\n// " + msg) + + def main(args: Array[String]) { + val f: A => Unit = _ => () + + p("new C { }") + f(new C { }) + p("new C { 5 }") + f(new C { 5 }) + + p("new D()") + f(new D()) + p("new D() { }") + f(new D() { }) + + p("new D() { val x = 5 }") + f(new D() { val x = 5 }) + p("new { val x = 5 } with D()") + f(new { val x = 5 } with D()) + + p("new E() { val x = 5 }") + f(new E() { val x = 5 }) + p("new { val x = 5 } with E()") + f(new { val x = 5 } with E()) + + p("new { val x = 5 } with E() { }") + f(new { val x = 5 } with E() { }) + p("new { val x = 5 } with E() { 5 }") + f(new { val x = 5 } with E() { 5 }) + + println("") + } +} diff --git a/test/files/run/delay-good.check b/test/files/run/delay-good.check new file mode 100644 index 0000000000..8eb04c7cff --- /dev/null +++ b/test/files/run/delay-good.check @@ -0,0 +1,41 @@ + + +// new C { } +-A -B -C + A+ B+ C+ + +// new C { 5 } +-A -B -C + A+ B+ C+ + +// new D() +-A -B -C -D + A+ B+ C+ D+ + +// new D() { } +-A -B -C -D + A+ B+ C+ D+ + +// new D() { val x = 5 } +-A -B -C -D + A+ B+ C+ D+ + +// new { val x = 5 } with D() +-A -B -C -D + A+ B+ C+ D+ + +// new E() { val x = 5 } +-A -B -C -D -E + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() +-A -B -C -D -E + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() { } +-A -B -C -D -E + A+ B+ C+ D+ E+ + +// new { val x = 5 } with E() { 5 } +-A -B -C -D -E + A+ B+ C+ D+ E+ diff --git a/test/files/run/delay-good.scala b/test/files/run/delay-good.scala new file mode 100644 index 0000000000..2e4487b92c --- /dev/null +++ b/test/files/run/delay-good.scala @@ -0,0 +1,77 @@ +trait A +{ + print("-A") + + def delayedInit(body: => Unit) = { + body + postConstructionCode + } + def postConstructionCode: Unit = { + print("\n A+") + } +} +trait B extends A { + print(" -B") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" B+") + } +} + +trait C extends B { + print(" -C") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" C+") + } +} + +class D() extends C { + print(" -D") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" D+") + } +} +class E() extends D() { + print(" -E") + override def postConstructionCode: Unit = { + super.postConstructionCode + print(" E+") + } +} + +object Test { + def p(msg: String) = println("\n\n// " + msg) + + def main(args: Array[String]) { + val f: A => Unit = _.postConstructionCode + + p("new C { }") + f(new C { }) + p("new C { 5 }") + f(new C { 5 }) + + p("new D()") + f(new D()) + p("new D() { }") + f(new D() { }) + + p("new D() { val x = 5 }") + f(new D() { val x = 5 }) + p("new { val x = 5 } with D()") + f(new { val x = 5 } with D()) + + p("new E() { val x = 5 }") + f(new E() { val x = 5 }) + p("new { val x = 5 } with E()") + f(new { val x = 5 } with E()) + + p("new { val x = 5 } with E() { }") + f(new { val x = 5 } with E() { }) + p("new { val x = 5 } with E() { 5 }") + f(new { val x = 5 } with E() { 5 }) + + println("") + } +} -- cgit v1.2.3