From 9f688d53c2b5af5960d1e8d8fb09b26aa9d8b5f9 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Sun, 18 Dec 2011 03:05:02 +0000 Subject: * ext/psych/lib/psych/scalar_scanner.rb: Strings that look like dates should be treated as strings and not dates. * test/psych/test_scalar_scanner.rb: corresponding tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34067 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ ext/psych/lib/psych/scalar_scanner.rb | 8 ++++++-- test/psych/test_scalar_scanner.rb | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 666200de0b..6ab1afed51 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sun Dec 18 12:03:13 2011 Aaron Patterson + + * ext/psych/lib/psych/scalar_scanner.rb: Strings that look like dates + should be treated as strings and not dates. + + * test/psych/test_scalar_scanner.rb: corresponding tests. + Sun Dec 18 09:43:21 2011 CHIKANAGA Tomoyuki * test/thread/test_queue.rb (test_thr_kill): extend timeout. diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index a265a2baf3..fa2d385a63 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -46,9 +46,13 @@ module Psych end when TIME parse_time string - when /^\d{4}-\d{1,2}-\d{1,2}$/ + when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/ require 'date' - Date.strptime(string, '%Y-%m-%d') + begin + Date.strptime(string, '%Y-%m-%d') + rescue ArgumentError + string + end when /^\.inf$/i 1 / 0.0 when /^-\.inf$/i diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb index 659909909e..cf0dfff6aa 100644 --- a/test/psych/test_scalar_scanner.rb +++ b/test/psych/test_scalar_scanner.rb @@ -1,4 +1,5 @@ require 'psych/helper' +require 'date' module Psych class TestScalarScanner < TestCase @@ -20,6 +21,27 @@ module Psych end end + def test_scan_bad_dates + x = '2000-15-01' + assert_equal x, @ss.tokenize(x) + + x = '2000-10-51' + assert_equal x, @ss.tokenize(x) + + x = '2000-10-32' + assert_equal x, @ss.tokenize(x) + end + + def test_scan_good_edge_date + x = '2000-1-31' + assert_equal Date.strptime(x, '%Y-%m-%d'), @ss.tokenize(x) + end + + def test_scan_bad_edge_date + x = '2000-11-31' + assert_equal x, @ss.tokenize(x) + end + def test_scan_date date = '1980-12-16' token = @ss.tokenize date -- cgit v1.2.3