[前][次][番号順一覧][スレッド一覧]

ruby-changes:45910

From: nobu <ko1@a...>
Date: Thu, 16 Mar 2017 09:43:12 +0900 (JST)
Subject: [ruby-changes:45910] nobu:r57983 (trunk): test_rational.rb: shorten

nobu	2017-03-16 09:43:07 +0900 (Thu, 16 Mar 2017)

  New Revision: 57983

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57983

  Log:
    test_rational.rb: shorten
    
    * test/ruby/test_rational.rb (test_parse): reduce repeated
      arguments.

  Modified files:
    trunk/test/ruby/test_rational.rb
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 57982)
+++ test/ruby/test_rational.rb	(revision 57983)
@@ -675,100 +675,105 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L675
     end
   end
 
+  def assert_valid_rational(n, d, r)
+    x = Rational(n, d)
+    assert_equal(x, r.to_r, "#{r.dump}.to_r")
+    assert_equal(x, Rational(r), "Rational(#{r.dump})")
+  end
+
+  def assert_invalid_rational(n, d, r)
+    x = Rational(n, d)
+    assert_equal(x, r.to_r, "#{r.dump}.to_r")
+    assert_raise(ArgumentError, "Rational(#{r.dump})") {Rational(r)}
+  end
+
   def test_parse
-    assert_equal(Rational(5), '5'.to_r, "'5'.to_r")
-    assert_equal(Rational(-5), '-5'.to_r, "'-5'.to_r")
-    assert_equal(Rational(5,3), '5/3'.to_r, "'5/3'.to_r")
-    assert_equal(Rational(-5,3), '-5/3'.to_r, "'-5/3'.to_r")
-
-    assert_equal(Rational(5), '5.0'.to_r, "'5.0'.to_r")
-    assert_equal(Rational(-5), '-5.0'.to_r, "'-5.0'.to_r")
-    assert_equal(Rational(5,3), '5.0/3'.to_r, "'5.0/3'.to_r")
-    assert_equal(Rational(-5,3), '-5.0/3'.to_r, "'-5.0/3'.to_r")
-
-    assert_equal(Rational(5), '5e0'.to_r, "'5e0'.to_r")
-    assert_equal(Rational(-5), '-5e0'.to_r, "'-5e0'.to_r")
-    assert_equal(Rational(5,3), '5e0/3'.to_r, "'5e0/3'.to_r")
-    assert_equal(Rational(-5,3), '-5e0/3'.to_r, "'-5e0/3'.to_r")
-
-    assert_equal(Rational(5e1), '5e1'.to_r, "'5e1'.to_r")
-    assert_equal(Rational(-5e2), '-5e2'.to_r, "'-5e2'.to_r")
-    assert_equal(Rational(5e3,3), '5e003/3'.to_r, "'5e003/3'.to_r")
-    assert_equal(Rational(-5e4,3), '-5e004/3'.to_r, "'-5e004/3'.to_r")
-
-    assert_equal(Rational(33,100), '.33'.to_r, "'.33'.to_r")
-    assert_equal(Rational(33,100), '0.33'.to_r, "'0.33'.to_r")
-    assert_equal(Rational(-33,100), '-.33'.to_r, "'-.33'.to_r")
-    assert_equal(Rational(-33,100), '-0.33'.to_r, "'-0.33'.to_r")
-    assert_equal(Rational(-33,100), '-0.3_3'.to_r, "'-0.3_3'.to_r")
-
-    assert_equal(Rational(1,2), '5e-1'.to_r, "'5e-1'.to_r")
-    assert_equal(Rational(50), '5e+1'.to_r, "'5e+1'.to_r")
-    assert_equal(Rational(1,2), '5.0e-1'.to_r, "'5.0e-1'.to_r")
-    assert_equal(Rational(50), '5.0e+1'.to_r, "'5.0e+1'.to_r")
-    assert_equal(Rational(50), '5e1'.to_r, "'5e1'.to_r")
-    assert_equal(Rational(50), '5E1'.to_r, "'5E1'.to_r")
-    assert_equal(Rational(500), '5e2'.to_r, "'5e2'.to_r")
-    assert_equal(Rational(5000), '5e3'.to_r, "'5e3'.to_r")
-    assert_equal(Rational(500000000000), '5e1_1'.to_r, "'5e1_1'.to_r")
-
-    assert_equal(Rational(5), Rational('5'), "Rational('5')")
-    assert_equal(Rational(-5), Rational('-5'), "Rational('-5')")
-    assert_equal(Rational(5,3), Rational('5/3'), "Rational('5/3')")
-    assert_equal(Rational(-5,3), Rational('-5/3'), "Rational('-5/3')")
-
-    assert_equal(Rational(5), Rational('5.0'), "Rational('5.0')")
-    assert_equal(Rational(-5), Rational('-5.0'), "Rational('-5.0')")
-    assert_equal(Rational(5,3), Rational('5.0/3'), "Rational('5.0/3')")
-    assert_equal(Rational(-5,3), Rational('-5.0/3'), "Rational('-5.0/3')")
-
-    assert_equal(Rational(5), Rational('5e0'), "Rational('5e0')")
-    assert_equal(Rational(-5), Rational('-5e0'), "Rational('-5e0')")
-    assert_equal(Rational(5,3), Rational('5e0/3'), "Rational('5e0/3')")
-    assert_equal(Rational(-5,3), Rational('-5e0/3'), "Rational('-5e0/3')")
-
-    assert_equal(Rational(5e1), Rational('5e1'), "Rational('5e1')")
-    assert_equal(Rational(-5e2), Rational('-5e2'), "Rational('-5e2')")
-    assert_equal(Rational(5e3,3), Rational('5e003/3'), "Rational('5e003/3')")
-    assert_equal(Rational(-5e4,3), Rational('-5e004/3'), "Rational('-5e004/3')")
-
-    assert_equal(Rational(33,100), Rational('.33'), "Rational('.33')")
-    assert_equal(Rational(33,100), Rational('0.33'), "Rational('0.33')")
-    assert_equal(Rational(-33,100), Rational('-.33'), "Rational('-.33')")
-    assert_equal(Rational(-33,100), Rational('-0.33'), "Rational('-0.33')")
-    assert_equal(Rational(-33,100), Rational('-0.3_3'), "Rational('-0.3_3')")
-
-    assert_equal(Rational(1,2), Rational('5e-1'), "Rational('5e-1')")
-    assert_equal(Rational(50), Rational('5e+1'), "Rational('5e+1')")
-    assert_equal(Rational(1,2), Rational('5.0e-1'), "Rational('5.0e-1')")
-    assert_equal(Rational(50), Rational('5.0e+1'), "Rational('5.0e+1')")
-    assert_equal(Rational(50), Rational('5e1'), "Rational('5e1')")
-    assert_equal(Rational(50), Rational('5E1'), "Rational('5E1')")
-    assert_equal(Rational(500), Rational('5e2'), "Rational('5e2')")
-    assert_equal(Rational(5000), Rational('5e3'), "Rational('5e3')")
-    assert_equal(Rational(500000000000), Rational('5e1_1'), "Rational('5e1_1')")
-
-    assert_equal(Rational(0), ''.to_r, "''.to_r")
-    assert_equal(Rational(0), ' '.to_r, "' '.to_r")
-    assert_equal(Rational(5), "\f\n\r\t\v5\0".to_r, '"\f\n\r\t\v5\0".to_r')
-    assert_equal(Rational(0), '_'.to_r, "'_'.to_r")
-    assert_equal(Rational(0), '_5'.to_r, "'_5'.to_r")
-    assert_equal(Rational(5), '5_'.to_r, "'5_'.to_r")
-    assert_equal(Rational(5), '5x'.to_r, "'5x'.to_r")
-    assert_equal(Rational(5), '5/_3'.to_r, "'5/_3'.to_r")
-    assert_equal(Rational(5,3), '5/3_'.to_r, "'5/3_'.to_r")
-    assert_equal(Rational(5,3), '5/3.3'.to_r, "'5/3.3'.to_r")
-    assert_equal(Rational(5,3), '5/3x'.to_r, "'5/3x'.to_r")
-    assert_raise(ArgumentError, "Rational('')") {Rational('')}
-    assert_raise(ArgumentError, "Rational('_')") {Rational('_')}
-    assert_raise(ArgumentError, 'Rational("\f\n\r\t\v5\0")') {Rational("\f\n\r\t\v5\0")}
-    assert_raise(ArgumentError, "Rational('_5')") {Rational('_5')}
-    assert_raise(ArgumentError, "Rational('5_')") {Rational('5_')}
-    assert_raise(ArgumentError, "Rational('5x')") {Rational('5x')}
-    assert_raise(ArgumentError, "Rational('5/_3')") {Rational('5/_3')}
-    assert_raise(ArgumentError, "Rational('5/3_')") {Rational('5/3_')}
-    assert_raise(ArgumentError, "Rational('5/3.3')") {Rational('5/3.3')}
-    assert_raise(ArgumentError, "Rational('5/3x')") {Rational('5/3x')}
+    ok = method(:assert_valid_rational)
+    ng = method(:assert_invalid_rational)
+
+    ok[ 5, 1, '5']
+    ok[-5, 1, '-5']
+    ok[ 5, 3, '5/3']
+    ok[-5, 3, '-5/3']
+
+    ok[ 5, 1, '5.0']
+    ok[-5, 1, '-5.0']
+    ok[ 5, 3, '5.0/3']
+    ok[-5, 3, '-5.0/3']
+
+    ok[ 5, 1, '5e0']
+    ok[-5, 1, '-5e0']
+    ok[ 5, 3, '5e0/3']
+    ok[-5, 3, '-5e0/3']
+
+    ok[ 5e1, 1, '5e1']
+    ok[-5e2, 1, '-5e2']
+    ok[ 5e3, 3, '5e003/3']
+    ok[-5e4, 3, '-5e004/3']
+
+    ok[ 33, 100, '.33']
+    ok[ 33, 100, '0.33']
+    ok[-33, 100, '-.33']
+    ok[-33, 100, '-0.33']
+    ok[-33, 100, '-0.3_3']
+
+    ok[ 1, 2, '5e-1']
+    ok[50, 1, '5e+1']
+    ok[ 1, 2, '5.0e-1']
+    ok[50, 1, '5.0e+1']
+    ok[50, 1, '5e1']
+    ok[50, 1, '5E1']
+    ok[500, 1, '5e2']
+    ok[5000, 1, '5e3']
+    ok[500000000000, 1, '5e1_1']
+
+    ok[ 5, 1, '5']
+    ok[-5, 1, '-5']
+    ok[ 5, 3, '5/3']
+    ok[-5, 3, '-5/3']
+
+    ok[ 5, 1, '5.0']
+    ok[-5, 1, '-5.0']
+    ok[ 5, 3, '5.0/3']
+    ok[-5, 3, '-5.0/3']
+
+    ok[ 5, 1, '5e0']
+    ok[-5, 1, '-5e0']
+    ok[ 5, 3, '5e0/3']
+    ok[-5, 3, '-5e0/3']
+
+    ok[ 5e1, 1, '5e1']
+    ok[-5e2, 1, '-5e2']
+    ok[ 5e3, 3, '5e003/3']
+    ok[-5e4, 3, '-5e004/3']
+
+    ok[ 33, 100, '.33']
+    ok[ 33, 100, '0.33']
+    ok[-33, 100, '-.33']
+    ok[-33, 100, '-0.33']
+    ok[-33, 100, '-0.3_3']
+
+    ok[ 1, 2, '5e-1']
+    ok[50, 1, '5e+1']
+    ok[ 1, 2, '5.0e-1']
+    ok[50, 1, '5.0e+1']
+    ok[50, 1, '5e1']
+    ok[50, 1, '5E1']
+    ok[500, 1, '5e2']
+    ok[5000, 1, '5e3']
+    ok[500000000000, 1, '5e1_1']
+
+    ng[0, 1, '']
+    ng[0, 1, ' ']
+    ng[5, 1, "\f\n\r\t\v5\0"]
+    ng[0, 1, '_']
+    ng[0, 1, '_5']
+    ng[5, 1, '5_']
+    ng[5, 1, '5x']
+    ng[5, 1, '5/_3']
+    ng[5, 3, '5/3_']
+    ng[5, 3, '5/3.3']
+    ng[5, 3, '5/3x']
   end
 
   def test_to_i

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]