|
| 1 | +# ------------------------------------------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# ------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import unittest |
| 8 | +import json |
| 9 | +from json import JSONDecodeError |
| 10 | +from azure.appconfiguration.provider._json import ( |
| 11 | + remove_json_comments, |
| 12 | + _find_string_end, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestJsonUtils(unittest.TestCase): |
| 17 | + def test_remove_json_comments_no_comments(self): |
| 18 | + # Test a simple JSON with no comments |
| 19 | + input_json = '{"key": "value", "number": 123}' |
| 20 | + result = remove_json_comments(input_json) |
| 21 | + # Verify the result is valid JSON |
| 22 | + parsed = json.loads(result) |
| 23 | + self.assertEqual(parsed["key"], "value") |
| 24 | + self.assertEqual(parsed["number"], 123) |
| 25 | + |
| 26 | + def test_remove_json_comments_single_line(self): |
| 27 | + # Test removing single line comments |
| 28 | + input_json = """{ |
| 29 | + "key": "value" // this is a comment |
| 30 | + }""" |
| 31 | + result = remove_json_comments(input_json) |
| 32 | + # Verify the result is valid JSON |
| 33 | + parsed = json.loads(result) |
| 34 | + self.assertEqual(parsed["key"], "value") |
| 35 | + |
| 36 | + def test_remove_json_comments_single_line_no_newline(self): |
| 37 | + # Test removing single line comments without a newline at the end |
| 38 | + input_json = '{ "key": "value" } // this is a comment' |
| 39 | + result = remove_json_comments(input_json) |
| 40 | + # Verify the result is valid JSON |
| 41 | + parsed = json.loads(result) |
| 42 | + self.assertEqual(parsed["key"], "value") |
| 43 | + |
| 44 | + def test_remove_json_comments_multi_line(self): |
| 45 | + # Test removing multi-line comments |
| 46 | + input_json = """{/* This is a |
| 47 | + multi-line |
| 48 | + comment */ |
| 49 | + "key": "value" |
| 50 | + }""" |
| 51 | + result = remove_json_comments(input_json) |
| 52 | + # Verify the result is valid JSON |
| 53 | + parsed = json.loads(result) |
| 54 | + self.assertEqual(parsed["key"], "value") |
| 55 | + |
| 56 | + def test_remove_json_comments_mixed(self): |
| 57 | + # Test removing both single-line and multi-line comments |
| 58 | + input_json = """{ |
| 59 | + "key1": "value1", // single line comment |
| 60 | + /* multi-line comment |
| 61 | + spanning multiple lines */ |
| 62 | + "key2": "value2" |
| 63 | + }""" |
| 64 | + result = remove_json_comments(input_json) |
| 65 | + # Verify the result is valid JSON |
| 66 | + parsed = json.loads(result) |
| 67 | + self.assertEqual(parsed["key1"], "value1") |
| 68 | + self.assertEqual(parsed["key2"], "value2") |
| 69 | + |
| 70 | + def test_remove_json_comments_inside_string(self): |
| 71 | + # Test that comments within string literals are not removed |
| 72 | + input_json = '{"key": "value with // not a comment"}' |
| 73 | + result = remove_json_comments(input_json) |
| 74 | + # Verify the result is valid JSON |
| 75 | + parsed = json.loads(result) |
| 76 | + self.assertEqual(parsed["key"], "value with // not a comment") |
| 77 | + |
| 78 | + input_json = '{"key": "value with /* not a comment */"}' |
| 79 | + result = remove_json_comments(input_json) |
| 80 | + # Verify the result is valid JSON |
| 81 | + parsed = json.loads(result) |
| 82 | + self.assertEqual(parsed["key"], "value with /* not a comment */") |
| 83 | + |
| 84 | + def test_remove_json_comments_nested_comments(self): |
| 85 | + # Test with nested comments (which aren't really supported in JSON) |
| 86 | + input_json = """{ |
| 87 | + /* outer comment /* inner comment */ */ |
| 88 | + "key": "value" |
| 89 | + }""" |
| 90 | + result = remove_json_comments(input_json) |
| 91 | + # Verify the json is valid after comment removal, we don't support nested comments |
| 92 | + with self.assertRaises(JSONDecodeError): |
| 93 | + json.loads(result) |
| 94 | + |
| 95 | + def test_remove_json_comments_escaped_quotes(self): |
| 96 | + # Test with escaped quotes |
| 97 | + input_json = '{"key": "value with \\" escaped quote // not a comment"}' |
| 98 | + result = remove_json_comments(input_json) |
| 99 | + # Verify the result is valid JSON |
| 100 | + parsed = json.loads(result) |
| 101 | + self.assertEqual(parsed["key"], 'value with " escaped quote // not a comment') |
| 102 | + |
| 103 | + def test_find_string_end_basic(self): |
| 104 | + # Test basic string end finding |
| 105 | + text = '"string" more' |
| 106 | + index = _find_string_end(text, 1) |
| 107 | + self.assertEqual(index, 8) |
| 108 | + self.assertEqual(text[1:index], 'string"') |
| 109 | + |
| 110 | + def test_find_string_end_escaped_quote(self): |
| 111 | + # Test with escaped quotes |
| 112 | + text = '"escaped \\" quote" end' |
| 113 | + index = _find_string_end(text, 1) |
| 114 | + self.assertEqual(index, 18) |
| 115 | + self.assertEqual(text[1:index], 'escaped \\" quote"') |
| 116 | + |
| 117 | + def test_find_string_end_unterminated(self): |
| 118 | + # Test with unterminated string |
| 119 | + text = '"unterminated string' |
| 120 | + with self.assertRaises(ValueError): |
| 121 | + _find_string_end(text, 1) |
| 122 | + |
| 123 | + def test_find_string_end_multiple_escapes(self): |
| 124 | + # Test with multiple escape characters |
| 125 | + text = '"multiple \\\\" end' |
| 126 | + index = _find_string_end(text, 1) |
| 127 | + self.assertEqual(index, 13) |
| 128 | + self.assertEqual(text[1:index], 'multiple \\\\"') |
| 129 | + |
| 130 | + def test_remove_json_comments_unterminated_string(self): |
| 131 | + # Test with unterminated string |
| 132 | + input_json = '{ "key": "unterminated string }' |
| 133 | + with self.assertRaises(ValueError): |
| 134 | + remove_json_comments(input_json) |
| 135 | + |
| 136 | + def test_remove_json_comments_unterminated_multiline(self): |
| 137 | + # Test with unterminated multi-line comment |
| 138 | + # This should raise a ValueError |
| 139 | + input_json = """{ |
| 140 | + "key": "value" |
| 141 | + /* unterminated comment |
| 142 | + }""" |
| 143 | + with self.assertRaises(ValueError): |
| 144 | + remove_json_comments(input_json) |
| 145 | + |
| 146 | + # Another test case for unterminated multi-line comment |
| 147 | + input_json2 = """{ |
| 148 | + "key1": "value1", |
| 149 | + "key2": "value2" /* unterminated comment |
| 150 | + }""" |
| 151 | + with self.assertRaises(ValueError): |
| 152 | + remove_json_comments(input_json2) |
0 commit comments