"""Tests for morse.py. Run: python3 -m unittest test_morse (from this folder).""" import contextlib import io import os import tempfile import unittest import wave import morse class Table(unittest.TestCase): def test_letters_digits_and_signs(self): self.assertEqual(morse.encode("SOS"), "... --- ...") self.assertEqual(morse.encode("paris"), ".--. .- .-. .. ...") self.assertEqual(morse.encode("0123456789"), "----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.") self.assertEqual(morse.encode("?"), "..--..") def test_codes_are_unique_except_prosign_twins(self): twins = {".-.-.", ".-...", "-...-", "-.--."} # AR/+, AS/&, BT/=, KN/( seen = {} for c, code, _ in morse.TABLE: if code in seen: self.assertIn(code, twins, f"{c} and {seen[code]}") seen[code] = c def test_round_trip_every_character(self): for c, code, _ in morse.TABLE: if c.startswith("<"): continue self.assertEqual(morse.decode(morse.encode(c)), c) class EncodeDecode(unittest.TestCase): def test_words_lines_and_unknowns(self): self.assertEqual(morse.encode("Hi there"), ".... .. / - .... . .-. .") self.assertEqual(morse.encode("a\nb"), ".-\n-...") self.assertEqual(morse.encode("a~b"), ".- # -...") self.assertEqual(morse.encode("“ok”"), ".-..-. --- -.- .-..-.") def test_prosigns_are_one_signal(self): self.assertEqual(morse.encode("73 "), "--... ...-- / ...-.-") self.assertEqual(morse.encode(""), "...-.") self.assertEqual(morse.decode("...-.-"), "") self.assertEqual(morse.decode(".-.-."), "+") # the character wins over AR def test_decode_separators_and_lookalikes(self): self.assertEqual(morse.decode(".... .. / - .... . .-. ."), "HI THERE") self.assertEqual(morse.decode(".... ..|-.-."), "HI C") self.assertEqual(morse.decode(".... .. -.-."), "HI C") self.assertEqual(morse.decode("··· ——— •••"), "SOS") self.assertEqual(morse.decode("...... x"), "##") class Timing(unittest.TestCase): def test_paris_is_50_units(self): self.assertEqual(morse.units(".--. .- .-. .. ..."), 50) self.assertEqual(morse.units("PARIS"), 50) def test_unit_is_1200_over_wpm(self): t = morse.timing(20) self.assertAlmostEqual(t["unit"], 60) self.assertAlmostEqual(t["dash"], 180) self.assertAlmostEqual(t["letter_gap"], 180) self.assertAlmostEqual(t["word_gap"], 420) def test_farnsworth_matches_the_arrl_formula(self): # The values in the table on https://morsekeyboard.com/morse-code-speed/#farnsworth for c, s, tc, tw in [(18, 5, 1.57, 3.66), (18, 10, 0.62, 1.45), (20, 13, 0.44, 1.02)]: gc, gw = morse.farnsworth_gaps(c, s) self.assertAlmostEqual(gc, tc, places=2) self.assertAlmostEqual(gw, tw, places=2) t = morse.timing(18, 10) self.assertAlmostEqual(t["dot"], 1200 / 18) self.assertAlmostEqual(t["letter_gap"], 621.05, places=1) self.assertEqual(morse.timing(18, 18)["letter_gap"], 3 * 1200 / 18) with self.assertRaises(ValueError): morse.farnsworth_gaps(10, 18) def test_schedule(self): segs = morse.schedule("ET", 20) self.assertEqual(segs, [(True, 60.0), (False, 180.0), (True, 180.0)]) self.assertEqual(morse.schedule("E T", 20)[1], (False, 420.0)) class Wav(unittest.TestCase): def test_writes_a_wav_of_the_right_length(self): with tempfile.TemporaryDirectory() as d: path = os.path.join(d, "paris.wav") secs = morse.write_wav("PARIS", path, wpm=20, pitch=700, sample_rate=8000) # 43 units of signal (50 less the trailing 7-unit gap) at 60 ms, plus 0.2 s of padding. self.assertAlmostEqual(secs, 43 * 0.06 + 0.2, places=2) with wave.open(path) as w: self.assertEqual(w.getnchannels(), 1) self.assertEqual(w.getsampwidth(), 2) self.assertEqual(w.getframerate(), 8000) self.assertAlmostEqual(w.getnframes() / 8000, secs, places=3) def test_cli(self): self.assertEqual(morse.main(["encode", "sos"]), 0) with tempfile.TemporaryDirectory() as d: self.assertEqual(morse.main(["wav", "CQ", os.path.join(d, "cq.wav"), "--wpm", "18", "--farnsworth", "10"]), 0) def test_cli_rejects_bad_input_with_a_usage_error(self): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "x.wav") for args in (["timing", "--wpm", "0"], ["timing", "--wpm", "-5"], ["wav", "CQ", out, "--volume", "-1"], ["wav", "", out], ["wav", "~~~", out], ["wav", "CQ", out, "--pitch", "4000"]): with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as cm: morse.main(args) self.assertEqual(cm.exception.code, 2, args) self.assertFalse(os.path.exists(out), "no file is written for bad input") def test_cli_notes_an_ignored_farnsworth(self): err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(morse.main(["timing", "--wpm", "10", "--farnsworth", "12"]), 0) self.assertIn("ignored", err.getvalue()) if __name__ == "__main__": unittest.main()