Files
paperspec/tests/test_jcs.py
T
alexmandClaude 4fb8b4f5a7 Add paperspec correct, check math forms and datasets, and give the tools public names
The work the other paper tools planned for paperspec (paperdesk PLAN M5.4,
paperform PLAN 4.2):

- paperspec correct and paperspec.revision apply proposed corrections to a
  package, folder or zip, and write it only once it validates.
- doc.math.form is checked: label and style commands always, the form's own
  spelling when papermath is installed or forms= is passed.
- paperspec.datasets and seven doc.dataset.* rules check a package's datasets.
- fixtures/invalid gains suspect-witness and math-form-label (33 in all).
- Canonical JSON reads numbers as doubles, so 1e20 round-trips.
- The registries and the JSON-LD context ship in the wheel.
- Public names for what paperdesk and paperform imported as private ones,
  with the old names kept as aliases.
- The text and lossy-projection functions are about three times faster
  through the gate, tested equal to the old code on every code point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013XwDhn4LkrKQfw4ArDEr2E
2026-10-20 22:10:17 -04:00

272 lines
8.8 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Scienting
#
# Source Code: https://git.scient.ing/papers/paperspec
# See the LICENSE.md file for full license terms.
"""Canonical JSON: the vectors, the refusals of spec/01 §1.1, and round-trip properties."""
import json
import math
import re
import pytest
from hypothesis import given, settings, strategies as st
from helpers_lib1 import parse_doubles, vectors
from paperspec import jcs
PROPERTIES = settings(derandomize=True, max_examples=200, database=None, deadline=None)
JSON_VALUES = st.recursive(
st.none()
| st.booleans()
| st.integers(min_value=-(2**53), max_value=2**53)
| st.floats(allow_nan=False, allow_infinity=False)
| st.text(),
lambda children: (
st.lists(children, max_size=4)
| st.dictionaries(st.text(), children, max_size=4)
),
max_leaves=12,
)
@pytest.mark.parametrize("case", vectors("canonical-json"))
def test_canonical_json_vector(case):
assert jcs.dumps(parse_doubles(case["input_json"])) == case["output"]
@pytest.mark.parametrize(
("number", "text"),
[
(0.0, "0"),
(1.0, "1"),
(100.0, "100"),
(1.5, "1.5"),
(-2.5, "-2.5"),
(0.1 + 0.2, "0.30000000000000004"),
(4.35, "4.35"),
(1e20, "100000000000000000000"),
(1e21, "1e+21"),
(1.2345678901234568e22, "1.2345678901234568e+22"),
(123456789012345680000.0, "123456789012345680000"),
(0.000001, "0.000001"),
(1e-7, "1e-7"),
(1.5e-7, "1.5e-7"),
(-2.5e-5, "-0.000025"),
(5e-324, "5e-324"),
(1.7976931348623157e308, "1.7976931348623157e+308"),
(2.0**53, "9007199254740992"),
(2.0**60, "1152921504606847000"),
],
)
def test_floats_are_written_as_ecmascript_writes_them(number, text):
assert jcs.dumps(number) == text
assert jcs.dumps([number, "x"]) == f'[{text},"x"]'
def test_negative_zero_is_written_as_zero():
assert jcs.dumps(-0.0) == "0"
assert jcs.dumps([-0.0, "x"]) == '[0,"x"]'
@pytest.mark.parametrize("number", [2**53, -(2**53), 0, -1, 12345])
def test_integers_in_the_exact_range_are_written_in_full(number):
assert jcs.dumps(number) == str(number)
@pytest.mark.parametrize("number", [2**53 + 1, -(2**53) - 1, 10**30])
def test_integers_beyond_the_exact_range_are_refused(number):
with pytest.raises(ValueError, match="integer outside the IEEE 754 exact range"):
jcs.dumps(number)
with pytest.raises(ValueError, match="integer outside the IEEE 754 exact range"):
jcs.dumps([1.5, number])
@pytest.mark.parametrize("number", [math.nan, math.inf, -math.inf])
def test_nan_and_infinity_have_no_canonical_form(number):
with pytest.raises(ValueError, match="NaN and Infinity are not JSON"):
jcs.dumps(number)
with pytest.raises(ValueError, match="NaN and Infinity are not JSON"):
jcs.dumps({"a": [number]})
@pytest.mark.parametrize(
"lone",
[chr(0xD800), chr(0xDFFF), "a" + chr(0xDC00) + "b"],
ids=["high", "low", "inside"],
)
def test_a_lone_surrogate_has_no_canonical_form(lone):
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps(lone)
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps({lone: 1})
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps([1.5, lone])
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumpb([lone])
def test_a_surrogate_pair_held_as_two_code_points_is_two_lone_surrogates():
with pytest.raises(ValueError, match="lone surrogate"):
jcs.dumps(chr(0xD83D) + chr(0xDE00))
def test_object_keys_must_be_strings():
with pytest.raises(TypeError, match="object keys must be strings"):
jcs.dumps({1: "a"})
with pytest.raises(TypeError, match="object keys must be strings"):
jcs.dumps({"a": 1.5, 2: 3})
@pytest.mark.parametrize("value", [{1, 2}, b"bytes", object(), 1 + 2j])
def test_values_outside_json_are_refused(value):
with pytest.raises(TypeError, match="not JSON"):
jcs.dumps(value)
with pytest.raises(TypeError, match="not JSON"):
jcs.dumps([1.5, value])
def test_literals_and_containers():
assert jcs.dumps(None) == "null"
assert jcs.dumps(True) == "true"
assert jcs.dumps(False) == "false"
assert jcs.dumps([]) == "[]"
assert jcs.dumps({}) == "{}"
assert jcs.dumps((1, "a")) == '[1,"a"]'
assert jcs.dumps([1.5, True, None, False]) == "[1.5,true,null,false]"
assert jcs.dumps([1.5, (2, 3)]) == "[1.5,[2,3]]"
def test_members_are_sorted_by_utf16_code_units_not_code_points():
# U+FF5E sorts after the astral U+1F600 in code points, before it in UTF-16.
value = {"\U0001f600": 1, "\N{FULLWIDTH TILDE}": 2, "": 3}
assert jcs.dumps(value) == '{"\U0001f600":1,"":3,"\N{FULLWIDTH TILDE}":2}'
assert sorted(value) == ["", "\N{FULLWIDTH TILDE}", "\U0001f600"]
def test_members_are_sorted_in_nested_objects_and_with_floats_present():
value = {"b": {"z": 1, "a": 2.5}, "a": [{"y": 1, "x": 2}]}
assert jcs.dumps(value) == '{"a":[{"x":2,"y":1}],"b":{"a":2.5,"z":1}}'
def test_strings_use_the_short_escapes_and_lower_case_unicode_escapes():
s = '\b\t\n\f\r"\\\x01\x1f\x7f/\N{LINE SEPARATOR}'
expected = '"\\b\\t\\n\\f\\r\\"\\\\\\u0001\\u001f\x7f/\N{LINE SEPARATOR}"'
assert jcs.dumps(s) == expected
assert jcs.dumps([1.5, s]) == "[1.5," + expected + "]"
def test_non_ascii_text_is_written_as_it_is():
assert jcs.dumps("café \U0001f600") == '"café \U0001f600"'
assert jcs.dumpb("é") == b'"\xc3\xa9"'
def test_dumpb_is_the_utf8_of_dumps():
value = {"k": ["é", 1.25]}
assert jcs.dumpb(value) == jcs.dumps(value).encode("utf-8")
@given(JSON_VALUES)
@PROPERTIES
def test_canonical_text_parses_back_to_the_value(value):
assert parse_doubles(jcs.dumps(value)) == value
@given(JSON_VALUES)
@PROPERTIES
def test_canonical_json_is_a_fixed_point(value):
once = jcs.dumps(value)
assert jcs.dumps(parse_doubles(once)) == once
@given(st.text())
@PROPERTIES
def test_strings_agree_whichever_path_writes_them(s):
assert jcs.dumps([1.5, s]) == "[1.5," + jcs.dumps(s) + "]"
@given(st.dictionaries(st.text(), st.integers(min_value=0, max_value=9), max_size=6))
@PROPERTIES
def test_members_come_out_in_utf16_order(value):
keys = list(json.loads(jcs.dumps(value)))
assert keys == sorted(value, key=lambda k: k.encode("utf-16-be"))
@given(st.floats(allow_nan=False, allow_infinity=False))
@PROPERTIES
def test_a_float_is_written_in_the_shortest_form_that_reads_back_exactly(x):
text = jcs.dumps(x)
assert float(text) == x
assert re.fullmatch(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?(e[+-][0-9]+)?", text)
@given(st.floats(min_value=-(2.0**53), max_value=2.0**53).filter(float.is_integer))
@PROPERTIES
def test_a_whole_float_is_written_like_an_integer(x):
assert jcs.dumps(x) == str(int(x))
def test_loads_line_reads_canonical_lines():
line = b'{"a":[1,2.5,null],"b":"\xc3\xa9"}'
assert jcs.loads_line(line) == {"a": [1, 2.5, None], "b": "é"}
@pytest.mark.parametrize(
"line",
[
b'{"b":1,"a":2}',
b'{"a": 1}',
b'{"a":1.0}',
b'{"a":1e2}',
b'{"a":"\\u00e9"}',
b'{"a":"\\/"}',
b" 1",
],
)
def test_loads_line_refuses_a_line_that_is_not_canonical(line):
with pytest.raises(ValueError, match="not canonical JSON"):
jcs.loads_line(line)
@pytest.mark.parametrize("x", [1e20, -1e20, 2.0**60, 123456789012345680000.0])
def test_loads_line_reads_back_a_whole_double_beyond_2_53(x):
# The canonical form of 1e20 is 100000000000000000000, an integer literal beyond 2^53,
# which is read as the double it is (spec/01 §1.1), not refused.
line = jcs.dumpb({"a": x})
assert jcs.loads_line(line) == {"a": x}
def test_loads_line_refuses_an_integer_beyond_2_53_that_is_no_double():
with pytest.raises(ValueError, match="not canonical JSON"):
jcs.loads_line(b"9007199254740993")
@pytest.mark.parametrize("text", ["NaN", "[Infinity]", '{"a":-Infinity}'])
def test_loads_refuses_nan_and_the_infinities(text):
with pytest.raises(ValueError, match="no canonical form"):
jcs.loads(text)
def test_loads_reads_numbers_as_doubles_without_requiring_canonical_text():
assert jcs.loads('{ "b": 100000000000000000000, "a": 7 }') == {"a": 7, "b": 1e20}
assert type(jcs.loads("100000000000000000000")) is float
assert type(jcs.loads("9007199254740992")) is int
@given(JSON_VALUES)
@PROPERTIES
def test_loads_line_reads_back_every_canonical_line(value):
assert jcs.dumps(jcs.loads_line(jcs.dumpb(value))) == jcs.dumps(value)
def test_loads_line_refuses_bytes_that_are_not_utf8():
with pytest.raises(UnicodeDecodeError):
jcs.loads_line(b'"\xff"')
def test_loads_line_refuses_text_that_is_not_json():
with pytest.raises(json.JSONDecodeError):
jcs.loads_line(b"{")