81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
"""Tests for the formatter module."""
|
|
|
|
import json
|
|
import pytest
|
|
|
|
from configconverter.formatters import Formatter
|
|
from configconverter.exceptions import IndentationError
|
|
|
|
|
|
class TestFormatter:
|
|
"""Tests for the Formatter class."""
|
|
|
|
@pytest.fixture
|
|
def formatter(self):
|
|
return Formatter()
|
|
|
|
def test_format_json_with_indent_2(self, formatter):
|
|
content = '{"name":"test","value":123}'
|
|
result = formatter.format(content, indent=2, format="json")
|
|
assert "\n " in result
|
|
|
|
def test_format_json_with_indent_4(self, formatter):
|
|
content = '{"name":"test","value":123}'
|
|
result = formatter.format(content, indent=4, format="json")
|
|
assert "\n " in result
|
|
|
|
def test_format_json_compact(self, formatter):
|
|
content = '{"name":"test","value":123}'
|
|
result = formatter.format(content, indent=2, compact=True, format="json")
|
|
assert result.strip() == '{"name":"test","value":123}'
|
|
|
|
def test_format_invalid_indent(self, formatter):
|
|
content = '{"name":"test"}'
|
|
with pytest.raises(IndentationError):
|
|
formatter.format(content, indent=3, format="json")
|
|
|
|
def test_format_json_to_yaml(self, formatter):
|
|
content = '{"name": "test", "value": 123}'
|
|
result = formatter.format(content, format="yaml")
|
|
assert "name: test" in result
|
|
|
|
def test_format_yaml_to_json(self, formatter):
|
|
content = 'name: test\nvalue: 123\n'
|
|
result = formatter.format(content, format="json")
|
|
data = json.loads(result)
|
|
assert data["name"] == "test"
|
|
assert data["value"] == 123
|
|
|
|
def test_format_toml_to_json(self, formatter):
|
|
content = 'name = "test"\nvalue = 123\n'
|
|
result = formatter.format(content, format="json")
|
|
data = json.loads(result)
|
|
assert data["name"] == "test"
|
|
assert data["value"] == 123
|
|
|
|
def test_to_json_method(self, formatter):
|
|
content = 'name: test\n'
|
|
result = formatter.to_json(content, indent=4)
|
|
data = json.loads(result)
|
|
assert data["name"] == "test"
|
|
|
|
def test_to_yaml_method(self, formatter):
|
|
content = '{"name": "test"}'
|
|
result = formatter.to_yaml(content)
|
|
assert "name: test" in result
|
|
|
|
def test_to_toml_method(self, formatter):
|
|
content = '{"name": "test"}'
|
|
result = formatter.to_toml(content)
|
|
assert 'name = "test"' in result
|
|
|
|
def test_nested_structure_formatting(self, formatter):
|
|
content = '{"a":{"b":{"c":"d"}}}'
|
|
result = formatter.format(content, indent=2, format="json")
|
|
assert '"c": "d"' in result
|
|
|
|
def test_array_formatting(self, formatter):
|
|
content = '[1,2,3,4,5]'
|
|
result = formatter.format(content, indent=2, format="json")
|
|
assert "[\n 1," in result
|