summaryrefslogtreecommitdiff
path: root/test/psych
diff options
context:
space:
mode:
authorSeth Boyles <sethboyles@gmail.com>2022-01-14 19:58:20 +0000
committergit <svn-admin@ruby-lang.org>2022-01-22 10:00:51 +0900
commitc1a6ff046d4f27c972adf96f9a6724abc2f0647a (patch)
treef3924a5c026669589d07d2eab9a9e6673684c8f8 /test/psych
parentb815a0bd75b660652bf05479467904fa06399092 (diff)
[ruby/psych] Add strict_integer option to parse numbers with commas as strings
Authored-by: Seth Boyles <sethboyles@gmail.com> https://github.com/ruby/psych/commit/75bebb37b8
Diffstat (limited to 'test/psych')
-rw-r--r--test/psych/test_numeric.rb11
-rw-r--r--test/psych/test_scalar_scanner.rb25
2 files changed, 36 insertions, 0 deletions
diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb
index 8c3dcd173c..9c75c016cd 100644
--- a/test/psych/test_numeric.rb
+++ b/test/psych/test_numeric.rb
@@ -43,5 +43,16 @@ module Psych
str = Psych.load('--- 1.1.1')
assert_equal '1.1.1', str
end
+
+ # This behavior is not to YML spec, but is kept for backwards compatibility
+ def test_string_with_commas
+ number = Psych.load('--- 12,34,56')
+ assert_equal 123456, number
+ end
+
+ def test_string_with_commas_with_strict_integer
+ str = Psych.load('--- 12,34,56', strict_integer: true)
+ assert_equal '12,34,56', str
+ end
end
end
diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb
index ebc9fbdcd2..145db58fd9 100644
--- a/test/psych/test_scalar_scanner.rb
+++ b/test/psych/test_scalar_scanner.rb
@@ -149,6 +149,31 @@ module Psych
assert_equal 0x123456789abcdef, ss.tokenize('0x12_,34,_56,_789abcdef__')
end
+ def test_scan_strict_int_commas_and_underscores
+ # this test is to ensure adherance to YML spec using the 'strict_integer' option
+ scanner = Psych::ScalarScanner.new ClassLoader.new, strict_integer: true
+ assert_equal 123_456_789, scanner.tokenize('123_456_789')
+ assert_equal '123,456,789', scanner.tokenize('123,456,789')
+ assert_equal '1_2,3,4_5,6_789', scanner.tokenize('1_2,3,4_5,6_789')
+
+ assert_equal 1, scanner.tokenize('1')
+ assert_equal 1, scanner.tokenize('+1')
+ assert_equal(-1, scanner.tokenize('-1'))
+
+ assert_equal 0b010101010, scanner.tokenize('0b010101010')
+ assert_equal 0b010101010, scanner.tokenize('0b01_01_01_010')
+ assert_equal '0b0,1_0,1_,0,1_01,0', scanner.tokenize('0b0,1_0,1_,0,1_01,0')
+
+ assert_equal 01234567, scanner.tokenize('01234567')
+ assert_equal '0_,,,1_2,_34567', scanner.tokenize('0_,,,1_2,_34567')
+
+ assert_equal 0x123456789abcdef, scanner.tokenize('0x123456789abcdef')
+ assert_equal 0x123456789abcdef, scanner.tokenize('0x12_34_56_789abcdef')
+ assert_equal '0x12_,34,_56,_789abcdef', scanner.tokenize('0x12_,34,_56,_789abcdef')
+ assert_equal '0x_12_,34,_56,_789abcdef', scanner.tokenize('0x_12_,34,_56,_789abcdef')
+ assert_equal '0x12_,34,_56,_789abcdef__', scanner.tokenize('0x12_,34,_56,_789abcdef__')
+ end
+
def test_scan_dot
assert_equal '.', ss.tokenize('.')
end