RSpecでgemのtestを書いた
gemを作ってrubygems.orgで公開する - 橋本詳解 の続き。ArgsPasrerのテストを書いた。
前回testの中身を書いてなかったので。
newgemで雛形を作る時に-T rspecしておくと、rspecのテストコード雛形とそれを呼び出すrakeコマンドが自動配置される。
前回-T rspecしなかったのでtest::unitの雛形ができてしまっていた。
newgem asdfhujiko -T rspec
適当に-T rspecしたプロジェクトを作って、そこからtasksディレクトリとspecディレクトリのファイルを持ってきてArgsParser内に配置した。spec内の名前もasdfhujikoから変更した。
testディレクトリ下のTest::Unitも全部削除した。
ディレクトリ内はこうなった
ArgsParser |-- History.txt |-- Manifest.txt |-- README.rdoc |-- Rakefile |-- examples |-- lib |-- pkg |-- script |-- spec | |-- parser_spec.rb | |-- spec.opts | `-- spec_helper.rb `-- tasks `-- rspec.rake
spec/spec_helper.rb 内を編集。最後にrequire 'asdfhujiko'になってた所をrequire 'ArgsParser'に修正。
begin require 'spec' rescue LoadError require 'rubygems' unless ENV['NO_RUBYGEMS'] gem 'rspec' require 'spec' end $:.unshift(File.dirname(__FILE__) + '/../lib') require 'ArgsParser'
この状態で
rake
するとparser_spec.rb内のtestが実行される。最初から1つだけ、絶対に通るテストが書いてあるので緑文字で成功画面が表示される。
ArgsParserにはコマンドラインで受けた引数ARGVを
- -debug等のスイッチなのか
- -x 320 -y 240 等のパラメータを取得する
- -hと-helpと--helpなどの名前をbindしてあれば、同じ物として扱う
等の機能がある。これらが正しく動いているかどうかチェックするtestコードを書きたい。
なるべく複雑なARGVが引数として与えられた例を与えて、正しい挙動をしているかチェックするtestコードを書いた。
参考:Rubyist Magazine - スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編)
まあ返り値に数値を返す関数なら境界値(北緯90.1度とか)を入れて調べればいいし、文字列だったら正しい値が出てるかどうか調べればいい。
spec/parser_spec.rb
require File.dirname(__FILE__) + '/spec_helper.rb' describe "options" do before do @argv = ['-x', '320', '-yscale', '240', '-m', 'hello world', '-h', '--output', '/path/to/output/', '-debug'] @parser = ArgsParser.parser @parser.bind(:message, :m, "message text") @parser.bind(:output, :o, "path to output directory") @parser.comment(:debug, "debug mode") @parser.bind(:help, :h, "show help") @parser.bind(:xscale, :x, "width") @parser.bind(:yscale, :y, "height") @params = @parser.parse(@argv) end it "has param" do @parser.has_param(:message).should == true @parser.has_param(:output).should == true end it "has not param" do @parser.has_param(:m).should == false @parser.has_param(:o).should == false @parser.has_param(:help).should == false @parser.has_param(:name).should == false end it "has option" do @parser.has_option(:debug).should == true @parser.has_option(:help).should == true end it "has not option" do @parser.has_option(:h).should == false @parser.has_option(:m).should == false @parser.has_option(:message).should == false @parser.has_option(:output).should == false end it "has params" do @parser.has_params([:message, :output]).should == true end it "has not params" do @parser.has_params([:message, :output, :help]).should == false end it "has options" do @parser.has_options([:help, :debug]).should == true end it "has not options" do @parser.has_options([:help, :debug, :h]).should == false end it "has String in params" do @params[:output].should == '/path/to/output/' end it "has Number in params" do @params[:yscale].to_i.should == 240 @params[:xscale].to_i.should == 320 end end
Manifest.txt に追加したrspecのテストコード等を書いておかないと、.gemに格納されない。Test::Unitのファイルはもう入れないので削除した。
History.txt Manifest.txt README.rdoc Rakefile lib/ArgsParser.rb lib/ArgsParser/Parser.rb examples/example.rb script/console script/destroy script/generate tasks/rspec.rake spec/parser_spec.rb spec/spec.opts spec/spec_helper.rb
gem作ってrubygems.orgにpush
rake package gem push pkg/ArgsParser-0.0.4.gem
1分も待たずに更新されて、0.0.4が公開されてた
http://rubygems.org/gems/ArgsParser
今後の予定
- RDocを書く。gemのインストール時にrdocがないと言われるのがなんか嫌。でもhamlとかも無いから最近は書かないものなの?