|
| 1 | +# English conventions for capitalizing titles can be a bit tricky. In this lesson you'll work on putting |
| 2 | +# some of these finnicky rules into practice. |
| 3 | +# Create a Book class that accepts a string parameter - the book's title - and assigns the correctly capitalized |
| 4 | +# version of that title as a `Book.title` attribute. |
| 5 | +# This is the first time you'll be working with classes in this course, so be sure to check out the Python documentation |
| 6 | +# for how to declare a class and how to assign instance variables like `title`. |
| 7 | + |
| 8 | +# Capitalization rules - we'll use the Chicago Manual of Style rules for this exercise |
| 9 | +# 1. Capitalize the first and last word |
| 10 | +# 2. Capitalize nouns, pronouns, adjectives, verbs, adverbs, and subordinate conjunctions (although, because) |
| 11 | +# 3. Lowercase articles (a, an, the), coordinating conjunctions (and, but, or), and prepositions |
| 12 | +# 4. Lowercase the `to` in an infinitive (Let's Learn to Play Chess) |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from books import Book |
| 19 | + |
| 20 | +class TestBookClass(): |
| 21 | + def test_can_create_book(self): |
| 22 | + book = Book("a") |
| 23 | + assert book is not None |
| 24 | + |
| 25 | + def test_capitalize_first_letter(self): |
| 26 | + book = Book("homegoing") |
| 27 | + assert book.title == "Homegoing" |
| 28 | + |
| 29 | + def test_capitalize_every_word(self): |
| 30 | + book = Book("animal farm") |
| 31 | + assert book.title == "Animal Farm" |
| 32 | + book = Book("true grit") |
| 33 | + assert book.title == "True Grit" |
| 34 | + |
| 35 | + def test_no_article_capitalization(self): |
| 36 | + book = Book("to kill a mockingbird") |
| 37 | + assert book.title == "To Kill a Mockingbird" |
| 38 | + |
| 39 | + def test_no_coordinating_conjunction_capitalization(self): |
| 40 | + book = Book("pride and prejudice") |
| 41 | + assert book.title == "Pride and Prejudice" |
| 42 | + |
| 43 | + def test_no_article_capitalization_except_beginning_of_title(self): |
| 44 | + book = Book("the great gatsby") |
| 45 | + assert book.title == "The Great Gatsby" |
| 46 | + book = Book("the sound and the fury") |
| 47 | + assert book.title == "The Sound and the Fury" |
| 48 | + |
| 49 | + def test_coordinating_conjunction_capitalized_as_first_word(self): |
| 50 | + book = Book("and then there were none") |
| 51 | + assert book.title == "And Then There Were None" |
| 52 | + |
| 53 | + def test_no_preposition_capitalization(self): |
| 54 | + book = Book("love in the time of cholera") |
| 55 | + assert book.title == "Love in the Time of Cholera" |
| 56 | + book = Book("one flew over the cuckoo's nest") |
| 57 | + assert book.title == "One Flew over the Cuckoo's Nest" |
| 58 | + |
| 59 | + def test_no_preposition_capitalization_except_beginning_of_title(self): |
| 60 | + book = Book("in cold blood") |
| 61 | + assert book.title == "In Cold Blood" |
| 62 | + book = Book("under the pendulum sun") |
| 63 | + assert book.title == "Under the Pendulum Sun" |
| 64 | + |
| 65 | + def test_subordinate_conjunction_is_capitalized(self): |
| 66 | + book = Book("because it is bitter, and because it is my heart") |
| 67 | + assert book.title == "Because It Is Bitter, and Because It Is My Heart" |
| 68 | + |
| 69 | + def test_always_capitalize_i(self): |
| 70 | + book = Book("everything i never told you") |
| 71 | + assert book.title == "Everything I Never Told You" |
| 72 | + |
| 73 | + def test_can_handle_mixed_case_input(self): |
| 74 | + book = Book("hArRy PoTtEr AnD tHe PhIlOsOpHeR's StOnE") |
| 75 | + assert book.title == "Harry Potter and the Philosopher's Stone" |
0 commit comments