Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Python/src/pyJanus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ void init_Janus(py::module_ &m)
.def_property_readonly("xml_filename", &Janus::getXmlFileName)

.def("__repr__",
[](Janus &self)
[](const Janus &self)
{
std::ostringstream out;
out << "Janus(xml_filename='" << self.getXmlFileName() << ")"
out << "Janus(xml_filename='" << self.getXmlFileName() << "')"
<< " at JanusInstance<"
<< self.getVariableDef().front().getJanusInstance() << ">";
<< &self << ">";
return out.str();
})
.def("__str__",
[](const Janus &self)
{
std::ostringstream out;
out << "Janus(xml_filename='" << self.getXmlFileName() << ")";
out << "Janus(xml_filename='" << self.getXmlFileName() << "')";
return out.str();
});
}
39 changes: 39 additions & 0 deletions Python/tests/test_janus_representation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import re
import subprocess
import sys

import pyJanus


def test_empty_janus_repr():
# Isolate the call so a native crash is reported as a test failure instead
# of terminating the entire test suite.
result = subprocess.run(
[sys.executable, "-c", "import pyJanus; print(repr(pyJanus.Janus()))"],
capture_output=True,
text=True,
timeout=30,
)

assert result.returncode == 0, result.stderr
assert re.fullmatch(
r"Janus\(xml_filename=''\) at JanusInstance<[^<>\s]+>\n",
result.stdout,
)


def test_empty_janus_str():
text = str(pyJanus.Janus())
assert text == "Janus(xml_filename='')"


def test_loaded_janus_representation():
xml_path = f"{os.path.dirname(__file__)}/../../Examples/CombinedExample.xml"
janus = pyJanus.Janus(xml_path)
expected = f"Janus(xml_filename='{janus.xml_filename}')"
variable = janus.get_variabledef("angleOfAttack")
instance = repr(variable).split(" from ", 1)[1]

assert str(janus) == expected
assert repr(janus) == f"{expected} at {instance}"