#! /usr/bin/env ruby # -*- coding: UTF-8 -*- require 'ayk/options' # describe AYK::Options do # describe 'simple usage' do before(:all) do @opt = AYK::Options.new 'Spec tests' end it "should set option key1" do @opt.o("key 1 doc", :key1, 666) { puts 'trigger' } end it "should raise override existing key" do lambda { @opt.o("key 1 doc", :key1, 666) { puts 'trigger' } }.should raise_error AYK::OptionsError end it "should set sub options" do @opt.sub(:sub) do o 'var 1', :var1, 69 o 'var 2', :var2, 999 end @opt[:sub][:var1].should eql 69 @opt[:sub][:var2].should eql 999 end it "should access subeys through [,]" do @opt[:sub,:var1].should eql 69 @opt[:sub,:var2].should eql 999 end it "should raise calling sub on existing option" do lambda { @opt.sub :key1 }.should raise_error AYK::OptionsError end it "should raise unknown option when setting trigger" do lambda { @opt.trigger :unknonwn }.should raise_error AYK::OptionsError end it "Options.get is private" do lambda{ @opt.get(:key1) }.should raise_error NoMethodError end it "should set a trigger on assignment" do @opt[:sub][:var1].should eql 69 @opt[:sub].trigger :var1 do raise Exception.new "trigger" end lambda{ @opt[:sub][:var1] = 96}.should raise_error Exception @opt[:sub][:var1].should eql 69 begin @opt[:sub][:var1] = 96 rescue Exception $!.message.should eql "trigger" end @opt[:sub][:var1].should eql 69 end it "should get option key1 through method missing" do @opt.key1.should eql 666 end it "should get option key1 through []" do @opt[:key1].should eql 666 end it "should get option nokey through method missing" do @opt.nokey.should eql nil end it "should get option nokey through []" do @opt[:nokey].should eql nil end it "should return default valuei through method missing" do @opt.default 'default value', 'none' @opt.nokey.should eql 'none' end it "should return default value through []" do @opt.default 'default value', 'none' @opt[:nokey].should eql 'none' end it "should assign new value through []=" do @opt[:sub][:var2].should eql 999 @opt[:sub][:var2] = 666 @opt[:sub][:var2].should eql 666 end it "should nice init" do opts = AYK::Options.new('Spec nice') do |opt| opt.dsl do o 'var doc', :var, 666 end end opts.var.should eql 666 end it "should be nicely included" do class A__A; include AYK::Optioned end A__A.options.name.should eql 'a__a' end end # end #