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_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 write_string(self, osu): s = StringIO() osufile.write(s, osu) return s.getvalue()