Skip to content

Commit 1bbeaac

Browse files
committed
Add __str__ for StarSystem and related tests
1 parent 73b57d9 commit 1bbeaac

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

openmoo2/objects/system.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ def supernova(self):
7070
self.color = 'black'
7171
self.__administrator = None
7272
self.__planets = {x + 1: None for x in range(StarSystem.max_planets)}
73+
74+
def __str__(self):
75+
if self.color == 'black':
76+
return "Black hole star system named: {!s}".format(self.name)
77+
tmpstring = "{!s} star named {!s} with planets:\n".format(self.color, self.name)
78+
tmpstring += "".join(["Orbit {!s}:\n {!s}\n".format(x, y) for x, y in self.planets.items() if y])
79+
if self.administrator:
80+
tmpstring += "Star system has assigned administrator: {!s}".format(self.administrator)
81+
return tmpstring

tests/test_system.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,23 @@ def test_supernova(self):
9393
eq_(star.administrator, None)
9494
eq_(star.color, 'black')
9595
eq_(star.planets, {x + 1: None for x in range(StarSystem.max_planets)})
96+
97+
def test_string_blackhole(self):
98+
eq_(str(self.blackhole), "Black hole star system named: hole")
99+
100+
def test_string_planets(self):
101+
star = StarSystem("gamma", "gelb")
102+
star.planets = self.planetA
103+
star.planets = self.planetD
104+
star.planets = self.planetB
105+
star.planets = self.planetC
106+
eq_(str(star), 'gelb star named gamma with planets:\nOrbit 1:\n Planeta : asteroids\nOrbit 3:\n Planeta : giant\nOrbit 4:\n Planeta : planet\n')
107+
108+
def test_string_planets_administrator(self):
109+
star = StarSystem("delta", "gelb")
110+
star.planets = self.planetA
111+
star.planets = self.planetD
112+
star.planets = self.planetB
113+
star.planets = self.planetC
114+
star.administrator = "cpt. Kirk"
115+
eq_(str(star), 'gelb star named delta with planets:\nOrbit 1:\n Planeta : asteroids\nOrbit 3:\n Planeta : giant\nOrbit 4:\n Planeta : planet\nStar system has assigned administrator: cpt. Kirk')

0 commit comments

Comments
 (0)