def test_roundtrip(self): inp = StringIO(SAMPLE_FILE) first_pass = osufile.parse(inp, parser=self.parser_with_numbers) out = StringIO() osufile.write(out, first_pass, parser=self.parser_with_numbers) out.seek(0) second_pass = osufile.parse(out, parser=self.parser_with_numbers) self.assertEqual(first_pass, second_pass)
def roundtrip(self, sample): '''Run a roundtrip test on an .osu file (parse string -> write string -> parse string again -> check that first and second parse are the same)''' if isinstance(sample, osufile.OsuFile): expected_osu = sample elif isinstance(sample, str): expected_osu = self.parse_string(sample) else: expected_osu = osufile.parse(sample) output = StringIO() osufile.write(output, expected_osu) output.seek(0) actual_osu = osufile.parse(output) self.assertEqual(expected_osu, actual_osu)
def test_roundtrip(self): inp = StringIO(SAMPLE_FILE) first_pass = osufile.parse(inp, parser=self.my_parser) # check that floats were parsed into Decimal self.assertTrue( isinstance(first_pass['General']['StackLeniency'], Decimal)) # round trip out = StringIO() osufile.write(out, first_pass, parser=self.my_parser) out.seek(0) second_pass = osufile.parse(out, parser=self.my_parser) self.assertEqual(first_pass, second_pass)
def test_parse(self): as_path = self.SAMPLE_FILE as_str = str(as_path) with self.subTest('pathlib.Path'): osu_path = osufile.parse(as_path) with self.subTest('string filepath'): osu_str = osufile.parse(as_str) with self.subTest('file object'): with open(as_path, 'r', encoding='utf8') as as_fileobj: osu_fileobj = osufile.parse(as_fileobj) # check that they all parsed the same way self.assertEqual(osu_path, osu_str) self.assertEqual(osu_str, osu_fileobj)
def test_write(self): def read_sample_file(): with open(self.SAMPLE_OUT, 'r', encoding='utf8') as f: return f.read() # get some sample data to write out osu = osufile.parse(self.SAMPLE_FILE) as_path = self.SAMPLE_OUT as_str = str(as_path) with self.subTest('pathlib.Path'): osufile.write(as_path, osu) osu_path = read_sample_file() with self.subTest('string filepath'): osufile.write(as_str, osu) osu_str = read_sample_file() with self.subTest('file object'): as_fileobj = StringIO() osufile.write(as_fileobj, osu) osu_fileobj = as_fileobj.getvalue() # check that they all output the same way self.assertEqual(osu_path, osu_str) self.assertEqual(osu_str, osu_fileobj)
def get_osu(name): """ Get an OsuFile object by name. (For now names are just filenames of test files.) Objects are lazily generated and cached afterwards. """ # for now, name is the name of the file if name not in FILE_CACHE: FILE_CACHE[name] = osufile.parse(__CWD__ / 'files' / name) return FILE_CACHE[name]
def parse_string(self, s): '''Parse .osu file held as contents of string''' return osufile.parse(StringIO(s))