From 8a7eb3c659b70b990579bc56f0b3f65063716579 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 20:30:59 +0200 Subject: [PATCH 01/18] Move BigDecimal to rely less on shared examples --- library/bigdecimal/case_compare_spec.rb | 6 ++- library/bigdecimal/clone_spec.rb | 8 ++-- library/bigdecimal/dup_spec.rb | 12 ++++- library/bigdecimal/eql_spec.rb | 6 ++- library/bigdecimal/equal_value_spec.rb | 60 +++++++++++++++++++++++- library/bigdecimal/modulo_spec.rb | 15 ++++-- library/bigdecimal/shared/clone.rb | 13 ------ library/bigdecimal/shared/eql.rb | 61 ------------------------- library/bigdecimal/shared/modulo.rb | 10 ---- library/bigdecimal/shared/to_int.rb | 16 ------- library/bigdecimal/to_i_spec.rb | 14 +++++- library/bigdecimal/to_int_spec.rb | 6 +-- 12 files changed, 108 insertions(+), 119 deletions(-) delete mode 100644 library/bigdecimal/shared/clone.rb delete mode 100644 library/bigdecimal/shared/eql.rb delete mode 100644 library/bigdecimal/shared/to_int.rb diff --git a/library/bigdecimal/case_compare_spec.rb b/library/bigdecimal/case_compare_spec.rb index fac6714356..1f1d223d6a 100644 --- a/library/bigdecimal/case_compare_spec.rb +++ b/library/bigdecimal/case_compare_spec.rb @@ -1,7 +1,9 @@ require_relative '../../spec_helper' -require_relative 'shared/eql' +require 'bigdecimal' describe "BigDecimal#===" do - it_behaves_like :bigdecimal_eql, :=== + it "is an alias of BigDecimal#==" do + BigDecimal.instance_method(:===).should == BigDecimal.instance_method(:==) + end end diff --git a/library/bigdecimal/clone_spec.rb b/library/bigdecimal/clone_spec.rb index b3a1c61d6a..979cc4c8e9 100644 --- a/library/bigdecimal/clone_spec.rb +++ b/library/bigdecimal/clone_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/clone' +require 'bigdecimal' -describe "BigDecimal#dup" do - it_behaves_like :bigdecimal_clone, :clone +describe "BigDecimal#clone" do + it "is an alias of BigDecimal#dup" do + BigDecimal.instance_method(:clone).should == BigDecimal.instance_method(:dup) + end end diff --git a/library/bigdecimal/dup_spec.rb b/library/bigdecimal/dup_spec.rb index bfabaf6e8b..dbf79ce5a6 100644 --- a/library/bigdecimal/dup_spec.rb +++ b/library/bigdecimal/dup_spec.rb @@ -1,6 +1,14 @@ require_relative '../../spec_helper' -require_relative 'shared/clone' +require 'bigdecimal' describe "BigDecimal#dup" do - it_behaves_like :bigdecimal_clone, :dup + before :each do + @obj = BigDecimal("1.2345") + end + + it "returns self" do + copy = @obj.dup + + copy.should.equal?(@obj) + end end diff --git a/library/bigdecimal/eql_spec.rb b/library/bigdecimal/eql_spec.rb index 1be5862751..0346175e1e 100644 --- a/library/bigdecimal/eql_spec.rb +++ b/library/bigdecimal/eql_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/eql' +require 'bigdecimal' describe "BigDecimal#eql?" do - it_behaves_like :bigdecimal_eql, :eql? + it "is an alias of BigDecimal#==" do + BigDecimal.instance_method(:eql?).should == BigDecimal.instance_method(:==) + end end diff --git a/library/bigdecimal/equal_value_spec.rb b/library/bigdecimal/equal_value_spec.rb index 933060eada..d1133765b1 100644 --- a/library/bigdecimal/equal_value_spec.rb +++ b/library/bigdecimal/equal_value_spec.rb @@ -1,7 +1,63 @@ require_relative '../../spec_helper' -require_relative 'shared/eql' +require 'bigdecimal' describe "BigDecimal#==" do - it_behaves_like :bigdecimal_eql, :== + before :each do + @bg6543_21 = BigDecimal("6543.21") + @bg5667_19 = BigDecimal("5667.19") + @a = BigDecimal("1.0000000000000000000000000000000000000000005") + @b = BigDecimal("1.00000000000000000000000000000000000000000005") + @bigint = BigDecimal("1000.0") + @nan = BigDecimal("NaN") + @infinity = BigDecimal("Infinity") + @infinity_minus = BigDecimal("-Infinity") + end + + it "tests for equality" do + (@bg6543_21 == @bg6543_21).should == true + (@a == @a).should == true + (@a == @b).should == false + (@bg6543_21 == @a).should == false + (@bigint == 1000).should == true + end + + it "returns false for NaN as it is never equal to any number" do + (@nan == @nan).should == false + (@a == @nan).should == false + (@nan == @a).should == false + (@nan == @infinity).should == false + (@nan == @infinity_minus).should == false + (@infinity == @nan).should == false + (@infinity_minus == @nan).should == false + end + + it "returns true for infinity values with the same sign" do + (@infinity == @infinity).should == true + (@infinity == BigDecimal("Infinity")).should == true + (BigDecimal("Infinity") == @infinity).should == true + + (@infinity_minus == @infinity_minus).should == true + (@infinity_minus == BigDecimal("-Infinity")).should == true + (BigDecimal("-Infinity") == @infinity_minus).should == true + end + + it "returns false for infinity values with different signs" do + (@infinity == @infinity_minus).should == false + (@infinity_minus == @infinity).should == false + end + + it "returns false when infinite value compared to finite one" do + (@infinity == @a).should == false + (@infinity_minus == @a).should == false + + (@a == @infinity).should == false + (@a == @infinity_minus).should == false + end + + it "returns false when compared objects that can not be coerced into BigDecimal" do + (@infinity == nil).should == false + (@bigint == nil).should == false + (@nan == nil).should == false + end end diff --git a/library/bigdecimal/modulo_spec.rb b/library/bigdecimal/modulo_spec.rb index 035d31bd98..d6b4f91b6d 100644 --- a/library/bigdecimal/modulo_spec.rb +++ b/library/bigdecimal/modulo_spec.rb @@ -1,12 +1,21 @@ require_relative '../../spec_helper' require_relative 'shared/modulo' +require 'bigdecimal' describe "BigDecimal#%" do it_behaves_like :bigdecimal_modulo, :% - it_behaves_like :bigdecimal_modulo_zerodivisionerror, :% + + it "raises ZeroDivisionError if other is zero" do + bd5667 = BigDecimal("5667.19") + + -> { bd5667 % 0 }.should.raise(ZeroDivisionError) + -> { bd5667 % BigDecimal("0") }.should.raise(ZeroDivisionError) + -> { @zero % @zero }.should.raise(ZeroDivisionError) + end end describe "BigDecimal#modulo" do - it_behaves_like :bigdecimal_modulo, :modulo - it_behaves_like :bigdecimal_modulo_zerodivisionerror, :modulo + it "is an alias of BigDecimal#%" do + BigDecimal.instance_method(:modulo).should == BigDecimal.instance_method(:%) + end end diff --git a/library/bigdecimal/shared/clone.rb b/library/bigdecimal/shared/clone.rb deleted file mode 100644 index 03de6d0fc3..0000000000 --- a/library/bigdecimal/shared/clone.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'bigdecimal' - -describe :bigdecimal_clone, shared: true do - before :each do - @obj = BigDecimal("1.2345") - end - - it "returns self" do - copy = @obj.public_send(@method) - - copy.should.equal?(@obj) - end -end diff --git a/library/bigdecimal/shared/eql.rb b/library/bigdecimal/shared/eql.rb deleted file mode 100644 index 8e3e388bab..0000000000 --- a/library/bigdecimal/shared/eql.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'bigdecimal' - -describe :bigdecimal_eql, shared: true do - before :each do - @bg6543_21 = BigDecimal("6543.21") - @bg5667_19 = BigDecimal("5667.19") - @a = BigDecimal("1.0000000000000000000000000000000000000000005") - @b = BigDecimal("1.00000000000000000000000000000000000000000005") - @bigint = BigDecimal("1000.0") - @nan = BigDecimal("NaN") - @infinity = BigDecimal("Infinity") - @infinity_minus = BigDecimal("-Infinity") - end - - it "tests for equality" do - @bg6543_21.send(@method, @bg6543_21).should == true - @a.send(@method, @a).should == true - @a.send(@method, @b).should == false - @bg6543_21.send(@method, @a).should == false - @bigint.send(@method, 1000).should == true - end - - it "returns false for NaN as it is never equal to any number" do - @nan.send(@method, @nan).should == false - @a.send(@method, @nan).should == false - @nan.send(@method, @a).should == false - @nan.send(@method, @infinity).should == false - @nan.send(@method, @infinity_minus).should == false - @infinity.send(@method, @nan).should == false - @infinity_minus.send(@method, @nan).should == false - end - - it "returns true for infinity values with the same sign" do - @infinity.send(@method, @infinity).should == true - @infinity.send(@method, BigDecimal("Infinity")).should == true - BigDecimal("Infinity").send(@method, @infinity).should == true - - @infinity_minus.send(@method, @infinity_minus).should == true - @infinity_minus.send(@method, BigDecimal("-Infinity")).should == true - BigDecimal("-Infinity").send(@method, @infinity_minus).should == true - end - - it "returns false for infinity values with different signs" do - @infinity.send(@method, @infinity_minus).should == false - @infinity_minus.send(@method, @infinity).should == false - end - - it "returns false when infinite value compared to finite one" do - @infinity.send(@method, @a).should == false - @infinity_minus.send(@method, @a).should == false - - @a.send(@method, @infinity).should == false - @a.send(@method, @infinity_minus).should == false - end - - it "returns false when compared objects that can not be coerced into BigDecimal" do - @infinity.send(@method, nil).should == false - @bigint.send(@method, nil).should == false - @nan.send(@method, nil).should == false - end -end diff --git a/library/bigdecimal/shared/modulo.rb b/library/bigdecimal/shared/modulo.rb index 63470d0977..5b5e3503c4 100644 --- a/library/bigdecimal/shared/modulo.rb +++ b/library/bigdecimal/shared/modulo.rb @@ -119,13 +119,3 @@ }.should.raise(TypeError) end end - -describe :bigdecimal_modulo_zerodivisionerror, shared: true do - it "raises ZeroDivisionError if other is zero" do - bd5667 = BigDecimal("5667.19") - - -> { bd5667.send(@method, 0) }.should.raise(ZeroDivisionError) - -> { bd5667.send(@method, BigDecimal("0")) }.should.raise(ZeroDivisionError) - -> { @zero.send(@method, @zero) }.should.raise(ZeroDivisionError) - end -end diff --git a/library/bigdecimal/shared/to_int.rb b/library/bigdecimal/shared/to_int.rb deleted file mode 100644 index 3c9f3b4f97..0000000000 --- a/library/bigdecimal/shared/to_int.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'bigdecimal' - -describe :bigdecimal_to_int, shared: true do - it "raises FloatDomainError if BigDecimal is infinity or NaN" do - -> { BigDecimal("Infinity").send(@method) }.should.raise(FloatDomainError) - -> { BigDecimal("NaN").send(@method) }.should.raise(FloatDomainError) - end - - it "returns Integer otherwise" do - BigDecimal("3E-20001").send(@method).should == 0 - BigDecimal("2E4000").send(@method).should == 2 * 10 ** 4000 - BigDecimal("2").send(@method).should == 2 - BigDecimal("2E10").send(@method).should == 20000000000 - BigDecimal("3.14159").send(@method).should == 3 - end -end diff --git a/library/bigdecimal/to_i_spec.rb b/library/bigdecimal/to_i_spec.rb index e5e65c562e..b6d9e43e57 100644 --- a/library/bigdecimal/to_i_spec.rb +++ b/library/bigdecimal/to_i_spec.rb @@ -1,7 +1,17 @@ require_relative '../../spec_helper' -require_relative 'shared/to_int' require 'bigdecimal' describe "BigDecimal#to_i" do - it_behaves_like :bigdecimal_to_int, :to_i + it "raises FloatDomainError if BigDecimal is infinity or NaN" do + -> { BigDecimal("Infinity").to_i }.should.raise(FloatDomainError) + -> { BigDecimal("NaN").to_i }.should.raise(FloatDomainError) + end + + it "returns Integer otherwise" do + BigDecimal("3E-20001").to_i.should == 0 + BigDecimal("2E4000").to_i.should == 2 * 10 ** 4000 + BigDecimal("2").to_i.should == 2 + BigDecimal("2E10").to_i.should == 20000000000 + BigDecimal("3.14159").to_i.should == 3 + end end diff --git a/library/bigdecimal/to_int_spec.rb b/library/bigdecimal/to_int_spec.rb index 4df6749845..18f3620f5e 100644 --- a/library/bigdecimal/to_int_spec.rb +++ b/library/bigdecimal/to_int_spec.rb @@ -1,8 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/to_int' require 'bigdecimal' - describe "BigDecimal#to_int" do - it_behaves_like :bigdecimal_to_int, :to_int + it "is an alias of BigDecimal#to_i" do + BigDecimal.instance_method(:to_int).should == BigDecimal.instance_method(:to_i) + end end From 8352bf630618d4da73c53f5afdf14f4a6f94477b Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 20:48:59 +0200 Subject: [PATCH 02/18] Move Date to rely less on shared examples --- library/date/commercial_spec.rb | 46 +++++++++++++++++++++---- library/date/ctime_spec.rb | 4 ++- library/date/jd_spec.rb | 15 +++++--- library/date/mday_spec.rb | 4 ++- library/date/mon_spec.rb | 5 +-- library/date/month_spec.rb | 6 ++-- library/date/ordinal_spec.rb | 14 ++++++-- library/date/shared/commercial.rb | 39 --------------------- library/date/shared/jd.rb | 14 -------- library/date/shared/month.rb | 6 ---- library/date/shared/ordinal.rb | 22 ------------ library/date/shared/valid_civil.rb | 36 ------------------- library/date/shared/valid_commercial.rb | 34 ------------------ library/date/shared/valid_jd.rb | 20 ----------- library/date/shared/valid_ordinal.rb | 26 -------------- library/date/succ_spec.rb | 4 ++- library/date/valid_civil_spec.rb | 9 +++-- library/date/valid_commercial_spec.rb | 33 ++++++++++++++++-- library/date/valid_date_spec.rb | 35 +++++++++++++++++-- library/date/valid_jd_spec.rb | 18 ++++++++-- library/date/valid_ordinal_spec.rb | 26 ++++++++++++-- 21 files changed, 183 insertions(+), 233 deletions(-) delete mode 100644 library/date/shared/commercial.rb delete mode 100644 library/date/shared/jd.rb delete mode 100644 library/date/shared/month.rb delete mode 100644 library/date/shared/ordinal.rb delete mode 100644 library/date/shared/valid_civil.rb delete mode 100644 library/date/shared/valid_commercial.rb delete mode 100644 library/date/shared/valid_jd.rb delete mode 100644 library/date/shared/valid_ordinal.rb diff --git a/library/date/commercial_spec.rb b/library/date/commercial_spec.rb index d7fc34d74a..8e2df79b90 100644 --- a/library/date/commercial_spec.rb +++ b/library/date/commercial_spec.rb @@ -1,12 +1,5 @@ require 'date' require_relative '../../spec_helper' -require_relative 'shared/commercial' - -describe "Date#commercial" do - - it_behaves_like :date_commercial, :commercial - -end # reference: # October 1582 (the Gregorian calendar, Civil Date) @@ -15,3 +8,42 @@ # 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 # 31 +describe "Date.commercial" do + it "creates a Date for Julian Day Number day 0 by default" do + d = Date.commercial + d.year.should == -4712 + d.month.should == 1 + d.day.should == 1 + end + + it "creates a Date for the monday in the year and week given" do + d = Date.commercial(2000, 1) + d.year.should == 2000 + d.month.should == 1 + d.day.should == 3 + d.cwday.should == 1 + end + + it "creates a Date for the correct day given the year, week and day number" do + d = Date.commercial(2004, 1, 1) + d.year.should == 2003 + d.month.should == 12 + d.day.should == 29 + d.cwday.should == 1 + d.cweek.should == 1 + d.cwyear.should == 2004 + end + + it "creates only Date objects for valid weeks" do + -> { Date.commercial(2004, 53, 1) }.should_not.raise(ArgumentError) + -> { Date.commercial(2004, 53, 0) }.should.raise(ArgumentError) + -> { Date.commercial(2004, 53, 8) }.should.raise(ArgumentError) + -> { Date.commercial(2004, 54, 1) }.should.raise(ArgumentError) + -> { Date.commercial(2004, 0, 1) }.should.raise(ArgumentError) + + -> { Date.commercial(2003, 52, 1) }.should_not.raise(ArgumentError) + -> { Date.commercial(2003, 53, 1) }.should.raise(ArgumentError) + -> { Date.commercial(2003, 52, 0) }.should.raise(ArgumentError) + -> { Date.commercial(2003, 52, 8) }.should.raise(ArgumentError) + end +end diff --git a/library/date/ctime_spec.rb b/library/date/ctime_spec.rb index 3faa7c6380..330076a735 100644 --- a/library/date/ctime_spec.rb +++ b/library/date/ctime_spec.rb @@ -2,5 +2,7 @@ require 'date' describe "Date#ctime" do - it "needs to be reviewed for spec completeness" + it "is an alias of Date#asctime" do + Date.instance_method(:ctime).should == Date.instance_method(:asctime) + end end diff --git a/library/date/jd_spec.rb b/library/date/jd_spec.rb index 336b783e8d..e5cfe10eff 100644 --- a/library/date/jd_spec.rb +++ b/library/date/jd_spec.rb @@ -1,15 +1,22 @@ require_relative '../../spec_helper' -require_relative 'shared/jd' require 'date' describe "Date#jd" do - it "determines the Julian day for a Date object" do Date.civil(2008, 1, 16).jd.should == 2454482 end - end describe "Date.jd" do - it_behaves_like :date_jd, :jd + it "constructs a Date object if passed a Julian day" do + Date.jd(2454482).should == Date.civil(2008, 1, 16) + end + + it "returns a Date object representing Julian day 0 (-4712-01-01) if no arguments passed" do + Date.jd.should == Date.civil(-4712, 1, 1) + end + + it "constructs a Date object if passed a negative number" do + Date.jd(-1).should == Date.civil(-4713, 12, 31) + end end diff --git a/library/date/mday_spec.rb b/library/date/mday_spec.rb index 53f6f98169..32fd8fc7f1 100644 --- a/library/date/mday_spec.rb +++ b/library/date/mday_spec.rb @@ -2,5 +2,7 @@ require 'date' describe "Date#mday" do - it "needs to be reviewed for spec completeness" + it "is an alias of Date#day" do + Date.instance_method(:mday).should == Date.instance_method(:day) + end end diff --git a/library/date/mon_spec.rb b/library/date/mon_spec.rb index 616d72cf88..15754ffb1f 100644 --- a/library/date/mon_spec.rb +++ b/library/date/mon_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/month' require 'date' describe "Date#mon" do - it_behaves_like :date_month, :mon + it "is an alias of Date#month" do + Date.instance_method(:mon).should == Date.instance_method(:month) + end end diff --git a/library/date/month_spec.rb b/library/date/month_spec.rb index f493ec8119..e040f9a94c 100644 --- a/library/date/month_spec.rb +++ b/library/date/month_spec.rb @@ -1,7 +1,9 @@ require_relative '../../spec_helper' -require_relative 'shared/month' require 'date' describe "Date#month" do - it_behaves_like :date_month, :month + it "returns the month" do + m = Date.new(2000, 7, 1).month + m.should == 7 + end end diff --git a/library/date/ordinal_spec.rb b/library/date/ordinal_spec.rb index ec490fd49c..c8bf715163 100644 --- a/library/date/ordinal_spec.rb +++ b/library/date/ordinal_spec.rb @@ -1,7 +1,17 @@ require 'date' require_relative '../../spec_helper' -require_relative 'shared/ordinal' describe "Date.ordinal" do - it_behaves_like :date_ordinal, :ordinal + it "constructs a Date object from an ordinal date" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # 274 275 276 277 278 279 + # 280 281 282 283 284 285 286 + # 287 288 289 290 291 292 293 + # 294 + Date.ordinal(1582, 274).should == Date.civil(1582, 10, 1) + Date.ordinal(1582, 277).should == Date.civil(1582, 10, 4) + Date.ordinal(1582, 278).should == Date.civil(1582, 10, 15) + Date.ordinal(1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND) + end end diff --git a/library/date/shared/commercial.rb b/library/date/shared/commercial.rb deleted file mode 100644 index f53d83235a..0000000000 --- a/library/date/shared/commercial.rb +++ /dev/null @@ -1,39 +0,0 @@ -describe :date_commercial, shared: true do - it "creates a Date for Julian Day Number day 0 by default" do - d = Date.send(@method) - d.year.should == -4712 - d.month.should == 1 - d.day.should == 1 - end - - it "creates a Date for the monday in the year and week given" do - d = Date.send(@method, 2000, 1) - d.year.should == 2000 - d.month.should == 1 - d.day.should == 3 - d.cwday.should == 1 - end - - it "creates a Date for the correct day given the year, week and day number" do - d = Date.send(@method, 2004, 1, 1) - d.year.should == 2003 - d.month.should == 12 - d.day.should == 29 - d.cwday.should == 1 - d.cweek.should == 1 - d.cwyear.should == 2004 - end - - it "creates only Date objects for valid weeks" do - -> { Date.send(@method, 2004, 53, 1) }.should_not.raise(ArgumentError) - -> { Date.send(@method, 2004, 53, 0) }.should.raise(ArgumentError) - -> { Date.send(@method, 2004, 53, 8) }.should.raise(ArgumentError) - -> { Date.send(@method, 2004, 54, 1) }.should.raise(ArgumentError) - -> { Date.send(@method, 2004, 0, 1) }.should.raise(ArgumentError) - - -> { Date.send(@method, 2003, 52, 1) }.should_not.raise(ArgumentError) - -> { Date.send(@method, 2003, 53, 1) }.should.raise(ArgumentError) - -> { Date.send(@method, 2003, 52, 0) }.should.raise(ArgumentError) - -> { Date.send(@method, 2003, 52, 8) }.should.raise(ArgumentError) - end -end diff --git a/library/date/shared/jd.rb b/library/date/shared/jd.rb deleted file mode 100644 index 511557b4f7..0000000000 --- a/library/date/shared/jd.rb +++ /dev/null @@ -1,14 +0,0 @@ -describe :date_jd, shared: true do - it "constructs a Date object if passed a Julian day" do - Date.send(@method, 2454482).should == Date.civil(2008, 1, 16) - end - - it "returns a Date object representing Julian day 0 (-4712-01-01) if no arguments passed" do - Date.send(@method).should == Date.civil(-4712, 1, 1) - end - - it "constructs a Date object if passed a negative number" do - Date.send(@method, -1).should == Date.civil(-4713, 12, 31) - end - -end diff --git a/library/date/shared/month.rb b/library/date/shared/month.rb deleted file mode 100644 index 5fcb2cbeb0..0000000000 --- a/library/date/shared/month.rb +++ /dev/null @@ -1,6 +0,0 @@ -describe :date_month, shared: true do - it "returns the month" do - m = Date.new(2000, 7, 1).send(@method) - m.should == 7 - end -end diff --git a/library/date/shared/ordinal.rb b/library/date/shared/ordinal.rb deleted file mode 100644 index 4b182d5a25..0000000000 --- a/library/date/shared/ordinal.rb +++ /dev/null @@ -1,22 +0,0 @@ -# reference: -# October 1582 (the Gregorian calendar, Civil Date) -# S M Tu W Th F S -# 1 2 3 4 15 16 -# 17 18 19 20 21 22 23 -# 24 25 26 27 28 29 30 -# 31 - -describe :date_ordinal, shared: true do - it "constructs a Date object from an ordinal date" do - # October 1582 (the Gregorian calendar, Ordinal Date) - # S M Tu W Th F S - # 274 275 276 277 278 279 - # 280 281 282 283 284 285 286 - # 287 288 289 290 291 292 293 - # 294 - Date.send(@method, 1582, 274).should == Date.civil(1582, 10, 1) - Date.send(@method, 1582, 277).should == Date.civil(1582, 10, 4) - Date.send(@method, 1582, 278).should == Date.civil(1582, 10, 15) - Date.send(@method, 1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND) - end -end diff --git a/library/date/shared/valid_civil.rb b/library/date/shared/valid_civil.rb deleted file mode 100644 index 425fee4d2d..0000000000 --- a/library/date/shared/valid_civil.rb +++ /dev/null @@ -1,36 +0,0 @@ -describe :date_valid_civil?, shared: true do - - # reference: - # October 1582 (the Gregorian calendar, Civil Date) - # S M Tu W Th F S - # 1 2 3 4 15 16 - # 17 18 19 20 21 22 23 - # 24 25 26 27 28 29 30 - # 31 - - it "returns true if it is a valid civil date" do - Date.send(@method, 1582, 10, 15).should == true - Date.send(@method, 1582, 10, 14, Date::ENGLAND).should == true - end - - it "returns false if it is not a valid civil date" do - Date.send(@method, 1582, 10, 14).should == false - end - - it "handles negative months and days" do - # October 1582 (the Gregorian calendar, Civil Date) - # S M Tu W Th F S - # -21 -20 -19 -18 -17 -16 - # -15 -14 -13 -12 -11 -10 -9 - # -8 -7 -6 -5 -4 -3 -2 - # -1 - Date.send(@method, 1582, -3, -22).should == false - Date.send(@method, 1582, -3, -21).should == true - Date.send(@method, 1582, -3, -18).should == true - Date.send(@method, 1582, -3, -17).should == true - - Date.send(@method, 2007, -11, -10).should == true - Date.send(@method, 2008, -11, -10).should == true - end - -end diff --git a/library/date/shared/valid_commercial.rb b/library/date/shared/valid_commercial.rb deleted file mode 100644 index 573b851fdd..0000000000 --- a/library/date/shared/valid_commercial.rb +++ /dev/null @@ -1,34 +0,0 @@ -describe :date_valid_commercial?, shared: true do - - it "returns true if it is a valid commercial date" do - # October 1582 (the Gregorian calendar, Commercial Date) - # M Tu W Th F Sa Su - # 39: 1 2 3 4 5 6 7 - # 40: 1 2 3 4 5 6 7 - # 41: 1 2 3 4 5 6 7 - Date.send(@method, 1582, 39, 4).should == true - Date.send(@method, 1582, 39, 5).should == true - Date.send(@method, 1582, 41, 4).should == true - Date.send(@method, 1582, 41, 5).should == true - Date.send(@method, 1582, 41, 4, Date::ENGLAND).should == true - Date.send(@method, 1752, 37, 4, Date::ENGLAND).should == true - end - - it "returns false it is not a valid commercial date" do - Date.send(@method, 1999, 53, 1).should == false - end - - it "handles negative week and day numbers" do - # October 1582 (the Gregorian calendar, Commercial Date) - # M Tu W Th F Sa Su - # -12: -7 -6 -5 -4 -3 -2 -1 - # -11: -7 -6 -5 -4 -3 -2 -1 - # -10: -7 -6 -5 -4 -3 -2 -1 - Date.send(@method, 1582, -12, -4).should == true - Date.send(@method, 1582, -12, -3).should == true - Date.send(@method, 2007, -44, -2).should == true - Date.send(@method, 2008, -44, -2).should == true - Date.send(@method, 1999, -53, -1).should == false - end - -end diff --git a/library/date/shared/valid_jd.rb b/library/date/shared/valid_jd.rb deleted file mode 100644 index 0c01710208..0000000000 --- a/library/date/shared/valid_jd.rb +++ /dev/null @@ -1,20 +0,0 @@ -describe :date_valid_jd?, shared: true do - it "returns true if passed a number value" do - Date.send(@method, -100).should == true - Date.send(@method, 100.0).should == true - Date.send(@method, 2**100).should == true - Date.send(@method, Rational(1,2)).should == true - end - - it "returns false if passed nil" do - Date.send(@method, nil).should == false - end - - it "returns false if passed symbol" do - Date.send(@method, :number).should == false - end - - it "returns false if passed false" do - Date.send(@method, false).should == false - end -end diff --git a/library/date/shared/valid_ordinal.rb b/library/date/shared/valid_ordinal.rb deleted file mode 100644 index 1ed961be23..0000000000 --- a/library/date/shared/valid_ordinal.rb +++ /dev/null @@ -1,26 +0,0 @@ -describe :date_valid_ordinal?, shared: true do - it "determines if the date is a valid ordinal date" do - # October 1582 (the Gregorian calendar, Ordinal Date) - # S M Tu W Th F S - # 274 275 276 277 278 279 - # 280 281 282 283 284 285 286 - # 287 288 289 290 291 292 293 - # 294 - Date.send(@method, 1582, 277).should == true - Date.send(@method, 1582, 278).should == true - Date.send(@method, 1582, 287).should == true - Date.send(@method, 1582, 288).should == true - end - - it "handles negative day numbers" do - # October 1582 (the Gregorian calendar, Ordinal Date) - # S M Tu W Th F S - # -82 -81 -80 -79 -78 -77 - # -76 -75 -74 -73 -72 -71 -70 - # -69 -68 -67 -66 -65 -64 -63 - # -62 - Date.send(@method, 1582, -79).should == true - Date.send(@method, 1582, -78).should == true - Date.send(@method, 2007, -100).should == true - end -end diff --git a/library/date/succ_spec.rb b/library/date/succ_spec.rb index c4a902aa63..0b14d3bb73 100644 --- a/library/date/succ_spec.rb +++ b/library/date/succ_spec.rb @@ -2,5 +2,7 @@ require 'date' describe "Date#succ" do - it "needs to be reviewed for spec completeness" + it "is an alias of Date#next" do + Date.instance_method(:succ).should == Date.instance_method(:next) + end end diff --git a/library/date/valid_civil_spec.rb b/library/date/valid_civil_spec.rb index 00f2c57205..8cffc80310 100644 --- a/library/date/valid_civil_spec.rb +++ b/library/date/valid_civil_spec.rb @@ -1,9 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/valid_civil' require 'date' -describe "Date#valid_civil?" do - - it_behaves_like :date_valid_civil?, :valid_civil? - +describe "Date.valid_civil?" do + it "is an alias of Date.valid_date?" do + Date.method(:valid_civil?).should == Date.method(:valid_date?) + end end diff --git a/library/date/valid_commercial_spec.rb b/library/date/valid_commercial_spec.rb index 7e96782b6b..21a91ad867 100644 --- a/library/date/valid_commercial_spec.rb +++ b/library/date/valid_commercial_spec.rb @@ -1,8 +1,35 @@ require_relative '../../spec_helper' -require_relative 'shared/valid_commercial' require 'date' -describe "Date#valid_commercial?" do +describe "Date.valid_commercial?" do + it "returns true if it is a valid commercial date" do + # October 1582 (the Gregorian calendar, Commercial Date) + # M Tu W Th F Sa Su + # 39: 1 2 3 4 5 6 7 + # 40: 1 2 3 4 5 6 7 + # 41: 1 2 3 4 5 6 7 + Date.valid_commercial?(1582, 39, 4).should == true + Date.valid_commercial?(1582, 39, 5).should == true + Date.valid_commercial?(1582, 41, 4).should == true + Date.valid_commercial?(1582, 41, 5).should == true + Date.valid_commercial?(1582, 41, 4, Date::ENGLAND).should == true + Date.valid_commercial?(1752, 37, 4, Date::ENGLAND).should == true + end - it_behaves_like :date_valid_commercial?, :valid_commercial? + it "returns false it is not a valid commercial date" do + Date.valid_commercial?(1999, 53, 1).should == false + end + + it "handles negative week and day numbers" do + # October 1582 (the Gregorian calendar, Commercial Date) + # M Tu W Th F Sa Su + # -12: -7 -6 -5 -4 -3 -2 -1 + # -11: -7 -6 -5 -4 -3 -2 -1 + # -10: -7 -6 -5 -4 -3 -2 -1 + Date.valid_commercial?(1582, -12, -4).should == true + Date.valid_commercial?(1582, -12, -3).should == true + Date.valid_commercial?(2007, -44, -2).should == true + Date.valid_commercial?(2008, -44, -2).should == true + Date.valid_commercial?(1999, -53, -1).should == false + end end diff --git a/library/date/valid_date_spec.rb b/library/date/valid_date_spec.rb index f12a71d966..f0d5ec7b4d 100644 --- a/library/date/valid_date_spec.rb +++ b/library/date/valid_date_spec.rb @@ -1,7 +1,36 @@ require_relative '../../spec_helper' -require_relative 'shared/valid_civil' require 'date' -describe "Date#valid_date?" do - it_behaves_like :date_valid_civil?, :valid_date? +describe "Date.valid_date?" do + # reference: + # October 1582 (the Gregorian calendar, Civil Date) + # S M Tu W Th F S + # 1 2 3 4 15 16 + # 17 18 19 20 21 22 23 + # 24 25 26 27 28 29 30 + # 31 + it "returns true if it is a valid civil date" do + Date.valid_date?(1582, 10, 15).should == true + Date.valid_date?(1582, 10, 14, Date::ENGLAND).should == true + end + + it "returns false if it is not a valid civil date" do + Date.valid_date?(1582, 10, 14).should == false + end + + it "handles negative months and days" do + # October 1582 (the Gregorian calendar, Civil Date) + # S M Tu W Th F S + # -21 -20 -19 -18 -17 -16 + # -15 -14 -13 -12 -11 -10 -9 + # -8 -7 -6 -5 -4 -3 -2 + # -1 + Date.valid_date?(1582, -3, -22).should == false + Date.valid_date?(1582, -3, -21).should == true + Date.valid_date?(1582, -3, -18).should == true + Date.valid_date?(1582, -3, -17).should == true + + Date.valid_date?(2007, -11, -10).should == true + Date.valid_date?(2008, -11, -10).should == true + end end diff --git a/library/date/valid_jd_spec.rb b/library/date/valid_jd_spec.rb index aecaaabcf4..46f22de497 100644 --- a/library/date/valid_jd_spec.rb +++ b/library/date/valid_jd_spec.rb @@ -1,9 +1,23 @@ require_relative '../../spec_helper' -require_relative 'shared/valid_jd' require 'date' describe "Date.valid_jd?" do + it "returns true if passed a number value" do + Date.valid_jd?(-100).should == true + Date.valid_jd?(100.0).should == true + Date.valid_jd?(2**100).should == true + Date.valid_jd?(Rational(1,2)).should == true + end - it_behaves_like :date_valid_jd?, :valid_jd? + it "returns false if passed nil" do + Date.valid_jd?(nil).should == false + end + it "returns false if passed symbol" do + Date.valid_jd?(:number).should == false + end + + it "returns false if passed false" do + Date.valid_jd?(false).should == false + end end diff --git a/library/date/valid_ordinal_spec.rb b/library/date/valid_ordinal_spec.rb index 58d548c704..bb5c259606 100644 --- a/library/date/valid_ordinal_spec.rb +++ b/library/date/valid_ordinal_spec.rb @@ -1,9 +1,29 @@ require_relative '../../spec_helper' -require_relative 'shared/valid_ordinal' require 'date' describe "Date.valid_ordinal?" do + it "determines if the date is a valid ordinal date" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # 274 275 276 277 278 279 + # 280 281 282 283 284 285 286 + # 287 288 289 290 291 292 293 + # 294 + Date.valid_ordinal?(1582, 277).should == true + Date.valid_ordinal?(1582, 278).should == true + Date.valid_ordinal?(1582, 287).should == true + Date.valid_ordinal?(1582, 288).should == true + end - it_behaves_like :date_valid_ordinal?, :valid_ordinal? - + it "handles negative day numbers" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # -82 -81 -80 -79 -78 -77 + # -76 -75 -74 -73 -72 -71 -70 + # -69 -68 -67 -66 -65 -64 -63 + # -62 + Date.valid_ordinal?(1582, -79).should == true + Date.valid_ordinal?(1582, -78).should == true + Date.valid_ordinal?(2007, -100).should == true + end end From 0f8e805d42b3364ad8a027d75ea46162c85b1d9a Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 20:57:34 +0200 Subject: [PATCH 03/18] Move DateTime to rely less on shared examples --- library/datetime/min_spec.rb | 8 +++-- library/datetime/minute_spec.rb | 41 +++++++++++++++++++-- library/datetime/sec_spec.rb | 8 +++-- library/datetime/second_fraction_spec.rb | 4 ++- library/datetime/second_spec.rb | 44 +++++++++++++++++++++-- library/datetime/shared/min.rb | 40 --------------------- library/datetime/shared/sec.rb | 45 ------------------------ library/datetime/xmlschema_spec.rb | 4 ++- 8 files changed, 96 insertions(+), 98 deletions(-) delete mode 100644 library/datetime/shared/min.rb delete mode 100644 library/datetime/shared/sec.rb diff --git a/library/datetime/min_spec.rb b/library/datetime/min_spec.rb index a1eaa214cb..ca995a7eed 100644 --- a/library/datetime/min_spec.rb +++ b/library/datetime/min_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/min' +require 'date' -describe "DateTime.min" do - it_behaves_like :datetime_min, :min +describe "DateTime#min" do + it "is an alias of DateTime#minute" do + DateTime.instance_method(:min).should == DateTime.instance_method(:minute) + end end diff --git a/library/datetime/minute_spec.rb b/library/datetime/minute_spec.rb index acdfeda345..670e9a24d7 100644 --- a/library/datetime/minute_spec.rb +++ b/library/datetime/minute_spec.rb @@ -1,6 +1,41 @@ require_relative '../../spec_helper' -require_relative 'shared/min' +require 'date' -describe "DateTime.minute" do - it_behaves_like :datetime_min, :minute +describe "DateTime#minute" do + it "returns 0 if no argument is passed" do + DateTime.new.minute.should == 0 + end + + it "returns the minute passed as argument" do + new_datetime(minute: 5).minute.should == 5 + end + + it "adds 60 to negative minutes" do + new_datetime(minute: -20).minute.should == 40 + end + + it "raises an error for Rational" do + -> { new_datetime minute: 5 + Rational(1,2) }.should.raise(ArgumentError) + end + + it "raises an error for Float" do + -> { new_datetime minute: 5.5 }.should.raise(ArgumentError) + end + + it "raises an error for Rational" do + -> { new_datetime(hour: 2 + Rational(1,2)) }.should.raise(ArgumentError) + end + + it "raises an error, when the minute is smaller than -60" do + -> { new_datetime(minute: -61) }.should.raise(ArgumentError) + end + + it "raises an error, when the minute is greater or equal than 60" do + -> { new_datetime(minute: 60) }.should.raise(ArgumentError) + end + + it "raises an error for minute fractions smaller than -60" do + -> { new_datetime(minute: -60 - Rational(1,2))}.should( + raise_error(ArgumentError)) + end end diff --git a/library/datetime/sec_spec.rb b/library/datetime/sec_spec.rb index f681283c8e..f8a8b4646e 100644 --- a/library/datetime/sec_spec.rb +++ b/library/datetime/sec_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/sec' +require 'date' -describe "DateTime.sec" do - it_behaves_like :datetime_sec, :sec +describe "DateTime#sec" do + it "is an alias of DateTime#second" do + DateTime.instance_method(:sec).should == DateTime.instance_method(:second) + end end diff --git a/library/datetime/second_fraction_spec.rb b/library/datetime/second_fraction_spec.rb index d5393149ba..70f5abf560 100644 --- a/library/datetime/second_fraction_spec.rb +++ b/library/datetime/second_fraction_spec.rb @@ -2,5 +2,7 @@ require 'date' describe "DateTime#second_fraction" do - it "needs to be reviewed for spec completeness" + it "is an alias of DateTime#sec_fraction" do + DateTime.instance_method(:second_fraction).should == DateTime.instance_method(:sec_fraction) + end end diff --git a/library/datetime/second_spec.rb b/library/datetime/second_spec.rb index 545c3f9109..aa70f64487 100644 --- a/library/datetime/second_spec.rb +++ b/library/datetime/second_spec.rb @@ -1,6 +1,46 @@ require_relative '../../spec_helper' -require_relative 'shared/sec' +require 'date' describe "DateTime#second" do - it_behaves_like :datetime_sec, :second + it "returns 0 seconds if passed no arguments" do + d = DateTime.new + d.second.should == 0 + end + + it "returns the seconds passed in the arguments" do + new_datetime(second: 5).second.should == 5 + end + + it "adds 60 to negative values" do + new_datetime(second: -20).second.should == 40 + end + + it "returns the absolute value of a Rational" do + new_datetime(second: 5 + Rational(1,2)).second.should == 5 + end + + it "returns the absolute value of a float" do + new_datetime(second: 5.5).second.should == 5 + end + + it "raises an error when minute is given as a rational" do + -> { new_datetime(minute: 5 + Rational(1,2)) }.should.raise(ArgumentError) + end + + it "raises an error, when the second is smaller than -60" do + -> { new_datetime(second: -61) }.should.raise(ArgumentError) + end + + it "raises an error, when the second is greater or equal than 60" do + -> { new_datetime(second: 60) }.should.raise(ArgumentError) + end + + it "raises an error for second fractions smaller than -60" do + -> { new_datetime(second: -60 - Rational(1,2))}.should( + raise_error(ArgumentError)) + end + + it "takes a second fraction near 60" do + new_datetime(second: 59 + Rational(1,2)).second.should == 59 + end end diff --git a/library/datetime/shared/min.rb b/library/datetime/shared/min.rb deleted file mode 100644 index 04e5f3457a..0000000000 --- a/library/datetime/shared/min.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'date' - -describe :datetime_min, shared: true do - it "returns 0 if no argument is passed" do - DateTime.new.send(@method).should == 0 - end - - it "returns the minute passed as argument" do - new_datetime(minute: 5).send(@method).should == 5 - end - - it "adds 60 to negative minutes" do - new_datetime(minute: -20).send(@method).should == 40 - end - - it "raises an error for Rational" do - -> { new_datetime minute: 5 + Rational(1,2) }.should.raise(ArgumentError) - end - - it "raises an error for Float" do - -> { new_datetime minute: 5.5 }.should.raise(ArgumentError) - end - - it "raises an error for Rational" do - -> { new_datetime(hour: 2 + Rational(1,2)) }.should.raise(ArgumentError) - end - - it "raises an error, when the minute is smaller than -60" do - -> { new_datetime(minute: -61) }.should.raise(ArgumentError) - end - - it "raises an error, when the minute is greater or equal than 60" do - -> { new_datetime(minute: 60) }.should.raise(ArgumentError) - end - - it "raises an error for minute fractions smaller than -60" do - -> { new_datetime(minute: -60 - Rational(1,2))}.should( - raise_error(ArgumentError)) - end -end diff --git a/library/datetime/shared/sec.rb b/library/datetime/shared/sec.rb deleted file mode 100644 index 5af5db4fb2..0000000000 --- a/library/datetime/shared/sec.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'date' - -describe :datetime_sec, shared: true do - it "returns 0 seconds if passed no arguments" do - d = DateTime.new - d.send(@method).should == 0 - end - - it "returns the seconds passed in the arguments" do - new_datetime(second: 5).send(@method).should == 5 - end - - it "adds 60 to negative values" do - new_datetime(second: -20).send(@method).should == 40 - end - - it "returns the absolute value of a Rational" do - new_datetime(second: 5 + Rational(1,2)).send(@method).should == 5 - end - - it "returns the absolute value of a float" do - new_datetime(second: 5.5).send(@method).should == 5 - end - - it "raises an error when minute is given as a rational" do - -> { new_datetime(minute: 5 + Rational(1,2)) }.should.raise(ArgumentError) - end - - it "raises an error, when the second is smaller than -60" do - -> { new_datetime(second: -61) }.should.raise(ArgumentError) - end - - it "raises an error, when the second is greater or equal than 60" do - -> { new_datetime(second: 60) }.should.raise(ArgumentError) - end - - it "raises an error for second fractions smaller than -60" do - -> { new_datetime(second: -60 - Rational(1,2))}.should( - raise_error(ArgumentError)) - end - - it "takes a second fraction near 60" do - new_datetime(second: 59 + Rational(1,2)).send(@method).should == 59 - end -end diff --git a/library/datetime/xmlschema_spec.rb b/library/datetime/xmlschema_spec.rb index 42832631ed..fd0026cac2 100644 --- a/library/datetime/xmlschema_spec.rb +++ b/library/datetime/xmlschema_spec.rb @@ -6,5 +6,7 @@ end describe "DateTime#xmlschema" do - it "needs to be reviewed for spec completeness" + it "is an alias of DateTime#iso8601" do + DateTime.instance_method(:xmlschema).should == DateTime.instance_method(:iso8601) + end end From 48c27ea411d117f59cc2f5a8d136741e847aa293 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:10:23 +0200 Subject: [PATCH 04/18] Move Digest to rely less on shared examples --- library/digest/instance/append_spec.rb | 8 ++++++-- library/digest/instance/shared/update.rb | 8 -------- library/digest/instance/update_spec.rb | 5 +++-- library/digest/md5/append_spec.rb | 7 +++++-- library/digest/md5/length_spec.rb | 8 ++++++-- library/digest/md5/shared/length.rb | 8 -------- library/digest/md5/shared/update.rb | 7 ------- library/digest/md5/size_spec.rb | 5 +++-- library/digest/md5/update_spec.rb | 7 ++++--- library/digest/sha256/append_spec.rb | 7 +++++-- library/digest/sha256/length_spec.rb | 8 ++++++-- library/digest/sha256/shared/length.rb | 8 -------- library/digest/sha256/shared/update.rb | 7 ------- library/digest/sha256/size_spec.rb | 5 +++-- library/digest/sha256/update_spec.rb | 7 ++++--- library/digest/sha384/append_spec.rb | 7 +++++-- library/digest/sha384/length_spec.rb | 8 ++++++-- library/digest/sha384/shared/length.rb | 8 -------- library/digest/sha384/shared/update.rb | 7 ------- library/digest/sha384/size_spec.rb | 7 ++++--- library/digest/sha384/update_spec.rb | 7 ++++--- library/digest/sha512/append_spec.rb | 7 +++++-- library/digest/sha512/length_spec.rb | 8 ++++++-- library/digest/sha512/shared/length.rb | 8 -------- library/digest/sha512/shared/update.rb | 7 ------- library/digest/sha512/size_spec.rb | 7 ++++--- library/digest/sha512/update_spec.rb | 7 ++++--- 27 files changed, 83 insertions(+), 110 deletions(-) delete mode 100644 library/digest/instance/shared/update.rb delete mode 100644 library/digest/md5/shared/length.rb delete mode 100644 library/digest/md5/shared/update.rb delete mode 100644 library/digest/sha256/shared/length.rb delete mode 100644 library/digest/sha256/shared/update.rb delete mode 100644 library/digest/sha384/shared/length.rb delete mode 100644 library/digest/sha384/shared/update.rb delete mode 100644 library/digest/sha512/shared/length.rb delete mode 100644 library/digest/sha512/shared/update.rb diff --git a/library/digest/instance/append_spec.rb b/library/digest/instance/append_spec.rb index 2499579298..7f4ce3d121 100644 --- a/library/digest/instance/append_spec.rb +++ b/library/digest/instance/append_spec.rb @@ -1,7 +1,11 @@ require_relative '../../../spec_helper' require 'digest' -require_relative 'shared/update' describe "Digest::Instance#<<" do - it_behaves_like :digest_instance_update, :<< + it "raises a RuntimeError if called" do + c = Class.new do + include Digest::Instance + end + -> { c.new << "test" }.should.raise(RuntimeError) + end end diff --git a/library/digest/instance/shared/update.rb b/library/digest/instance/shared/update.rb deleted file mode 100644 index e064a90087..0000000000 --- a/library/digest/instance/shared/update.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :digest_instance_update, shared: true do - it "raises a RuntimeError if called" do - c = Class.new do - include Digest::Instance - end - -> { c.new.send(@method, "test") }.should.raise(RuntimeError) - end -end diff --git a/library/digest/instance/update_spec.rb b/library/digest/instance/update_spec.rb index 3bb4dd7f1b..d15b976213 100644 --- a/library/digest/instance/update_spec.rb +++ b/library/digest/instance/update_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' require 'digest' -require_relative 'shared/update' describe "Digest::Instance#update" do - it_behaves_like :digest_instance_update, :update + it "is an alias of Digest::Instance#<<" do + Digest::Instance.instance_method(:update).should == Digest::Instance.instance_method(:<<) + end end diff --git a/library/digest/md5/append_spec.rb b/library/digest/md5/append_spec.rb index 0abdc074a1..6f42e4f286 100644 --- a/library/digest/md5/append_spec.rb +++ b/library/digest/md5/append_spec.rb @@ -1,7 +1,10 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/update' describe "Digest::MD5#<<" do - it_behaves_like :md5_update, :<< + it "can update" do + cur_digest = Digest::MD5.new + cur_digest << MD5Constants::Contents + cur_digest.digest.should == MD5Constants::Digest + end end diff --git a/library/digest/md5/length_spec.rb b/library/digest/md5/length_spec.rb index b05b2a20fd..18bda51129 100644 --- a/library/digest/md5/length_spec.rb +++ b/library/digest/md5/length_spec.rb @@ -1,7 +1,11 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::MD5#length" do - it_behaves_like :md5_length, :length + it "returns the length of the digest" do + cur_digest = Digest::MD5.new + cur_digest.length.should == MD5Constants::BlankDigest.size + cur_digest << MD5Constants::Contents + cur_digest.length.should == MD5Constants::Digest.size + end end diff --git a/library/digest/md5/shared/length.rb b/library/digest/md5/shared/length.rb deleted file mode 100644 index c5b2b97b58..0000000000 --- a/library/digest/md5/shared/length.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :md5_length, shared: true do - it "returns the length of the digest" do - cur_digest = Digest::MD5.new - cur_digest.send(@method).should == MD5Constants::BlankDigest.size - cur_digest << MD5Constants::Contents - cur_digest.send(@method).should == MD5Constants::Digest.size - end -end diff --git a/library/digest/md5/shared/update.rb b/library/digest/md5/shared/update.rb deleted file mode 100644 index be8622aed5..0000000000 --- a/library/digest/md5/shared/update.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe :md5_update, shared: true do - it "can update" do - cur_digest = Digest::MD5.new - cur_digest.send @method, MD5Constants::Contents - cur_digest.digest.should == MD5Constants::Digest - end -end diff --git a/library/digest/md5/size_spec.rb b/library/digest/md5/size_spec.rb index 22e3272d36..54709234de 100644 --- a/library/digest/md5/size_spec.rb +++ b/library/digest/md5/size_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::MD5#size" do - it_behaves_like :md5_length, :size + it "is an alias of Digest::MD5#length" do + Digest::MD5.instance_method(:size).should == Digest::MD5.instance_method(:length) + end end diff --git a/library/digest/md5/update_spec.rb b/library/digest/md5/update_spec.rb index 4773db308c..830ccfead6 100644 --- a/library/digest/md5/update_spec.rb +++ b/library/digest/md5/update_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/update' +require 'digest' describe "Digest::MD5#update" do - it_behaves_like :md5_update, :update + it "is an alias of Digest::MD5#<<" do + Digest::MD5.instance_method(:update).should == Digest::MD5.instance_method(:<<) + end end diff --git a/library/digest/sha256/append_spec.rb b/library/digest/sha256/append_spec.rb index ab594c105f..f18b06c2a1 100644 --- a/library/digest/sha256/append_spec.rb +++ b/library/digest/sha256/append_spec.rb @@ -1,7 +1,10 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/update' describe "Digest::SHA256#<<" do - it_behaves_like :sha256_update, :<< + it "can update" do + cur_digest = Digest::SHA256.new + cur_digest << SHA256Constants::Contents + cur_digest.digest.should == SHA256Constants::Digest + end end diff --git a/library/digest/sha256/length_spec.rb b/library/digest/sha256/length_spec.rb index 181ac564ad..fc3db6548e 100644 --- a/library/digest/sha256/length_spec.rb +++ b/library/digest/sha256/length_spec.rb @@ -1,7 +1,11 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::SHA256#length" do - it_behaves_like :sha256_length, :length + it "returns the length of the digest" do + cur_digest = Digest::SHA256.new + cur_digest.length.should == SHA256Constants::BlankDigest.size + cur_digest << SHA256Constants::Contents + cur_digest.length.should == SHA256Constants::Digest.size + end end diff --git a/library/digest/sha256/shared/length.rb b/library/digest/sha256/shared/length.rb deleted file mode 100644 index 996673a5bd..0000000000 --- a/library/digest/sha256/shared/length.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :sha256_length, shared: true do - it "returns the length of the digest" do - cur_digest = Digest::SHA256.new - cur_digest.send(@method).should == SHA256Constants::BlankDigest.size - cur_digest << SHA256Constants::Contents - cur_digest.send(@method).should == SHA256Constants::Digest.size - end -end diff --git a/library/digest/sha256/shared/update.rb b/library/digest/sha256/shared/update.rb deleted file mode 100644 index 0edc07935b..0000000000 --- a/library/digest/sha256/shared/update.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe :sha256_update, shared: true do - it "can update" do - cur_digest = Digest::SHA256.new - cur_digest.send @method, SHA256Constants::Contents - cur_digest.digest.should == SHA256Constants::Digest - end -end diff --git a/library/digest/sha256/size_spec.rb b/library/digest/sha256/size_spec.rb index 1028263342..6102e1c8aa 100644 --- a/library/digest/sha256/size_spec.rb +++ b/library/digest/sha256/size_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::SHA256#size" do - it_behaves_like :sha256_length, :size + it "is an alias of Digest::SHA256#length" do + Digest::SHA256.instance_method(:size).should == Digest::SHA256.instance_method(:length) + end end diff --git a/library/digest/sha256/update_spec.rb b/library/digest/sha256/update_spec.rb index 92316eb752..d6724936f1 100644 --- a/library/digest/sha256/update_spec.rb +++ b/library/digest/sha256/update_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/update' +require 'digest' describe "Digest::SHA256#update" do - it_behaves_like :sha256_update, :update + it "is an alias of Digest::SHA256#<<" do + Digest::SHA256.instance_method(:update).should == Digest::SHA256.instance_method(:<<) + end end diff --git a/library/digest/sha384/append_spec.rb b/library/digest/sha384/append_spec.rb index 94c036cc3f..b9a862f1c2 100644 --- a/library/digest/sha384/append_spec.rb +++ b/library/digest/sha384/append_spec.rb @@ -1,7 +1,10 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/update' describe "Digest::SHA384#<<" do - it_behaves_like :sha384_update, :<< + it "can update" do + cur_digest = Digest::SHA384.new + cur_digest << SHA384Constants::Contents + cur_digest.digest.should == SHA384Constants::Digest + end end diff --git a/library/digest/sha384/length_spec.rb b/library/digest/sha384/length_spec.rb index 33fed492ef..e5cd6131fd 100644 --- a/library/digest/sha384/length_spec.rb +++ b/library/digest/sha384/length_spec.rb @@ -1,7 +1,11 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::SHA384#length" do - it_behaves_like :sha384_length, :length + it "returns the length of the digest" do + cur_digest = Digest::SHA384.new + cur_digest.length.should == SHA384Constants::BlankDigest.size + cur_digest << SHA384Constants::Contents + cur_digest.length.should == SHA384Constants::Digest.size + end end diff --git a/library/digest/sha384/shared/length.rb b/library/digest/sha384/shared/length.rb deleted file mode 100644 index 0c88288bcf..0000000000 --- a/library/digest/sha384/shared/length.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :sha384_length, shared: true do - it "returns the length of the digest" do - cur_digest = Digest::SHA384.new - cur_digest.send(@method).should == SHA384Constants::BlankDigest.size - cur_digest << SHA384Constants::Contents - cur_digest.send(@method).should == SHA384Constants::Digest.size - end -end diff --git a/library/digest/sha384/shared/update.rb b/library/digest/sha384/shared/update.rb deleted file mode 100644 index 1c6e31cf6a..0000000000 --- a/library/digest/sha384/shared/update.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe :sha384_update, shared: true do - it "can update" do - cur_digest = Digest::SHA384.new - cur_digest.send @method, SHA384Constants::Contents - cur_digest.digest.should == SHA384Constants::Digest - end -end diff --git a/library/digest/sha384/size_spec.rb b/library/digest/sha384/size_spec.rb index 4c3b14f7a0..40c291c623 100644 --- a/library/digest/sha384/size_spec.rb +++ b/library/digest/sha384/size_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/length' +require 'digest' describe "Digest::SHA384#size" do - it_behaves_like :sha384_length, :size + it "is an alias of Digest::SHA384#length" do + Digest::SHA384.instance_method(:size).should == Digest::SHA384.instance_method(:length) + end end diff --git a/library/digest/sha384/update_spec.rb b/library/digest/sha384/update_spec.rb index a1d0dd6068..561dcad3ec 100644 --- a/library/digest/sha384/update_spec.rb +++ b/library/digest/sha384/update_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/update' +require 'digest' describe "Digest::SHA384#update" do - it_behaves_like :sha384_update, :update + it "is an alias of Digest::SHA384#<<" do + Digest::SHA384.instance_method(:update).should == Digest::SHA384.instance_method(:<<) + end end diff --git a/library/digest/sha512/append_spec.rb b/library/digest/sha512/append_spec.rb index 9106e9685d..f297005403 100644 --- a/library/digest/sha512/append_spec.rb +++ b/library/digest/sha512/append_spec.rb @@ -1,7 +1,10 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/update' describe "Digest::SHA512#<<" do - it_behaves_like :sha512_update, :<< + it "can update" do + cur_digest = Digest::SHA512.new + cur_digest << SHA512Constants::Contents + cur_digest.digest.should == SHA512Constants::Digest + end end diff --git a/library/digest/sha512/length_spec.rb b/library/digest/sha512/length_spec.rb index e9fde90577..8e909482c5 100644 --- a/library/digest/sha512/length_spec.rb +++ b/library/digest/sha512/length_spec.rb @@ -1,7 +1,11 @@ require_relative '../../../spec_helper' require_relative 'shared/constants' -require_relative 'shared/length' describe "Digest::SHA512#length" do - it_behaves_like :sha512_length, :length + it "returns the length of the digest" do + cur_digest = Digest::SHA512.new + cur_digest.length.should == SHA512Constants::BlankDigest.size + cur_digest << SHA512Constants::Contents + cur_digest.length.should == SHA512Constants::Digest.size + end end diff --git a/library/digest/sha512/shared/length.rb b/library/digest/sha512/shared/length.rb deleted file mode 100644 index c0609d5386..0000000000 --- a/library/digest/sha512/shared/length.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :sha512_length, shared: true do - it "returns the length of the digest" do - cur_digest = Digest::SHA512.new - cur_digest.send(@method).should == SHA512Constants::BlankDigest.size - cur_digest << SHA512Constants::Contents - cur_digest.send(@method).should == SHA512Constants::Digest.size - end -end diff --git a/library/digest/sha512/shared/update.rb b/library/digest/sha512/shared/update.rb deleted file mode 100644 index ca74dbf4df..0000000000 --- a/library/digest/sha512/shared/update.rb +++ /dev/null @@ -1,7 +0,0 @@ -describe :sha512_update, shared: true do - it "can update" do - cur_digest = Digest::SHA512.new - cur_digest.send @method, SHA512Constants::Contents - cur_digest.digest.should == SHA512Constants::Digest - end -end diff --git a/library/digest/sha512/size_spec.rb b/library/digest/sha512/size_spec.rb index 6d0acdabdb..498d686802 100644 --- a/library/digest/sha512/size_spec.rb +++ b/library/digest/sha512/size_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/length' +require 'digest' describe "Digest::SHA512#size" do - it_behaves_like :sha512_length, :size + it "is an alias of Digest::SHA512#length" do + Digest::SHA512.instance_method(:size).should == Digest::SHA512.instance_method(:length) + end end diff --git a/library/digest/sha512/update_spec.rb b/library/digest/sha512/update_spec.rb index 682d3a19bb..33edf216ac 100644 --- a/library/digest/sha512/update_spec.rb +++ b/library/digest/sha512/update_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' -require_relative 'shared/constants' -require_relative 'shared/update' +require 'digest' describe "Digest::SHA512#update" do - it_behaves_like :sha512_update, :update + it "is an alias of Digest::SHA512#<<" do + Digest::SHA512.instance_method(:update).should == Digest::SHA512.instance_method(:<<) + end end From d7dbdb70d495e69a984bf04e5458a635c625f32d Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:14:22 +0200 Subject: [PATCH 05/18] Move GetoptLong to rely less on shared examples --- library/getoptlong/each_option_spec.rb | 5 ++- library/getoptlong/each_spec.rb | 18 +++++++- library/getoptlong/get_option_spec.rb | 5 ++- library/getoptlong/get_spec.rb | 62 +++++++++++++++++++++++++- library/getoptlong/shared/each.rb | 18 -------- library/getoptlong/shared/get.rb | 62 -------------------------- 6 files changed, 82 insertions(+), 88 deletions(-) delete mode 100644 library/getoptlong/shared/each.rb delete mode 100644 library/getoptlong/shared/get.rb diff --git a/library/getoptlong/each_option_spec.rb b/library/getoptlong/each_option_spec.rb index c6d82af86d..53987e4874 100644 --- a/library/getoptlong/each_option_spec.rb +++ b/library/getoptlong/each_option_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' require 'getoptlong' -require_relative 'shared/each' describe "GetoptLong#each_option" do - it_behaves_like :getoptlong_each, :each_option + it "is an alias of GetoptLong#each" do + GetoptLong.instance_method(:each_option).should == GetoptLong.instance_method(:each) + end end diff --git a/library/getoptlong/each_spec.rb b/library/getoptlong/each_spec.rb index d9022f02af..d342db47c7 100644 --- a/library/getoptlong/each_spec.rb +++ b/library/getoptlong/each_spec.rb @@ -1,7 +1,21 @@ require_relative '../../spec_helper' require 'getoptlong' -require_relative 'shared/each' describe "GetoptLong#each" do - it_behaves_like :getoptlong_each, :each + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + end + + it "passes each argument/value pair to the block" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + pairs = [] + @opts.each { |arg, val| pairs << [ arg, val ] } + pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ] + end + end end diff --git a/library/getoptlong/get_option_spec.rb b/library/getoptlong/get_option_spec.rb index 3cb2044379..1d80e3622e 100644 --- a/library/getoptlong/get_option_spec.rb +++ b/library/getoptlong/get_option_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' require 'getoptlong' -require_relative 'shared/get' describe "GetoptLong#get_option" do - it_behaves_like :getoptlong_get, :get_option + it "is an alias of GetoptLong#get" do + GetoptLong.instance_method(:get_option).should == GetoptLong.instance_method(:get) + end end diff --git a/library/getoptlong/get_spec.rb b/library/getoptlong/get_spec.rb index a8ec586fc9..bfc6697a5a 100644 --- a/library/getoptlong/get_spec.rb +++ b/library/getoptlong/get_spec.rb @@ -1,7 +1,65 @@ require_relative '../../spec_helper' require 'getoptlong' -require_relative 'shared/get' describe "GetoptLong#get" do - it_behaves_like :getoptlong_get, :get + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + @opts.quiet = true # silence using $deferr + end + + it "returns the next option name and its argument as an Array" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + @opts.get.should == [ "--size", "10k" ] + @opts.get.should == [ "--verbose", "" ] + @opts.get.should == [ "--query", ""] + @opts.get.should == nil + end + end + + it "shifts ARGV on each call" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + @opts.get + ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ] + + @opts.get + ARGV.should == [ "-q", "a.txt", "b.txt" ] + + @opts.get + ARGV.should == [ "a.txt", "b.txt" ] + + @opts.get + ARGV.should == [ "a.txt", "b.txt" ] + end + end + + it "terminates processing when encountering '--'" do + argv [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ] do + @opts.get + ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"] + + @opts.get + ARGV.should == ["-v", "-q", "a.txt", "b.txt"] + + @opts.get + ARGV.should == ["-v", "-q", "a.txt", "b.txt"] + end + end + + it "raises a if an argument was required, but none given" do + argv [ "--size" ] do + -> { @opts.get }.should.raise(GetoptLong::MissingArgument) + end + end + + # https://bugs.ruby-lang.org/issues/13858 + it "returns multiline argument" do + argv [ "--size=\n10k\n" ] do + @opts.get.should == [ "--size", "\n10k\n" ] + end + end end diff --git a/library/getoptlong/shared/each.rb b/library/getoptlong/shared/each.rb deleted file mode 100644 index b534e24c0f..0000000000 --- a/library/getoptlong/shared/each.rb +++ /dev/null @@ -1,18 +0,0 @@ -describe :getoptlong_each, shared: true do - before :each do - @opts = GetoptLong.new( - [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], - [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], - [ '--query', '-q', GetoptLong::NO_ARGUMENT ], - [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] - ) - end - - it "passes each argument/value pair to the block" do - argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do - pairs = [] - @opts.send(@method) { |arg, val| pairs << [ arg, val ] } - pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ] - end - end -end diff --git a/library/getoptlong/shared/get.rb b/library/getoptlong/shared/get.rb deleted file mode 100644 index 8d24c4c255..0000000000 --- a/library/getoptlong/shared/get.rb +++ /dev/null @@ -1,62 +0,0 @@ -describe :getoptlong_get, shared: true do - before :each do - @opts = GetoptLong.new( - [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], - [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], - [ '--query', '-q', GetoptLong::NO_ARGUMENT ], - [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] - ) - @opts.quiet = true # silence using $deferr - end - - it "returns the next option name and its argument as an Array" do - argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do - @opts.send(@method).should == [ "--size", "10k" ] - @opts.send(@method).should == [ "--verbose", "" ] - @opts.send(@method).should == [ "--query", ""] - @opts.send(@method).should == nil - end - end - - it "shifts ARGV on each call" do - argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do - @opts.send(@method) - ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ] - - @opts.send(@method) - ARGV.should == [ "-q", "a.txt", "b.txt" ] - - @opts.send(@method) - ARGV.should == [ "a.txt", "b.txt" ] - - @opts.send(@method) - ARGV.should == [ "a.txt", "b.txt" ] - end - end - - it "terminates processing when encountering '--'" do - argv [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ] do - @opts.send(@method) - ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"] - - @opts.send(@method) - ARGV.should == ["-v", "-q", "a.txt", "b.txt"] - - @opts.send(@method) - ARGV.should == ["-v", "-q", "a.txt", "b.txt"] - end - end - - it "raises a if an argument was required, but none given" do - argv [ "--size" ] do - -> { @opts.send(@method) }.should.raise(GetoptLong::MissingArgument) - end - end - - # https://bugs.ruby-lang.org/issues/13858 - it "returns multiline argument" do - argv [ "--size=\n10k\n" ] do - @opts.send(@method).should == [ "--size", "\n10k\n" ] - end - end -end From 3bf5a2f0d906a277935aa578eccc0c065592ad50 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:28:37 +0200 Subject: [PATCH 06/18] Move Matrix to rely less on shared examples --- library/matrix/I_spec.rb | 6 +++-- library/matrix/collect_spec.rb | 6 +++-- library/matrix/conj_spec.rb | 6 +++-- library/matrix/conjugate_spec.rb | 18 +++++++++++-- library/matrix/det_spec.rb | 5 ++-- library/matrix/determinant_spec.rb | 36 ++++++++++++++++++++++++-- library/matrix/identity_spec.rb | 18 +++++++++++-- library/matrix/imag_spec.rb | 6 +++-- library/matrix/imaginary_spec.rb | 19 ++++++++++++-- library/matrix/inv_spec.rb | 7 ++--- library/matrix/inverse_spec.rb | 36 ++++++++++++++++++++++++-- library/matrix/map_spec.rb | 24 ++++++++++++++++-- library/matrix/rect_spec.rb | 6 +++-- library/matrix/rectangular_spec.rb | 17 +++++++++++-- library/matrix/shared/collect.rb | 26 ------------------- library/matrix/shared/conjugate.rb | 20 --------------- library/matrix/shared/determinant.rb | 38 ---------------------------- library/matrix/shared/identity.rb | 19 -------------- library/matrix/shared/imaginary.rb | 20 --------------- library/matrix/shared/inverse.rb | 38 ---------------------------- library/matrix/shared/rectangular.rb | 18 ------------- library/matrix/shared/trace.rb | 12 --------- library/matrix/shared/transpose.rb | 19 -------------- library/matrix/t_spec.rb | 8 +++--- library/matrix/tr_spec.rb | 5 ++-- library/matrix/trace_spec.rb | 9 +++++-- library/matrix/transpose_spec.rb | 17 +++++++++++-- library/matrix/unit_spec.rb | 6 +++-- 28 files changed, 215 insertions(+), 250 deletions(-) delete mode 100644 library/matrix/shared/collect.rb delete mode 100644 library/matrix/shared/conjugate.rb delete mode 100644 library/matrix/shared/determinant.rb delete mode 100644 library/matrix/shared/identity.rb delete mode 100644 library/matrix/shared/imaginary.rb delete mode 100644 library/matrix/shared/inverse.rb delete mode 100644 library/matrix/shared/rectangular.rb delete mode 100644 library/matrix/shared/trace.rb delete mode 100644 library/matrix/shared/transpose.rb diff --git a/library/matrix/I_spec.rb b/library/matrix/I_spec.rb index 6eeffe8e98..ca5e79279a 100644 --- a/library/matrix/I_spec.rb +++ b/library/matrix/I_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/identity' +require 'matrix' describe "Matrix.I" do - it_behaves_like :matrix_identity, :I + it "is an alias of Matrix.identity" do + Matrix.method(:I).should == Matrix.method(:identity) + end end diff --git a/library/matrix/collect_spec.rb b/library/matrix/collect_spec.rb index bba640213b..664c3f3038 100644 --- a/library/matrix/collect_spec.rb +++ b/library/matrix/collect_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/collect' +require 'matrix' describe "Matrix#collect" do - it_behaves_like :collect, :collect + it "is an alias of Matrix#map" do + Matrix.instance_method(:collect).should == Matrix.instance_method(:map) + end end diff --git a/library/matrix/conj_spec.rb b/library/matrix/conj_spec.rb index ecee95c255..eff5986fc4 100644 --- a/library/matrix/conj_spec.rb +++ b/library/matrix/conj_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/conjugate' +require 'matrix' describe "Matrix#conj" do - it_behaves_like :matrix_conjugate, :conj + it "is an alias of Matrix#conjugate" do + Matrix.instance_method(:conj).should == Matrix.instance_method(:conjugate) + end end diff --git a/library/matrix/conjugate_spec.rb b/library/matrix/conjugate_spec.rb index 682bd41d94..46077d4fa9 100644 --- a/library/matrix/conjugate_spec.rb +++ b/library/matrix/conjugate_spec.rb @@ -1,6 +1,20 @@ require_relative '../../spec_helper' -require_relative 'shared/conjugate' +require_relative 'fixtures/classes' describe "Matrix#conjugate" do - it_behaves_like :matrix_conjugate, :conjugate + it "returns a matrix with all entries 'conjugated'" do + Matrix[ [1, 2], [3, 4] ].conjugate.should == Matrix[ [1, 2], [3, 4] ] + Matrix[ [1.9, Complex(1,1)], [3, 4] ].conjugate.should == Matrix[ [1.9, Complex(1,-1)], [3, 4] ] + end + + it "returns empty matrices on the same size if empty" do + Matrix.empty(0, 3).conjugate.should == Matrix.empty(0, 3) + Matrix.empty(3, 0).conjugate.should == Matrix.empty(3, 0) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.conjugate.should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/det_spec.rb b/library/matrix/det_spec.rb index aa7086cacf..fc4b1c252a 100644 --- a/library/matrix/det_spec.rb +++ b/library/matrix/det_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/determinant' require 'matrix' describe "Matrix#det" do - it_behaves_like :determinant, :det + it "is an alias of Matrix#determinant" do + Matrix.instance_method(:det).should == Matrix.instance_method(:determinant) + end end diff --git a/library/matrix/determinant_spec.rb b/library/matrix/determinant_spec.rb index 825c9907b1..603e13ba28 100644 --- a/library/matrix/determinant_spec.rb +++ b/library/matrix/determinant_spec.rb @@ -1,7 +1,39 @@ require_relative '../../spec_helper' -require_relative 'shared/determinant' require 'matrix' describe "Matrix#determinant" do - it_behaves_like :determinant, :determinant + it "returns the determinant of a square Matrix" do + m = Matrix[ [7,6], [3,9] ] + m.determinant.should == 45 + + m = Matrix[ [9, 8], [6,5] ] + m.determinant.should == -3 + + m = Matrix[ [9,8,3], [4,20,5], [1,1,1] ] + m.determinant.should == 95 + end + + it "returns the determinant of a single-element Matrix" do + m = Matrix[ [2] ] + m.determinant.should == 2 + end + + it "returns 1 for an empty Matrix" do + m = Matrix[ ] + m.determinant.should == 1 + end + + it "returns the determinant even for Matrices containing 0 as first entry" do + Matrix[[0,1],[1,0]].determinant.should == -1 + end + + it "raises an error for rectangular matrices" do + -> { + Matrix[[1], [2], [3]].determinant + }.should.raise(Matrix::ErrDimensionMismatch) + + -> { + Matrix.empty(3,0).determinant + }.should.raise(Matrix::ErrDimensionMismatch) + end end diff --git a/library/matrix/identity_spec.rb b/library/matrix/identity_spec.rb index 646462bc47..afefd27565 100644 --- a/library/matrix/identity_spec.rb +++ b/library/matrix/identity_spec.rb @@ -1,6 +1,20 @@ require_relative '../../spec_helper' -require_relative 'shared/identity' +require_relative 'fixtures/classes' +require 'matrix' describe "Matrix.identity" do - it_behaves_like :matrix_identity, :identity + it "returns a Matrix" do + Matrix.identity(2).should.is_a?(Matrix) + end + + it "returns a n x n identity matrix" do + Matrix.identity(3).should == Matrix.scalar(3, 1) + Matrix.identity(100).should == Matrix.scalar(100, 1) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.identity(2).should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/imag_spec.rb b/library/matrix/imag_spec.rb index 1c988753d8..9d6cc2e953 100644 --- a/library/matrix/imag_spec.rb +++ b/library/matrix/imag_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/imaginary' +require 'matrix' describe "Matrix#imag" do - it_behaves_like :matrix_imaginary, :imag + it "is an alias of Matrix#imaginary" do + Matrix.instance_method(:imag).should == Matrix.instance_method(:imaginary) + end end diff --git a/library/matrix/imaginary_spec.rb b/library/matrix/imaginary_spec.rb index ceae4bbe8d..bbd06677b7 100644 --- a/library/matrix/imaginary_spec.rb +++ b/library/matrix/imaginary_spec.rb @@ -1,6 +1,21 @@ require_relative '../../spec_helper' -require_relative 'shared/imaginary' +require_relative 'fixtures/classes' +require 'matrix' describe "Matrix#imaginary" do - it_behaves_like :matrix_imaginary, :imaginary + it "returns a matrix with the imaginary part of the elements of the receiver" do + Matrix[ [1, 2], [3, 4] ].imaginary.should == Matrix[ [0, 0], [0, 0] ] + Matrix[ [1.9, Complex(1,1)], [Complex(-2,0.42), 4] ].imaginary.should == Matrix[ [0, 1], [0.42, 0] ] + end + + it "returns empty matrices on the same size if empty" do + Matrix.empty(0, 3).imaginary.should == Matrix.empty(0, 3) + Matrix.empty(3, 0).imaginary.should == Matrix.empty(3, 0) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.imaginary.should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/inv_spec.rb b/library/matrix/inv_spec.rb index 82879a6d82..02684030d2 100644 --- a/library/matrix/inv_spec.rb +++ b/library/matrix/inv_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'spec_helper' -require_relative 'shared/inverse' +require 'matrix' describe "Matrix#inv" do - it_behaves_like :inverse, :inv + it "is an alias of Matrix#inverse" do + Matrix.instance_method(:inv).should == Matrix.instance_method(:inverse) + end end diff --git a/library/matrix/inverse_spec.rb b/library/matrix/inverse_spec.rb index fa3fa7de8a..38b01b28fb 100644 --- a/library/matrix/inverse_spec.rb +++ b/library/matrix/inverse_spec.rb @@ -1,7 +1,39 @@ require_relative '../../spec_helper' require_relative 'spec_helper' -require_relative 'shared/inverse' +require_relative 'fixtures/classes' +require 'matrix' describe "Matrix#inverse" do - it_behaves_like :inverse, :inverse + it "returns a Matrix" do + Matrix[ [1,2], [2,1] ].inverse.should.instance_of?(Matrix) + end + + it "returns the inverse of the Matrix" do + Matrix[ + [1, 3, 3], [1, 4, 3], [1, 3, 4] + ].inverse.should == + Matrix[ + [7, -3, -3], [-1, 1, 0], [-1, 0, 1] + ] + end + + it "returns the inverse of the Matrix (other case)" do + Matrix[ + [1, 2, 3], [0, 1, 4], [5, 6, 0] + ].inverse.should be_close_to_matrix([ + [-24, 18, 5], [20, -15, -4], [-5, 4, 1] + ]) + end + + it "raises a ErrDimensionMismatch if the Matrix is not square" do + ->{ + Matrix[ [1,2,3], [1,2,3] ].inverse + }.should.raise(Matrix::ErrDimensionMismatch) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.inverse.should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/map_spec.rb b/library/matrix/map_spec.rb index bc07c48cda..bae96db381 100644 --- a/library/matrix/map_spec.rb +++ b/library/matrix/map_spec.rb @@ -1,6 +1,26 @@ require_relative '../../spec_helper' -require_relative 'shared/collect' +require_relative 'fixtures/classes' describe "Matrix#map" do - it_behaves_like :collect, :map + before :all do + @m = Matrix[ [1, 2], [1, 2] ] + end + + it "returns an instance of Matrix" do + @m.map{|n| n * 2 }.should.is_a?(Matrix) + end + + it "returns a Matrix where each element is the result of the block" do + @m.map { |n| n * 2 }.should == Matrix[ [2, 4], [2, 4] ] + end + + it "returns an enumerator if no block is given" do + @m.map.should.instance_of?(Enumerator) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.map{1}.should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/rect_spec.rb b/library/matrix/rect_spec.rb index 83a0404e47..b0ca3f0421 100644 --- a/library/matrix/rect_spec.rb +++ b/library/matrix/rect_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/rectangular' +require 'matrix' describe "Matrix#rect" do - it_behaves_like :matrix_rectangular, :rect + it "is an alias of Matrix#rectangular" do + Matrix.instance_method(:rect).should == Matrix.instance_method(:rectangular) + end end diff --git a/library/matrix/rectangular_spec.rb b/library/matrix/rectangular_spec.rb index a235fac640..c0732f96bc 100644 --- a/library/matrix/rectangular_spec.rb +++ b/library/matrix/rectangular_spec.rb @@ -1,6 +1,19 @@ require_relative '../../spec_helper' -require_relative 'shared/rectangular' +require_relative 'fixtures/classes' +require 'matrix' describe "Matrix#rectangular" do - it_behaves_like :matrix_rectangular, :rectangular + it "returns [receiver.real, receiver.imag]" do + m = Matrix[ [1.2, Complex(1,2)], [Complex(-2,0.42), 4] ] + m.rectangular.should == [m.real, m.imag] + + m = Matrix.empty(3, 0) + m.rectangular.should == [m.real, m.imag] + end + + describe "for a subclass of Matrix" do + it "returns instances of that subclass" do + MatrixSub.ins.rectangular.each{|m| m.should.instance_of?(MatrixSub) } + end + end end diff --git a/library/matrix/shared/collect.rb b/library/matrix/shared/collect.rb deleted file mode 100644 index 3a4dbe3a36..0000000000 --- a/library/matrix/shared/collect.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :collect, shared: true do - before :all do - @m = Matrix[ [1, 2], [1, 2] ] - end - - it "returns an instance of Matrix" do - @m.send(@method){|n| n * 2 }.should.is_a?(Matrix) - end - - it "returns a Matrix where each element is the result of the block" do - @m.send(@method) { |n| n * 2 }.should == Matrix[ [2, 4], [2, 4] ] - end - - it "returns an enumerator if no block is given" do - @m.send(@method).should.instance_of?(Enumerator) - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.ins.send(@method){1}.should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/shared/conjugate.rb b/library/matrix/shared/conjugate.rb deleted file mode 100644 index ffdf5ebca1..0000000000 --- a/library/matrix/shared/conjugate.rb +++ /dev/null @@ -1,20 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :matrix_conjugate, shared: true do - it "returns a matrix with all entries 'conjugated'" do - Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [1, 2], [3, 4] ] - Matrix[ [1.9, Complex(1,1)], [3, 4] ].send(@method).should == Matrix[ [1.9, Complex(1,-1)], [3, 4] ] - end - - it "returns empty matrices on the same size if empty" do - Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3) - Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0) - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.ins.send(@method).should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/shared/determinant.rb b/library/matrix/shared/determinant.rb deleted file mode 100644 index b7c86393ba..0000000000 --- a/library/matrix/shared/determinant.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'matrix' - -describe :determinant, shared: true do - it "returns the determinant of a square Matrix" do - m = Matrix[ [7,6], [3,9] ] - m.send(@method).should == 45 - - m = Matrix[ [9, 8], [6,5] ] - m.send(@method).should == -3 - - m = Matrix[ [9,8,3], [4,20,5], [1,1,1] ] - m.send(@method).should == 95 - end - - it "returns the determinant of a single-element Matrix" do - m = Matrix[ [2] ] - m.send(@method).should == 2 - end - - it "returns 1 for an empty Matrix" do - m = Matrix[ ] - m.send(@method).should == 1 - end - - it "returns the determinant even for Matrices containing 0 as first entry" do - Matrix[[0,1],[1,0]].send(@method).should == -1 - end - - it "raises an error for rectangular matrices" do - -> { - Matrix[[1], [2], [3]].send(@method) - }.should.raise(Matrix::ErrDimensionMismatch) - - -> { - Matrix.empty(3,0).send(@method) - }.should.raise(Matrix::ErrDimensionMismatch) - end -end diff --git a/library/matrix/shared/identity.rb b/library/matrix/shared/identity.rb deleted file mode 100644 index df957b5a75..0000000000 --- a/library/matrix/shared/identity.rb +++ /dev/null @@ -1,19 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :matrix_identity, shared: true do - it "returns a Matrix" do - Matrix.send(@method, 2).should.is_a?(Matrix) - end - - it "returns a n x n identity matrix" do - Matrix.send(@method, 3).should == Matrix.scalar(3, 1) - Matrix.send(@method, 100).should == Matrix.scalar(100, 1) - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.send(@method, 2).should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/shared/imaginary.rb b/library/matrix/shared/imaginary.rb deleted file mode 100644 index 16615213a2..0000000000 --- a/library/matrix/shared/imaginary.rb +++ /dev/null @@ -1,20 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :matrix_imaginary, shared: true do - it "returns a matrix with the imaginary part of the elements of the receiver" do - Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [0, 0], [0, 0] ] - Matrix[ [1.9, Complex(1,1)], [Complex(-2,0.42), 4] ].send(@method).should == Matrix[ [0, 1], [0.42, 0] ] - end - - it "returns empty matrices on the same size if empty" do - Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3) - Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0) - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.ins.send(@method).should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/shared/inverse.rb b/library/matrix/shared/inverse.rb deleted file mode 100644 index ac463cf680..0000000000 --- a/library/matrix/shared/inverse.rb +++ /dev/null @@ -1,38 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :inverse, shared: true do - - it "returns a Matrix" do - Matrix[ [1,2], [2,1] ].send(@method).should.instance_of?(Matrix) - end - - it "returns the inverse of the Matrix" do - Matrix[ - [1, 3, 3], [1, 4, 3], [1, 3, 4] - ].send(@method).should == - Matrix[ - [7, -3, -3], [-1, 1, 0], [-1, 0, 1] - ] - end - - it "returns the inverse of the Matrix (other case)" do - Matrix[ - [1, 2, 3], [0, 1, 4], [5, 6, 0] - ].send(@method).should be_close_to_matrix([ - [-24, 18, 5], [20, -15, -4], [-5, 4, 1] - ]) - end - - it "raises a ErrDimensionMismatch if the Matrix is not square" do - ->{ - Matrix[ [1,2,3], [1,2,3] ].send(@method) - }.should.raise(Matrix::ErrDimensionMismatch) - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.ins.send(@method).should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/shared/rectangular.rb b/library/matrix/shared/rectangular.rb deleted file mode 100644 index 0229e614c6..0000000000 --- a/library/matrix/shared/rectangular.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :matrix_rectangular, shared: true do - it "returns [receiver.real, receiver.imag]" do - m = Matrix[ [1.2, Complex(1,2)], [Complex(-2,0.42), 4] ] - m.send(@method).should == [m.real, m.imag] - - m = Matrix.empty(3, 0) - m.send(@method).should == [m.real, m.imag] - end - - describe "for a subclass of Matrix" do - it "returns instances of that subclass" do - MatrixSub.ins.send(@method).each{|m| m.should.instance_of?(MatrixSub) } - end - end -end diff --git a/library/matrix/shared/trace.rb b/library/matrix/shared/trace.rb deleted file mode 100644 index c4a5491b22..0000000000 --- a/library/matrix/shared/trace.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'matrix' - -describe :trace, shared: true do - it "returns the sum of diagonal elements in a square Matrix" do - Matrix[[7,6], [3,9]].trace.should == 16 - end - - it "returns the sum of diagonal elements in a rectangular Matrix" do - ->{ Matrix[[1,2,3], [4,5,6]].trace}.should.raise(Matrix::ErrDimensionMismatch) - end - -end diff --git a/library/matrix/shared/transpose.rb b/library/matrix/shared/transpose.rb deleted file mode 100644 index a0b495359b..0000000000 --- a/library/matrix/shared/transpose.rb +++ /dev/null @@ -1,19 +0,0 @@ -require_relative '../fixtures/classes' -require 'matrix' - -describe :matrix_transpose, shared: true do - it "returns a transposed matrix" do - Matrix[[1, 2], [3, 4], [5, 6]].send(@method).should == Matrix[[1, 3, 5], [2, 4, 6]] - end - - it "can transpose empty matrices" do - m = Matrix[[], [], []] - m.send(@method).send(@method).should == m - end - - describe "for a subclass of Matrix" do - it "returns an instance of that subclass" do - MatrixSub.ins.send(@method).should.instance_of?(MatrixSub) - end - end -end diff --git a/library/matrix/t_spec.rb b/library/matrix/t_spec.rb index 6f1a5178e0..9411597e7c 100644 --- a/library/matrix/t_spec.rb +++ b/library/matrix/t_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/transpose' +require 'matrix' -describe "Matrix#transpose" do - it_behaves_like :matrix_transpose, :t +describe "Matrix#t" do + it "is an alias of Matrix#transpose" do + Matrix.instance_method(:t).should == Matrix.instance_method(:transpose) + end end diff --git a/library/matrix/tr_spec.rb b/library/matrix/tr_spec.rb index e17bd790d7..04d237d483 100644 --- a/library/matrix/tr_spec.rb +++ b/library/matrix/tr_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/trace' require 'matrix' describe "Matrix#tr" do - it_behaves_like :trace, :tr + it "is an alias of Matrix#trace" do + Matrix.instance_method(:tr).should == Matrix.instance_method(:trace) + end end diff --git a/library/matrix/trace_spec.rb b/library/matrix/trace_spec.rb index 290e7cb1f7..831278c838 100644 --- a/library/matrix/trace_spec.rb +++ b/library/matrix/trace_spec.rb @@ -1,7 +1,12 @@ require_relative '../../spec_helper' -require_relative 'shared/trace' require 'matrix' describe "Matrix#trace" do - it_behaves_like :trace, :trace + it "returns the sum of diagonal elements in a square Matrix" do + Matrix[[7,6], [3,9]].trace.should == 16 + end + + it "returns the sum of diagonal elements in a rectangular Matrix" do + ->{ Matrix[[1,2,3], [4,5,6]].trace}.should.raise(Matrix::ErrDimensionMismatch) + end end diff --git a/library/matrix/transpose_spec.rb b/library/matrix/transpose_spec.rb index 79600dd439..0b24ab32a7 100644 --- a/library/matrix/transpose_spec.rb +++ b/library/matrix/transpose_spec.rb @@ -1,6 +1,19 @@ require_relative '../../spec_helper' -require_relative 'shared/transpose' +require_relative 'fixtures/classes' describe "Matrix#transpose" do - it_behaves_like :matrix_transpose, :transpose + it "returns a transposed matrix" do + Matrix[[1, 2], [3, 4], [5, 6]].transpose.should == Matrix[[1, 3, 5], [2, 4, 6]] + end + + it "can transpose empty matrices" do + m = Matrix[[], [], []] + m.transpose.transpose.should == m + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.transpose.should.instance_of?(MatrixSub) + end + end end diff --git a/library/matrix/unit_spec.rb b/library/matrix/unit_spec.rb index 6a41d729c7..1121996122 100644 --- a/library/matrix/unit_spec.rb +++ b/library/matrix/unit_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/identity' +require 'matrix' describe "Matrix.unit" do - it_behaves_like :matrix_identity, :unit + it "is an alias of Matrix.identity" do + Matrix.method(:unit).should == Matrix.method(:identity) + end end From bfeea7a6ff244279139816bdcc90835b865586c3 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 14:21:27 +0200 Subject: [PATCH 07/18] Move Net::HTTP to rely less on shared examples --- library/net-http/http/active_spec.rb | 6 +- library/net-http/http/get2_spec.rb | 6 +- library/net-http/http/head2_spec.rb | 6 +- library/net-http/http/is_version_1_1_spec.rb | 5 +- library/net-http/http/is_version_1_2_spec.rb | 5 +- library/net-http/http/post2_spec.rb | 6 +- library/net-http/http/put2_spec.rb | 6 +- library/net-http/http/request_get_spec.rb | 41 ++++++++- library/net-http/http/request_head_spec.rb | 41 ++++++++- library/net-http/http/request_post_spec.rb | 41 ++++++++- library/net-http/http/request_put_spec.rb | 41 ++++++++- library/net-http/http/shared/request_get.rb | 41 --------- library/net-http/http/shared/request_head.rb | 41 --------- library/net-http/http/shared/request_post.rb | 41 --------- library/net-http/http/shared/request_put.rb | 41 --------- library/net-http/http/shared/started.rb | 26 ------ library/net-http/http/shared/version_1_1.rb | 6 -- library/net-http/http/shared/version_1_2.rb | 6 -- library/net-http/http/started_spec.rb | 26 +++++- library/net-http/http/version_1_1_spec.rb | 6 +- library/net-http/http/version_1_2_spec.rb | 6 +- .../httpheader/canonical_each_spec.rb | 7 +- .../net-http/httpheader/content_type_spec.rb | 6 +- .../httpheader/each_capitalized_spec.rb | 31 ++++++- .../net-http/httpheader/each_header_spec.rb | 31 ++++++- library/net-http/httpheader/each_key_spec.rb | 31 ++++++- library/net-http/httpheader/each_name_spec.rb | 6 +- library/net-http/httpheader/each_spec.rb | 7 +- library/net-http/httpheader/form_data_spec.rb | 6 +- library/net-http/httpheader/length_spec.rb | 7 +- library/net-http/httpheader/range_spec.rb | 6 +- .../httpheader/set_content_type_spec.rb | 18 +++- .../net-http/httpheader/set_form_data_spec.rb | 27 +++++- library/net-http/httpheader/set_range_spec.rb | 89 ++++++++++++++++++- .../httpheader/shared/each_capitalized.rb | 31 ------- .../net-http/httpheader/shared/each_header.rb | 31 ------- .../net-http/httpheader/shared/each_name.rb | 31 ------- .../httpheader/shared/set_content_type.rb | 18 ---- .../httpheader/shared/set_form_data.rb | 27 ------ .../net-http/httpheader/shared/set_range.rb | 89 ------------------- library/net-http/httpheader/shared/size.rb | 18 ---- library/net-http/httpheader/size_spec.rb | 18 +++- library/net-http/httpresponse/body_spec.rb | 19 +++- library/net-http/httpresponse/entity_spec.rb | 6 +- library/net-http/httpresponse/shared/body.rb | 20 ----- 45 files changed, 489 insertions(+), 535 deletions(-) delete mode 100644 library/net-http/http/shared/request_get.rb delete mode 100644 library/net-http/http/shared/request_head.rb delete mode 100644 library/net-http/http/shared/request_post.rb delete mode 100644 library/net-http/http/shared/request_put.rb delete mode 100644 library/net-http/http/shared/started.rb delete mode 100644 library/net-http/http/shared/version_1_1.rb delete mode 100644 library/net-http/http/shared/version_1_2.rb delete mode 100644 library/net-http/httpheader/shared/each_capitalized.rb delete mode 100644 library/net-http/httpheader/shared/each_header.rb delete mode 100644 library/net-http/httpheader/shared/each_name.rb delete mode 100644 library/net-http/httpheader/shared/set_content_type.rb delete mode 100644 library/net-http/httpheader/shared/set_form_data.rb delete mode 100644 library/net-http/httpheader/shared/set_range.rb delete mode 100644 library/net-http/httpheader/shared/size.rb delete mode 100644 library/net-http/httpresponse/shared/body.rb diff --git a/library/net-http/http/active_spec.rb b/library/net-http/http/active_spec.rb index c260274594..ba870b39d2 100644 --- a/library/net-http/http/active_spec.rb +++ b/library/net-http/http/active_spec.rb @@ -1,8 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/http_server' -require_relative 'shared/started' describe "Net::HTTP#active?" do - it_behaves_like :net_http_started_p, :active? + it "is an alias of Net::HTTP#started?" do + Net::HTTP.instance_method(:active?).should == Net::HTTP.instance_method(:started?) + end end diff --git a/library/net-http/http/get2_spec.rb b/library/net-http/http/get2_spec.rb index 57c05ec64b..046443d73e 100644 --- a/library/net-http/http/get2_spec.rb +++ b/library/net-http/http/get2_spec.rb @@ -1,8 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/http_server' -require_relative 'shared/request_get' describe "Net::HTTP#get2" do - it_behaves_like :net_http_request_get, :get2 + it "is an alias of Net::HTTP#request_get" do + Net::HTTP.instance_method(:get2).should == Net::HTTP.instance_method(:request_get) + end end diff --git a/library/net-http/http/head2_spec.rb b/library/net-http/http/head2_spec.rb index 84cfff33d7..19c0cede9f 100644 --- a/library/net-http/http/head2_spec.rb +++ b/library/net-http/http/head2_spec.rb @@ -1,8 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/http_server' -require_relative 'shared/request_head' describe "Net::HTTP#head2" do - it_behaves_like :net_http_request_head, :head2 + it "is an alias of Net::HTTP#request_head" do + Net::HTTP.instance_method(:head2).should == Net::HTTP.instance_method(:request_head) + end end diff --git a/library/net-http/http/is_version_1_1_spec.rb b/library/net-http/http/is_version_1_1_spec.rb index bdb343f9e0..f4910ef1e4 100644 --- a/library/net-http/http/is_version_1_1_spec.rb +++ b/library/net-http/http/is_version_1_1_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/version_1_1' describe "Net::HTTP.is_version_1_1?" do - it_behaves_like :net_http_version_1_1_p, :is_version_1_1? + it "is an alias of Net::HTTP.version_1_1?" do + Net::HTTP.method(:is_version_1_1?).should == Net::HTTP.method(:version_1_1?) + end end diff --git a/library/net-http/http/is_version_1_2_spec.rb b/library/net-http/http/is_version_1_2_spec.rb index 555bb205dd..555724babe 100644 --- a/library/net-http/http/is_version_1_2_spec.rb +++ b/library/net-http/http/is_version_1_2_spec.rb @@ -1,7 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/version_1_2' describe "Net::HTTP.is_version_1_2?" do - it_behaves_like :net_http_version_1_2_p, :is_version_1_2? + it "is an alias of Net::HTTP.version_1_2?" do + Net::HTTP.method(:is_version_1_2?).should == Net::HTTP.method(:version_1_2?) + end end diff --git a/library/net-http/http/post2_spec.rb b/library/net-http/http/post2_spec.rb index abc998709f..68c2a9ea06 100644 --- a/library/net-http/http/post2_spec.rb +++ b/library/net-http/http/post2_spec.rb @@ -1,8 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/http_server' -require_relative 'shared/request_post' describe "Net::HTTP#post2" do - it_behaves_like :net_http_request_post, :post2 + it "is an alias of Net::HTTP#request_post" do + Net::HTTP.instance_method(:post2).should == Net::HTTP.instance_method(:request_post) + end end diff --git a/library/net-http/http/put2_spec.rb b/library/net-http/http/put2_spec.rb index 7b03a39d0b..237df67e82 100644 --- a/library/net-http/http/put2_spec.rb +++ b/library/net-http/http/put2_spec.rb @@ -1,8 +1,8 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/http_server' -require_relative 'shared/request_put' describe "Net::HTTP#put2" do - it_behaves_like :net_http_request_put, :put2 + it "is an alias of Net::HTTP#request_put" do + Net::HTTP.instance_method(:put2).should == Net::HTTP.instance_method(:request_put) + end end diff --git a/library/net-http/http/request_get_spec.rb b/library/net-http/http/request_get_spec.rb index 98025a14a1..1737e62439 100644 --- a/library/net-http/http/request_get_spec.rb +++ b/library/net-http/http/request_get_spec.rb @@ -1,8 +1,45 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/http_server' -require_relative 'shared/request_get' describe "Net::HTTP#request_get" do - it_behaves_like :net_http_request_get, :get2 + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a GET request to the passed path and returns the response" do + response = @http.request_get("/request") + response.body.should == "Request type: GET" + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_get("/request") + response.should.is_a?(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a GET request to the passed path and returns the response" do + response = @http.request_get("/request") {} + response.body.should == "Request type: GET" + end + + it "yields the response to the passed block" do + @http.request_get("/request") do |response| + response.body.should == "Request type: GET" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_get("/request") {} + response.should.is_a?(Net::HTTPResponse) + end + end end diff --git a/library/net-http/http/request_head_spec.rb b/library/net-http/http/request_head_spec.rb index 8f514d4eee..7c46ebfc53 100644 --- a/library/net-http/http/request_head_spec.rb +++ b/library/net-http/http/request_head_spec.rb @@ -1,8 +1,45 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/http_server' -require_relative 'shared/request_head' describe "Net::HTTP#request_head" do - it_behaves_like :net_http_request_head, :request_head + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a head request to the passed path and returns the response" do + response = @http.request_head("/request") + response.body.should == nil + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_head("/request") + response.should.is_a?(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a head request to the passed path and returns the response" do + response = @http.request_head("/request") {} + response.body.should == nil + end + + it "yields the response to the passed block" do + @http.request_head("/request") do |response| + response.body.should == nil + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_head("/request") {} + response.should.is_a?(Net::HTTPResponse) + end + end end diff --git a/library/net-http/http/request_post_spec.rb b/library/net-http/http/request_post_spec.rb index 719bd5a7ee..8cfdd3469e 100644 --- a/library/net-http/http/request_post_spec.rb +++ b/library/net-http/http/request_post_spec.rb @@ -1,8 +1,45 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/http_server' -require_relative 'shared/request_post' describe "Net::HTTP#request_post" do - it_behaves_like :net_http_request_post, :request_post + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a post request to the passed path and returns the response" do + response = @http.request_post("/request", "test=test") + response.body.should == "Request type: POST" + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_post("/request", "test=test") + response.should.is_a?(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a post request to the passed path and returns the response" do + response = @http.request_post("/request", "test=test") {} + response.body.should == "Request type: POST" + end + + it "yields the response to the passed block" do + @http.request_post("/request", "test=test") do |response| + response.body.should == "Request type: POST" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_post("/request", "test=test") {} + response.should.is_a?(Net::HTTPResponse) + end + end end diff --git a/library/net-http/http/request_put_spec.rb b/library/net-http/http/request_put_spec.rb index 9fcf3a98d6..b7388a21c8 100644 --- a/library/net-http/http/request_put_spec.rb +++ b/library/net-http/http/request_put_spec.rb @@ -1,8 +1,45 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/http_server' -require_relative 'shared/request_put' describe "Net::HTTP#request_put" do - it_behaves_like :net_http_request_put, :request_put + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + describe "when passed no block" do + it "sends a put request to the passed path and returns the response" do + response = @http.request_put("/request", "test=test") + response.body.should == "Request type: PUT" + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_put("/request", "test=test") + response.should.is_a?(Net::HTTPResponse) + end + end + + describe "when passed a block" do + it "sends a put request to the passed path and returns the response" do + response = @http.request_put("/request", "test=test") {} + response.body.should == "Request type: PUT" + end + + it "yields the response to the passed block" do + @http.request_put("/request", "test=test") do |response| + response.body.should == "Request type: PUT" + end + end + + it "returns a Net::HTTPResponse object" do + response = @http.request_put("/request", "test=test") {} + response.should.is_a?(Net::HTTPResponse) + end + end end diff --git a/library/net-http/http/shared/request_get.rb b/library/net-http/http/shared/request_get.rb deleted file mode 100644 index 4f2f152ea4..0000000000 --- a/library/net-http/http/shared/request_get.rb +++ /dev/null @@ -1,41 +0,0 @@ -describe :net_http_request_get, shared: true do - before :each do - NetHTTPSpecs.start_server - @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) - end - - after :each do - @http.finish if @http.started? - NetHTTPSpecs.stop_server - end - - describe "when passed no block" do - it "sends a GET request to the passed path and returns the response" do - response = @http.send(@method, "/request") - response.body.should == "Request type: GET" - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request") - response.should.is_a?(Net::HTTPResponse) - end - end - - describe "when passed a block" do - it "sends a GET request to the passed path and returns the response" do - response = @http.send(@method, "/request") {} - response.body.should == "Request type: GET" - end - - it "yields the response to the passed block" do - @http.send(@method, "/request") do |response| - response.body.should == "Request type: GET" - end - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request") {} - response.should.is_a?(Net::HTTPResponse) - end - end -end diff --git a/library/net-http/http/shared/request_head.rb b/library/net-http/http/shared/request_head.rb deleted file mode 100644 index a5fe787d32..0000000000 --- a/library/net-http/http/shared/request_head.rb +++ /dev/null @@ -1,41 +0,0 @@ -describe :net_http_request_head, shared: true do - before :each do - NetHTTPSpecs.start_server - @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) - end - - after :each do - @http.finish if @http.started? - NetHTTPSpecs.stop_server - end - - describe "when passed no block" do - it "sends a head request to the passed path and returns the response" do - response = @http.send(@method, "/request") - response.body.should == nil - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request") - response.should.is_a?(Net::HTTPResponse) - end - end - - describe "when passed a block" do - it "sends a head request to the passed path and returns the response" do - response = @http.send(@method, "/request") {} - response.body.should == nil - end - - it "yields the response to the passed block" do - @http.send(@method, "/request") do |response| - response.body.should == nil - end - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request") {} - response.should.is_a?(Net::HTTPResponse) - end - end -end diff --git a/library/net-http/http/shared/request_post.rb b/library/net-http/http/shared/request_post.rb deleted file mode 100644 index 73cfd577cf..0000000000 --- a/library/net-http/http/shared/request_post.rb +++ /dev/null @@ -1,41 +0,0 @@ -describe :net_http_request_post, shared: true do - before :each do - NetHTTPSpecs.start_server - @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) - end - - after :each do - @http.finish if @http.started? - NetHTTPSpecs.stop_server - end - - describe "when passed no block" do - it "sends a post request to the passed path and returns the response" do - response = @http.send(@method, "/request", "test=test") - response.body.should == "Request type: POST" - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request", "test=test") - response.should.is_a?(Net::HTTPResponse) - end - end - - describe "when passed a block" do - it "sends a post request to the passed path and returns the response" do - response = @http.send(@method, "/request", "test=test") {} - response.body.should == "Request type: POST" - end - - it "yields the response to the passed block" do - @http.send(@method, "/request", "test=test") do |response| - response.body.should == "Request type: POST" - end - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request", "test=test") {} - response.should.is_a?(Net::HTTPResponse) - end - end -end diff --git a/library/net-http/http/shared/request_put.rb b/library/net-http/http/shared/request_put.rb deleted file mode 100644 index 3b64d7e055..0000000000 --- a/library/net-http/http/shared/request_put.rb +++ /dev/null @@ -1,41 +0,0 @@ -describe :net_http_request_put, shared: true do - before :each do - NetHTTPSpecs.start_server - @http = Net::HTTP.start("localhost", NetHTTPSpecs.port) - end - - after :each do - @http.finish if @http.started? - NetHTTPSpecs.stop_server - end - - describe "when passed no block" do - it "sends a put request to the passed path and returns the response" do - response = @http.send(@method, "/request", "test=test") - response.body.should == "Request type: PUT" - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request", "test=test") - response.should.is_a?(Net::HTTPResponse) - end - end - - describe "when passed a block" do - it "sends a put request to the passed path and returns the response" do - response = @http.send(@method, "/request", "test=test") {} - response.body.should == "Request type: PUT" - end - - it "yields the response to the passed block" do - @http.send(@method, "/request", "test=test") do |response| - response.body.should == "Request type: PUT" - end - end - - it "returns a Net::HTTPResponse object" do - response = @http.send(@method, "/request", "test=test") {} - response.should.is_a?(Net::HTTPResponse) - end - end -end diff --git a/library/net-http/http/shared/started.rb b/library/net-http/http/shared/started.rb deleted file mode 100644 index 0ab18a4e83..0000000000 --- a/library/net-http/http/shared/started.rb +++ /dev/null @@ -1,26 +0,0 @@ -describe :net_http_started_p, shared: true do - before :each do - NetHTTPSpecs.start_server - @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) - end - - after :each do - @http.finish if @http.started? - NetHTTPSpecs.stop_server - end - - it "returns true when self has been started" do - @http.start - @http.send(@method).should == true - end - - it "returns false when self has not been started yet" do - @http.send(@method).should == false - end - - it "returns false when self has been stopped again" do - @http.start - @http.finish - @http.send(@method).should == false - end -end diff --git a/library/net-http/http/shared/version_1_1.rb b/library/net-http/http/shared/version_1_1.rb deleted file mode 100644 index 84e7264eab..0000000000 --- a/library/net-http/http/shared/version_1_1.rb +++ /dev/null @@ -1,6 +0,0 @@ -describe :net_http_version_1_1_p, shared: true do - it "returns the state of net/http 1.1 features" do - Net::HTTP.version_1_2 - Net::HTTP.send(@method).should == false - end -end diff --git a/library/net-http/http/shared/version_1_2.rb b/library/net-http/http/shared/version_1_2.rb deleted file mode 100644 index dcf5419704..0000000000 --- a/library/net-http/http/shared/version_1_2.rb +++ /dev/null @@ -1,6 +0,0 @@ -describe :net_http_version_1_2_p, shared: true do - it "returns the state of net/http 1.2 features" do - Net::HTTP.version_1_2 - Net::HTTP.send(@method).should == true - end -end diff --git a/library/net-http/http/started_spec.rb b/library/net-http/http/started_spec.rb index cbb82ceefa..a0b46fcbd2 100644 --- a/library/net-http/http/started_spec.rb +++ b/library/net-http/http/started_spec.rb @@ -1,8 +1,30 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/http_server' -require_relative 'shared/started' describe "Net::HTTP#started?" do - it_behaves_like :net_http_started_p, :started? + before :each do + NetHTTPSpecs.start_server + @http = Net::HTTP.new("localhost", NetHTTPSpecs.port) + end + + after :each do + @http.finish if @http.started? + NetHTTPSpecs.stop_server + end + + it "returns true when self has been started" do + @http.start + @http.started?.should == true + end + + it "returns false when self has not been started yet" do + @http.started?.should == false + end + + it "returns false when self has been stopped again" do + @http.start + @http.finish + @http.started?.should == false + end end diff --git a/library/net-http/http/version_1_1_spec.rb b/library/net-http/http/version_1_1_spec.rb index 34a4ac8a6b..7f87bf30f9 100644 --- a/library/net-http/http/version_1_1_spec.rb +++ b/library/net-http/http/version_1_1_spec.rb @@ -1,7 +1,9 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/version_1_1' describe "Net::HTTP.version_1_1?" do - it_behaves_like :net_http_version_1_1_p, :version_1_1? + it "returns the state of net/http 1.1 features" do + Net::HTTP.version_1_2 + Net::HTTP.version_1_1?.should == false + end end diff --git a/library/net-http/http/version_1_2_spec.rb b/library/net-http/http/version_1_2_spec.rb index 4918597234..73ca70ac7b 100644 --- a/library/net-http/http/version_1_2_spec.rb +++ b/library/net-http/http/version_1_2_spec.rb @@ -1,6 +1,5 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/version_1_2' describe "Net::HTTP.version_1_2" do it "turns on net/http 1.2 features" do @@ -16,5 +15,8 @@ end describe "Net::HTTP.version_1_2?" do - it_behaves_like :net_http_version_1_2_p, :version_1_2? + it "returns the state of net/http 1.2 features" do + Net::HTTP.version_1_2 + Net::HTTP.version_1_2?.should == true + end end diff --git a/library/net-http/httpheader/canonical_each_spec.rb b/library/net-http/httpheader/canonical_each_spec.rb index 64a5cae89e..c009e9f7ea 100644 --- a/library/net-http/httpheader/canonical_each_spec.rb +++ b/library/net-http/httpheader/canonical_each_spec.rb @@ -1,8 +1,9 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/classes' -require_relative 'shared/each_capitalized' describe "Net::HTTPHeader#canonical_each" do - it_behaves_like :net_httpheader_each_capitalized, :canonical_each + it "is an alias of Net::HTTPHeader#each_capitalized" do + Net::HTTPHeader.instance_method(:canonical_each).should == + Net::HTTPHeader.instance_method(:each_capitalized) + end end diff --git a/library/net-http/httpheader/content_type_spec.rb b/library/net-http/httpheader/content_type_spec.rb index c9c936ba0f..0ee43a6942 100644 --- a/library/net-http/httpheader/content_type_spec.rb +++ b/library/net-http/httpheader/content_type_spec.rb @@ -1,7 +1,6 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_content_type' describe "Net::HTTPHeader#content_type" do before :each do @@ -22,5 +21,8 @@ end describe "Net::HTTPHeader#content_type=" do - it_behaves_like :net_httpheader_set_content_type, :content_type= + it "is an alias of Net::HTTPHeader#set_content_type" do + Net::HTTPHeader.instance_method(:content_type=).should == + Net::HTTPHeader.instance_method(:set_content_type) + end end diff --git a/library/net-http/httpheader/each_capitalized_spec.rb b/library/net-http/httpheader/each_capitalized_spec.rb index 1e853995ea..e24e778238 100644 --- a/library/net-http/httpheader/each_capitalized_spec.rb +++ b/library/net-http/httpheader/each_capitalized_spec.rb @@ -1,8 +1,35 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/each_capitalized' describe "Net::HTTPHeader#each_capitalized" do - it_behaves_like :net_httpheader_each_capitalized, :each_capitalized + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["my-header"] = "test" + @headers.add_field("my-Other-Header", "a") + @headers.add_field("My-Other-header", "b") + end + + describe "when passed a block" do + it "yields each header entry to the passed block (capitalized keys, values joined)" do + res = [] + @headers.each_capitalized do |key, value| + res << [key, value] + end + res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.each_capitalized + enumerator.should.instance_of?(Enumerator) + + res = [] + enumerator.each do |*key| + res << key + end + res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] + end + end end diff --git a/library/net-http/httpheader/each_header_spec.rb b/library/net-http/httpheader/each_header_spec.rb index 869feebacf..63c1106d18 100644 --- a/library/net-http/httpheader/each_header_spec.rb +++ b/library/net-http/httpheader/each_header_spec.rb @@ -1,8 +1,35 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/each_header' describe "Net::HTTPHeader#each_header" do - it_behaves_like :net_httpheader_each_header, :each_header + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header entry to the passed block (keys in lower case, values joined)" do + res = [] + @headers.each_header do |key, value| + res << [key, value] + end + res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.each_header + enumerator.should.instance_of?(Enumerator) + + res = [] + enumerator.each do |*key| + res << key + end + res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] + end + end end diff --git a/library/net-http/httpheader/each_key_spec.rb b/library/net-http/httpheader/each_key_spec.rb index 1ad145629f..a5635da5db 100644 --- a/library/net-http/httpheader/each_key_spec.rb +++ b/library/net-http/httpheader/each_key_spec.rb @@ -1,8 +1,35 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/each_name' describe "Net::HTTPHeader#each_key" do - it_behaves_like :net_httpheader_each_name, :each_key + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + @headers["My-Header"] = "test" + @headers.add_field("My-Other-Header", "a") + @headers.add_field("My-Other-Header", "b") + end + + describe "when passed a block" do + it "yields each header key to the passed block (keys in lower case)" do + res = [] + @headers.each_key do |key| + res << key + end + res.sort.should == ["my-header", "my-other-header"] + end + end + + describe "when passed no block" do + it "returns an Enumerator" do + enumerator = @headers.each_key + enumerator.should.instance_of?(Enumerator) + + res = [] + enumerator.each do |key| + res << key + end + res.sort.should == ["my-header", "my-other-header"] + end + end end diff --git a/library/net-http/httpheader/each_name_spec.rb b/library/net-http/httpheader/each_name_spec.rb index f819bd989d..02f9761f80 100644 --- a/library/net-http/httpheader/each_name_spec.rb +++ b/library/net-http/httpheader/each_name_spec.rb @@ -1,8 +1,10 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/each_name' describe "Net::HTTPHeader#each_name" do - it_behaves_like :net_httpheader_each_name, :each_name + it "is an alias of Net::HTTPHeader#each_key" do + Net::HTTPHeader.instance_method(:each_name).should == + Net::HTTPHeader.instance_method(:each_key) + end end diff --git a/library/net-http/httpheader/each_spec.rb b/library/net-http/httpheader/each_spec.rb index ff37249d0a..e219609b67 100644 --- a/library/net-http/httpheader/each_spec.rb +++ b/library/net-http/httpheader/each_spec.rb @@ -1,8 +1,9 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/classes' -require_relative 'shared/each_header' describe "Net::HTTPHeader#each" do - it_behaves_like :net_httpheader_each_header, :each + it "is an alias of Net::HTTPHeader#each_header" do + Net::HTTPHeader.instance_method(:each).should == + Net::HTTPHeader.instance_method(:each_header) + end end diff --git a/library/net-http/httpheader/form_data_spec.rb b/library/net-http/httpheader/form_data_spec.rb index acd913f53a..8d4974cde3 100644 --- a/library/net-http/httpheader/form_data_spec.rb +++ b/library/net-http/httpheader/form_data_spec.rb @@ -1,8 +1,10 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_form_data' describe "Net::HTTPHeader#form_data=" do - it_behaves_like :net_httpheader_set_form_data, :form_data= + it "is an alias of Net::HTTPHeader#set_form_data" do + Net::HTTPHeader.instance_method(:form_data=).should == + Net::HTTPHeader.instance_method(:set_form_data) + end end diff --git a/library/net-http/httpheader/length_spec.rb b/library/net-http/httpheader/length_spec.rb index 57e32742e4..1f059719e9 100644 --- a/library/net-http/httpheader/length_spec.rb +++ b/library/net-http/httpheader/length_spec.rb @@ -1,8 +1,9 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'fixtures/classes' -require_relative 'shared/size' describe "Net::HTTPHeader#length" do - it_behaves_like :net_httpheader_size, :length + it "is an alias of Net::HTTPHeader#size" do + Net::HTTPHeader.instance_method(:length).should == + Net::HTTPHeader.instance_method(:size) + end end diff --git a/library/net-http/httpheader/range_spec.rb b/library/net-http/httpheader/range_spec.rb index 0fc0feb5c9..8944e2d5f2 100644 --- a/library/net-http/httpheader/range_spec.rb +++ b/library/net-http/httpheader/range_spec.rb @@ -1,7 +1,6 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_range' describe "Net::HTTPHeader#range" do before :each do @@ -44,5 +43,8 @@ end describe "Net::HTTPHeader#range=" do - it_behaves_like :net_httpheader_set_range, :range= + it "is an alias of Net::HTTPHeader#set_range" do + Net::HTTPHeader.instance_method(:range=).should == + Net::HTTPHeader.instance_method(:set_range) + end end diff --git a/library/net-http/httpheader/set_content_type_spec.rb b/library/net-http/httpheader/set_content_type_spec.rb index 7ec4f90b8e..3674061626 100644 --- a/library/net-http/httpheader/set_content_type_spec.rb +++ b/library/net-http/httpheader/set_content_type_spec.rb @@ -1,8 +1,22 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_content_type' describe "Net::HTTPHeader#set_content_type" do - it_behaves_like :net_httpheader_set_content_type, :set_content_type + describe "when passed type, params" do + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "sets the 'Content-Type' header entry based on the passed type and params" do + @headers.set_content_type("text/html") + @headers["Content-Type"].should == "text/html" + + @headers.set_content_type("text/html", "charset" => "utf-8") + @headers["Content-Type"].should == "text/html; charset=utf-8" + + @headers.set_content_type("text/html", "charset" => "utf-8", "rubyspec" => "rocks") + @headers["Content-Type"].split(/; /).sort.should == %w[charset=utf-8 rubyspec=rocks text/html] + end + end end diff --git a/library/net-http/httpheader/set_form_data_spec.rb b/library/net-http/httpheader/set_form_data_spec.rb index 7aac19f045..093dc100d5 100644 --- a/library/net-http/httpheader/set_form_data_spec.rb +++ b/library/net-http/httpheader/set_form_data_spec.rb @@ -1,8 +1,31 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_form_data' describe "Net::HTTPHeader#set_form_data" do - it_behaves_like :net_httpheader_set_form_data, :set_form_data + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + describe "when passed params" do + it "automatically set the 'Content-Type' to 'application/x-www-form-urlencoded'" do + @headers.set_form_data("cmd" => "search", "q" => "ruby", "max" => "50") + @headers["Content-Type"].should == "application/x-www-form-urlencoded" + end + + it "sets self's body based on the passed form parameters" do + @headers.set_form_data("cmd" => "search", "q" => "ruby", "max" => "50") + @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] + end + end + + describe "when passed params, separator" do + it "sets self's body based on the passed form parameters and the passed separator" do + @headers.set_form_data({"cmd" => "search", "q" => "ruby", "max" => "50"}, "&") + @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] + + @headers.set_form_data({"cmd" => "search", "q" => "ruby", "max" => "50"}, ";") + @headers.body.split(";").sort.should == ["cmd=search", "max=50", "q=ruby"] + end + end end diff --git a/library/net-http/httpheader/set_range_spec.rb b/library/net-http/httpheader/set_range_spec.rb index 0f98de55e6..d48ed1897a 100644 --- a/library/net-http/httpheader/set_range_spec.rb +++ b/library/net-http/httpheader/set_range_spec.rb @@ -1,8 +1,93 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/set_range' describe "Net::HTTPHeader#set_range" do - it_behaves_like :net_httpheader_set_range, :set_range + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + describe "when passed nil" do + it "returns nil" do + @headers.set_range(nil).should == nil + end + + it "deletes the 'Range' header entry" do + @headers["Range"] = "bytes 0-499/1234" + @headers.set_range(nil) + @headers["Range"].should == nil + end + end + + describe "when passed Numeric" do + it "sets the 'Range' header entry based on the passed Numeric" do + @headers.set_range(10) + @headers["Range"].should == "bytes=0-9" + + @headers.set_range(-10) + @headers["Range"].should == "bytes=-10" + + @headers.set_range(10.9) + @headers["Range"].should == "bytes=0-9" + end + end + + describe "when passed Range" do + it "sets the 'Range' header entry based on the passed Range" do + @headers.set_range(10..200) + @headers["Range"].should == "bytes=10-200" + + @headers.set_range(1..5) + @headers["Range"].should == "bytes=1-5" + + @headers.set_range(1...5) + @headers["Range"].should == "bytes=1-4" + + @headers.set_range(234..567) + @headers["Range"].should == "bytes=234-567" + + @headers.set_range(-5..-1) + @headers["Range"].should == "bytes=-5" + + @headers.set_range(1..-1) + @headers["Range"].should == "bytes=1-" + end + + it "raises a Net::HTTPHeaderSyntaxError when the first Range element is negative" do + -> { @headers.set_range(-10..5) }.should.raise(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when the last Range element is negative" do + -> { @headers.set_range(10..-5) }.should.raise(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when the last Range element is smaller than the first" do + -> { @headers.set_range(10..5) }.should.raise(Net::HTTPHeaderSyntaxError) + end + end + + describe "when passed start, end" do + it "sets the 'Range' header entry based on the passed start and length values" do + @headers.set_range(10, 200) + @headers["Range"].should == "bytes=10-209" + + @headers.set_range(1, 5) + @headers["Range"].should == "bytes=1-5" + + @headers.set_range(234, 567) + @headers["Range"].should == "bytes=234-800" + end + + it "raises a Net::HTTPHeaderSyntaxError when start is negative" do + -> { @headers.set_range(-10, 5) }.should.raise(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when start + length is negative" do + -> { @headers.set_range(10, -15) }.should.raise(Net::HTTPHeaderSyntaxError) + end + + it "raises a Net::HTTPHeaderSyntaxError when length is negative" do + -> { @headers.set_range(10, -4) }.should.raise(Net::HTTPHeaderSyntaxError) + end + end end diff --git a/library/net-http/httpheader/shared/each_capitalized.rb b/library/net-http/httpheader/shared/each_capitalized.rb deleted file mode 100644 index c12df62787..0000000000 --- a/library/net-http/httpheader/shared/each_capitalized.rb +++ /dev/null @@ -1,31 +0,0 @@ -describe :net_httpheader_each_capitalized, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - @headers["my-header"] = "test" - @headers.add_field("my-Other-Header", "a") - @headers.add_field("My-Other-header", "b") - end - - describe "when passed a block" do - it "yields each header entry to the passed block (capitalized keys, values joined)" do - res = [] - @headers.send(@method) do |key, value| - res << [key, value] - end - res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] - end - end - - describe "when passed no block" do - it "returns an Enumerator" do - enumerator = @headers.send(@method) - enumerator.should.instance_of?(Enumerator) - - res = [] - enumerator.each do |*key| - res << key - end - res.sort.should == [["My-Header", "test"], ["My-Other-Header", "a, b"]] - end - end -end diff --git a/library/net-http/httpheader/shared/each_header.rb b/library/net-http/httpheader/shared/each_header.rb deleted file mode 100644 index 5913665a4d..0000000000 --- a/library/net-http/httpheader/shared/each_header.rb +++ /dev/null @@ -1,31 +0,0 @@ -describe :net_httpheader_each_header, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - @headers["My-Header"] = "test" - @headers.add_field("My-Other-Header", "a") - @headers.add_field("My-Other-Header", "b") - end - - describe "when passed a block" do - it "yields each header entry to the passed block (keys in lower case, values joined)" do - res = [] - @headers.send(@method) do |key, value| - res << [key, value] - end - res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] - end - end - - describe "when passed no block" do - it "returns an Enumerator" do - enumerator = @headers.send(@method) - enumerator.should.instance_of?(Enumerator) - - res = [] - enumerator.each do |*key| - res << key - end - res.sort.should == [["my-header", "test"], ["my-other-header", "a, b"]] - end - end -end diff --git a/library/net-http/httpheader/shared/each_name.rb b/library/net-http/httpheader/shared/each_name.rb deleted file mode 100644 index 29c9400fef..0000000000 --- a/library/net-http/httpheader/shared/each_name.rb +++ /dev/null @@ -1,31 +0,0 @@ -describe :net_httpheader_each_name, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - @headers["My-Header"] = "test" - @headers.add_field("My-Other-Header", "a") - @headers.add_field("My-Other-Header", "b") - end - - describe "when passed a block" do - it "yields each header key to the passed block (keys in lower case)" do - res = [] - @headers.send(@method) do |key| - res << key - end - res.sort.should == ["my-header", "my-other-header"] - end - end - - describe "when passed no block" do - it "returns an Enumerator" do - enumerator = @headers.send(@method) - enumerator.should.instance_of?(Enumerator) - - res = [] - enumerator.each do |key| - res << key - end - res.sort.should == ["my-header", "my-other-header"] - end - end -end diff --git a/library/net-http/httpheader/shared/set_content_type.rb b/library/net-http/httpheader/shared/set_content_type.rb deleted file mode 100644 index b7359bdca6..0000000000 --- a/library/net-http/httpheader/shared/set_content_type.rb +++ /dev/null @@ -1,18 +0,0 @@ -describe :net_httpheader_set_content_type, shared: true do - describe "when passed type, params" do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - end - - it "sets the 'Content-Type' header entry based on the passed type and params" do - @headers.send(@method, "text/html") - @headers["Content-Type"].should == "text/html" - - @headers.send(@method, "text/html", "charset" => "utf-8") - @headers["Content-Type"].should == "text/html; charset=utf-8" - - @headers.send(@method, "text/html", "charset" => "utf-8", "rubyspec" => "rocks") - @headers["Content-Type"].split(/; /).sort.should == %w[charset=utf-8 rubyspec=rocks text/html] - end - end -end diff --git a/library/net-http/httpheader/shared/set_form_data.rb b/library/net-http/httpheader/shared/set_form_data.rb deleted file mode 100644 index db20b18803..0000000000 --- a/library/net-http/httpheader/shared/set_form_data.rb +++ /dev/null @@ -1,27 +0,0 @@ -describe :net_httpheader_set_form_data, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - end - - describe "when passed params" do - it "automatically set the 'Content-Type' to 'application/x-www-form-urlencoded'" do - @headers.send(@method, "cmd" => "search", "q" => "ruby", "max" => "50") - @headers["Content-Type"].should == "application/x-www-form-urlencoded" - end - - it "sets self's body based on the passed form parameters" do - @headers.send(@method, "cmd" => "search", "q" => "ruby", "max" => "50") - @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] - end - end - - describe "when passed params, separator" do - it "sets self's body based on the passed form parameters and the passed separator" do - @headers.send(@method, {"cmd" => "search", "q" => "ruby", "max" => "50"}, "&") - @headers.body.split("&").sort.should == ["cmd=search", "max=50", "q=ruby"] - - @headers.send(@method, {"cmd" => "search", "q" => "ruby", "max" => "50"}, ";") - @headers.body.split(";").sort.should == ["cmd=search", "max=50", "q=ruby"] - end - end -end diff --git a/library/net-http/httpheader/shared/set_range.rb b/library/net-http/httpheader/shared/set_range.rb deleted file mode 100644 index 9ab50a075e..0000000000 --- a/library/net-http/httpheader/shared/set_range.rb +++ /dev/null @@ -1,89 +0,0 @@ -describe :net_httpheader_set_range, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - end - - describe "when passed nil" do - it "returns nil" do - @headers.send(@method, nil).should == nil - end - - it "deletes the 'Range' header entry" do - @headers["Range"] = "bytes 0-499/1234" - @headers.send(@method, nil) - @headers["Range"].should == nil - end - end - - describe "when passed Numeric" do - it "sets the 'Range' header entry based on the passed Numeric" do - @headers.send(@method, 10) - @headers["Range"].should == "bytes=0-9" - - @headers.send(@method, -10) - @headers["Range"].should == "bytes=-10" - - @headers.send(@method, 10.9) - @headers["Range"].should == "bytes=0-9" - end - end - - describe "when passed Range" do - it "sets the 'Range' header entry based on the passed Range" do - @headers.send(@method, 10..200) - @headers["Range"].should == "bytes=10-200" - - @headers.send(@method, 1..5) - @headers["Range"].should == "bytes=1-5" - - @headers.send(@method, 1...5) - @headers["Range"].should == "bytes=1-4" - - @headers.send(@method, 234..567) - @headers["Range"].should == "bytes=234-567" - - @headers.send(@method, -5..-1) - @headers["Range"].should == "bytes=-5" - - @headers.send(@method, 1..-1) - @headers["Range"].should == "bytes=1-" - end - - it "raises a Net::HTTPHeaderSyntaxError when the first Range element is negative" do - -> { @headers.send(@method, -10..5) }.should.raise(Net::HTTPHeaderSyntaxError) - end - - it "raises a Net::HTTPHeaderSyntaxError when the last Range element is negative" do - -> { @headers.send(@method, 10..-5) }.should.raise(Net::HTTPHeaderSyntaxError) - end - - it "raises a Net::HTTPHeaderSyntaxError when the last Range element is smaller than the first" do - -> { @headers.send(@method, 10..5) }.should.raise(Net::HTTPHeaderSyntaxError) - end - end - - describe "when passed start, end" do - it "sets the 'Range' header entry based on the passed start and length values" do - @headers.send(@method, 10, 200) - @headers["Range"].should == "bytes=10-209" - - @headers.send(@method, 1, 5) - @headers["Range"].should == "bytes=1-5" - - @headers.send(@method, 234, 567) - @headers["Range"].should == "bytes=234-800" - end - - it "raises a Net::HTTPHeaderSyntaxError when start is negative" do - -> { @headers.send(@method, -10, 5) }.should.raise(Net::HTTPHeaderSyntaxError) - end - - it "raises a Net::HTTPHeaderSyntaxError when start + length is negative" do - -> { @headers.send(@method, 10, -15) }.should.raise(Net::HTTPHeaderSyntaxError) - end - - it "raises a Net::HTTPHeaderSyntaxError when length is negative" do - -> { @headers.send(@method, 10, -4) }.should.raise(Net::HTTPHeaderSyntaxError) - end - end -end diff --git a/library/net-http/httpheader/shared/size.rb b/library/net-http/httpheader/shared/size.rb deleted file mode 100644 index b38310a940..0000000000 --- a/library/net-http/httpheader/shared/size.rb +++ /dev/null @@ -1,18 +0,0 @@ -describe :net_httpheader_size, shared: true do - before :each do - @headers = NetHTTPHeaderSpecs::Example.new - end - - it "returns the number of header entries in self" do - @headers.send(@method).should.eql?(0) - - @headers["a"] = "b" - @headers.send(@method).should.eql?(1) - - @headers["b"] = "b" - @headers.send(@method).should.eql?(2) - - @headers["c"] = "c" - @headers.send(@method).should.eql?(3) - end -end diff --git a/library/net-http/httpheader/size_spec.rb b/library/net-http/httpheader/size_spec.rb index 210060ce21..f84a0fb5ab 100644 --- a/library/net-http/httpheader/size_spec.rb +++ b/library/net-http/httpheader/size_spec.rb @@ -1,8 +1,22 @@ require_relative '../../../spec_helper' require 'net/http' require_relative 'fixtures/classes' -require_relative 'shared/size' describe "Net::HTTPHeader#size" do - it_behaves_like :net_httpheader_size, :size + before :each do + @headers = NetHTTPHeaderSpecs::Example.new + end + + it "returns the number of header entries in self" do + @headers.size.should.eql?(0) + + @headers["a"] = "b" + @headers.size.should.eql?(1) + + @headers["b"] = "b" + @headers.size.should.eql?(2) + + @headers["c"] = "c" + @headers.size.should.eql?(3) + end end diff --git a/library/net-http/httpresponse/body_spec.rb b/library/net-http/httpresponse/body_spec.rb index ddfcd834c4..5b00913687 100644 --- a/library/net-http/httpresponse/body_spec.rb +++ b/library/net-http/httpresponse/body_spec.rb @@ -1,7 +1,22 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/body' +require 'stringio' describe "Net::HTTPResponse#body" do - it_behaves_like :net_httpresponse_body, :body + before :each do + @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") + @socket = Net::BufferedIO.new(StringIO.new("test body")) + end + + it "returns the read body" do + @res.reading_body(@socket, true) do + @res.body.should == "test body" + end + end + + it "returns the previously read body if called a second time" do + @res.reading_body(@socket, true) do + @res.body.should.equal?(@res.body) + end + end end diff --git a/library/net-http/httpresponse/entity_spec.rb b/library/net-http/httpresponse/entity_spec.rb index ca8c4b29c0..d2201db37b 100644 --- a/library/net-http/httpresponse/entity_spec.rb +++ b/library/net-http/httpresponse/entity_spec.rb @@ -1,7 +1,9 @@ require_relative '../../../spec_helper' require 'net/http' -require_relative 'shared/body' describe "Net::HTTPResponse#entity" do - it_behaves_like :net_httpresponse_body, :entity + it "is an alias of Net::HTTPResponse#body" do + Net::HTTPResponse.instance_method(:entity).should == + Net::HTTPResponse.instance_method(:body) + end end diff --git a/library/net-http/httpresponse/shared/body.rb b/library/net-http/httpresponse/shared/body.rb deleted file mode 100644 index 368774fb52..0000000000 --- a/library/net-http/httpresponse/shared/body.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'stringio' - -describe :net_httpresponse_body, shared: true do - before :each do - @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") - @socket = Net::BufferedIO.new(StringIO.new("test body")) - end - - it "returns the read body" do - @res.reading_body(@socket, true) do - @res.send(@method).should == "test body" - end - end - - it "returns the previously read body if called a second time" do - @res.reading_body(@socket, true) do - @res.send(@method).should.equal?(@res.send(@method)) - end - end -end From 7c4b2691109eeed17ae0e8fe520aa0e21c65b5e6 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:34:12 +0200 Subject: [PATCH 08/18] Move OpenStruct to rely less on shared examples --- library/openstruct/equal_value_spec.rb | 2 +- library/openstruct/inspect_spec.rb | 6 +++--- library/openstruct/shared/inspect.rb | 20 -------------------- library/openstruct/to_s_spec.rb | 20 ++++++++++++++++++-- 4 files changed, 22 insertions(+), 26 deletions(-) delete mode 100644 library/openstruct/shared/inspect.rb diff --git a/library/openstruct/equal_value_spec.rb b/library/openstruct/equal_value_spec.rb index c72c09ce14..ec30214fd3 100644 --- a/library/openstruct/equal_value_spec.rb +++ b/library/openstruct/equal_value_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require "ostruct" +require 'ostruct' require_relative 'fixtures/classes' describe "OpenStruct#==" do diff --git a/library/openstruct/inspect_spec.rb b/library/openstruct/inspect_spec.rb index e2fed41528..81da96d6bf 100644 --- a/library/openstruct/inspect_spec.rb +++ b/library/openstruct/inspect_spec.rb @@ -1,8 +1,8 @@ require_relative '../../spec_helper' require 'ostruct' -require_relative 'fixtures/classes' -require_relative 'shared/inspect' describe "OpenStruct#inspect" do - it_behaves_like :ostruct_inspect, :inspect + it "is an alias of OpenStruct#to_s" do + OpenStruct.instance_method(:inspect).should == OpenStruct.instance_method(:to_s) + end end diff --git a/library/openstruct/shared/inspect.rb b/library/openstruct/shared/inspect.rb deleted file mode 100644 index d5fffa0e2e..0000000000 --- a/library/openstruct/shared/inspect.rb +++ /dev/null @@ -1,20 +0,0 @@ -describe :ostruct_inspect, shared: true do - it "returns a String representation of self" do - os = OpenStruct.new(name: "John Smith") - os.send(@method).should == "#" - - os = OpenStruct.new(age: 20, name: "John Smith") - os.send(@method).should.is_a?(String) - end - - it "correctly handles self-referential OpenStructs" do - os = OpenStruct.new - os.self = os - os.send(@method).should == "#>" - end - - it "correctly handles OpenStruct subclasses" do - os = OpenStructSpecs::OpenStructSub.new(name: "John Smith") - os.send(@method).should == "#" - end -end diff --git a/library/openstruct/to_s_spec.rb b/library/openstruct/to_s_spec.rb index 73d91bf981..9131cd4897 100644 --- a/library/openstruct/to_s_spec.rb +++ b/library/openstruct/to_s_spec.rb @@ -1,8 +1,24 @@ require_relative '../../spec_helper' require 'ostruct' require_relative 'fixtures/classes' -require_relative 'shared/inspect' describe "OpenStruct#to_s" do - it_behaves_like :ostruct_inspect, :to_s + it "returns a String representation of self" do + os = OpenStruct.new(name: "John Smith") + os.to_s.should == "#" + + os = OpenStruct.new(age: 20, name: "John Smith") + os.to_s.should.is_a?(String) + end + + it "correctly handles self-referential OpenStructs" do + os = OpenStruct.new + os.self = os + os.to_s.should == "#>" + end + + it "correctly handles OpenStruct subclasses" do + os = OpenStructSpecs::OpenStructSub.new(name: "John Smith") + os.to_s.should == "#" + end end From c1a5636bbacac44cff99820ae6c421a22946cf07 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:38:48 +0200 Subject: [PATCH 09/18] Move Pathname to rely less on shared examples Also add one alias test --- library/pathname/case_compare_spec.rb | 8 ++++++++ library/pathname/divide_spec.rb | 6 ++++-- library/pathname/plus_spec.rb | 7 +++++-- library/pathname/shared/plus.rb | 8 -------- 4 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 library/pathname/case_compare_spec.rb delete mode 100644 library/pathname/shared/plus.rb diff --git a/library/pathname/case_compare_spec.rb b/library/pathname/case_compare_spec.rb new file mode 100644 index 0000000000..0cf799dd23 --- /dev/null +++ b/library/pathname/case_compare_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'pathname' + +describe "Pathname#===" do + it "is an alias of Pathname#==" do + Pathname.instance_method(:===).should == Pathname.instance_method(:==) + end +end diff --git a/library/pathname/divide_spec.rb b/library/pathname/divide_spec.rb index 8af79d0c8f..e5afc9f864 100644 --- a/library/pathname/divide_spec.rb +++ b/library/pathname/divide_spec.rb @@ -1,6 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/plus' +require 'pathname' describe "Pathname#/" do - it_behaves_like :pathname_plus, :/ + it "is an alias of Pathname#+" do + Pathname.instance_method(:/).should == Pathname.instance_method(:+) + end end diff --git a/library/pathname/plus_spec.rb b/library/pathname/plus_spec.rb index 57e472c266..76316df9d2 100644 --- a/library/pathname/plus_spec.rb +++ b/library/pathname/plus_spec.rb @@ -1,6 +1,9 @@ require_relative '../../spec_helper' -require_relative 'shared/plus' +require 'pathname' describe "Pathname#+" do - it_behaves_like :pathname_plus, :+ + it "appends a pathname to self" do + p = Pathname.new("/usr") + (p + "bin/ruby").should == Pathname.new("/usr/bin/ruby") + end end diff --git a/library/pathname/shared/plus.rb b/library/pathname/shared/plus.rb deleted file mode 100644 index b3b896ea43..0000000000 --- a/library/pathname/shared/plus.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'pathname' - -describe :pathname_plus, shared: true do - it "appends a pathname to self" do - p = Pathname.new("/usr") - p.send(@method, "bin/ruby").should == Pathname.new("/usr/bin/ruby") - end -end From b0d6fb4cf94df85af666f8f354af16836e02f2a8 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:44:36 +0200 Subject: [PATCH 10/18] Move Prime to rely less on shared examples --- library/prime/next_spec.rb | 8 ++++++-- library/prime/shared/next.rb | 8 -------- library/prime/succ_spec.rb | 6 ++++-- 3 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 library/prime/shared/next.rb diff --git a/library/prime/next_spec.rb b/library/prime/next_spec.rb index 39c4ae16ae..07e80ab3a5 100644 --- a/library/prime/next_spec.rb +++ b/library/prime/next_spec.rb @@ -1,7 +1,11 @@ require_relative '../../spec_helper' -require_relative 'shared/next' require 'prime' describe "Prime#next" do - it_behaves_like :prime_next, :next + it "returns the element at the current position and moves forward" do + p = Prime.instance.each + p.next.should == 2 + p.next.should == 3 + p.next.next.should == 6 + end end diff --git a/library/prime/shared/next.rb b/library/prime/shared/next.rb deleted file mode 100644 index f79b2c051e..0000000000 --- a/library/prime/shared/next.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :prime_next, shared: true do - it "returns the element at the current position and moves forward" do - p = Prime.instance.each - p.next.should == 2 - p.next.should == 3 - p.next.next.should == 6 - end -end diff --git a/library/prime/succ_spec.rb b/library/prime/succ_spec.rb index 34c18d2ba0..86f76c2513 100644 --- a/library/prime/succ_spec.rb +++ b/library/prime/succ_spec.rb @@ -1,7 +1,9 @@ require_relative '../../spec_helper' -require_relative 'shared/next' require 'prime' describe "Prime#succ" do - it_behaves_like :prime_next, :succ + it "is an alias of Prime#next" do + p = Prime.instance.each + p.method(:succ).should == p.method(:next) + end end From 459abee06f9967c3ee834f26bb3159c7519b57a2 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 16:37:01 +0200 Subject: [PATCH 11/18] Move Socket to rely less on shared examples --- library/socket/addrinfo/shared/to_sockaddr.rb | 47 ------ library/socket/addrinfo/to_s_spec.rb | 5 +- library/socket/addrinfo/to_sockaddr_spec.rb | 47 +++++- library/socket/shared/pack_sockaddr.rb | 92 ------------ library/socket/shared/socketpair.rb | 138 ------------------ .../socket/socket/pack_sockaddr_in_spec.rb | 6 +- .../socket/socket/pack_sockaddr_un_spec.rb | 8 +- library/socket/socket/pair_spec.rb | 138 +++++++++++++++++- library/socket/socket/sockaddr_in_spec.rb | 48 +++++- library/socket/socket/sockaddr_un_spec.rb | 46 +++++- library/socket/socket/socketpair_spec.rb | 6 +- library/socket/unixsocket/pair_spec.rb | 45 +++++- library/socket/unixsocket/shared/pair.rb | 47 ------ library/socket/unixsocket/socketpair_spec.rb | 15 +- 14 files changed, 327 insertions(+), 361 deletions(-) delete mode 100644 library/socket/addrinfo/shared/to_sockaddr.rb delete mode 100644 library/socket/shared/pack_sockaddr.rb delete mode 100644 library/socket/shared/socketpair.rb delete mode 100644 library/socket/unixsocket/shared/pair.rb diff --git a/library/socket/addrinfo/shared/to_sockaddr.rb b/library/socket/addrinfo/shared/to_sockaddr.rb deleted file mode 100644 index 70d6bfbbfe..0000000000 --- a/library/socket/addrinfo/shared/to_sockaddr.rb +++ /dev/null @@ -1,47 +0,0 @@ -describe :socket_addrinfo_to_sockaddr, shared: true do - describe "for an ipv4 socket" do - before :each do - @addrinfo = Addrinfo.tcp("127.0.0.1", 80) - end - - it "returns a sockaddr packed structure" do - @addrinfo.send(@method).should == Socket.sockaddr_in(80, '127.0.0.1') - end - end - - describe "for an ipv6 socket" do - before :each do - @addrinfo = Addrinfo.tcp("::1", 80) - end - - it "returns a sockaddr packed structure" do - @addrinfo.send(@method).should == Socket.sockaddr_in(80, '::1') - end - end - - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end - - it "returns a sockaddr packed structure" do - @addrinfo.send(@method).should == Socket.sockaddr_un('/tmp/sock') - end - end - - describe 'using a Addrinfo with just an IP address' do - it 'returns a String' do - addr = Addrinfo.ip('127.0.0.1') - - addr.send(@method).should == Socket.sockaddr_in(0, '127.0.0.1') - end - end - - describe 'using a Addrinfo without an IP and port' do - it 'returns a String' do - addr = Addrinfo.new(['AF_INET', 0, '', '']) - - addr.send(@method).should == Socket.sockaddr_in(0, '') - end - end -end diff --git a/library/socket/addrinfo/to_s_spec.rb b/library/socket/addrinfo/to_s_spec.rb index ddf994e051..5c1c82793c 100644 --- a/library/socket/addrinfo/to_s_spec.rb +++ b/library/socket/addrinfo/to_s_spec.rb @@ -1,6 +1,7 @@ require_relative '../spec_helper' -require_relative 'shared/to_sockaddr' describe "Addrinfo#to_s" do - it_behaves_like :socket_addrinfo_to_sockaddr, :to_s + it "is an alias of Addrinfo#to_sockaddr" do + Addrinfo.instance_method(:to_s).should == Addrinfo.instance_method(:to_sockaddr) + end end diff --git a/library/socket/addrinfo/to_sockaddr_spec.rb b/library/socket/addrinfo/to_sockaddr_spec.rb index b9f75454bd..c703c7b28f 100644 --- a/library/socket/addrinfo/to_sockaddr_spec.rb +++ b/library/socket/addrinfo/to_sockaddr_spec.rb @@ -1,6 +1,49 @@ require_relative '../spec_helper' -require_relative 'shared/to_sockaddr' describe "Addrinfo#to_sockaddr" do - it_behaves_like :socket_addrinfo_to_sockaddr, :to_sockaddr + describe "for an ipv4 socket" do + before :each do + @addrinfo = Addrinfo.tcp("127.0.0.1", 80) + end + + it "returns a sockaddr packed structure" do + @addrinfo.to_sockaddr.should == Socket.sockaddr_in(80, '127.0.0.1') + end + end + + describe "for an ipv6 socket" do + before :each do + @addrinfo = Addrinfo.tcp("::1", 80) + end + + it "returns a sockaddr packed structure" do + @addrinfo.to_sockaddr.should == Socket.sockaddr_in(80, '::1') + end + end + + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end + + it "returns a sockaddr packed structure" do + @addrinfo.to_sockaddr.should == Socket.sockaddr_un('/tmp/sock') + end + end + + describe 'using a Addrinfo with just an IP address' do + it 'returns a String' do + addr = Addrinfo.ip('127.0.0.1') + + addr.to_sockaddr.should == Socket.sockaddr_in(0, '127.0.0.1') + end + end + + describe 'using a Addrinfo without an IP and port' do + it 'returns a String' do + addr = Addrinfo.new(['AF_INET', 0, '', '']) + + addr.to_sockaddr.should == Socket.sockaddr_in(0, '') + end + end end diff --git a/library/socket/shared/pack_sockaddr.rb b/library/socket/shared/pack_sockaddr.rb deleted file mode 100644 index db6f39612d..0000000000 --- a/library/socket/shared/pack_sockaddr.rb +++ /dev/null @@ -1,92 +0,0 @@ -# coding: utf-8 -describe :socket_pack_sockaddr_in, shared: true do - it "packs and unpacks" do - sockaddr_in = Socket.public_send(@method, 0, nil) - port, addr = Socket.unpack_sockaddr_in(sockaddr_in) - ["127.0.0.1", "::1"].include?(addr).should == true - port.should == 0 - - sockaddr_in = Socket.public_send(@method, 0, '') - Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '0.0.0.0'] - - sockaddr_in = Socket.public_send(@method, 80, '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] - - sockaddr_in = Socket.public_send(@method, '80', '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] - - sockaddr_in = Socket.public_send(@method, nil, '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1'] - - sockaddr_in = Socket.public_send(@method, 80, Socket::INADDR_ANY) - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '0.0.0.0'] - end - - it 'resolves the service name to a port' do - sockaddr_in = Socket.public_send(@method, 'http', '127.0.0.1') - Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] - end - - describe 'using an IPv4 address' do - it 'returns a String of 16 bytes' do - str = Socket.public_send(@method, 80, '127.0.0.1') - - str.should.instance_of?(String) - str.bytesize.should == 16 - end - end - - describe 'using an IPv6 address' do - it 'returns a String of 28 bytes' do - str = Socket.public_send(@method, 80, '::1') - - str.should.instance_of?(String) - str.bytesize.should == 28 - end - end -end - -describe :socket_pack_sockaddr_un, shared: true do - it 'should be idempotent' do - bytes = Socket.public_send(@method, '/tmp/foo').bytes - bytes[2..9].should == [47, 116, 109, 112, 47, 102, 111, 111] - bytes[10..-1].all?(&:zero?).should == true - end - - it "packs and unpacks" do - sockaddr_un = Socket.public_send(@method, '/tmp/s') - Socket.unpack_sockaddr_un(sockaddr_un).should == '/tmp/s' - end - - it "handles correctly paths with multibyte chars" do - sockaddr_un = Socket.public_send(@method, '/home/вася/sock') - path = Socket.unpack_sockaddr_un(sockaddr_un).encode('UTF-8', 'UTF-8') - path.should == '/home/вася/sock' - end - - platform_is :linux do - it 'returns a String of 110 bytes' do - str = Socket.public_send(@method, '/tmp/test.sock') - - str.should.instance_of?(String) - str.bytesize.should == 110 - end - end - - platform_is :bsd do - it 'returns a String of 106 bytes' do - str = Socket.public_send(@method, '/tmp/test.sock') - - str.should.instance_of?(String) - str.bytesize.should == 106 - end - end - - platform_is_not :aix do - it "raises ArgumentError for paths that are too long" do - # AIX doesn't raise error - long_path = 'a' * 110 - -> { Socket.public_send(@method, long_path) }.should.raise(ArgumentError) - end - end -end diff --git a/library/socket/shared/socketpair.rb b/library/socket/shared/socketpair.rb deleted file mode 100644 index 7fcd4d6b46..0000000000 --- a/library/socket/shared/socketpair.rb +++ /dev/null @@ -1,138 +0,0 @@ -describe :socket_socketpair, shared: true do - platform_is_not :windows do - it "ensures the returned sockets are connected" do - s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, 1, 0) - s1.puts("test") - s2.gets.should == "test\n" - s1.close - s2.close - end - - it "responses with array of two sockets" do - begin - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - ensure - s1.close - s2.close - end - end - - describe 'using an Integer as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, Socket::SOCK_STREAM) - - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - s1.close - s2.close - end - end - - describe 'using a Symbol as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - s1.close - s2.close - end - - it 'raises SocketError for an unknown address family' do - -> { Socket.public_send(@method, :CATS, :STREAM) }.should.raise(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - -> { Socket.public_send(@method, :UNIX, :CATS) }.should.raise(SocketError) - end - end - - describe 'using a String as the 1st and 2nd argument' do - it 'returns two Socket objects' do - s1, s2 = Socket.public_send(@method, 'UNIX', 'STREAM') - - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - s1.close - s2.close - end - - it 'raises SocketError for an unknown address family' do - -> { Socket.public_send(@method, 'CATS', 'STREAM') }.should.raise(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - -> { Socket.public_send(@method, 'UNIX', 'CATS') }.should.raise(SocketError) - end - end - - describe 'using an object that responds to #to_str as the 1st and 2nd argument' do - it 'returns two Socket objects' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('UNIX') - type.stub!(:to_str).and_return('STREAM') - - s1, s2 = Socket.public_send(@method, family, type) - - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - s1.close - s2.close - end - - it 'raises TypeError when #to_str does not return a String' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return(Socket::AF_UNIX) - type.stub!(:to_str).and_return(Socket::SOCK_STREAM) - - -> { Socket.public_send(@method, family, type) }.should.raise(TypeError) - end - - it 'raises SocketError for an unknown address family' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('CATS') - type.stub!(:to_str).and_return('STREAM') - - -> { Socket.public_send(@method, family, type) }.should.raise(SocketError) - end - - it 'raises SocketError for an unknown socket type' do - family = mock(:family) - type = mock(:type) - - family.stub!(:to_str).and_return('UNIX') - type.stub!(:to_str).and_return('CATS') - - -> { Socket.public_send(@method, family, type) }.should.raise(SocketError) - end - end - - it 'accepts a custom protocol as an Integer as the 3rd argument' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM, Socket::IPPROTO_IP) - s1.should.instance_of?(Socket) - s2.should.instance_of?(Socket) - s1.close - s2.close - end - - it 'connects the returned Socket objects' do - s1, s2 = Socket.public_send(@method, :UNIX, :STREAM) - begin - s1.write('hello') - s2.recv(5).should == 'hello' - ensure - s1.close - s2.close - end - end - end -end diff --git a/library/socket/socket/pack_sockaddr_in_spec.rb b/library/socket/socket/pack_sockaddr_in_spec.rb index ef2a2d4ba9..17a737cacd 100644 --- a/library/socket/socket/pack_sockaddr_in_spec.rb +++ b/library/socket/socket/pack_sockaddr_in_spec.rb @@ -1,7 +1,7 @@ require_relative '../spec_helper' -require_relative '../fixtures/classes' -require_relative '../shared/pack_sockaddr' describe "Socket.pack_sockaddr_in" do - it_behaves_like :socket_pack_sockaddr_in, :pack_sockaddr_in + it "is an alias of Socket.sockaddr_in" do + Socket.method(:pack_sockaddr_in).should == Socket.method(:sockaddr_in) + end end diff --git a/library/socket/socket/pack_sockaddr_un_spec.rb b/library/socket/socket/pack_sockaddr_un_spec.rb index 1ee0bc6157..34d4fc1f51 100644 --- a/library/socket/socket/pack_sockaddr_un_spec.rb +++ b/library/socket/socket/pack_sockaddr_un_spec.rb @@ -1,7 +1,7 @@ require_relative '../spec_helper' -require_relative '../fixtures/classes' -require_relative '../shared/pack_sockaddr' -describe "Socket#pack_sockaddr_un" do - it_behaves_like :socket_pack_sockaddr_un, :pack_sockaddr_un +describe "Socket.pack_sockaddr_un" do + it "is an alias of Socket.sockaddr_un" do + Socket.method(:pack_sockaddr_un).should == Socket.method(:sockaddr_un) + end end diff --git a/library/socket/socket/pair_spec.rb b/library/socket/socket/pair_spec.rb index 8dd470a95e..91317a8d07 100644 --- a/library/socket/socket/pair_spec.rb +++ b/library/socket/socket/pair_spec.rb @@ -1,7 +1,141 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -require_relative '../shared/socketpair' describe "Socket.pair" do - it_behaves_like :socket_socketpair, :pair + platform_is_not :windows do + it "ensures the returned sockets are connected" do + s1, s2 = Socket.pair(Socket::AF_UNIX, 1, 0) + s1.puts("test") + s2.gets.should == "test\n" + s1.close + s2.close + end + + it "returns an array of two sockets" do + begin + s1, s2 = Socket.pair(:UNIX, :STREAM) + + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + ensure + s1.close + s2.close + end + end + + describe 'using an Integer as the 1st and 2nd argument' do + it 'returns two Socket objects' do + s1, s2 = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM) + + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + s1.close + s2.close + end + end + + describe 'using a Symbol as the 1st and 2nd argument' do + it 'returns two Socket objects' do + s1, s2 = Socket.pair(:UNIX, :STREAM) + + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + s1.close + s2.close + end + + it 'raises SocketError for an unknown address family' do + -> { Socket.pair(:CATS, :STREAM) }.should.raise(SocketError) + end + + it 'raises SocketError for an unknown socket type' do + -> { Socket.pair(:UNIX, :CATS) }.should.raise(SocketError) + end + end + + describe 'using a String as the 1st and 2nd argument' do + it 'returns two Socket objects' do + s1, s2 = Socket.pair('UNIX', 'STREAM') + + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + s1.close + s2.close + end + + it 'raises SocketError for an unknown address family' do + -> { Socket.pair('CATS', 'STREAM') }.should.raise(SocketError) + end + + it 'raises SocketError for an unknown socket type' do + -> { Socket.pair('UNIX', 'CATS') }.should.raise(SocketError) + end + end + + describe 'using an object that responds to #to_str as the 1st and 2nd argument' do + it 'returns two Socket objects' do + family = mock(:family) + type = mock(:type) + + family.stub!(:to_str).and_return('UNIX') + type.stub!(:to_str).and_return('STREAM') + + s1, s2 = Socket.pair(family, type) + + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + s1.close + s2.close + end + + it 'raises TypeError when #to_str does not return a String' do + family = mock(:family) + type = mock(:type) + + family.stub!(:to_str).and_return(Socket::AF_UNIX) + type.stub!(:to_str).and_return(Socket::SOCK_STREAM) + + -> { Socket.pair(family, type) }.should.raise(TypeError) + end + + it 'raises SocketError for an unknown address family' do + family = mock(:family) + type = mock(:type) + + family.stub!(:to_str).and_return('CATS') + type.stub!(:to_str).and_return('STREAM') + + -> { Socket.pair(family, type) }.should.raise(SocketError) + end + + it 'raises SocketError for an unknown socket type' do + family = mock(:family) + type = mock(:type) + + family.stub!(:to_str).and_return('UNIX') + type.stub!(:to_str).and_return('CATS') + + -> { Socket.pair(family, type) }.should.raise(SocketError) + end + end + + it 'accepts a custom protocol as an Integer as the 3rd argument' do + s1, s2 = Socket.pair(:UNIX, :STREAM, Socket::IPPROTO_IP) + s1.should.instance_of?(Socket) + s2.should.instance_of?(Socket) + s1.close + s2.close + end + + it 'connects the returned Socket objects' do + s1, s2 = Socket.pair(:UNIX, :STREAM) + begin + s1.write('hello') + s2.recv(5).should == 'hello' + ensure + s1.close + s2.close + end + end + end end diff --git a/library/socket/socket/sockaddr_in_spec.rb b/library/socket/socket/sockaddr_in_spec.rb index 8ee956ac26..9d3367cd69 100644 --- a/library/socket/socket/sockaddr_in_spec.rb +++ b/library/socket/socket/sockaddr_in_spec.rb @@ -1,7 +1,49 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -require_relative '../shared/pack_sockaddr' -describe "Socket#sockaddr_in" do - it_behaves_like :socket_pack_sockaddr_in, :sockaddr_in +describe "Socket.sockaddr_in" do + it "packs and unpacks" do + sockaddr_in = Socket.sockaddr_in(0, nil) + port, addr = Socket.unpack_sockaddr_in(sockaddr_in) + ["127.0.0.1", "::1"].include?(addr).should == true + port.should == 0 + + sockaddr_in = Socket.sockaddr_in(0, '') + Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '0.0.0.0'] + + sockaddr_in = Socket.sockaddr_in(80, '127.0.0.1') + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] + + sockaddr_in = Socket.sockaddr_in('80', '127.0.0.1') + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] + + sockaddr_in = Socket.sockaddr_in(nil, '127.0.0.1') + Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1'] + + sockaddr_in = Socket.sockaddr_in(80, Socket::INADDR_ANY) + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '0.0.0.0'] + end + + it 'resolves the service name to a port' do + sockaddr_in = Socket.sockaddr_in('http', '127.0.0.1') + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] + end + + describe 'using an IPv4 address' do + it 'returns a String of 16 bytes' do + str = Socket.sockaddr_in(80, '127.0.0.1') + + str.should.instance_of?(String) + str.bytesize.should == 16 + end + end + + describe 'using an IPv6 address' do + it 'returns a String of 28 bytes' do + str = Socket.sockaddr_in(80, '::1') + + str.should.instance_of?(String) + str.bytesize.should == 28 + end + end end diff --git a/library/socket/socket/sockaddr_un_spec.rb b/library/socket/socket/sockaddr_un_spec.rb index 8922ff4d6d..548dc526ff 100644 --- a/library/socket/socket/sockaddr_un_spec.rb +++ b/library/socket/socket/sockaddr_un_spec.rb @@ -1,7 +1,47 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -require_relative '../shared/pack_sockaddr' -describe "Socket#sockaddr_un" do - it_behaves_like :socket_pack_sockaddr_un, :sockaddr_un +describe "Socket.sockaddr_un" do + it 'should be idempotent' do + bytes = Socket.sockaddr_un('/tmp/foo').bytes + bytes[2..9].should == [47, 116, 109, 112, 47, 102, 111, 111] + bytes[10..-1].all?(&:zero?).should == true + end + + it "packs and unpacks" do + sockaddr_un = Socket.sockaddr_un('/tmp/s') + Socket.unpack_sockaddr_un(sockaddr_un).should == '/tmp/s' + end + + it "handles correctly paths with multibyte chars" do + sockaddr_un = Socket.sockaddr_un('/home/вася/sock') + path = Socket.unpack_sockaddr_un(sockaddr_un).encode('UTF-8', 'UTF-8') + path.should == '/home/вася/sock' + end + + platform_is :linux do + it 'returns a String of 110 bytes' do + str = Socket.sockaddr_un('/tmp/test.sock') + + str.should.instance_of?(String) + str.bytesize.should == 110 + end + end + + platform_is :bsd do + it 'returns a String of 106 bytes' do + str = Socket.sockaddr_un('/tmp/test.sock') + + str.should.instance_of?(String) + str.bytesize.should == 106 + end + end + + platform_is_not :aix do + it "raises ArgumentError for paths that are too long" do + # AIX doesn't raise error + long_path = 'a' * 110 + -> { Socket.sockaddr_un(long_path) }.should.raise(ArgumentError) + end + end end diff --git a/library/socket/socket/socketpair_spec.rb b/library/socket/socket/socketpair_spec.rb index 551c376d49..c5bef1dcbe 100644 --- a/library/socket/socket/socketpair_spec.rb +++ b/library/socket/socket/socketpair_spec.rb @@ -1,7 +1,7 @@ require_relative '../spec_helper' -require_relative '../fixtures/classes' -require_relative '../shared/socketpair' describe "Socket.socketpair" do - it_behaves_like :socket_socketpair, :socketpair + it "is an alias for Socket.pair" do + Socket.method(:socketpair).should == Socket.method(:pair) + end end diff --git a/library/socket/unixsocket/pair_spec.rb b/library/socket/unixsocket/pair_spec.rb index 9690142668..9f04f568fa 100644 --- a/library/socket/unixsocket/pair_spec.rb +++ b/library/socket/unixsocket/pair_spec.rb @@ -1,10 +1,8 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/partially_closable_sockets' -require_relative 'shared/pair' describe "UNIXSocket.pair" do - it_should_behave_like :unixsocket_pair it_should_behave_like :partially_closable_sockets before :each do @@ -15,4 +13,47 @@ @s1.close @s2.close end + + it "returns two UNIXSockets" do + @s1.should.instance_of?(UNIXSocket) + @s2.should.instance_of?(UNIXSocket) + end + + it "returns a pair of connected sockets" do + @s1.puts "foo" + @s2.gets.should == "foo\n" + end + + platform_is_not :windows do + it "sets the socket paths to empty Strings" do + @s1.path.should == "" + @s2.path.should == "" + end + + it "sets the socket addresses to empty Strings" do + @s1.addr.should == ["AF_UNIX", ""] + @s2.addr.should == ["AF_UNIX", ""] + end + + it "sets the socket peer addresses to empty Strings" do + @s1.peeraddr.should == ["AF_UNIX", ""] + @s2.peeraddr.should == ["AF_UNIX", ""] + end + end + + platform_is :windows do + it "emulates unnamed sockets with a temporary file with a path" do + @s1.addr.should == ["AF_UNIX", @s1.path] + @s2.peeraddr.should == ["AF_UNIX", @s1.path] + end + + it "sets the peer address of first socket to an empty string" do + @s1.peeraddr.should == ["AF_UNIX", ""] + end + + it "sets the address and path of second socket to an empty string" do + @s2.addr.should == ["AF_UNIX", ""] + @s2.path.should == "" + end + end end diff --git a/library/socket/unixsocket/shared/pair.rb b/library/socket/unixsocket/shared/pair.rb deleted file mode 100644 index 49b6a6a413..0000000000 --- a/library/socket/unixsocket/shared/pair.rb +++ /dev/null @@ -1,47 +0,0 @@ -require_relative '../../spec_helper' -require_relative '../../fixtures/classes' - -describe :unixsocket_pair, shared: true do - it "returns two UNIXSockets" do - @s1.should.instance_of?(UNIXSocket) - @s2.should.instance_of?(UNIXSocket) - end - - it "returns a pair of connected sockets" do - @s1.puts "foo" - @s2.gets.should == "foo\n" - end - - platform_is_not :windows do - it "sets the socket paths to empty Strings" do - @s1.path.should == "" - @s2.path.should == "" - end - - it "sets the socket addresses to empty Strings" do - @s1.addr.should == ["AF_UNIX", ""] - @s2.addr.should == ["AF_UNIX", ""] - end - - it "sets the socket peer addresses to empty Strings" do - @s1.peeraddr.should == ["AF_UNIX", ""] - @s2.peeraddr.should == ["AF_UNIX", ""] - end - end - - platform_is :windows do - it "emulates unnamed sockets with a temporary file with a path" do - @s1.addr.should == ["AF_UNIX", @s1.path] - @s2.peeraddr.should == ["AF_UNIX", @s1.path] - end - - it "sets the peer address of first socket to an empty string" do - @s1.peeraddr.should == ["AF_UNIX", ""] - end - - it "sets the address and path of second socket to an empty string" do - @s2.addr.should == ["AF_UNIX", ""] - @s2.path.should == "" - end - end -end diff --git a/library/socket/unixsocket/socketpair_spec.rb b/library/socket/unixsocket/socketpair_spec.rb index c61fc00be4..a8bfb412e5 100644 --- a/library/socket/unixsocket/socketpair_spec.rb +++ b/library/socket/unixsocket/socketpair_spec.rb @@ -1,18 +1,7 @@ require_relative '../spec_helper' -require_relative '../fixtures/classes' -require_relative '../shared/partially_closable_sockets' -require_relative 'shared/pair' describe "UNIXSocket.socketpair" do - it_should_behave_like :unixsocket_pair - it_should_behave_like :partially_closable_sockets - - before :each do - @s1, @s2 = UNIXSocket.socketpair - end - - after :each do - @s1.close - @s2.close + it "is an alias of UNIXSocket.pair" do + UNIXSocket.method(:socketpair).should == UNIXSocket.method(:pair) end end From ddd8fe8d5f0ded0540c08fccffd68625cbc177da Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:31:20 +0200 Subject: [PATCH 12/18] Move StringIO to rely less on shared examples --- library/stringio/each_byte_spec.rb | 46 +++++- library/stringio/each_char_spec.rb | 33 +++- library/stringio/each_codepoint_spec.rb | 46 +++++- library/stringio/each_line_spec.rb | 199 +++++++++++++++++++++- library/stringio/each_spec.rb | 33 +--- library/stringio/eof_spec.rb | 30 +++- library/stringio/isatty_spec.rb | 7 +- library/stringio/length_spec.rb | 7 +- library/stringio/pos_spec.rb | 12 +- library/stringio/shared/codepoints.rb | 45 ----- library/stringio/shared/each.rb | 209 ------------------------ library/stringio/shared/each_byte.rb | 48 ------ library/stringio/shared/each_char.rb | 36 ---- library/stringio/shared/eof.rb | 24 --- library/stringio/shared/isatty.rb | 5 - library/stringio/shared/length.rb | 5 - library/stringio/shared/tell.rb | 12 -- library/stringio/size_spec.rb | 7 +- library/stringio/tell_spec.rb | 7 +- library/stringio/tty_spec.rb | 7 +- 20 files changed, 368 insertions(+), 450 deletions(-) delete mode 100644 library/stringio/shared/codepoints.rb delete mode 100644 library/stringio/shared/each.rb delete mode 100644 library/stringio/shared/each_byte.rb delete mode 100644 library/stringio/shared/each_char.rb delete mode 100644 library/stringio/shared/eof.rb delete mode 100644 library/stringio/shared/isatty.rb delete mode 100644 library/stringio/shared/length.rb delete mode 100644 library/stringio/shared/tell.rb diff --git a/library/stringio/each_byte_spec.rb b/library/stringio/each_byte_spec.rb index 6f82a32441..1be0081c1e 100644 --- a/library/stringio/each_byte_spec.rb +++ b/library/stringio/each_byte_spec.rb @@ -1,11 +1,51 @@ require_relative '../../spec_helper' require 'stringio' -require_relative 'shared/each_byte' describe "StringIO#each_byte" do - it_behaves_like :stringio_each_byte, :each_byte + before :each do + @io = StringIO.new("xyz") + end + + it "yields each character code in turn" do + seen = [] + @io.each_byte { |b| seen << b } + seen.should == [120, 121, 122] + end + + it "updates the position before each yield" do + seen = [] + @io.each_byte { |b| seen << @io.pos } + seen.should == [1, 2, 3] + end + + it "does not yield if the current position is out of bounds" do + @io.pos = 1000 + seen = nil + @io.each_byte { |b| seen = b } + seen.should == nil + end + + it "returns self" do + @io.each_byte {}.should.equal?(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.each_byte + enum.instance_of?(Enumerator).should == true + + seen = [] + enum.each { |b| seen << b } + seen.should == [120, 121, 122] + end end describe "StringIO#each_byte when self is not readable" do - it_behaves_like :stringio_each_byte_not_readable, :each_byte + it "raises an IOError" do + io = StringIO.new(+"xyz", "w") + -> { io.each_byte { |b| b } }.should.raise(IOError) + + io = StringIO.new("xyz") + io.close_read + -> { io.each_byte { |b| b } }.should.raise(IOError) + end end diff --git a/library/stringio/each_char_spec.rb b/library/stringio/each_char_spec.rb index 14b2f09a17..1db80c7d07 100644 --- a/library/stringio/each_char_spec.rb +++ b/library/stringio/each_char_spec.rb @@ -1,11 +1,38 @@ require_relative '../../spec_helper' require 'stringio' -require_relative 'shared/each_char' describe "StringIO#each_char" do - it_behaves_like :stringio_each_char, :each_char + before :each do + @io = StringIO.new("xyz äöü") + end + + it "yields each character code in turn" do + seen = [] + @io.each_char { |c| seen << c } + seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] + end + + it "returns self" do + @io.each_char {}.should.equal?(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.each_char + enum.instance_of?(Enumerator).should == true + + seen = [] + enum.each { |c| seen << c } + seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] + end end describe "StringIO#each_char when self is not readable" do - it_behaves_like :stringio_each_char_not_readable, :each_char + it "raises an IOError" do + io = StringIO.new(+"xyz", "w") + -> { io.each_char { |b| b } }.should.raise(IOError) + + io = StringIO.new("xyz") + io.close_read + -> { io.each_char { |b| b } }.should.raise(IOError) + end end diff --git a/library/stringio/each_codepoint_spec.rb b/library/stringio/each_codepoint_spec.rb index f18de22aad..d4f461db90 100644 --- a/library/stringio/each_codepoint_spec.rb +++ b/library/stringio/each_codepoint_spec.rb @@ -1,9 +1,47 @@ -# -*- encoding: utf-8 -*- require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/codepoints' +require 'stringio' # See redmine #1667 describe "StringIO#each_codepoint" do - it_behaves_like :stringio_codepoints, :each_codepoint + before :each do + @io = StringIO.new("∂φ/∂x = gaîté") + @enum = @io.each_codepoint + end + + it "returns an Enumerator" do + @enum.should.instance_of?(Enumerator) + end + + it "yields each codepoint code in turn" do + @enum.to_a.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] + end + + it "yields each codepoint starting from the current position" do + @io.pos = 15 + @enum.to_a.should == [238, 116, 233] + end + + it "raises an error if reading invalid sequence" do + @io.pos = 1 # inside of a multibyte sequence + -> { @enum.first }.should.raise(ArgumentError) + end + + it "raises an IOError if not readable" do + @io.close_read + -> { @enum.to_a }.should.raise(IOError) + + io = StringIO.new(+"xyz", "w") + -> { io.each_codepoint.to_a }.should.raise(IOError) + end + + + it "calls the given block" do + r = [] + @io.each_codepoint{|c| r << c } + r.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] + end + + it "returns self" do + @io.each_codepoint {|l| l }.should.equal?(@io) + end end diff --git a/library/stringio/each_line_spec.rb b/library/stringio/each_line_spec.rb index 4ac0db7c45..4abecbf026 100644 --- a/library/stringio/each_line_spec.rb +++ b/library/stringio/each_line_spec.rb @@ -1,27 +1,212 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/each' describe "StringIO#each_line when passed a separator" do - it_behaves_like :stringio_each_separator, :each_line + before :each do + @io = StringIO.new("a b c d e\n1 2 3 4 5") + end + + it "uses the passed argument as the line separator" do + seen = [] + @io.each_line(" ") {|s| seen << s} + seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] + end + + it "does not change $_" do + $_ = "test" + @io.each_line(" ") { |s| s} + $_.should == "test" + end + + it "returns self" do + @io.each_line {|l| l }.should.equal?(@io) + end + + it "tries to convert the passed separator to a String using #to_str" do + obj = mock("to_str") + obj.stub!(:to_str).and_return(" ") + + seen = [] + @io.each_line(obj) { |l| seen << l } + seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] + end + + it "yields self's content starting from the current position when the passed separator is nil" do + seen = [] + io = StringIO.new("1 2 1 2 1 2") + io.pos = 2 + io.each_line(nil) {|s| seen << s} + seen.should == ["2 1 2 1 2"] + end + + it "yields each paragraph with all separation characters when passed an empty String as separator" do + seen = [] + io = StringIO.new("para1\n\npara2\n\n\npara3") + io.each_line("") {|s| seen << s} + seen.should == ["para1\n\n", "para2\n\n\n", "para3"] + end end describe "StringIO#each_line when passed no arguments" do - it_behaves_like :stringio_each_no_arguments, :each_line + before :each do + @io = StringIO.new("a b c d e\n1 2 3 4 5") + end + + it "yields each line to the passed block" do + seen = [] + @io.each_line {|s| seen << s } + seen.should == ["a b c d e\n", "1 2 3 4 5"] + end + + it "yields each line starting from the current position" do + seen = [] + @io.pos = 4 + @io.each_line {|s| seen << s } + seen.should == ["c d e\n", "1 2 3 4 5"] + end + + it "does not change $_" do + $_ = "test" + @io.each_line { |s| s} + $_.should == "test" + end + + it "uses $/ as the default line separator" do + seen = [] + begin + old_rs = $/ + suppress_warning {$/ = " "} + @io.each_line {|s| seen << s } + seen.should.eql?(["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"]) + ensure + suppress_warning {$/ = old_rs} + end + end + + it "returns self" do + @io.each_line {|l| l }.should.equal?(@io) + end + + it "returns an Enumerator when passed no block" do + enum = @io.each_line + enum.instance_of?(Enumerator).should == true + + seen = [] + enum.each { |b| seen << b } + seen.should == ["a b c d e\n", "1 2 3 4 5"] + end end describe "StringIO#each_line when self is not readable" do - it_behaves_like :stringio_each_not_readable, :each_line + it "raises an IOError" do + io = StringIO.new(+"a b c d e", "w") + -> { io.each_line { |b| b } }.should.raise(IOError) + + io = StringIO.new("a b c d e") + io.close_read + -> { io.each_line { |b| b } }.should.raise(IOError) + end +end + +describe "StringIO#each_line when passed chomp" do + it "yields each line with removed newline characters to the passed block" do + seen = [] + io = StringIO.new("a b \rc d e\n1 2 3 4 5\r\nthe end") + io.each_line(chomp: true) {|s| seen << s } + seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"] + end + + it "returns each line with removed newline characters when called without block" do + seen = [] + io = StringIO.new("a b \rc d e\n1 2 3 4 5\r\nthe end") + enum = io.each_line(chomp: true) + enum.each {|s| seen << s } + seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"] + end end describe "StringIO#each_line when passed chomp" do - it_behaves_like :stringio_each_chomp, :each_line + it "yields each line with removed separator to the passed block" do + seen = [] + io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end") + io.each_line("|", chomp: true) {|s| seen << s } + seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"] + end + + it "returns each line with removed separator when called without block" do + seen = [] + io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end") + enum = io.each_line("|", chomp: true) + enum.each {|s| seen << s } + seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"] + end end describe "StringIO#each_line when passed limit" do - it_behaves_like :stringio_each_limit, :each_line + before :each do + @io = StringIO.new("a b c d e\n1 2 3 4 5") + end + + it "returns the data read until the limit is met" do + seen = [] + @io.each_line(4) { |s| seen << s } + seen.should == ["a b ", "c d ", "e\n", "1 2 ", "3 4 ", "5"] + end end describe "StringIO#each when passed separator and limit" do - it_behaves_like :stringio_each_separator_and_limit, :each_line + before :each do + @io = StringIO.new("this>is>an>example") + end + + it "returns the data read until the limit is consumed or the separator is met" do + @io.each_line('>', 8) { |s| break s }.should == "this>" + @io.each_line('>', 2) { |s| break s }.should == "is" + @io.each_line('>', 10) { |s| break s }.should == ">" + @io.each_line('>', 6) { |s| break s }.should == "an>" + @io.each_line('>', 5) { |s| break s }.should == "examp" + end + + it "truncates the multi-character separator at the end to meet the limit" do + @io.each_line("is>an", 7) { |s| break s }.should == "this>is" + end + + it "does not change $_" do + $_ = "test" + @io.each_line('>', 8) { |s| s } + $_.should == "test" + end + + it "updates self's lineno by one" do + @io.each_line('>', 3) { |s| break s } + @io.lineno.should.eql?(1) + + @io.each_line('>', 3) { |s| break s } + @io.lineno.should.eql?(2) + + @io.each_line('>', 3) { |s| break s } + @io.lineno.should.eql?(3) + end + + it "tries to convert the passed separator to a String using #to_str" do # TODO + obj = mock('to_str') + obj.should_receive(:to_str).and_return('>') + + seen = [] + @io.each_line(obj, 5) { |s| seen << s } + seen.should == ["this>", "is>", "an>", "examp", "le"] + end + + it "does not raise TypeError if passed separator is nil" do + @io.each_line(nil, 5) { |s| break s }.should == "this>" + end + + it "tries to convert the passed limit to an Integer using #to_int" do # TODO + obj = mock('to_int') + obj.should_receive(:to_int).and_return(5) + + seen = [] + @io.each_line('>', obj) { |s| seen << s } + seen.should == ["this>", "is>", "an>", "examp", "le"] + end end diff --git a/library/stringio/each_spec.rb b/library/stringio/each_spec.rb index 7eb322f3ff..f3785bc18f 100644 --- a/library/stringio/each_spec.rb +++ b/library/stringio/each_spec.rb @@ -1,31 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/each' +require 'stringio' -describe "StringIO#each when passed a separator" do - it_behaves_like :stringio_each_separator, :each -end - -describe "StringIO#each when passed no arguments" do - it_behaves_like :stringio_each_no_arguments, :each -end - -describe "StringIO#each when self is not readable" do - it_behaves_like :stringio_each_not_readable, :each -end - -describe "StringIO#each when passed chomp" do - it_behaves_like :stringio_each_chomp, :each -end - -describe "StringIO#each when passed chomp" do - it_behaves_like :stringio_each_separator_and_chomp, :each -end - -describe "StringIO#each when passed limit" do - it_behaves_like :stringio_each_limit, :each -end - -describe "StringIO#each when passed separator and limit" do - it_behaves_like :stringio_each_separator_and_limit, :each +describe "StringIO#each" do + it "is an alias of StringIO#each_line" do + StringIO.instance_method(:each).should == StringIO.instance_method(:each_line) + end end diff --git a/library/stringio/eof_spec.rb b/library/stringio/eof_spec.rb index af0170977c..acc49305f5 100644 --- a/library/stringio/eof_spec.rb +++ b/library/stringio/eof_spec.rb @@ -1,11 +1,33 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/eof' +require 'stringio' describe "StringIO#eof?" do - it_behaves_like :stringio_eof, :eof? + before :each do + @io = StringIO.new("eof") + end + + it "returns true when self's position is greater than or equal to self's size" do + @io.pos = 3 + @io.eof?.should == true + + @io.pos = 6 + @io.eof?.should == true + end + + it "returns false when self's position is less than self's size" do + @io.pos = 0 + @io.eof?.should == false + + @io.pos = 1 + @io.eof?.should == false + + @io.pos = 2 + @io.eof?.should == false + end end describe "StringIO#eof" do - it_behaves_like :stringio_eof, :eof + it "is an alias of StringIO#eof?" do + StringIO.instance_method(:eof).should == StringIO.instance_method(:eof?) + end end diff --git a/library/stringio/isatty_spec.rb b/library/stringio/isatty_spec.rb index 1ef33978b5..be648ba9b7 100644 --- a/library/stringio/isatty_spec.rb +++ b/library/stringio/isatty_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/isatty' +require 'stringio' describe "StringIO#isatty" do - it_behaves_like :stringio_isatty, :isatty + it "is an alias of StringIO.tty?" do + StringIO.instance_method(:isatty).should == StringIO.instance_method(:tty?) + end end diff --git a/library/stringio/length_spec.rb b/library/stringio/length_spec.rb index d3070f50a7..a83be6256a 100644 --- a/library/stringio/length_spec.rb +++ b/library/stringio/length_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/length' +require 'stringio' describe "StringIO#length" do - it_behaves_like :stringio_length, :length + it "returns the length of the wrapped string" do + StringIO.new("example").length.should == 7 + end end diff --git a/library/stringio/pos_spec.rb b/library/stringio/pos_spec.rb index ba640f8c18..16f068b049 100644 --- a/library/stringio/pos_spec.rb +++ b/library/stringio/pos_spec.rb @@ -1,9 +1,17 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/tell' describe "StringIO#pos" do - it_behaves_like :stringio_tell, :pos + before :each do + @io = StringIOSpecs.build + end + + it "returns the current byte offset" do + @io.getc + @io.pos.should == 1 + @io.read(7) + @io.pos.should == 8 + end end describe "StringIO#pos=" do diff --git a/library/stringio/shared/codepoints.rb b/library/stringio/shared/codepoints.rb deleted file mode 100644 index e35a02ccb4..0000000000 --- a/library/stringio/shared/codepoints.rb +++ /dev/null @@ -1,45 +0,0 @@ -# -*- encoding: utf-8 -*- -describe :stringio_codepoints, shared: true do - before :each do - @io = StringIO.new("∂φ/∂x = gaîté") - @enum = @io.send(@method) - end - - it "returns an Enumerator" do - @enum.should.instance_of?(Enumerator) - end - - it "yields each codepoint code in turn" do - @enum.to_a.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] - end - - it "yields each codepoint starting from the current position" do - @io.pos = 15 - @enum.to_a.should == [238, 116, 233] - end - - it "raises an error if reading invalid sequence" do - @io.pos = 1 # inside of a multibyte sequence - -> { @enum.first }.should.raise(ArgumentError) - end - - it "raises an IOError if not readable" do - @io.close_read - -> { @enum.to_a }.should.raise(IOError) - - io = StringIO.new(+"xyz", "w") - -> { io.send(@method).to_a }.should.raise(IOError) - end - - - it "calls the given block" do - r = [] - @io.send(@method){|c| r << c } - r.should == [8706, 966, 47, 8706, 120, 32, 61, 32, 103, 97, 238, 116, 233] - end - - it "returns self" do - @io.send(@method) {|l| l }.should.equal?(@io) - end - -end diff --git a/library/stringio/shared/each.rb b/library/stringio/shared/each.rb deleted file mode 100644 index 04e40b6b2a..0000000000 --- a/library/stringio/shared/each.rb +++ /dev/null @@ -1,209 +0,0 @@ -describe :stringio_each_separator, shared: true do - before :each do - @io = StringIO.new("a b c d e\n1 2 3 4 5") - end - - it "uses the passed argument as the line separator" do - seen = [] - @io.send(@method, " ") {|s| seen << s} - seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] - end - - it "does not change $_" do - $_ = "test" - @io.send(@method, " ") { |s| s} - $_.should == "test" - end - - it "returns self" do - @io.send(@method) {|l| l }.should.equal?(@io) - end - - it "tries to convert the passed separator to a String using #to_str" do - obj = mock("to_str") - obj.stub!(:to_str).and_return(" ") - - seen = [] - @io.send(@method, obj) { |l| seen << l } - seen.should == ["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"] - end - - it "yields self's content starting from the current position when the passed separator is nil" do - seen = [] - io = StringIO.new("1 2 1 2 1 2") - io.pos = 2 - io.send(@method, nil) {|s| seen << s} - seen.should == ["2 1 2 1 2"] - end - - it "yields each paragraph with all separation characters when passed an empty String as separator" do - seen = [] - io = StringIO.new("para1\n\npara2\n\n\npara3") - io.send(@method, "") {|s| seen << s} - seen.should == ["para1\n\n", "para2\n\n\n", "para3"] - end -end - -describe :stringio_each_no_arguments, shared: true do - before :each do - @io = StringIO.new("a b c d e\n1 2 3 4 5") - end - - it "yields each line to the passed block" do - seen = [] - @io.send(@method) {|s| seen << s } - seen.should == ["a b c d e\n", "1 2 3 4 5"] - end - - it "yields each line starting from the current position" do - seen = [] - @io.pos = 4 - @io.send(@method) {|s| seen << s } - seen.should == ["c d e\n", "1 2 3 4 5"] - end - - it "does not change $_" do - $_ = "test" - @io.send(@method) { |s| s} - $_.should == "test" - end - - it "uses $/ as the default line separator" do - seen = [] - begin - old_rs = $/ - suppress_warning {$/ = " "} - @io.send(@method) {|s| seen << s } - seen.should.eql?(["a ", "b ", "c ", "d ", "e\n1 ", "2 ", "3 ", "4 ", "5"]) - ensure - suppress_warning {$/ = old_rs} - end - end - - it "returns self" do - @io.send(@method) {|l| l }.should.equal?(@io) - end - - it "returns an Enumerator when passed no block" do - enum = @io.send(@method) - enum.instance_of?(Enumerator).should == true - - seen = [] - enum.each { |b| seen << b } - seen.should == ["a b c d e\n", "1 2 3 4 5"] - end -end - -describe :stringio_each_not_readable, shared: true do - it "raises an IOError" do - io = StringIO.new(+"a b c d e", "w") - -> { io.send(@method) { |b| b } }.should.raise(IOError) - - io = StringIO.new("a b c d e") - io.close_read - -> { io.send(@method) { |b| b } }.should.raise(IOError) - end -end - -describe :stringio_each_chomp, shared: true do - it "yields each line with removed newline characters to the passed block" do - seen = [] - io = StringIO.new("a b \rc d e\n1 2 3 4 5\r\nthe end") - io.send(@method, chomp: true) {|s| seen << s } - seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"] - end - - it "returns each line with removed newline characters when called without block" do - seen = [] - io = StringIO.new("a b \rc d e\n1 2 3 4 5\r\nthe end") - enum = io.send(@method, chomp: true) - enum.each {|s| seen << s } - seen.should == ["a b \rc d e", "1 2 3 4 5", "the end"] - end -end - -describe :stringio_each_separator_and_chomp, shared: true do - it "yields each line with removed separator to the passed block" do - seen = [] - io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end") - io.send(@method, "|", chomp: true) {|s| seen << s } - seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"] - end - - it "returns each line with removed separator when called without block" do - seen = [] - io = StringIO.new("a b \nc d e|1 2 3 4 5\n|the end") - enum = io.send(@method, "|", chomp: true) - enum.each {|s| seen << s } - seen.should == ["a b \nc d e", "1 2 3 4 5\n", "the end"] - end -end - -describe :stringio_each_limit, shared: true do - before :each do - @io = StringIO.new("a b c d e\n1 2 3 4 5") - end - - it "returns the data read until the limit is met" do - seen = [] - @io.send(@method, 4) { |s| seen << s } - seen.should == ["a b ", "c d ", "e\n", "1 2 ", "3 4 ", "5"] - end -end - -describe :stringio_each_separator_and_limit, shared: true do - before :each do - @io = StringIO.new("this>is>an>example") - end - - it "returns the data read until the limit is consumed or the separator is met" do - @io.send(@method, '>', 8) { |s| break s }.should == "this>" - @io.send(@method, '>', 2) { |s| break s }.should == "is" - @io.send(@method, '>', 10) { |s| break s }.should == ">" - @io.send(@method, '>', 6) { |s| break s }.should == "an>" - @io.send(@method, '>', 5) { |s| break s }.should == "examp" - end - - it "truncates the multi-character separator at the end to meet the limit" do - @io.send(@method, "is>an", 7) { |s| break s }.should == "this>is" - end - - it "does not change $_" do - $_ = "test" - @io.send(@method, '>', 8) { |s| s } - $_.should == "test" - end - - it "updates self's lineno by one" do - @io.send(@method, '>', 3) { |s| break s } - @io.lineno.should.eql?(1) - - @io.send(@method, '>', 3) { |s| break s } - @io.lineno.should.eql?(2) - - @io.send(@method, '>', 3) { |s| break s } - @io.lineno.should.eql?(3) - end - - it "tries to convert the passed separator to a String using #to_str" do # TODO - obj = mock('to_str') - obj.should_receive(:to_str).and_return('>') - - seen = [] - @io.send(@method, obj, 5) { |s| seen << s } - seen.should == ["this>", "is>", "an>", "examp", "le"] - end - - it "does not raise TypeError if passed separator is nil" do - @io.send(@method, nil, 5) { |s| break s }.should == "this>" - end - - it "tries to convert the passed limit to an Integer using #to_int" do # TODO - obj = mock('to_int') - obj.should_receive(:to_int).and_return(5) - - seen = [] - @io.send(@method, '>', obj) { |s| seen << s } - seen.should == ["this>", "is>", "an>", "examp", "le"] - end -end diff --git a/library/stringio/shared/each_byte.rb b/library/stringio/shared/each_byte.rb deleted file mode 100644 index b3939c26de..0000000000 --- a/library/stringio/shared/each_byte.rb +++ /dev/null @@ -1,48 +0,0 @@ -describe :stringio_each_byte, shared: true do - before :each do - @io = StringIO.new("xyz") - end - - it "yields each character code in turn" do - seen = [] - @io.send(@method) { |b| seen << b } - seen.should == [120, 121, 122] - end - - it "updates the position before each yield" do - seen = [] - @io.send(@method) { |b| seen << @io.pos } - seen.should == [1, 2, 3] - end - - it "does not yield if the current position is out of bounds" do - @io.pos = 1000 - seen = nil - @io.send(@method) { |b| seen = b } - seen.should == nil - end - - it "returns self" do - @io.send(@method) {}.should.equal?(@io) - end - - it "returns an Enumerator when passed no block" do - enum = @io.send(@method) - enum.instance_of?(Enumerator).should == true - - seen = [] - enum.each { |b| seen << b } - seen.should == [120, 121, 122] - end -end - -describe :stringio_each_byte_not_readable, shared: true do - it "raises an IOError" do - io = StringIO.new(+"xyz", "w") - -> { io.send(@method) { |b| b } }.should.raise(IOError) - - io = StringIO.new("xyz") - io.close_read - -> { io.send(@method) { |b| b } }.should.raise(IOError) - end -end diff --git a/library/stringio/shared/each_char.rb b/library/stringio/shared/each_char.rb deleted file mode 100644 index 4215a9952b..0000000000 --- a/library/stringio/shared/each_char.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -*- encoding: utf-8 -*- -describe :stringio_each_char, shared: true do - before :each do - @io = StringIO.new("xyz äöü") - end - - it "yields each character code in turn" do - seen = [] - @io.send(@method) { |c| seen << c } - seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] - end - - it "returns self" do - @io.send(@method) {}.should.equal?(@io) - end - - it "returns an Enumerator when passed no block" do - enum = @io.send(@method) - enum.instance_of?(Enumerator).should == true - - seen = [] - enum.each { |c| seen << c } - seen.should == ["x", "y", "z", " ", "ä", "ö", "ü"] - end -end - -describe :stringio_each_char_not_readable, shared: true do - it "raises an IOError" do - io = StringIO.new(+"xyz", "w") - -> { io.send(@method) { |b| b } }.should.raise(IOError) - - io = StringIO.new("xyz") - io.close_read - -> { io.send(@method) { |b| b } }.should.raise(IOError) - end -end diff --git a/library/stringio/shared/eof.rb b/library/stringio/shared/eof.rb deleted file mode 100644 index a9489581fc..0000000000 --- a/library/stringio/shared/eof.rb +++ /dev/null @@ -1,24 +0,0 @@ -describe :stringio_eof, shared: true do - before :each do - @io = StringIO.new("eof") - end - - it "returns true when self's position is greater than or equal to self's size" do - @io.pos = 3 - @io.send(@method).should == true - - @io.pos = 6 - @io.send(@method).should == true - end - - it "returns false when self's position is less than self's size" do - @io.pos = 0 - @io.send(@method).should == false - - @io.pos = 1 - @io.send(@method).should == false - - @io.pos = 2 - @io.send(@method).should == false - end -end diff --git a/library/stringio/shared/isatty.rb b/library/stringio/shared/isatty.rb deleted file mode 100644 index 2b92e8d656..0000000000 --- a/library/stringio/shared/isatty.rb +++ /dev/null @@ -1,5 +0,0 @@ -describe :stringio_isatty, shared: true do - it "returns false" do - StringIO.new("tty").send(@method).should == false - end -end diff --git a/library/stringio/shared/length.rb b/library/stringio/shared/length.rb deleted file mode 100644 index 60a4eb1bdd..0000000000 --- a/library/stringio/shared/length.rb +++ /dev/null @@ -1,5 +0,0 @@ -describe :stringio_length, shared: true do - it "returns the length of the wrapped string" do - StringIO.new("example").send(@method).should == 7 - end -end diff --git a/library/stringio/shared/tell.rb b/library/stringio/shared/tell.rb deleted file mode 100644 index 852c51c192..0000000000 --- a/library/stringio/shared/tell.rb +++ /dev/null @@ -1,12 +0,0 @@ -describe :stringio_tell, shared: true do - before :each do - @io = StringIOSpecs.build - end - - it "returns the current byte offset" do - @io.getc - @io.send(@method).should == 1 - @io.read(7) - @io.send(@method).should == 8 - end -end diff --git a/library/stringio/size_spec.rb b/library/stringio/size_spec.rb index f674d22db9..33e574ddae 100644 --- a/library/stringio/size_spec.rb +++ b/library/stringio/size_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/length' +require 'stringio' describe "StringIO#size" do - it_behaves_like :stringio_length, :size + it "is an alias of StringIO#length" do + StringIO.instance_method(:size).should == StringIO.instance_method(:length) + end end diff --git a/library/stringio/tell_spec.rb b/library/stringio/tell_spec.rb index 8350ee6f4d..dd58fb8088 100644 --- a/library/stringio/tell_spec.rb +++ b/library/stringio/tell_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/tell' +require 'stringio' describe "StringIO#tell" do - it_behaves_like :stringio_tell, :tell + it "is an alias of StringIO.pos" do + StringIO.instance_method(:tell).should == StringIO.instance_method(:pos) + end end diff --git a/library/stringio/tty_spec.rb b/library/stringio/tty_spec.rb index c6293dcbd7..87e22d49a5 100644 --- a/library/stringio/tty_spec.rb +++ b/library/stringio/tty_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/isatty' +require 'stringio' describe "StringIO#tty?" do - it_behaves_like :stringio_isatty, :tty? + it "returns false" do + StringIO.new("tty").tty?.should == false + end end From 37807f795ccc299ac8016eb35196d6986b15589d Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:40:09 +0200 Subject: [PATCH 13/18] Move StringScanner to rely less on shared examples --- library/stringscanner/append_spec.rb | 28 ++++++++- .../stringscanner/beginning_of_line_spec.rb | 25 +++++++- library/stringscanner/bol_spec.rb | 5 +- library/stringscanner/concat_spec.rb | 9 +-- library/stringscanner/pointer_spec.rb | 9 +-- library/stringscanner/pos_spec.rb | 57 +++++++++++++++++- library/stringscanner/shared/bol.rb | 25 -------- library/stringscanner/shared/concat.rb | 30 ---------- library/stringscanner/shared/pos.rb | 59 ------------------- 9 files changed, 111 insertions(+), 136 deletions(-) delete mode 100644 library/stringscanner/shared/bol.rb delete mode 100644 library/stringscanner/shared/concat.rb delete mode 100644 library/stringscanner/shared/pos.rb diff --git a/library/stringscanner/append_spec.rb b/library/stringscanner/append_spec.rb index fef5dcf2bd..68747d52d7 100644 --- a/library/stringscanner/append_spec.rb +++ b/library/stringscanner/append_spec.rb @@ -1,11 +1,33 @@ require_relative '../../spec_helper' -require_relative 'shared/concat' require 'strscan' describe "StringScanner#<<" do - it_behaves_like :strscan_concat, :<< + it "concatenates the given argument to self and returns self" do + s = StringScanner.new(+"hello ") + (s. << 'world').should == s + s.string.should == "hello world" + s.eos?.should == false + end + + it "raises a TypeError if the given argument can't be converted to a String" do + -> { StringScanner.new('hello') << :world }.should.raise(TypeError) + -> { StringScanner.new('hello') << mock('x') }.should.raise(TypeError) + end end describe "StringScanner#<< when passed an Integer" do - it_behaves_like :strscan_concat_fixnum, :<< + it "raises a TypeError" do + a = StringScanner.new("hello world") + -> { a << 333 }.should.raise(TypeError) + b = StringScanner.new("") + -> { b << (256 * 3 + 64) }.should.raise(TypeError) + -> { b << -200 }.should.raise(TypeError) + end + + it "doesn't call to_int on the argument" do + x = mock('x') + x.should_not_receive(:to_int) + + -> { StringScanner.new("") << x }.should.raise(TypeError) + end end diff --git a/library/stringscanner/beginning_of_line_spec.rb b/library/stringscanner/beginning_of_line_spec.rb index 3f6f0da75f..ae97f52fe0 100644 --- a/library/stringscanner/beginning_of_line_spec.rb +++ b/library/stringscanner/beginning_of_line_spec.rb @@ -1,7 +1,28 @@ require_relative '../../spec_helper' -require_relative 'shared/bol' require 'strscan' describe "StringScanner#beginning_of_line?" do - it_behaves_like :strscan_bol, :beginning_of_line? + it "returns true if the scan pointer is at the beginning of the line, false otherwise" do + s = StringScanner.new("This is a test") + s.beginning_of_line?.should == true + s.scan(/This/) + s.beginning_of_line?.should == false + s.terminate + s.beginning_of_line?.should == false + + s = StringScanner.new("hello\nworld") + s.beginning_of_line?.should == true + s.scan(/\w+/) + s.beginning_of_line?.should == false + s.scan(/\n/) + s.beginning_of_line?.should == true + s.unscan + s.beginning_of_line?.should == false + end + + it "returns true if the scan pointer is at the end of the line of an empty string." do + s = StringScanner.new('') + s.terminate + s.beginning_of_line?.should == true + end end diff --git a/library/stringscanner/bol_spec.rb b/library/stringscanner/bol_spec.rb index d31766e0e2..1d10c8f7c0 100644 --- a/library/stringscanner/bol_spec.rb +++ b/library/stringscanner/bol_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/bol' require 'strscan' describe "StringScanner#bol?" do - it_behaves_like :strscan_bol, :bol? + it "is an alias of StringScanner#beginning_of_line?" do + StringScanner.instance_method(:bol?).should == StringScanner.instance_method(:beginning_of_line?) + end end diff --git a/library/stringscanner/concat_spec.rb b/library/stringscanner/concat_spec.rb index 4f790e2505..716268c956 100644 --- a/library/stringscanner/concat_spec.rb +++ b/library/stringscanner/concat_spec.rb @@ -1,11 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/concat' require 'strscan' describe "StringScanner#concat" do - it_behaves_like :strscan_concat, :concat -end - -describe "StringScanner#concat when passed an Integer" do - it_behaves_like :strscan_concat_fixnum, :concat + it "is an alias of StringScanner#<<" do + StringScanner.instance_method(:concat).should == StringScanner.instance_method(:<<) + end end diff --git a/library/stringscanner/pointer_spec.rb b/library/stringscanner/pointer_spec.rb index bc0c0c50b7..c36086c3fe 100644 --- a/library/stringscanner/pointer_spec.rb +++ b/library/stringscanner/pointer_spec.rb @@ -1,11 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/pos' require 'strscan' describe "StringScanner#pointer" do - it_behaves_like :strscan_pos, :pointer -end - -describe "StringScanner#pointer=" do - it_behaves_like :strscan_pos_set, :pointer= + it "is an alias of StringScanner#pos" do + StringScanner.instance_method(:pointer).should == StringScanner.instance_method(:pos) + end end diff --git a/library/stringscanner/pos_spec.rb b/library/stringscanner/pos_spec.rb index 275fecf0f3..bc3003ebdf 100644 --- a/library/stringscanner/pos_spec.rb +++ b/library/stringscanner/pos_spec.rb @@ -1,11 +1,62 @@ require_relative '../../spec_helper' -require_relative 'shared/pos' require 'strscan' describe "StringScanner#pos" do - it_behaves_like :strscan_pos, :pos + before :each do + @s = StringScanner.new("This is a test") + end + + it "returns the position of the scan pointer" do + @s.pos.should == 0 + @s.scan_until(/This is/) + @s.pos.should == 7 + @s.get_byte + @s.pos.should == 8 + @s.terminate + @s.pos.should == 14 + end + + it "returns 0 in the reset position" do + @s.reset + @s.pos.should == 0 + end + + it "returns the length of the string in the terminate position" do + @s.terminate + @s.pos.should == @s.string.length + end + + it "is not multi-byte character sensitive" do + s = StringScanner.new("abcädeföghi") + + s.scan_until(/ö/) + s.pos.should == 10 + end end describe "StringScanner#pos=" do - it_behaves_like :strscan_pos_set, :pos= + before :each do + @s = StringScanner.new("This is a test") + end + + it "modify the scan pointer" do + @s.pos = 5 + @s.rest.should == "is a test" + end + + it "positions from the end if the argument is negative" do + @s.pos = -2 + @s.rest.should == "st" + @s.pos.should == 12 + end + + it "raises a RangeError if position too far backward" do + -> { + @s.pos = -20 + }.should.raise(RangeError) + end + + it "raises a RangeError when the passed argument is out of range" do + -> { @s.pos = 20 }.should.raise(RangeError) + end end diff --git a/library/stringscanner/shared/bol.rb b/library/stringscanner/shared/bol.rb deleted file mode 100644 index ec5c2051b5..0000000000 --- a/library/stringscanner/shared/bol.rb +++ /dev/null @@ -1,25 +0,0 @@ -describe :strscan_bol, shared: true do - it "returns true if the scan pointer is at the beginning of the line, false otherwise" do - s = StringScanner.new("This is a test") - s.send(@method).should == true - s.scan(/This/) - s.send(@method).should == false - s.terminate - s.send(@method).should == false - - s = StringScanner.new("hello\nworld") - s.bol?.should == true - s.scan(/\w+/) - s.bol?.should == false - s.scan(/\n/) - s.bol?.should == true - s.unscan - s.bol?.should == false - end - - it "returns true if the scan pointer is at the end of the line of an empty string." do - s = StringScanner.new('') - s.terminate - s.send(@method).should == true - end -end diff --git a/library/stringscanner/shared/concat.rb b/library/stringscanner/shared/concat.rb deleted file mode 100644 index 8138b0f8dc..0000000000 --- a/library/stringscanner/shared/concat.rb +++ /dev/null @@ -1,30 +0,0 @@ -describe :strscan_concat, shared: true do - it "concatenates the given argument to self and returns self" do - s = StringScanner.new(+"hello ") - s.send(@method, 'world').should == s - s.string.should == "hello world" - s.eos?.should == false - end - - it "raises a TypeError if the given argument can't be converted to a String" do - -> { StringScanner.new('hello').send(@method, :world) }.should.raise(TypeError) - -> { StringScanner.new('hello').send(@method, mock('x')) }.should.raise(TypeError) - end -end - -describe :strscan_concat_fixnum, shared: true do - it "raises a TypeError" do - a = StringScanner.new("hello world") - -> { a.send(@method, 333) }.should.raise(TypeError) - b = StringScanner.new("") - -> { b.send(@method, (256 * 3 + 64)) }.should.raise(TypeError) - -> { b.send(@method, -200) }.should.raise(TypeError) - end - - it "doesn't call to_int on the argument" do - x = mock('x') - x.should_not_receive(:to_int) - - -> { StringScanner.new("").send(@method, x) }.should.raise(TypeError) - end -end diff --git a/library/stringscanner/shared/pos.rb b/library/stringscanner/shared/pos.rb deleted file mode 100644 index 91f80fdf08..0000000000 --- a/library/stringscanner/shared/pos.rb +++ /dev/null @@ -1,59 +0,0 @@ -describe :strscan_pos, shared: true do - before :each do - @s = StringScanner.new("This is a test") - end - - it "returns the position of the scan pointer" do - @s.send(@method).should == 0 - @s.scan_until(/This is/) - @s.send(@method).should == 7 - @s.get_byte - @s.send(@method).should == 8 - @s.terminate - @s.send(@method).should == 14 - end - - it "returns 0 in the reset position" do - @s.reset - @s.send(@method).should == 0 - end - - it "returns the length of the string in the terminate position" do - @s.terminate - @s.send(@method).should == @s.string.length - end - - it "is not multi-byte character sensitive" do - s = StringScanner.new("abcädeföghi") - - s.scan_until(/ö/) - s.pos.should == 10 - end -end - -describe :strscan_pos_set, shared: true do - before :each do - @s = StringScanner.new("This is a test") - end - - it "modify the scan pointer" do - @s.send(@method, 5) - @s.rest.should == "is a test" - end - - it "positions from the end if the argument is negative" do - @s.send(@method, -2) - @s.rest.should == "st" - @s.pos.should == 12 - end - - it "raises a RangeError if position too far backward" do - -> { - @s.send(@method, -20) - }.should.raise(RangeError) - end - - it "raises a RangeError when the passed argument is out of range" do - -> { @s.send(@method, 20) }.should.raise(RangeError) - end -end From 7731cc00a8fa01f5a59c972b86782239ac744153 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:44:02 +0200 Subject: [PATCH 14/18] Move Syslog to rely less on shared examples --- library/syslog/open_spec.rb | 38 +++++++++++++++++++++++++++++-- library/syslog/reopen_spec.rb | 5 +++-- library/syslog/shared/reopen.rb | 40 --------------------------------- 3 files changed, 39 insertions(+), 44 deletions(-) delete mode 100644 library/syslog/shared/reopen.rb diff --git a/library/syslog/open_spec.rb b/library/syslog/open_spec.rb index 73e3780d78..3aceea007d 100644 --- a/library/syslog/open_spec.rb +++ b/library/syslog/open_spec.rb @@ -1,7 +1,6 @@ require_relative '../../spec_helper' platform_is_not :windows do - require_relative 'shared/reopen' require 'syslog' describe "Syslog.open" do @@ -87,6 +86,41 @@ end describe "Syslog.open!" do - it_behaves_like :syslog_reopen, :open! + before :each do + Syslog.opened?.should == false + end + + after :each do + Syslog.opened?.should == false + end + + it "reopens the log" do + Syslog.open + -> { Syslog.open! }.should_not.raise + Syslog.opened?.should == true + Syslog.close + end + + it "fails with RuntimeError if the log is closed" do + -> { Syslog.open! }.should.raise(RuntimeError) + end + + it "receives the same parameters as Syslog.open" do + Syslog.open + Syslog.open!("rubyspec", 3, 8) do |s| + s.should == Syslog + s.ident.should == "rubyspec" + s.options.should == 3 + s.facility.should == Syslog::LOG_USER + s.opened?.should == true + end + Syslog.opened?.should == false + end + + it "returns the module" do + Syslog.open + Syslog.open!.should == Syslog + Syslog.close + end end end diff --git a/library/syslog/reopen_spec.rb b/library/syslog/reopen_spec.rb index a78529fa1f..ef32d13a87 100644 --- a/library/syslog/reopen_spec.rb +++ b/library/syslog/reopen_spec.rb @@ -1,10 +1,11 @@ require_relative '../../spec_helper' platform_is_not :windows do - require_relative 'shared/reopen' require 'syslog' describe "Syslog.reopen" do - it_behaves_like :syslog_reopen, :reopen + it "is an alias of Syslog.open!" do + Syslog.method(:reopen).should == Syslog.method(:open!) + end end end diff --git a/library/syslog/shared/reopen.rb b/library/syslog/shared/reopen.rb deleted file mode 100644 index f04408e807..0000000000 --- a/library/syslog/shared/reopen.rb +++ /dev/null @@ -1,40 +0,0 @@ -describe :syslog_reopen, shared: true do - platform_is_not :windows do - before :each do - Syslog.opened?.should == false - end - - after :each do - Syslog.opened?.should == false - end - - it "reopens the log" do - Syslog.open - -> { Syslog.send(@method)}.should_not.raise - Syslog.opened?.should == true - Syslog.close - end - - it "fails with RuntimeError if the log is closed" do - -> { Syslog.send(@method)}.should.raise(RuntimeError) - end - - it "receives the same parameters as Syslog.open" do - Syslog.open - Syslog.send(@method, "rubyspec", 3, 8) do |s| - s.should == Syslog - s.ident.should == "rubyspec" - s.options.should == 3 - s.facility.should == Syslog::LOG_USER - s.opened?.should == true - end - Syslog.opened?.should == false - end - - it "returns the module" do - Syslog.open - Syslog.send(@method).should == Syslog - Syslog.close - end - end -end From 42d57685d648ac3b8b01ff52a80a5f1cb53b924f Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:47:05 +0200 Subject: [PATCH 15/18] Move Tempfile to rely less on shared examples --- library/tempfile/delete_spec.rb | 12 ++++++++++-- library/tempfile/length_spec.rb | 5 +++-- library/tempfile/shared/length.rb | 21 --------------------- library/tempfile/shared/unlink.rb | 12 ------------ library/tempfile/size_spec.rb | 21 +++++++++++++++++++-- library/tempfile/unlink_spec.rb | 5 +++-- 6 files changed, 35 insertions(+), 41 deletions(-) delete mode 100644 library/tempfile/shared/length.rb delete mode 100644 library/tempfile/shared/unlink.rb diff --git a/library/tempfile/delete_spec.rb b/library/tempfile/delete_spec.rb index 0332b44dde..b126ceae6a 100644 --- a/library/tempfile/delete_spec.rb +++ b/library/tempfile/delete_spec.rb @@ -1,7 +1,15 @@ require_relative '../../spec_helper' -require_relative 'shared/unlink' require 'tempfile' describe "Tempfile#delete" do - it_behaves_like :tempfile_unlink, :delete + before :each do + @tempfile = Tempfile.new("specs") + end + + it "unlinks self" do + @tempfile.close + path = @tempfile.path + @tempfile.delete + File.should_not.exist?(path) + end end diff --git a/library/tempfile/length_spec.rb b/library/tempfile/length_spec.rb index bc622b9a70..924c12942b 100644 --- a/library/tempfile/length_spec.rb +++ b/library/tempfile/length_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/length' require 'tempfile' describe "Tempfile#length" do - it_behaves_like :tempfile_length, :length + it "is an alias of Tempfile#size" do + Tempfile.instance_method(:length).should == Tempfile.instance_method(:size) + end end diff --git a/library/tempfile/shared/length.rb b/library/tempfile/shared/length.rb deleted file mode 100644 index 1a89ff7b4d..0000000000 --- a/library/tempfile/shared/length.rb +++ /dev/null @@ -1,21 +0,0 @@ -describe :tempfile_length, shared: true do - before :each do - @tempfile = Tempfile.new("specs") - end - - after :each do - @tempfile.close! - end - - it "returns the size of self" do - @tempfile.send(@method).should.eql?(0) - @tempfile.print("Test!") - @tempfile.send(@method).should.eql?(5) - end - - it "returns the size of self even if self is closed" do - @tempfile.print("Test!") - @tempfile.close - @tempfile.send(@method).should.eql?(5) - end -end diff --git a/library/tempfile/shared/unlink.rb b/library/tempfile/shared/unlink.rb deleted file mode 100644 index e821228d70..0000000000 --- a/library/tempfile/shared/unlink.rb +++ /dev/null @@ -1,12 +0,0 @@ -describe :tempfile_unlink, shared: true do - before :each do - @tempfile = Tempfile.new("specs") - end - - it "unlinks self" do - @tempfile.close - path = @tempfile.path - @tempfile.send(@method) - File.should_not.exist?(path) - end -end diff --git a/library/tempfile/size_spec.rb b/library/tempfile/size_spec.rb index f4824601c7..5a7edf8e4b 100644 --- a/library/tempfile/size_spec.rb +++ b/library/tempfile/size_spec.rb @@ -1,7 +1,24 @@ require_relative '../../spec_helper' -require_relative 'shared/length' require 'tempfile' describe "Tempfile#size" do - it_behaves_like :tempfile_length, :size + before :each do + @tempfile = Tempfile.new("specs") + end + + after :each do + @tempfile.close! + end + + it "returns the size of self" do + @tempfile.size.should.eql?(0) + @tempfile.print("Test!") + @tempfile.size.should.eql?(5) + end + + it "returns the size of self even if self is closed" do + @tempfile.print("Test!") + @tempfile.close + @tempfile.size.should.eql?(5) + end end diff --git a/library/tempfile/unlink_spec.rb b/library/tempfile/unlink_spec.rb index eac7df8472..c03fc34a54 100644 --- a/library/tempfile/unlink_spec.rb +++ b/library/tempfile/unlink_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/unlink' require 'tempfile' describe "Tempfile#unlink" do - it_behaves_like :tempfile_unlink, :unlink + it "is an alias of Tempfile#delete" do + Tempfile.instance_method(:unlink).should == Tempfile.instance_method(:delete) + end end From 6f1e5e8c6d3caa448df0c430e232f0f57e30743b Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:54:05 +0200 Subject: [PATCH 16/18] Move Time to rely less on shared examples --- library/time/iso8601_spec.rb | 5 ++- library/time/rfc2822_spec.rb | 65 +++++++++++++++++++++++++++++++- library/time/rfc822_spec.rb | 5 ++- library/time/shared/rfc2822.rb | 65 -------------------------------- library/time/shared/xmlschema.rb | 53 -------------------------- library/time/xmlschema_spec.rb | 53 +++++++++++++++++++++++++- 6 files changed, 120 insertions(+), 126 deletions(-) delete mode 100644 library/time/shared/rfc2822.rb delete mode 100644 library/time/shared/xmlschema.rb diff --git a/library/time/iso8601_spec.rb b/library/time/iso8601_spec.rb index ab35ab25d6..d78de76792 100644 --- a/library/time/iso8601_spec.rb +++ b/library/time/iso8601_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/xmlschema' require 'time' describe "Time.iso8601" do - it_behaves_like :time_library_xmlschema, :iso8601 + it "is an alias of Time.xmlschema" do + Time.method(:iso8601).should == Time.method(:xmlschema) + end end diff --git a/library/time/rfc2822_spec.rb b/library/time/rfc2822_spec.rb index 7fc5e9a64b..14824e2396 100644 --- a/library/time/rfc2822_spec.rb +++ b/library/time/rfc2822_spec.rb @@ -1,7 +1,68 @@ require_relative '../../spec_helper' -require_relative 'shared/rfc2822' require 'time' describe "Time.rfc2822" do - it_behaves_like :time_rfc2822, :rfc2822 + it "parses RFC-822 strings" do + t1 = (Time.utc(1976, 8, 26, 14, 30) + 4 * 3600) + t2 = Time.rfc2822("26 Aug 76 14:30 EDT") + t1.should == t2 + + t3 = Time.utc(1976, 8, 27, 9, 32) + 7 * 3600 + t4 = Time.rfc2822("27 Aug 76 09:32 PDT") + t3.should == t4 + end + + it "parses RFC-2822 strings" do + t1 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600 + t2 = Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") + t1.should == t2 + + t3 = Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600 + t4 = Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200") + t3.should == t4 + + t5 = Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600 + t6 = Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600") + t5.should == t6 + + t7 = Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600 + t8 = Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600") + t7.should == t8 + + t9 = Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600 + t10 = Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800") + t9.should == t10 + + begin + Time.at(-1) + rescue ArgumentError + # ignore + else + t11 = Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60 + t12 = Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330") + t11.should == t12 + + t13 = Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60 + t14 = Time.rfc2822(" Thu, + 13 + Feb + 1969 + 23:32 + -0330 (Newfoundland Time)") + t13.should == t14 + end + + t15 = Time.utc(1997, 11, 21, 9, 55, 6) + t16 = Time.rfc2822("21 Nov 97 09:55:06 GMT") + t15.should == t16 + + t17 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600 + t18 = Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600") + t17.should == t18 + + -> { + # inner comment is not supported. + Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600") + }.should.raise(ArgumentError) + end end diff --git a/library/time/rfc822_spec.rb b/library/time/rfc822_spec.rb index da77e6ee77..e32e9becae 100644 --- a/library/time/rfc822_spec.rb +++ b/library/time/rfc822_spec.rb @@ -1,7 +1,8 @@ require_relative '../../spec_helper' -require_relative 'shared/rfc2822' require 'time' describe "Time.rfc822" do - it_behaves_like :time_rfc2822, :rfc822 + it "is an alias of Time.rfc2822" do + Time.method(:rfc822).should == Time.method(:rfc2822) + end end diff --git a/library/time/shared/rfc2822.rb b/library/time/shared/rfc2822.rb deleted file mode 100644 index 49ef76db47..0000000000 --- a/library/time/shared/rfc2822.rb +++ /dev/null @@ -1,65 +0,0 @@ -describe :time_rfc2822, shared: true do - it "parses RFC-822 strings" do - t1 = (Time.utc(1976, 8, 26, 14, 30) + 4 * 3600) - t2 = Time.send(@method, "26 Aug 76 14:30 EDT") - t1.should == t2 - - t3 = Time.utc(1976, 8, 27, 9, 32) + 7 * 3600 - t4 = Time.send(@method, "27 Aug 76 09:32 PDT") - t3.should == t4 - end - - it "parses RFC-2822 strings" do - t1 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600 - t2 = Time.send(@method, "Fri, 21 Nov 1997 09:55:06 -0600") - t1.should == t2 - - t3 = Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600 - t4 = Time.send(@method, "Tue, 1 Jul 2003 10:52:37 +0200") - t3.should == t4 - - t5 = Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600 - t6 = Time.send(@method, "Fri, 21 Nov 1997 10:01:10 -0600") - t5.should == t6 - - t7 = Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600 - t8 = Time.send(@method, "Fri, 21 Nov 1997 11:00:00 -0600") - t7.should == t8 - - t9 = Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600 - t10 = Time.send(@method, "Mon, 24 Nov 1997 14:22:01 -0800") - t9.should == t10 - - begin - Time.at(-1) - rescue ArgumentError - # ignore - else - t11 = Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60 - t12 = Time.send(@method, "Thu, 13 Feb 1969 23:32:54 -0330") - t11.should == t12 - - t13 = Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60 - t14 = Time.send(@method, " Thu, - 13 - Feb - 1969 - 23:32 - -0330 (Newfoundland Time)") - t13.should == t14 - end - - t15 = Time.utc(1997, 11, 21, 9, 55, 6) - t16 = Time.send(@method, "21 Nov 97 09:55:06 GMT") - t15.should == t16 - - t17 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600 - t18 = Time.send(@method, "Fri, 21 Nov 1997 09 : 55 : 06 -0600") - t17.should == t18 - - -> { - # inner comment is not supported. - Time.send(@method, "Fri, 21 Nov 1997 09(comment): 55 : 06 -0600") - }.should.raise(ArgumentError) - end -end diff --git a/library/time/shared/xmlschema.rb b/library/time/shared/xmlschema.rb deleted file mode 100644 index 0002886ca5..0000000000 --- a/library/time/shared/xmlschema.rb +++ /dev/null @@ -1,53 +0,0 @@ -describe :time_library_xmlschema, shared: true do - it "parses ISO-8601 strings" do - t = Time.utc(1985, 4, 12, 23, 20, 50, 520000) - s = "1985-04-12T23:20:50.52Z" - t.should == Time.send(@method, s) - #s.should == t.send(@method, 2) - - t = Time.utc(1996, 12, 20, 0, 39, 57) - s = "1996-12-19T16:39:57-08:00" - t.should == Time.send(@method, s) - # There is no way to generate time string with arbitrary timezone. - s = "1996-12-20T00:39:57Z" - t.should == Time.send(@method, s) - #assert_equal(s, t.send(@method)) - - t = Time.utc(1990, 12, 31, 23, 59, 60) - s = "1990-12-31T23:59:60Z" - t.should == Time.send(@method, s) - # leap second is representable only if timezone file has it. - s = "1990-12-31T15:59:60-08:00" - t.should == Time.send(@method, s) - - begin - Time.at(-1) - rescue ArgumentError - # ignore - else - t = Time.utc(1937, 1, 1, 11, 40, 27, 870000) - s = "1937-01-01T12:00:27.87+00:20" - t.should == Time.send(@method, s) - end - - # more - - # (Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600).should == Time.send(@method, "1999-05-31T13:20:00-05:00") - # (Time.local(2000, 1, 20, 12, 0, 0)).should == Time.send(@method, "2000-01-20T12:00:00") - # (Time.utc(2000, 1, 20, 12, 0, 0)).should == Time.send(@method, "2000-01-20T12:00:00Z") - # (Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600).should == Time.send(@method, "2000-01-20T12:00:00+12:00") - # (Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600).should == Time.send(@method, "2000-01-20T12:00:00-13:00") - # (Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600).should == Time.send(@method, "2000-03-04T23:00:00+03:00") - # (Time.utc(2000, 3, 4, 20, 0, 0)).should == Time.send(@method, "2000-03-04T20:00:00Z") - # (Time.local(2000, 1, 15, 0, 0, 0)).should == Time.send(@method, "2000-01-15T00:00:00") - # (Time.local(2000, 2, 15, 0, 0, 0)).should == Time.send(@method, "2000-02-15T00:00:00") - # (Time.local(2000, 1, 15, 12, 0, 0)).should == Time.send(@method, "2000-01-15T12:00:00") - # (Time.utc(2000, 1, 16, 12, 0, 0)).should == Time.send(@method, "2000-01-16T12:00:00Z") - # (Time.local(2000, 1, 1, 12, 0, 0)).should == Time.send(@method, "2000-01-01T12:00:00") - # (Time.utc(1999, 12, 31, 23, 0, 0)).should == Time.send(@method, "1999-12-31T23:00:00Z") - # (Time.local(2000, 1, 16, 12, 0, 0)).should == Time.send(@method, "2000-01-16T12:00:00") - # (Time.local(2000, 1, 16, 0, 0, 0)).should == Time.send(@method, "2000-01-16T00:00:00") - # (Time.utc(2000, 1, 12, 12, 13, 14)).should == Time.send(@method, "2000-01-12T12:13:14Z") - # (Time.utc(2001, 4, 17, 19, 23, 17, 300000)).should == Time.send(@method, "2001-04-17T19:23:17.3Z") - end -end diff --git a/library/time/xmlschema_spec.rb b/library/time/xmlschema_spec.rb index ff3c864a02..1f7d63979a 100644 --- a/library/time/xmlschema_spec.rb +++ b/library/time/xmlschema_spec.rb @@ -1,7 +1,56 @@ require_relative '../../spec_helper' -require_relative 'shared/xmlschema' require 'time' describe "Time.xmlschema" do - it_behaves_like :time_library_xmlschema, :xmlschema + it "parses ISO-8601 strings" do + t = Time.utc(1985, 4, 12, 23, 20, 50, 520000) + s = "1985-04-12T23:20:50.52Z" + t.should == Time.xmlschema(s) + #s.should == t.xmlschema(2) + + t = Time.utc(1996, 12, 20, 0, 39, 57) + s = "1996-12-19T16:39:57-08:00" + t.should == Time.xmlschema(s) + # There is no way to generate time string with arbitrary timezone. + s = "1996-12-20T00:39:57Z" + t.should == Time.xmlschema(s) + #assert_equal(s, t.xmlschema) + + t = Time.utc(1990, 12, 31, 23, 59, 60) + s = "1990-12-31T23:59:60Z" + t.should == Time.xmlschema(s) + # leap second is representable only if timezone file has it. + s = "1990-12-31T15:59:60-08:00" + t.should == Time.xmlschema(s) + + begin + Time.at(-1) + rescue ArgumentError + # ignore + else + t = Time.utc(1937, 1, 1, 11, 40, 27, 870000) + s = "1937-01-01T12:00:27.87+00:20" + t.should == Time.xmlschema(s) + end + + # more + + # (Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600).should == Time.xmlschema("1999-05-31T13:20:00-05:00") + # (Time.local(2000, 1, 20, 12, 0, 0)).should == Time.xmlschema("2000-01-20T12:00:00") + # (Time.utc(2000, 1, 20, 12, 0, 0)).should == Time.xmlschema("2000-01-20T12:00:00Z") + # (Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600).should == Time.xmlschema("2000-01-20T12:00:00+12:00") + # (Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600).should == Time.xmlschema("2000-01-20T12:00:00-13:00") + # (Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600).should == Time.xmlschema("2000-03-04T23:00:00+03:00") + # (Time.utc(2000, 3, 4, 20, 0, 0)).should == Time.xmlschema("2000-03-04T20:00:00Z") + # (Time.local(2000, 1, 15, 0, 0, 0)).should == Time.xmlschema("2000-01-15T00:00:00") + # (Time.local(2000, 2, 15, 0, 0, 0)).should == Time.xmlschema("2000-02-15T00:00:00") + # (Time.local(2000, 1, 15, 12, 0, 0)).should == Time.xmlschema("2000-01-15T12:00:00") + # (Time.utc(2000, 1, 16, 12, 0, 0)).should == Time.xmlschema("2000-01-16T12:00:00Z") + # (Time.local(2000, 1, 1, 12, 0, 0)).should == Time.xmlschema("2000-01-01T12:00:00") + # (Time.utc(1999, 12, 31, 23, 0, 0)).should == Time.xmlschema("1999-12-31T23:00:00Z") + # (Time.local(2000, 1, 16, 12, 0, 0)).should == Time.xmlschema("2000-01-16T12:00:00") + # (Time.local(2000, 1, 16, 0, 0, 0)).should == Time.xmlschema("2000-01-16T00:00:00") + # (Time.utc(2000, 1, 12, 12, 13, 14)).should == Time.xmlschema("2000-01-12T12:13:14Z") + # (Time.utc(2001, 4, 17, 19, 23, 17, 300000)).should == Time.xmlschema("2001-04-17T19:23:17.3Z") + end end From 6831440316cce10d46684999334be832b9787174 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 19:02:35 +0200 Subject: [PATCH 17/18] Move URI to rely less on shared examples --- library/uri/parser/extract_spec.rb | 87 +++++++++++- library/uri/parser/join_spec.rb | 59 +++++++- library/uri/parser/parse_spec.rb | 210 ++++++++++++++++++++++++++++- library/uri/shared/extract.rb | 83 ------------ library/uri/shared/join.rb | 56 -------- library/uri/shared/parse.rb | 206 ---------------------------- 6 files changed, 350 insertions(+), 351 deletions(-) delete mode 100644 library/uri/shared/extract.rb delete mode 100644 library/uri/shared/join.rb delete mode 100644 library/uri/shared/parse.rb diff --git a/library/uri/parser/extract_spec.rb b/library/uri/parser/extract_spec.rb index 20d4565b08..f5ecd6ec8e 100644 --- a/library/uri/parser/extract_spec.rb +++ b/library/uri/parser/extract_spec.rb @@ -1,7 +1,90 @@ require_relative '../../../spec_helper' -require_relative '../shared/extract' require 'uri' describe "URI::Parser#extract" do - it_behaves_like :uri_extract, :extract, URI::Parser.new + before :all do + @parser = URI::Parser.new + end + + it "behaves according to its documentation" do + @parser.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.").should == ["http://foo.example.org/bla", "mailto:test@example.com"] + end + + it "treats contiguous URIs as a single URI" do + @parser.extract('http://example.jphttp://example.jp').should == ['http://example.jphttp://example.jp'] + end + + it "treats pretty much anything with a colon as a URI" do + @parser.extract('From: XXX [mailto:xxx@xxx.xxx.xxx]').should == ['From:', 'mailto:xxx@xxx.xxx.xxx]'] + end + + it "wraps a URI string in an array" do + @parser.extract("http://github.com/brixen/rubyspec/tree/master").should == ["http://github.com/brixen/rubyspec/tree/master"] + end + + it "pulls a variety of protocol URIs from a string" do + @parser.extract("this is a string, it has http://rubini.us/ in it").should == ["http://rubini.us/"] + @parser.extract("mailto:spambait@example.com").should == ["mailto:spambait@example.com"] + @parser.extract("ftp://ruby-lang.org/").should == ["ftp://ruby-lang.org/"] + @parser.extract("https://mail.google.com").should == ["https://mail.google.com"] + @parser.extract("anything://example.com/").should == ["anything://example.com/"] + end + + it "pulls all URIs within a string in order into an array when a block is not given" do + @parser.extract("1.3. Example URI + + The following examples illustrate URI that are in common use. + + ftp://ftp.is.co.za/rfc/rfc1808.txt + -- ftp scheme for File Transfer Protocol services + + gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles + -- gopher scheme for Gopher and Gopher+ Protocol services + + http://www.math.uio.no/faq/compression-faq/part1.html + -- http scheme for Hypertext Transfer Protocol services + + mailto:mduerst@ifi.unizh.ch + -- mailto scheme for electronic mail addresses + + news:comp.infosystems.www.servers.unix + -- news scheme for USENET news groups and articles + + telnet://melvyl.ucop.edu/ + -- telnet scheme for interactive services via the TELNET Protocol + ").should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch","news:comp.infosystems.www.servers.unix","telnet://melvyl.ucop.edu/"] + end + + it "yields each URI in the given string in order to a block, if given, and returns nil" do + results = ["http://foo.example.org/bla", "mailto:test@example.com"] + @parser.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.") {|uri| + uri.should == results.shift + }.should == nil + results.should == [] + end + + it "allows the user to specify a list of acceptable protocols of URIs to scan for" do + @parser.extract("1.3. Example URI + + The following examples illustrate URI that are in common use. + + ftp://ftp.is.co.za/rfc/rfc1808.txt + -- ftp scheme for File Transfer Protocol services + + gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles + -- gopher scheme for Gopher and Gopher+ Protocol services + + http://www.math.uio.no/faq/compression-faq/part1.html + -- http scheme for Hypertext Transfer Protocol services + + mailto:mduerst@ifi.unizh.ch + -- mailto scheme for electronic mail addresses + + news:comp.infosystems.www.servers.unix + -- news scheme for USENET news groups and articles + + telnet://melvyl.ucop.edu/ + -- telnet scheme for interactive services via the TELNET Protocol + ", ["http","ftp","mailto"]).should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch"] + end end diff --git a/library/uri/parser/join_spec.rb b/library/uri/parser/join_spec.rb index 0c9230be76..0fb29cf00a 100644 --- a/library/uri/parser/join_spec.rb +++ b/library/uri/parser/join_spec.rb @@ -1,7 +1,62 @@ require_relative '../../../spec_helper' -require_relative '../shared/join' require 'uri' describe "URI::Parser#join" do - it_behaves_like :uri_join, :join, URI::Parser.new + before :all do + @parser = URI::Parser.new + end + + it "returns a URI object of the concatenation of a protocol and domain, and a path" do + @parser.join("http://localhost/","main.rbx").should == URI.parse("http://localhost/main.rbx") + end + + it "accepts URI objects" do + @parser.join(URI("http://localhost/"),"main.rbx").should == URI.parse("http://localhost/main.rbx") + @parser.join("http://localhost/",URI("main.rbx")).should == URI.parse("http://localhost/main.rbx") + @parser.join(URI("http://localhost/"),URI("main.rbx")).should == URI.parse("http://localhost/main.rbx") + end + + it "accepts string-like arguments with to_str" do + str = mock('string-like') + str.should_receive(:to_str).and_return("http://ruby-lang.org") + str2 = mock('string-like also') + str2.should_receive(:to_str).and_return("foo/bar") + @parser.join(str, str2).should == URI.parse("http://ruby-lang.org/foo/bar") + end + + it "raises an error if given no argument" do + -> { + @parser.join + }.should.raise(ArgumentError) + end + + it "doesn't create redundant '/'s" do + @parser.join("http://localhost/", "/main.rbx").should == URI.parse("http://localhost/main.rbx") + end + + it "discards arguments given before an absolute uri" do + @parser.join("http://localhost/a/b/c/d", "http://ruby-lang.com/foo", "bar").should == URI.parse("http://ruby-lang.com/bar") + end + + it "resolves .. in paths" do + @parser.join("http://localhost/a/b/c/d", "../../e/f", "g/h/../i").to_s.should == "http://localhost/a/e/g/i" + end end + +# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar')) +# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar')) +# assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/')) +# +# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz')) +# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz')) +# assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/')) +# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz')) +# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge')) +# +# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz')) +# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) +# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) +# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) +# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) +# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) +# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) diff --git a/library/uri/parser/parse_spec.rb b/library/uri/parser/parse_spec.rb index df126eab6d..0e6a06ebe5 100644 --- a/library/uri/parser/parse_spec.rb +++ b/library/uri/parser/parse_spec.rb @@ -1,7 +1,213 @@ require_relative '../../../spec_helper' require_relative '../fixtures/classes' -require_relative '../shared/parse' describe "URI::Parser#parse" do - it_behaves_like :uri_parse, :parse, URI::Parser.new + before :all do + @parser = URI::Parser.new + end + + it "returns a URI::HTTP object when parsing an HTTP URI" do + @parser.parse("http://www.example.com/").should.is_a?(URI::HTTP) + end + + it "populates the components of a parsed URI::HTTP, setting the port to 80 by default" do + # general case + URISpec.components(@parser.parse("http://user:pass@example.com/path/?query=val&q2=val2#fragment")).should == { + scheme: "http", + userinfo: "user:pass", + host: "example.com", + port: 80, + path: "/path/", + query: "query=val&q2=val2", + fragment: "fragment" + } + + # multiple paths + URISpec.components(@parser.parse("http://a/b/c/d;p?q")).should == { + scheme: "http", + userinfo: nil, + host: "a", + port: 80, + path: "/b/c/d;p", + query: "q", + fragment: nil + } + + # multi-level domain + URISpec.components(@parser.parse('http://www.math.uio.no/faq/compression-faq/part1.html')).should == { + scheme: "http", + userinfo: nil, + host: "www.math.uio.no", + port: 80, + path: "/faq/compression-faq/part1.html", + query: nil, + fragment: nil + } + end + + it "parses out the port number of a URI, when given" do + @parser.parse("http://example.com:8080/").port.should == 8080 + end + + it "returns a URI::HTTPS object when parsing an HTTPS URI" do + @parser.parse("https://important-intern-net.net").should.is_a?(URI::HTTPS) + end + + it "sets the port of a parsed https URI to 443 by default" do + @parser.parse("https://example.com/").port.should == 443 + end + + it "populates the components of a parsed URI::FTP object" do + # generic, empty password. + url = @parser.parse("ftp://anonymous@ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.bz2;type=i") + url.should.is_a?(URI::FTP) + URISpec.components(url).should == { + scheme: "ftp", + userinfo: "anonymous", + host: "ruby-lang.org", + port: 21, + path: "pub/ruby/1.8/ruby-1.8.6.tar.bz2", + typecode: "i" + } + + # multidomain, no user or password + url = @parser.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt') + url.should.is_a?(URI::FTP) + URISpec.components(url).should == { + scheme: "ftp", + userinfo: nil, + host: "ftp.is.co.za", + port: 21, + path: "rfc/rfc1808.txt", + typecode: nil + } + + # empty user + url = @parser.parse('ftp://:pass@localhost/') + url.should.is_a?(URI::FTP) + URISpec.components(url).should == { + scheme: "ftp", + userinfo: ":pass", + host: "localhost", + port: 21, + path: "", + typecode: nil + } + url.password.should == "pass" + end + + it "returns a URI::LDAP object when parsing an LDAP URI" do + #taken from http://www.faqs.org/rfcs/rfc2255.html 'cause I don't really know what an LDAP url looks like + ldap_uris = %w{ ldap:///o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen) ldap://ldap.itd.umich.edu/c=GB?objectClass?one ldap://ldap.question.com/o=Question%3f,c=US?mail ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04) ldap:///??sub??bindname=cn=Manager%2co=Foo ldap:///??sub??!bindname=cn=Manager%2co=Foo } + ldap_uris.each do |ldap_uri| + @parser.parse(ldap_uri).should.is_a?(URI::LDAP) + end + end + + it "populates the components of a parsed URI::LDAP object" do + URISpec.components(@parser.parse("ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress?scope?filter?extensions")).should == { + scheme: "ldap", + host: "ldap.itd.umich.edu", + port: 389, + dn: "o=University%20of%20Michigan,c=US", + attributes: "postalAddress", + scope: "scope", + filter: "filter", + extensions: "extensions" + } + end + + it "returns a URI::MailTo object when passed a mailto URI" do + @parser.parse("mailto:spam@mailinator.com").should.is_a?(URI::MailTo) + end + + it "populates the components of a parsed URI::MailTo object" do + URISpec.components(@parser.parse("mailto:spam@mailinator.com?subject=Discounts%20On%20Imported%20methods!!!&body=Exciting%20offer")).should == { + scheme: "mailto", + to: "spam@mailinator.com", + headers: [["subject","Discounts%20On%20Imported%20methods!!!"], + ["body", "Exciting%20offer"]] + } + end + + # TODO + # Test registry + it "does its best to extract components from URI::Generic objects" do + # generic + URISpec.components(URI("scheme://userinfo@host/path?query#fragment")).should == { + scheme: "scheme", + userinfo: "userinfo", + host: "host", + port: nil, + path: "/path", + query: "query", + fragment: "fragment", + registry: nil, + opaque: nil + } + + # gopher + gopher = @parser.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles') + gopher.should.is_a?(URI::Generic) + + URISpec.components(gopher).should == { + scheme: "gopher", + userinfo: nil, + host: "spinaltap.micro.umn.edu", + port: nil, + path: "/00/Weather/California/Los%20Angeles", + query: nil, + fragment: nil, + registry: nil, + opaque: nil + } + + # news + news = @parser.parse('news:comp.infosystems.www.servers.unix') + news.should.is_a?(URI::Generic) + URISpec.components(news).should == { + scheme: "news", + userinfo: nil, + host: nil, + port: nil, + path: nil, + query: nil, + fragment: nil, + registry: nil, + opaque: "comp.infosystems.www.servers.unix" + } + + # telnet + telnet = @parser.parse('telnet://melvyl.ucop.edu/') + telnet.should.is_a?(URI::Generic) + URISpec.components(telnet).should == { + scheme: "telnet", + userinfo: nil, + host: "melvyl.ucop.edu", + port: nil, + path: "/", + query: nil, + fragment: nil, + registry: nil, + opaque: nil + } + + # files + file_l = @parser.parse('file:///foo/bar.txt') + file_l.should.is_a?(URI::Generic) + file = @parser.parse('file:/foo/bar.txt') + file.should.is_a?(URI::Generic) + end + + if URI::DEFAULT_PARSER == URI::RFC2396_Parser + it "raises errors on malformed URIs" do + -> { @parser.parse('http://a_b:80/') }.should.raise(URI::InvalidURIError) + -> { @parser.parse('http://a_b/') }.should.raise(URI::InvalidURIError) + end + elsif URI::DEFAULT_PARSER == URI::RFC3986_Parser + it "does not raise errors on URIs contained underscore" do + -> { @parser.parse('http://a_b:80/') }.should_not.raise(URI::InvalidURIError) + -> { @parser.parse('http://a_b/') }.should_not.raise(URI::InvalidURIError) + end + end end diff --git a/library/uri/shared/extract.rb b/library/uri/shared/extract.rb deleted file mode 100644 index efe60ae4b9..0000000000 --- a/library/uri/shared/extract.rb +++ /dev/null @@ -1,83 +0,0 @@ -describe :uri_extract, shared: true do - it "behaves according to its documentation" do - @object.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.").should == ["http://foo.example.org/bla", "mailto:test@example.com"] - end - - it "treats contiguous URIs as a single URI" do - @object.extract('http://example.jphttp://example.jp').should == ['http://example.jphttp://example.jp'] - end - - it "treats pretty much anything with a colon as a URI" do - @object.extract('From: XXX [mailto:xxx@xxx.xxx.xxx]').should == ['From:', 'mailto:xxx@xxx.xxx.xxx]'] - end - - it "wraps a URI string in an array" do - @object.extract("http://github.com/brixen/rubyspec/tree/master").should == ["http://github.com/brixen/rubyspec/tree/master"] - end - - it "pulls a variety of protocol URIs from a string" do - @object.extract("this is a string, it has http://rubini.us/ in it").should == ["http://rubini.us/"] - @object.extract("mailto:spambait@example.com").should == ["mailto:spambait@example.com"] - @object.extract("ftp://ruby-lang.org/").should == ["ftp://ruby-lang.org/"] - @object.extract("https://mail.google.com").should == ["https://mail.google.com"] - @object.extract("anything://example.com/").should == ["anything://example.com/"] - end - - it "pulls all URIs within a string in order into an array when a block is not given" do - @object.extract("1.3. Example URI - - The following examples illustrate URI that are in common use. - - ftp://ftp.is.co.za/rfc/rfc1808.txt - -- ftp scheme for File Transfer Protocol services - - gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles - -- gopher scheme for Gopher and Gopher+ Protocol services - - http://www.math.uio.no/faq/compression-faq/part1.html - -- http scheme for Hypertext Transfer Protocol services - - mailto:mduerst@ifi.unizh.ch - -- mailto scheme for electronic mail addresses - - news:comp.infosystems.www.servers.unix - -- news scheme for USENET news groups and articles - - telnet://melvyl.ucop.edu/ - -- telnet scheme for interactive services via the TELNET Protocol - ").should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch","news:comp.infosystems.www.servers.unix","telnet://melvyl.ucop.edu/"] - end - - it "yields each URI in the given string in order to a block, if given, and returns nil" do - results = ["http://foo.example.org/bla", "mailto:test@example.com"] - @object.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.") {|uri| - uri.should == results.shift - }.should == nil - results.should == [] - end - - it "allows the user to specify a list of acceptable protocols of URIs to scan for" do - @object.extract("1.3. Example URI - - The following examples illustrate URI that are in common use. - - ftp://ftp.is.co.za/rfc/rfc1808.txt - -- ftp scheme for File Transfer Protocol services - - gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles - -- gopher scheme for Gopher and Gopher+ Protocol services - - http://www.math.uio.no/faq/compression-faq/part1.html - -- http scheme for Hypertext Transfer Protocol services - - mailto:mduerst@ifi.unizh.ch - -- mailto scheme for electronic mail addresses - - news:comp.infosystems.www.servers.unix - -- news scheme for USENET news groups and articles - - telnet://melvyl.ucop.edu/ - -- telnet scheme for interactive services via the TELNET Protocol - ", ["http","ftp","mailto"]).should == ["ftp://ftp.is.co.za/rfc/rfc1808.txt","http://www.math.uio.no/faq/compression-faq/part1.html","mailto:mduerst@ifi.unizh.ch"] - end -end diff --git a/library/uri/shared/join.rb b/library/uri/shared/join.rb deleted file mode 100644 index b1f5f1c72b..0000000000 --- a/library/uri/shared/join.rb +++ /dev/null @@ -1,56 +0,0 @@ -describe :uri_join, shared: true do - it "returns a URI object of the concatenation of a protocol and domain, and a path" do - @object.join("http://localhost/","main.rbx").should == URI.parse("http://localhost/main.rbx") - end - - it "accepts URI objects" do - @object.join(URI("http://localhost/"),"main.rbx").should == URI.parse("http://localhost/main.rbx") - @object.join("http://localhost/",URI("main.rbx")).should == URI.parse("http://localhost/main.rbx") - @object.join(URI("http://localhost/"),URI("main.rbx")).should == URI.parse("http://localhost/main.rbx") - end - - it "accepts string-like arguments with to_str" do - str = mock('string-like') - str.should_receive(:to_str).and_return("http://ruby-lang.org") - str2 = mock('string-like also') - str2.should_receive(:to_str).and_return("foo/bar") - @object.join(str, str2).should == URI.parse("http://ruby-lang.org/foo/bar") - end - - it "raises an error if given no argument" do - -> { - @object.join - }.should.raise(ArgumentError) - end - - it "doesn't create redundant '/'s" do - @object.join("http://localhost/", "/main.rbx").should == URI.parse("http://localhost/main.rbx") - end - - it "discards arguments given before an absolute uri" do - @object.join("http://localhost/a/b/c/d", "http://ruby-lang.com/foo", "bar").should == URI.parse("http://ruby-lang.com/bar") - end - - it "resolves .. in paths" do - @object.join("http://localhost/a/b/c/d", "../../e/f", "g/h/../i").to_s.should == "http://localhost/a/e/g/i" - end -end - - -# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar')) -# assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar')) -# assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/')) -# -# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz')) -# assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz')) -# assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/')) -# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz')) -# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge')) -# -# assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz')) -# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) -# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) -# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) -# assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) -# assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) -# assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) diff --git a/library/uri/shared/parse.rb b/library/uri/shared/parse.rb deleted file mode 100644 index 7ec7179526..0000000000 --- a/library/uri/shared/parse.rb +++ /dev/null @@ -1,206 +0,0 @@ -describe :uri_parse, shared: true do - it "returns a URI::HTTP object when parsing an HTTP URI" do - @object.parse("http://www.example.com/").should.is_a?(URI::HTTP) - end - - it "populates the components of a parsed URI::HTTP, setting the port to 80 by default" do - # general case - URISpec.components(@object.parse("http://user:pass@example.com/path/?query=val&q2=val2#fragment")).should == { - scheme: "http", - userinfo: "user:pass", - host: "example.com", - port: 80, - path: "/path/", - query: "query=val&q2=val2", - fragment: "fragment" - } - - # multiple paths - URISpec.components(@object.parse("http://a/b/c/d;p?q")).should == { - scheme: "http", - userinfo: nil, - host: "a", - port: 80, - path: "/b/c/d;p", - query: "q", - fragment: nil - } - - # multi-level domain - URISpec.components(@object.parse('http://www.math.uio.no/faq/compression-faq/part1.html')).should == { - scheme: "http", - userinfo: nil, - host: "www.math.uio.no", - port: 80, - path: "/faq/compression-faq/part1.html", - query: nil, - fragment: nil - } - end - - it "parses out the port number of a URI, when given" do - @object.parse("http://example.com:8080/").port.should == 8080 - end - - it "returns a URI::HTTPS object when parsing an HTTPS URI" do - @object.parse("https://important-intern-net.net").should.is_a?(URI::HTTPS) - end - - it "sets the port of a parsed https URI to 443 by default" do - @object.parse("https://example.com/").port.should == 443 - end - - it "populates the components of a parsed URI::FTP object" do - # generic, empty password. - url = @object.parse("ftp://anonymous@ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.bz2;type=i") - url.should.is_a?(URI::FTP) - URISpec.components(url).should == { - scheme: "ftp", - userinfo: "anonymous", - host: "ruby-lang.org", - port: 21, - path: "pub/ruby/1.8/ruby-1.8.6.tar.bz2", - typecode: "i" - } - - # multidomain, no user or password - url = @object.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt') - url.should.is_a?(URI::FTP) - URISpec.components(url).should == { - scheme: "ftp", - userinfo: nil, - host: "ftp.is.co.za", - port: 21, - path: "rfc/rfc1808.txt", - typecode: nil - } - - # empty user - url = @object.parse('ftp://:pass@localhost/') - url.should.is_a?(URI::FTP) - URISpec.components(url).should == { - scheme: "ftp", - userinfo: ":pass", - host: "localhost", - port: 21, - path: "", - typecode: nil - } - url.password.should == "pass" - end - - it "returns a URI::LDAP object when parsing an LDAP URI" do - #taken from http://www.faqs.org/rfcs/rfc2255.html 'cause I don't really know what an LDAP url looks like - ldap_uris = %w{ ldap:///o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen) ldap://ldap.itd.umich.edu/c=GB?objectClass?one ldap://ldap.question.com/o=Question%3f,c=US?mail ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04) ldap:///??sub??bindname=cn=Manager%2co=Foo ldap:///??sub??!bindname=cn=Manager%2co=Foo } - ldap_uris.each do |ldap_uri| - @object.parse(ldap_uri).should.is_a?(URI::LDAP) - end - end - - it "populates the components of a parsed URI::LDAP object" do - URISpec.components(@object.parse("ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress?scope?filter?extensions")).should == { - scheme: "ldap", - host: "ldap.itd.umich.edu", - port: 389, - dn: "o=University%20of%20Michigan,c=US", - attributes: "postalAddress", - scope: "scope", - filter: "filter", - extensions: "extensions" - } - end - - it "returns a URI::MailTo object when passed a mailto URI" do - @object.parse("mailto:spam@mailinator.com").should.is_a?(URI::MailTo) - end - - it "populates the components of a parsed URI::MailTo object" do - URISpec.components(@object.parse("mailto:spam@mailinator.com?subject=Discounts%20On%20Imported%20methods!!!&body=Exciting%20offer")).should == { - scheme: "mailto", - to: "spam@mailinator.com", - headers: [["subject","Discounts%20On%20Imported%20methods!!!"], - ["body", "Exciting%20offer"]] - } - end - - # TODO - # Test registry - it "does its best to extract components from URI::Generic objects" do - # generic - URISpec.components(URI("scheme://userinfo@host/path?query#fragment")).should == { - scheme: "scheme", - userinfo: "userinfo", - host: "host", - port: nil, - path: "/path", - query: "query", - fragment: "fragment", - registry: nil, - opaque: nil - } - - # gopher - gopher = @object.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles') - gopher.should.is_a?(URI::Generic) - - URISpec.components(gopher).should == { - scheme: "gopher", - userinfo: nil, - host: "spinaltap.micro.umn.edu", - port: nil, - path: "/00/Weather/California/Los%20Angeles", - query: nil, - fragment: nil, - registry: nil, - opaque: nil - } - - # news - news = @object.parse('news:comp.infosystems.www.servers.unix') - news.should.is_a?(URI::Generic) - URISpec.components(news).should == { - scheme: "news", - userinfo: nil, - host: nil, - port: nil, - path: nil, - query: nil, - fragment: nil, - registry: nil, - opaque: "comp.infosystems.www.servers.unix" - } - - # telnet - telnet = @object.parse('telnet://melvyl.ucop.edu/') - telnet.should.is_a?(URI::Generic) - URISpec.components(telnet).should == { - scheme: "telnet", - userinfo: nil, - host: "melvyl.ucop.edu", - port: nil, - path: "/", - query: nil, - fragment: nil, - registry: nil, - opaque: nil - } - - # files - file_l = @object.parse('file:///foo/bar.txt') - file_l.should.is_a?(URI::Generic) - file = @object.parse('file:/foo/bar.txt') - file.should.is_a?(URI::Generic) - end - - if URI::DEFAULT_PARSER == URI::RFC2396_Parser - it "raises errors on malformed URIs" do - -> { @object.parse('http://a_b:80/') }.should.raise(URI::InvalidURIError) - -> { @object.parse('http://a_b/') }.should.raise(URI::InvalidURIError) - end - elsif URI::DEFAULT_PARSER == URI::RFC3986_Parser - it "does not raise errors on URIs contained underscore" do - -> { @object.parse('http://a_b:80/') }.should_not.raise(URI::InvalidURIError) - -> { @object.parse('http://a_b/') }.should_not.raise(URI::InvalidURIError) - end - end -end From 5224b3c63b53c1f1e5da677d70ffabaf3d213419 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 2 Jun 2026 19:05:54 +0200 Subject: [PATCH 18/18] Move YAML to rely less on shared examples --- library/yaml/load_stream_spec.rb | 20 +++++++++++++++++--- library/yaml/shared/each_document.rb | 19 ------------------- 2 files changed, 17 insertions(+), 22 deletions(-) delete mode 100644 library/yaml/shared/each_document.rb diff --git a/library/yaml/load_stream_spec.rb b/library/yaml/load_stream_spec.rb index 31bc862f5e..5f5d4c7337 100644 --- a/library/yaml/load_stream_spec.rb +++ b/library/yaml/load_stream_spec.rb @@ -1,9 +1,23 @@ require_relative '../../spec_helper' require_relative 'fixtures/strings' -require_relative 'shared/each_document' - require 'yaml' describe "YAML.load_stream" do - it_behaves_like :yaml_each_document, :load_stream + it "calls the block on each successive document" do + documents = [] + YAML.load_stream(YAMLSpecs::MULTIDOCUMENT) do |doc| + documents << doc + end + documents.should == [["Mark McGwire", "Sammy Sosa", "Ken Griffey"], + ["Chicago Cubs", "St Louis Cardinals"]] + end + + it "works on files" do + test_parse_file = fixture __FILE__, "test_yaml.yml" + File.open(test_parse_file, "r") do |file| + YAML.load_stream(file) do |doc| + doc.should == {"project"=>{"name"=>"RubySpec"}} + end + end + end end diff --git a/library/yaml/shared/each_document.rb b/library/yaml/shared/each_document.rb deleted file mode 100644 index 6f00aee297..0000000000 --- a/library/yaml/shared/each_document.rb +++ /dev/null @@ -1,19 +0,0 @@ -describe :yaml_each_document, shared: true do - it "calls the block on each successive document" do - documents = [] - YAML.send(@method, YAMLSpecs::MULTIDOCUMENT) do |doc| - documents << doc - end - documents.should == [["Mark McGwire", "Sammy Sosa", "Ken Griffey"], - ["Chicago Cubs", "St Louis Cardinals"]] - end - - it "works on files" do - test_parse_file = fixture __FILE__, "test_yaml.yml" - File.open(test_parse_file, "r") do |file| - YAML.send(@method, file) do |doc| - doc.should == {"project"=>{"name"=>"RubySpec"}} - end - end - end -end