From c1a6ff046d4f27c972adf96f9a6724abc2f0647a Mon Sep 17 00:00:00 2001 From: Seth Boyles Date: Fri, 14 Jan 2022 19:58:20 +0000 Subject: [ruby/psych] Add strict_integer option to parse numbers with commas as strings Authored-by: Seth Boyles https://github.com/ruby/psych/commit/75bebb37b8 --- test/psych/test_numeric.rb | 11 +++++++++++ test/psych/test_scalar_scanner.rb | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'test/psych') 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 -- cgit v1.2.3