|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'timeout' |
4 | | - |
5 | | -# Helper to enforce timeout for these specific tests |
6 | | -def with_timeout(seconds = 1, &) |
7 | | - Timeout.timeout(seconds, &) |
8 | | -end |
9 | | - |
10 | 3 | RSpec.describe JsonMend do |
11 | 4 | it 'has a version number' do |
12 | 5 | expect(described_class::VERSION).not_to be_nil |
@@ -947,6 +940,31 @@ def with_timeout(seconds = 1, &) |
947 | 940 |
|
948 | 941 | context 'with edge cases' do |
949 | 942 | [ |
| 943 | + { |
| 944 | + input: '1 ""', |
| 945 | + expected_output: '1', |
| 946 | + desc: 'multi-json with empty string skipped' |
| 947 | + }, |
| 948 | + { |
| 949 | + input: "[1, # comment inside array \n 2]", |
| 950 | + expected_output: JSON.dump([1, 2]), |
| 951 | + desc: 'array with hash-style line comment' |
| 952 | + }, |
| 953 | + { |
| 954 | + input: '[ "a", "b\"c" ]', |
| 955 | + expected_output: JSON.dump(['a', 'b"c']), |
| 956 | + desc: 'array with internal escaped quote logic' |
| 957 | + }, |
| 958 | + { |
| 959 | + input: '{"a": "val\'ue"}', |
| 960 | + expected_output: JSON.dump({ 'a' => "val'ue" }), |
| 961 | + desc: 'string with escaped single quote' |
| 962 | + }, |
| 963 | + { |
| 964 | + input: '{"key:colons": "value"}', |
| 965 | + expected_output: JSON.dump({ 'key:colons' => 'value' }), |
| 966 | + desc: 'key containing colon' |
| 967 | + }, |
950 | 968 | { |
951 | 969 | input: '[ "key": "value", "key2": "value2" ]', |
952 | 970 | expected_output: JSON.dump([{ 'key' => 'value', 'key2' => 'value2' }]), |
@@ -1050,35 +1068,6 @@ def with_timeout(seconds = 1, &) |
1050 | 1068 | end |
1051 | 1069 | end |
1052 | 1070 |
|
1053 | | - context 'when provided valid complex standard json' do |
1054 | | - [ |
1055 | | - { |
1056 | | - input: '{"simple": "string", "number": 123, "bool": true, "nil": null}', |
1057 | | - expected_output: JSON.dump({ simple: 'string', number: 123, bool: true, nil: nil }) |
1058 | | - }, |
1059 | | - { |
1060 | | - input: '{"nested": {"array": [1, 2, {"deep": "obj"}]}}', |
1061 | | - expected_output: JSON.dump({ nested: { array: [1, 2, { deep: 'obj' }] } }) |
1062 | | - }, |
1063 | | - { |
1064 | | - input: '{"unicode": "こんにちは", "emoji": "👍"}', |
1065 | | - expected_output: JSON.dump({ unicode: 'こんにちは', emoji: '👍' }) |
1066 | | - }, |
1067 | | - { |
1068 | | - input: '{"escapes":"\" \\\\ / \\b \\f \\n \\r \\t"}', |
1069 | | - expected_output: JSON.dump({ escapes: "\" \\ / \b \f \n \r \t" }) |
1070 | | - }, |
1071 | | - { |
1072 | | - input: '{"empty_obj": {}, "empty_arr": []}', |
1073 | | - expected_output: JSON.dump({ empty_obj: {}, empty_arr: [] }) |
1074 | | - } |
1075 | | - ].each do |test_case| |
1076 | | - it "preserves valid json: #{test_case[:input]}" do |
1077 | | - expect(described_class.repair(test_case[:input])).to eq(test_case[:expected_output]) |
1078 | | - end |
1079 | | - end |
1080 | | - end |
1081 | | - |
1082 | 1071 | context 'when provided ambiguous number formats' do |
1083 | 1072 | [ |
1084 | 1073 | { |
@@ -1173,5 +1162,46 @@ def with_timeout(seconds = 1, &) |
1173 | 1162 | end |
1174 | 1163 | end |
1175 | 1164 | end |
| 1165 | + |
| 1166 | + context 'with valid JSON (direct parser usage)' do |
| 1167 | + [ |
| 1168 | + { |
| 1169 | + input: '{"a": 1}', |
| 1170 | + expected_output: JSON.dump({ 'a' => 1 }) |
| 1171 | + }, |
| 1172 | + { |
| 1173 | + input: '[1, 2, 3]', |
| 1174 | + expected_output: JSON.dump([1, 2, 3]) |
| 1175 | + }, |
| 1176 | + { |
| 1177 | + input: '{"a": [1, 2], "b": {"c": 3}}', |
| 1178 | + expected_output: JSON.dump({ 'a' => [1, 2], 'b' => { 'c' => 3 } }) |
| 1179 | + }, |
| 1180 | + { |
| 1181 | + input: '{"simple": "string", "number": 123, "bool": true, "nil": null}', |
| 1182 | + expected_output: JSON.dump({ simple: 'string', number: 123, bool: true, nil: nil }) |
| 1183 | + }, |
| 1184 | + { |
| 1185 | + input: '{"nested": {"array": [1, 2, {"deep": "obj"}]}}', |
| 1186 | + expected_output: JSON.dump({ nested: { array: [1, 2, { deep: 'obj' }] } }) |
| 1187 | + }, |
| 1188 | + { |
| 1189 | + input: '{"unicode": "こんにちは", "emoji": "👍"}', |
| 1190 | + expected_output: JSON.dump({ unicode: 'こんにちは', emoji: '👍' }) |
| 1191 | + }, |
| 1192 | + { |
| 1193 | + input: '{"escapes":"\" \\\\ / \\b \\f \\n \\r \\t"}', |
| 1194 | + expected_output: JSON.dump({ escapes: "\" \\ / \b \f \n \r \t" }) |
| 1195 | + }, |
| 1196 | + { |
| 1197 | + input: '{"empty_obj": {}, "empty_arr": []}', |
| 1198 | + expected_output: JSON.dump({ empty_obj: {}, empty_arr: [] }) |
| 1199 | + } |
| 1200 | + ].each do |test_case| |
| 1201 | + it "parses valid JSON #{test_case[:input]}" do |
| 1202 | + expect(described_class.repair(test_case[:input])).to eq(test_case[:expected_output]) |
| 1203 | + end |
| 1204 | + end |
| 1205 | + end |
1176 | 1206 | end |
1177 | 1207 | end |
0 commit comments