diff options
author | Seth Boyles <sethboyles@gmail.com> | 2022-01-14 19:58:20 +0000 |
---|---|---|
committer | Seth Boyles <sethboyles@gmail.com> | 2022-01-14 20:09:47 +0000 |
commit | 75bebb37b80c7c3f3c6a157e1dd25112cfae8029 (patch) | |
tree | a6531188e9c0b33bd8b14a2bf83f6fafb1af545b /test | |
parent | ba203f19c03763e6f7b7b97a76ce89e3ae488a6f (diff) | |
download | psych-75bebb37b80c7c3f3c6a157e1dd25112cfae8029.tar.gz |
Add strict_integer option to parse numbers with commas as strings
Authored-by: Seth Boyles <sethboyles@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/psych/test_numeric.rb | 11 | ||||
-rw-r--r-- | test/psych/test_scalar_scanner.rb | 25 |
2 files changed, 36 insertions, 0 deletions
diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb index 8c3dcd1..9c75c01 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 ebc9fbd..145db58 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 |