Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Mar 14, 2023
1 parent 77bc621 commit 8ef8cab
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions tests/test_canonicaljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock

from math import inf, nan

Expand All @@ -21,7 +22,7 @@
encode_pretty_printed_json,
iterencode_canonical_json,
iterencode_pretty_printed_json,
set_json_library,
set_json_library, register_preserialisation_callback,
)

import unittest
Expand Down Expand Up @@ -156,4 +157,32 @@ class C:
pass

with self.assertRaises(Exception):
encode_canonical_json(C())
encode_canonical_json(C())

def test_preserialisation_callback(self) -> None:
class C:
pass

register_preserialisation_callback(C, lambda c: "I am a C instance")

result = encode_canonical_json(C())
self.assertEqual(result, b'"I am a C instance"')

def test_cannot_register_preserialisation_callback_for_object(self) -> None:
with self.assertRaises(Exception):
register_preserialisation_callback(object, lambda c: "shouldn't be able to do this")

def test_most_recent_preserialisation_callback_called(self) -> None:
class C:
pass

callback1 = Mock(return_value = "callback 1 was called")
callback2 = Mock(return_value = "callback 2 was called")

register_preserialisation_callback(C, callback1)
register_preserialisation_callback(C, callback2)

encode_canonical_json(C())

callback1.assert_not_called()
callback2.assert_called_once()

0 comments on commit 8ef8cab

Please sign in to comment.