Skip to content

Instantly share code, notes, and snippets.

@yamashiro
Created May 30, 2012 09:55
Show Gist options
  • Save yamashiro/2835272 to your computer and use it in GitHub Desktop.
Save yamashiro/2835272 to your computer and use it in GitHub Desktop.
Traitつき無名クラス
import org.specs2.mutable._
trait BarTrait {}
class Hoge {}
class TraitGetClassTest extends Specification {
"Trait mixin " should {
"getClass " in {
val hoge = new Hoge with BarTrait
hoge.getClass.toString must_== "Hoge"
}
}
}
@kmizu
Copy link

kmizu commented May 30, 2012

その辺(完全に実装依存)は承知の上です。自分もあまりやるべきとは思いませんが、必要そうだったので。

https://twitter.com/kmizu/status/207773777390604289

で、@yamashiroさんが必要だったのは、

あぁ、(無名クラス自体の名前じゃなく)無名クラスのsuperClassのHogeをとりたいってことか・・・?

ですね。ちなみに、実装依存ではあっても、この部分の実装(new ClassName with Trait1 with ...)で、ClassNameのClassオブジェクトを取る方法はそうそう変わらないだろう
という意味では、そこそこ精度は高いと思います。

@Yasushi
Copy link

Yasushi commented May 30, 2012

ためしてないんですがspecs2だとこのへんでしょうか

haveClass: to check the class of an object
haveSuperclass: to check if the class of an object as another class as one of its ancestors
haveInterface: to check if an object is implementing a given interface
beAssignableFrom: to check if a class is assignable from another
beAnInstanceOf[T]: to check if an object is an instance of 

@yamashiro
Copy link
Author

具体的な目的としては、ログに"Hoge"という文字列を出したいんですよね…

確かに実装依存ですよね…

@yamashiro
Copy link
Author

Yasushi さんの張ったspecs2のmatcherのソース明日見てみようっと…あざす

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment