Hatena Blog Tags

xmpfilter

(コンピュータ)
えっくすえむぴーふぃるたー

Rubyの試行錯誤支援・テスト支援ツール。
「# =>」という注釈記号を入れた行に式の値を注釈したり、Test::UnitのassertionやRSpecのexpectationを自動で入れてくれる便利なツール。

rcodetoolsパッケージに入っていて、RubyGemsパッケージがあるので、「gem install rcodetools」でインストールできる。

READMEより…

 a, b = "foo", "baz"
 a + b                                             # =>
 a.size                                            # =>

これを「xmpfilter」にかけると、

 a, b = "foo", "baz"
 a + b                                             # => "foobaz"
 a.size                                            # => 3

こうなる。

Test::Unitでは、

  def test_insertion
    @o.insert "bar"
    @o.insert "baz"
    @o.size                                        # =>
    @o.last                                        # =>
    @o.first                                       # =>
    @o.complex_computation                         # =>
    @o.last(2)                                     # =>
  end

これを「xmpfilter -t」にかけると、

  def test_insertion
    @o.insert "bar"
    @o.insert "baz"
    assert_equal(2, @o.size)
    assert_equal("baz", @o.last)
    assert_equal("bar", @o.first)
    assert_in_delta(3.14159265358979, @o.complex_computation, 0.0001)
    assert_equal(["baz", "bar"], @o.last(2))
  end

こんな感じになる。

RSpecでは、

  class X
    Y = Struct.new(:a)
    def foo(b); b ? Y.new(2) : 2 end
    def bar; raise "No good" end
    def baz; nil end
    def fubar(x); x ** 2.0 + 1 end
    def babar; [1,2] end
    A = 1
    A = 1
  end

  context "Testing xmpfilter's expectation expansion" do
    setup do
      @o = X.new
    end

    specify "Should expand should_equal expectations" do
      @o.foo(true)   # =>
      @o.foo(true).a # =>
      @o.foo(false)  # =>
    end

    specify "Should expand should_raise expectations" do
      @o.bar         # =>
    end

    specify "Should expand should_be_nil expectations" do
      @o.baz         # =>
    end

    specify "Should expand correct expectations for complex values" do
      @o.babar       # =>
    end

    specify "Should expand should_be_close expectations" do
      @o.fubar(10)   # =>
    end
  end

これを「xmpfilter -s」にかけると、

  class X
    Y = Struct.new(:a)
    def foo(b); b ? Y.new(2) : 2 end
    def bar; raise "No good" end
    def baz; nil end
    def fubar(x); x ** 2.0 + 1 end
    def babar; [1,2] end
    A = 1
    A = 1 # !> already initialized constant A
  end

  context "Testing xmpfilter's expectation expansion" do
    setup do
      @o = X.new
    end

    specify "Should expand should_equal expectations" do
      (@o.foo(true)).should_be_a_kind_of X::Y
      (@o.foo(true).inspect).should_equal "#<struct X::Y a=2>"
      (@o.foo(true).a).should_equal 2
      (@o.foo(false)).should_equal 2
    end

    specify "Should expand should_raise expectations" do
      lambda{(@o.bar)}.should_raise RuntimeError
    end

    specify "Should expand should_be_nil expectations" do
      (@o.baz).should_be_nil
    end

    specify "Should expand correct expectations for complex values" do
      (@o.babar).should_equal [1, 2]
    end

    specify "Should expand should_be_close expectations" do
      (@o.fubar(10)).should_be_close(101.0, 0.0001)
    end
  end

こうなる!

このタグの解説についてこの解説文は、すでに終了したサービス「はてなキーワード」内で有志のユーザーが作成・編集した内容に基づいています。その正確性や網羅性をはてなが保証するものではありません。問題のある記述を発見した場合には、お問い合わせフォームよりご連絡ください。

ネットで話題

もっと見る