|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Author : Luke McGuire <[email protected]> |
| 4 | +Date : 2024-06-29 |
| 5 | +Purpose: Southern fry text |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import re |
| 11 | + |
| 12 | + |
| 13 | +# -------------------------------------------------- |
| 14 | +def get_args(): |
| 15 | + """Get command-line arguments""" |
| 16 | + |
| 17 | + parser = argparse.ArgumentParser( |
| 18 | + description="Southern fry text", |
| 19 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 20 | + ) |
| 21 | + |
| 22 | + parser.add_argument("text", metavar="text", help="Input text or file") |
| 23 | + |
| 24 | + args = parser.parse_args() |
| 25 | + if os.path.isfile(args.text): |
| 26 | + with open(args.text, "rt", encoding="utf-8") as fh: |
| 27 | + args.text = fh.read().rstrip() |
| 28 | + |
| 29 | + return args |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------------------------- |
| 33 | +def drop_gs(text): |
| 34 | + """Drop the g's from two-syllable texts ending in 'ing'""" |
| 35 | + pattern = re.compile(r"\b(\w*[aeiou]+\w*in)g\b", flags=re.I) |
| 36 | + return pattern.sub(r"\1'", text) |
| 37 | + |
| 38 | + |
| 39 | +# -------------------------------------------------- |
| 40 | +def test_drop_gs(): |
| 41 | + """Test drop_gs function""" |
| 42 | + assert drop_gs("") == "" |
| 43 | + assert drop_gs("sing") == "sing" |
| 44 | + assert drop_gs("cooking") == "cookin'" |
| 45 | + assert drop_gs("SINGING") == "SINGIN'" |
| 46 | + |
| 47 | + |
| 48 | +def test_drop_gs_sentence(): |
| 49 | + """Test drop_gs function for full lines of text""" |
| 50 | + text = "That's some good looking chicken." |
| 51 | + expected_output = "That's some good lookin' chicken." |
| 52 | + assert drop_gs(text) == expected_output |
| 53 | + |
| 54 | + |
| 55 | +def test_drop_gs_first_word(): |
| 56 | + """Test drop_gs function for first word of sentence""" |
| 57 | + text = "Looking good, toots." |
| 58 | + expected_output = "Lookin' good, toots." |
| 59 | + assert drop_gs(text) == expected_output |
| 60 | + |
| 61 | + |
| 62 | +def test_drop_gs_punctuation(): |
| 63 | + """Test drop_gs function for words followed by punctuation""" |
| 64 | + text = "Hey good looking, what's cooking?" |
| 65 | + expected_output = "Hey good lookin', what's cookin'?" |
| 66 | + assert drop_gs(text) == expected_output |
| 67 | + |
| 68 | + |
| 69 | +# -------------------------------------------------- |
| 70 | +def hey_yall(text): |
| 71 | + """Change 'you' to 'y'all'""" |
| 72 | + pattern = re.compile(r"\b([yY])ou\b") |
| 73 | + return pattern.sub(r"\1'all", text) |
| 74 | + |
| 75 | + |
| 76 | +# -------------------------------------------------- |
| 77 | +def test_hey_yall_one_match(): |
| 78 | + """Test case 1: The text contains exactly one 'you'""" |
| 79 | + text = "You are learning Python." |
| 80 | + expected_output = "Y'all are learning Python." |
| 81 | + assert hey_yall(text) == expected_output |
| 82 | + |
| 83 | + |
| 84 | +def test_hey_yall_no_match(): |
| 85 | + """Test case 2: The text does not contain 'you'""" |
| 86 | + text = "Hello world!" |
| 87 | + expected_output = "Hello world!" |
| 88 | + assert hey_yall(text) == expected_output |
| 89 | + |
| 90 | + |
| 91 | +def test_hey_yall_multiple_matches(): |
| 92 | + """ |
| 93 | + Test case 3: The text contains multiple 'you' but |
| 94 | + only the last one should be replaced |
| 95 | + """ |
| 96 | + text = "You are so much fun. You're going to have a great time!" |
| 97 | + expected_output = "Y'all are so much fun. " "Y'all're going to have a great time!" |
| 98 | + assert hey_yall(text) == expected_output |
| 99 | + |
| 100 | + |
| 101 | +def test_hey_yall_match_case(): |
| 102 | + """ |
| 103 | + Test case 4: The text contains 'You', but it should be |
| 104 | + replaced with 'Y'all' as the function is case-sensitive |
| 105 | + """ |
| 106 | + text = "You are awesome." |
| 107 | + expected_output = "Y'all are awesome." |
| 108 | + assert hey_yall(text) == expected_output |
| 109 | + |
| 110 | + |
| 111 | +def test_hey_yall_your(): |
| 112 | + """Test case 5: The text contains 'your'""" |
| 113 | + text = "You should watch your mouth." |
| 114 | + expected_output = "Y'all should watch your mouth." |
| 115 | + assert hey_yall(text) == expected_output |
| 116 | + |
| 117 | + |
| 118 | +def test_hey_yall_empty_string(): |
| 119 | + """Test case 6: The input text is an empty string""" |
| 120 | + text = "" |
| 121 | + expected_output = "" |
| 122 | + assert hey_yall(text) == expected_output |
| 123 | + |
| 124 | + |
| 125 | +# -------------------------------------------------- |
| 126 | +def main(): |
| 127 | + """Make a jazz noise here""" |
| 128 | + |
| 129 | + args = get_args() |
| 130 | + for line in args.text.splitlines(): |
| 131 | + print(hey_yall(drop_gs(line))) |
| 132 | + |
| 133 | + |
| 134 | +# -------------------------------------------------- |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments