Skip to content

Commit

Permalink
added test of utils
Browse files Browse the repository at this point in the history
  • Loading branch information
veny committed Jun 10, 2012
1 parent 30ff52c commit a468e75
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/orientdb4r/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Orientdb4r
module Utils

def verify_options(options, pattern)
raise ArgumentError, 'options cannot be nil' if options.nil?

# unknown key?
options.keys.each do |k|
raise ArgumentError, "unknow option: #{k}" unless pattern.keys.include? k
Expand All @@ -11,6 +13,13 @@ def verify_options(options, pattern)
pattern.each do |k,v|
raise ArgumentError, "missing mandatory option: #{k}" if v == :mandatory and !options.keys.include? k
end
# option in a set of allowed values
pattern.each do |k,v|
if v.instance_of? Array
raise ArgumentError, "value '#{options[k]}' not in #{v.inspect}, key=#{k}" unless v.include?(options[k])
end
end

options
end

Expand Down
30 changes: 30 additions & 0 deletions test/test_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test/unit'
require 'orientdb4r'

###
# This class tests Utils methods.
class TestDmo < Test::Unit::TestCase
include Orientdb4r::Utils

def test_verify_options
opt_pattern = {:a => :mandatory, :b => :optional, :c => 'predefined', :d => [1, 2]}
assert_nothing_thrown do verify_options({:a => 'A', :b => 'B', :c => 'C', :d => 1}, opt_pattern); end

# missing mandatory
assert_raise ArgumentError do verify_options({}, opt_pattern); end
# unknown key
assert_raise ArgumentError do verify_options({:a => 1, :z => 2}, opt_pattern); end
# value not in predefined set
assert_raise ArgumentError do verify_options({:a => 1, :d => 3}, opt_pattern); end
end

def test_verify_and_sanitize_options
opt_pattern = {:a => 'A', :b => 'B'}
options = {:a => 'X'}
verify_and_sanitize_options(options, opt_pattern)
assert_equal 2, options.size
assert_equal 'X', options[:a]
assert_equal 'B', options[:b]
end

end

0 comments on commit a468e75

Please sign in to comment.