Skip to content

Commit

Permalink
Merge pull request #128 from CyberiaResurrection/ExtensionsWellFormed
Browse files Browse the repository at this point in the history
Check economic/social extensions are well formed in Star.is_well_formed()
  • Loading branch information
CyberiaResurrection authored Nov 16, 2024
2 parents 9fb6003 + 58de9c9 commit ff47316
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
9 changes: 9 additions & 0 deletions PyRoute/Star.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,15 @@ def is_well_formed(self):
assert self.hex is not None, "Star " + str(self.name) + " is missing hex attribute"
result, msg = self.hex.is_well_formed()
assert result, msg
assert hasattr(self, 'economics'), "Star " + str(self.name) + " is missing economics attribute"
assert hasattr(self, 'social'), "Star " + str(self.name) + " is missing social attribute"
if self.economics is not None:
assert (isinstance(self.economics, str) and 7 == len(self.economics)),\
"Star " + str(self.name) + " economics must be None or 7-char string"
if self.social is not None:
assert (isinstance(self.social, str) and 6 == len(self.social)),\
"Star " + str(self.name) + " social must be None or 6-char string"

assert hasattr(self, 'allegiance_base'), "Star " + str(self.name) + " is missing base allegiance attribute"
assert self.allegiance_base is not None, "Star " + str(self.name) + " has empty base allegiance attribute"
result, msg = self.uwp.is_well_formed()
Expand Down
13 changes: 12 additions & 1 deletion Tests/Hypothesis/Inputs/testHypothesisStarlineParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def comparison_line(draw):
' 0 0000000-0 (00000000000000 - (000-0) - B A A 000 0?'
'0111 -2Z4ig11RbxW010 0000001-1 wJED9E(E(T (HN6 - (113-0) - - - B 114 00y',
'0000 000000000000000 ???????-? (00000000000 )B - - A 000 ?00',
'0000 000000000000000 ???????-? (0 0000000000)A - - A 000 ?0'
'0000 000000000000000 ???????-? (0 0000000000)A - - A 000 ?0',
'2732 sa0)OkeWHEk4Mmb ???????-? (8PvzPS]UbZypt6 - (8Pv-2) [GZks] f * g 88C E7',
'0000 000000000000000 ???????-? [00000000000]0] - - A 000 ?0',
'0000 000000000000000 ???????-? (00000000000000 - (000-0) [0000] BB - A 000 ?0'
]

candidate = draw(from_regex(regex=ParseStarInput.starline, alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ -{}()[]?\'+*'))
Expand All @@ -89,6 +92,10 @@ def comparison_line(draw):
assume(False) # Skip generating cases where all three extensions end up in trade codes
if '*' in data[3]:
assume(False)
if ('(' in data[3] and ')' not in data[3]) or ('(' not in data[3] and ')' in data[3]):
assume(False)
if ('[' in data[3] and ']' not in data[3]) or ('[' not in data[3] and ']' in data[3]):
assume(False)
bitz = data[3].split(' ')
numbitz = len(bitz)
singleton = [item for item in bitz if 1 == len(item)]
Expand Down Expand Up @@ -312,6 +319,7 @@ class testHypothesisStarlineParser(unittest.TestCase):
@example('0000 000000000000000 ???????-? 000000000000000 {0} - [0000] - A 000 ?0', False)
@example('0001 000000000000000 ???????-? 000000000000000 - - A 000 --0', False)
@example('0111 -2Z4ig11RbxW010 0000001-1 wJED9E(E(T (HN6 - (113-0) - - - B 114 00y', False)
@example('0000 000000000000000 ???????-? [0000000000]00] - - A 000 ?0', False)
# Weird parsing cases
@example('0000 000000000000000 ???????-? (00000000000000 - - 0 000 00?)', 'weird')
@example('0000 000000000000000 ???????-? [00000000000000 - - 0 000 00?]', 'weird')
Expand All @@ -326,6 +334,7 @@ class testHypothesisStarlineParser(unittest.TestCase):
@example('0000 000000000000000 ???????-? 000000000000000 - - A 000 000?000-0 ', 'weird')
@example('0000 000000000000000 0000000-0 000000000000000 - - A 000 00?0000-0 ', 'weird')
@example('2234 00001111111111[ 1234446-N (111114GK\'ehilp - (005-1) - B - U 112 WW', 'weird')
@example('2732 sa0)OkeWHEk4Mmb ???????-? (8PvzPS]UbZypt6 - (8Pv-2) [GZks] f * g 88C E7', 'weird')
@example('0000 000000000000000 ???????-? (0 000000000)0? - - A 000 00?', 'weird')
@example('0000 000000000000000 ???????-? 000000000000 (0 - (000-0) [0000] B - A 000 00?', 'weird')
@example('0000 000000000000000 0000000-0 (00000000000000 B - A 000 ?0)0000000000', 'weird')
Expand All @@ -335,6 +344,8 @@ class testHypothesisStarlineParser(unittest.TestCase):
@example('0000 000000000000000 0000000-0 (00000000000000 - (000-0) - B A A 000 0?', 'weird')
@example('0000 000000000000000 ???????-? (00000000000 )B - - A 000 ?00', 'weird')
@example('0000 000000000000000 ???????-? (0 0000000000)A - - A 000 ?0', 'weird')
@example('0000 000000000000000 ???????-? [00000000000]0] - - A 000 ?0', 'weird')
@example('0000 000000000000000 ???????-? (00000000000000 - (000-0) [0000] BB - A 000 ?0', 'weird')
def test_starline_parser_against_regex(self, s, match):
# if it's a known weird-parse case, assume it out now
assume(match != 'weird')
Expand Down
55 changes: 54 additions & 1 deletion Tests/Hypothesis/testStar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
from datetime import timedelta

from hypothesis import given, assume, example, HealthCheck, settings
from hypothesis.strategies import text, from_regex, composite, booleans
from hypothesis.strategies import text, from_regex, composite, booleans, none

from PyRoute.AreaItems.Sector import Sector
from PyRoute.Inputs.ParseStarInput import ParseStarInput
from PyRoute.Position.Hex import Hex
from PyRoute.Star import Star
from PyRoute.SystemData.StarList import StarList
from PyRoute.SystemData.UWP import UWP
from PyRoute.TradeCodes import TradeCodes

@composite
def importance_starline(draw):
Expand Down Expand Up @@ -255,6 +259,55 @@ def test_star_canonicalise(self, s):
hyp_line
)

@given(from_regex(UWP.match, alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ -{}()[]?\'+*'),
from_regex(r'^\([0-9A-Za-z]{3}[+-]\d\)'),
from_regex(r'^\[[0-9A-Za-z]{4}\]'),
none())
@example('?000000-0', '(000-0)', '[0000]', None)
@example('?000000-0', '(000-0)0', '[0000]', 'Star Sample economics must be None or 7-char string')
@example('?000000-0', '(000-0)', '[0000]0', 'Star Sample social must be None or 6-char string')
@example('?000000-0', '000-0)', '[0000]', 'Star Sample economics must be None or 7-char string')
@example('?000000-0', '(000-0)', '[000]', 'Star Sample social must be None or 6-char string')
def test_check_economics_social_and_ru(self, uwp, ex, cx, well_formed_kaboom):
assume('?' not in uwp)
uwp_obj = None

try:
uwp_obj = UWP(uwp)
except ValueError:
pass
assume(uwp_obj is not None)

if well_formed_kaboom is None:
ex = ex[0:7]
cx = cx[0:6]

foo = Star()
foo.name = 'Sample'
foo.sector = Sector('# Core', '# 0, 0')
foo.hex = Hex(foo.sector, '0101')
foo.alg_base_code = 'Na'
foo.uwp = uwp_obj
foo.tradeCode = TradeCodes('')
foo.index = 0
foo.allegiance_base = foo.alg_base_code
foo.star_list_object = StarList('')
foo.economics = ex
foo.social = cx
try:
self.assertTrue(foo.is_well_formed())
except AssertionError as e:
if well_formed_kaboom is not None:
self.assertEqual(well_formed_kaboom, str(e), "Unexpected well-formed check error")
return
else:
raise e

foo.calculate_importance()
foo.check_ex()
foo.check_cx()
foo.calculate_ru('fixed')


if __name__ == '__main__':
unittest.main()

0 comments on commit ff47316

Please sign in to comment.