Created
July 7, 2013 15:02
-
-
Save zaneli/5943733 to your computer and use it in GitHub Desktop.
「CoffeeScript によるデザインパターン(Template Method)」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report | |
constructor: -> | |
@title = "月次報告" | |
@text = ["順調", "最高の調子"] | |
outputReport: -> | |
@outputStart() | |
@outputHead() | |
@outputBodyStart() | |
@outputBody() | |
@outputBodyEnd() | |
@outputEnd() | |
# 空実装 | |
outputStart: -> | |
outputHead: -> | |
outputBodyStart: -> | |
outputBodyEnd: -> | |
outputEnd: -> | |
outputBody: -> | |
for line in @text | |
@outputLine line | |
class @HTMLReport extends Report | |
outputStart: -> | |
console.log "<html>" | |
outputHead: -> | |
console.log " <head>" | |
console.log " <title>#{@title}</title>" | |
console.log " </head>" | |
outputBodyStart: -> | |
console.log " <body>" | |
outputLine: (line) -> | |
console.log " <p>#{line}</p>" | |
outputBodyEnd: -> | |
console.log " </body>" | |
outputEnd: -> | |
console.log "</html>" | |
class @PlainTextReport extends Report | |
outputHead: -> | |
console.log "*** #{@title} ***" | |
outputLine: (line) -> | |
console.log line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report | |
constructor: -> | |
@title = "月次報告" | |
@text = ["順調", "最高の調子"] | |
outputReport: -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{@title}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in @text | |
console.log " </body>" | |
console.log "</html>" | |
$ -> new Report().outputReport() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @Report | |
constructor: -> | |
@title = "月次報告" | |
@text = ["順調", "最高の調子"] | |
outputReport: (format) -> | |
switch format | |
when "plain" | |
console.log "*** #{@title} ***" | |
when "html" | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{@title}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
else | |
throw "Unknown format: #{format}" | |
for line in @text | |
switch format | |
when "plain" | |
console.log line | |
when "html" | |
console.log " <p>#{line}</p>" | |
else | |
throw "Unknown format: #{format}" | |
if format is "html" | |
console.log " </body>" | |
console.log "</html>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views | |
import org.specs2.mutable.Specification | |
import play.api.test.TestServer | |
import play.api.test.Helpers.{ running, HTMLUNIT } | |
class TemplateMethodSpec extends Specification { | |
private val testPort = 3333 | |
"TemplateMethod" should { | |
"output html report" in { | |
running(TestServer(testPort), HTMLUNIT) { browser => | |
browser.goTo(s"http://localhost:${testPort}/template-method") | |
browser.executeScript(""" | |
new HTMLReport().outputReport() | |
""") | |
browser.title must_== "Template Method" | |
browser.$("#__console").getValue must_== """<html> | |
<head> | |
<title>月次報告</title> | |
</head> | |
<body> | |
<p>順調</p> | |
<p>最高の調子</p> | |
</body> | |
</html> | |
""" | |
} | |
} | |
"output plain text report" in { | |
running(TestServer(testPort), HTMLUNIT) { browser => | |
browser.goTo(s"http://localhost:${testPort}/template-method") | |
browser.executeScript(""" | |
new PlainTextReport().outputReport() | |
""") | |
browser.title must_== "Template Method" | |
browser.$("#__console").getValue must_== """*** 月次報告 *** | |
順調 | |
最高の調子 | |
""" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment