Hatena::Grouprubyist

Going My Ruby Way このページをアンテナに追加 RSSフィード

Ruby ロゴ (C) Ruby Association LLC

2010年11月21日(日)

ユニットテスト

| 12:10 | ユニットテスト - Going My Ruby Way を含むブックマーク はてなブックマーク - ユニットテスト - Going My Ruby Way ユニットテスト - Going My Ruby Way のブックマークコメント

ruby1.9.1 では minitest が使用できる。

require 'minitest/unit'
require 'minitest/autorun'

class TestFoo < MiniTest::Unit::TestCase
    :

test/unit を使う場合以下のように使う。(ruby1.9.1 の場合、minitest が実行される)

require 'test/unit'

class TestFoo < Test::Unit::TestCase
   :

以前書いた String 拡張にユニットテストを付けた。

ruby1.8 と ruby1.9.1 で実行可能。


# coding: utf-8

class String
  module Extension
    module_function
    def remove(str, s)
      str.sub(s, "")
    end

    def indent(str, n, s=" ", d="")
      (s * n) + d + str
    end

    def wrap(str, w)
      w = w.split(//) if w.kind_of?(String)
      (w[0] || "") + str + (w[-1] || w[0] || "")
    end

    def at(str, n)
      str[n,1]
    end

    def ucc(str, sep=/[^[:alnum:]]+/)
      str.split(sep).map{|s| s.capitalize }.join
    end

    def lcc(str, sep=/[^[:alnum:]]+/)
      str.ucc(sep).sub(/./){ $&.downcase }
    end

    module Methods
      Extension.private_instance_methods.each {|method|
        module_eval %-
          def #{method}(*args)
            Extension.#{method}(self, *args)
          end
        -
      }
    end

    module Aliases
      include Extension::Methods

      alias - remove

      alias >> indent

      alias ** wrap
    end
  include Extension::Methods
  include Extension::Aliases
end


if __FILE__ == $0
  # <ruby1.9.1>
  # require 'minitest/unit'
  # require 'minitest/autorun'
  #
  # class TestString < MiniTest::Unit::TestCase
  #      :

  require 'test/unit'

  class TestString < Test::Unit::TestCase
    def test_remove
      s = "foo"

      assert_equal expected="",   s - "foo"
      assert_equal expected="o",  s - "fo"
      assert_equal expected="fo", s - "o"
      assert_equal expected=s,    s - "fooo"
      assert_equal expected=s,    s - "of"
      assert_equal expected=s,    s - ""
      assert_equal expected="f",  s - /o+/
      assert_equal expected="f",  s - /oo$/
      assert_equal expected=s,    s - /^oo/
    end

    def test_indent
      s = "foo"

      assert_equal expected="  foo",    s >> 2
      assert_equal expected="--foo",    s.indent(2, "-")
      assert_equal expected="->->foo",  s.indent(2, "->")
      assert_equal expected="->-> foo", s.indent(2, "->", " ")
    end

    def test_wrap
      s = "foo"

      assert_equal expected="(foo)",      s ** "()"
      assert_equal expected="(foo)",      s ** "(..)"
      assert_equal expected="(foo)",      s ** "({<>})"
      assert_equal expected="(foo)",      s ** %w[ ( )        ]
      assert_equal expected="(foo)",      s ** %w[ ( .. )     ]
      assert_equal expected="<<<foo>>>",  s ** %w[ <<< >>>    ]
      assert_equal expected="<<<foo>>>",  s ** %w[ <<< .. >>> ]
      assert_equal expected="'foo'",      s ** "'"
      assert_equal expected="'foo'",      s ** ["'"]
    end

    def test_at
      s = "bar"

      assert_equal expected="b",  s.at(0)
      assert_equal expected="a",  s.at(1)
      assert_equal expected="r",  s.at(2)
      assert_equal expected="r",  s.at(-1)
      assert_equal expected="",   s.at(3)
      assert_equal expected=nil,  s.at(4)
    end

    def test_ucc
      s1 = "foo_bar"
      s2 = "foo-bar"
      s3 = "foo bar"

      assert_equal expected="FooBar",  s1.ucc
      assert_equal expected="FooBar",  s2.ucc
      assert_equal expected="FooBar",  s3.ucc

      assert_equal expected="FooBar",  s1.ucc("_")
      assert_equal expected="Foo-bar", s2.ucc("_")
      assert_equal expected="Foo bar", s3.ucc("_")

      assert_equal expected="FooBar",  s1.ucc(/[-_]/)
      assert_equal expected="FooBar",  s2.ucc(/[-_]/)
      assert_equal expected="Foo bar", s3.ucc(/[-_]/)
    end

    def test_lcc
      s1 = "foo_bar"
      s2 = "foo-bar"
      s3 = "foo bar"

      assert_equal expected="fooBar",  s1.lcc
      assert_equal expected="fooBar",  s2.lcc
      assert_equal expected="fooBar",  s3.lcc

      assert_equal expected="fooBar",  s1.lcc("_")
      assert_equal expected="foo-bar", s2.lcc("_")
      assert_equal expected="foo bar", s3.lcc("_")
      assert_equal expected="fooBar",  s1.lcc(/[-_]/)
      assert_equal expected="fooBar",  s2.lcc(/[-_]/)
      assert_equal expected="foo bar", s3.lcc(/[-_]/)
    end
  end
end

# vim:set sts=2 sw=2 et ai: