
1 
require_relative 'test_optparse'


2 


3 
class TestOptionParser::Acceptable < TestOptionParser


4 


5 
def setup


6 
super


7 
@opt.def_option("integer VAL", Integer) { v @integer = v }


8 
@opt.def_option("float VAL", Float) { v @float = v }


9 
@opt.def_option("numeric VAL", Numeric) { v @numeric = v }


10 


11 
@opt.def_option("decimalinteger VAL",


12 
OptionParser::DecimalInteger) { i @decimal_integer = i }


13 
@opt.def_option("octalinteger VAL",


14 
OptionParser::OctalInteger) { i @octal_integer = i }


15 
@opt.def_option("decimalnumeric VAL",


16 
OptionParser::DecimalNumeric) { i @decimal_numeric = i }


17 
end


18 


19 
def test_integer


20 
assert_equal(%w"", no_error {@opt.parse!(%w"integer 0")})


21 
assert_equal(0, @integer)


22 


23 
assert_equal(%w"", no_error {@opt.parse!(%w"integer 0b10")})


24 
assert_equal(2, @integer)


25 


26 
assert_equal(%w"", no_error {@opt.parse!(%w"integer 077")})


27 
assert_equal(63, @integer)


28 


29 
assert_equal(%w"", no_error {@opt.parse!(%w"integer 10")})


30 
assert_equal(10, @integer)


31 


32 
assert_equal(%w"", no_error {@opt.parse!(%w"integer 0x3")})


33 
assert_equal(3, @integer)


34 


35 
assert_raises(OptionParser::InvalidArgument) do


36 
@opt.parse!(%w"integer 0b")


37 
end


38 


39 
assert_raises(OptionParser::InvalidArgument) do


40 
@opt.parse!(%w"integer 09")


41 
end


42 


43 
assert_raises(OptionParser::InvalidArgument) do


44 
@opt.parse!(%w"integer 0x")


45 
end


46 
end


47 


48 
def test_float


49 
assert_equal(%w"", no_error {@opt.parse!(%w"float 0")})


50 
assert_in_epsilon(0.0, @float)


51 


52 
assert_equal(%w"", no_error {@opt.parse!(%w"float 0.0")})


53 
assert_in_epsilon(0.0, @float)


54 


55 
assert_equal(%w"", no_error {@opt.parse!(%w"float 1.2")})


56 
assert_in_epsilon(1.2, @float)


57 


58 
assert_equal(%w"", no_error {@opt.parse!(%w"float 1E2")})


59 
assert_in_epsilon(100, @float)


60 


61 
assert_equal(%w"", no_error {@opt.parse!(%w"float 1E2")})


62 
assert_in_epsilon(0.01, @float)


63 


64 
assert_raises(OptionParser::InvalidArgument) do


65 
@opt.parse!(%w"float 0e")


66 
end


67 
end


68 


69 
def test_numeric


70 
assert_equal(%w"", no_error {@opt.parse!(%w"numeric 0")})


71 
assert_equal(0, @numeric)


72 


73 
assert_equal(%w"", no_error {@opt.parse!(%w"numeric 0/1")})


74 
assert_equal(0, @numeric)


75 


76 
assert_equal(%w"", no_error {@opt.parse!(%w"numeric 1/2")})


77 
assert_equal(Rational(1, 2), @numeric)


78 


79 
assert_equal(%w"", no_error {@opt.parse!(%w"numeric 1.2/2.3")})


80 
assert_equal(Rational(12, 23), @numeric)


81 


82 
assert_raises(OptionParser::InvalidArgument) do


83 
@opt.parse!(%w"numeric 1/")


84 
end


85 
end


86 


87 
def test_decimal_integer


88 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalinteger 0")})


89 
assert_equal(0, @decimal_integer)


90 


91 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalinteger 10")})


92 
assert_equal(10, @decimal_integer)


93 


94 
assert_raises(OptionParser::InvalidArgument) do


95 
@opt.parse!(%w"decimalinteger 0b1")


96 
end


97 


98 
e = assert_raises(OptionParser::InvalidArgument) do


99 
@opt.parse!(%w"decimalinteger 09")


100 
end


101 


102 
assert_equal("invalid argument: decimalinteger 09", e.message)


103 


104 
assert_raises(OptionParser::InvalidArgument) do


105 
@opt.parse!(%w"decimalinteger x")


106 
end


107 
end


108 


109 
def test_octal_integer


110 
assert_equal(%w"", no_error {@opt.parse!(%w"octalinteger 0")})


111 
assert_equal(0, @octal_integer)


112 


113 
assert_equal(%w"", no_error {@opt.parse!(%w"octalinteger 6")})


114 
assert_equal(6, @octal_integer)


115 


116 
assert_equal(%w"", no_error {@opt.parse!(%w"octalinteger 07")})


117 
assert_equal(7, @octal_integer)


118 


119 
assert_equal(%w"", no_error {@opt.parse!(%w"octalinteger 10")})


120 
assert_equal(8, @octal_integer)


121 


122 
assert_equal(%w"", no_error {@opt.parse!(%w"octalinteger 011")})


123 
assert_equal(9, @octal_integer)


124 


125 
assert_raises(OptionParser::InvalidArgument) do


126 
@opt.parse!(%w"octalinteger 09")


127 
end


128 


129 
assert_raises(OptionParser::InvalidArgument) do


130 
@opt.parse!(%w"octalinteger 0b1")


131 
end


132 


133 
assert_raises(OptionParser::InvalidArgument) do


134 
@opt.parse!(%w"octalinteger x")


135 
end


136 
end


137 


138 
def test_decimal_numeric


139 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalnumeric 0")})


140 
assert_equal(0, @decimal_numeric)


141 


142 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalnumeric 01")})


143 
assert_equal(1, @decimal_numeric)


144 


145 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalnumeric 1.2")})


146 
assert_in_delta(1.2, @decimal_numeric)


147 


148 
assert_equal(%w"", no_error {@opt.parse!(%w"decimalnumeric 1E2")})


149 
assert_in_delta(100.0, @decimal_numeric)


150 


151 
assert_raises(OptionParser::InvalidArgument) do


152 
@opt.parse!(%w"decimalnumeric 0b1")


153 
end


154 


155 
e = assert_raises(OptionParser::InvalidArgument) do


156 
@opt.parse!(%w"decimalnumeric 09")


157 
end


158 


159 
assert_equal("invalid argument: decimalnumeric 09", e.message)


160 
end


161 


162 
end


163 
