-
-
Save seratch/2651382 to your computer and use it in GitHub Desktop.
ScalaでDIというかServiceLocator的な名状しがたい何か
This file contains 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
trait ApiInjector { | |
//var twitter : TwitterApi = new TwitterApiImpl; | |
val twitter : TwitterApi = new TwitterApiImpl; | |
//他にもいろいろなサービス | |
} |
This file contains 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
trait TwitterApi { | |
def publicTimeLines : List[String] | |
//他にも沢山の api | |
} |
This file contains 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 TwitterApiImpl extends TwitterApi { | |
def publicTimeLines : List[String] = { | |
//Twitter API つかってごにょごにょするはず | |
List("本当は", "リアルに", "public", "timeline", "取得する") | |
} | |
} |
This file contains 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 TwitterClient extends ApiInjector { | |
def indexedPublicTimeLine : List[String] = { | |
twitter.publicTimeLines.zipWithIndex | |
.map{ case (s, i) => (i + 1) + " " + s} | |
} | |
} |
This file contains 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
import org.specs2.mutable._ | |
trait TestApiInjector extends ApiInjector { | |
//twitter = new TwitterApi { | |
override val twitter = new TwitterApi { | |
def publicTimeLines = { | |
List ("dummy", "public", "timeline") | |
} | |
} | |
} | |
class TwitterClientTest extends Specification { | |
"Twitter Api Client" should { | |
val client = new TwitterClient with TestApiInjector | |
val indexedPublicTimeLine = client.indexedPublicTimeLine | |
"indexedPublicTimeLine return size " in { | |
indexedPublicTimeLine.size must_== 3 | |
} | |
"indexedPublicTimeLine return indexedLine" in { | |
indexedPublicTimeLine(0) must_== "1 dummy" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment