예제 #1
0
def test_inline_dict(encoder_cls):

	class TestDict(dict, InlineTableDict):
		pass

	t = copy.deepcopy(TEST_DICT)
	t['d'] = TestDict()
	t['d']['x'] = "abc"
	o: Dict[str, Any] = loads(dumps(t, encoder=encoder_cls))
	assert o == loads(dumps(o, encoder=encoder_cls))
예제 #2
0
def test_dict_decoder():

	class TestDict(dict):
		pass

	test_dict_decoder = TomlDecoder(TestDict)
	assert isinstance(loads(TEST_STR, decoder=test_dict_decoder), TestDict)
예제 #3
0
def test_decoder():

	config = dedent("""\
	[project]
	license = {file = "LICENSE"}
	""")

	data = loads(config)["project"]
	assert isinstance(data, dict)
	assert isinstance(data["license"], dict)
	assert isinstance(data["license"], InlineTableDict)

	data = loads(config, decoder=TomlPureDecoder)["project"]
	assert isinstance(data, dict)
	assert isinstance(data["license"], dict)
	assert not isinstance(data["license"], InlineTableDict)
예제 #4
0
def test_parse_valid_config(
    toml_config: str,
    tmp_pathplus: PathPlus,
    advanced_data_regression: AdvancedDataRegressionFixture,
):
    (tmp_pathplus / "pyproject.toml").write_clean(toml_config)
    config = PEP621Parser().parse(
        dom_toml.loads(toml_config, decoder=TomlPureDecoder)["project"])
    advanced_data_regression.check(config)
예제 #5
0
def test_decimal():
	PLACES = Decimal(10)**-4

	d = {'a': Decimal("0.1")}
	o: Dict[str, Any] = loads(dumps(d))
	assert o == loads(dumps(o))
	assert Decimal(o['a']).quantize(PLACES) == d['a'].quantize(PLACES)

	with pytest.raises(TypeError):
		loads(2)  # type: ignore[call-overload]

	with pytest.raises(TypeError, match="expected str, bytes or os.PathLike object, not int"):
		load(2)  # type: ignore[call-overload]

	with pytest.raises(TypeError, match="expected str, bytes or os.PathLike object, not list"):
		load([])  # type: ignore[call-overload]

	with pytest.raises(
			TypeError,
			match="argument should be a str object or an os.PathLike object returning str, not <class 'bytes'>"
			):
		load(b"test.toml")  # type: ignore[call-overload]
예제 #6
0
def test_numpy_ints():
	np = pytest.importorskip("numpy")

	encoder = TomlNumpyEncoder()
	d = {'a': np.array([1, 3], dtype=np.int64)}
	o: Dict[str, Any] = loads(dumps(d, encoder=encoder))
	assert o == loads(dumps(o, encoder=encoder))

	d = {'a': np.array([1, 3], dtype=np.int32)}
	o = loads(dumps(d, encoder=encoder))
	assert o == loads(dumps(o, encoder=encoder))

	d = {'a': np.array([1, 3], dtype=np.int16)}
	o = loads(dumps(d, encoder=encoder))
	assert o == loads(dumps(o, encoder=encoder))
예제 #7
0
def test__dict():

	class TestDict(dict):
		pass

	assert isinstance(loads(TEST_STR, dict_=TestDict), TestDict)
예제 #8
0
def test_bug_196():
	d = datetime.datetime.now()
	bug_dict = {'x': d}
	round_trip_bug_dict: Dict[str, Any] = loads(dumps(bug_dict))
	assert round_trip_bug_dict == bug_dict
	assert round_trip_bug_dict['x'] == bug_dict['x']
예제 #9
0
def test_deepcopy_timezone():
	o: Dict[str, Any] = loads("dob = 1979-05-24T07:32:00-08:00")
	o2: Dict[str, Any] = copy.deepcopy(o)
	assert o2["dob"] == o["dob"]
	assert o2["dob"] is not o["dob"]
예제 #10
0
def test_commutativity():
	o: Dict[str, Any] = loads(dumps(TEST_DICT))
	assert o == loads(dumps(o))
예제 #11
0
def test_tuple():
	d = {'a': (3, 4)}
	o: Dict[str, Any] = loads(dumps(d))
	assert o == loads(dumps(o))
예제 #12
0
def test_ordered(encoder_cls, decoder_cls):
	o: Dict[str, Any] = loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls)
	assert o == loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls)
예제 #13
0
def test_array_sep(encoder_cls):
	d = {'a': [1, 2, 3]}
	o: Dict[str, Any] = loads(dumps(d, encoder=encoder_cls))
	assert o == loads(dumps(o, encoder=encoder_cls))
예제 #14
0
def test_parse_config_errors(config: str, expects: Type[Exception], match: str,
                             tmp_pathplus: PathPlus):
    with pytest.raises(expects, match=match):
        PEP621Parser().parse(dom_toml.loads(config)["project"],
                             set_defaults=True)
예제 #15
0
 def test_loads_decoder_none(self):
     with pytest.warns(DeprecationWarning, match=self.match):
         assert dom_toml.loads(
             "hello = 'world'",
             decoder=None) == self.expected  # type: ignore[call-overload]