Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. require 'book'
  2.  
  3. describe Book do
  4.  
  5. before do
  6. @book = Book.new
  7. end
  8.  
  9. describe 'title' do
  10. it 'should capitalize the first letter' do
  11. @book.title = "inferno"
  12. expect(@book.title).to eq("Inferno")
  13. end
  14.  
  15. it 'should capitalize every word' do
  16. @book.title = "stuart little"
  17. expect(@book.title).to eq("Stuart Little")
  18. end
  19.  
  20. describe 'should capitalize every word except...' do
  21. describe 'articles' do
  22. specify 'the' do
  23. @book.title = "alexander the great"
  24. expect(@book.title).to eq("Alexander the Great")
  25. end
  26.  
  27. specify 'a' do
  28. @book.title = "to kill a mockingbird"
  29. expect(@book.title).to eq("To Kill a Mockingbird")
  30. end
  31.  
  32. specify 'an' do
  33. @book.title = "to eat an apple a day"
  34. expect(@book.title).to eq("To Eat an Apple a Day")
  35. end
  36. end
  37.  
  38. specify 'conjunctions' do
  39. @book.title = "war and peace"
  40. expect(@book.title).to eq("War and Peace")
  41. end
  42.  
  43. specify 'prepositions' do
  44. @book.title = "love in the time of cholera"
  45. expect(@book.title).to eq("Love in the Time of Cholera")
  46. end
  47. end
  48.  
  49. describe 'should always capitalize...' do
  50. specify 'I' do
  51. @book.title = "what i wish i knew when i was 20"
  52. expect(@book.title).to eq("What I Wish I Knew When I Was 20")
  53. end
  54.  
  55. specify 'the first word' do
  56. @book.title = "the man in the iron mask"
  57. expect(@book.title).to eq("The Man in the Iron Mask")
  58. end
  59. end
  60. end
  61. end
  62.  
  63. class Book
  64. attr_accessor :title
  65.  
  66. def title=(book)
  67. lowercase = ["and", "or", "the", "of", "in", "a", "an"]
  68. @title = book.capitalize.split(" ").map do |word|
  69. if lowercase.include? word
  70. word
  71. else
  72. word.capitalize
  73. end
  74. end.join(" ")
  75. end
  76.  
  77. end
  78.  
  79. LOWERCASE = %w(and or the of in a an).freeze
  80.  
  81. class Book
  82. attr_accessor :title
  83.  
  84. LOWERCASE = %w(and or the of in a an).freeze
  85.  
  86. def title=(book)
  87. @title = book.capitalize.split.map do |word|
  88. next word if LOWERCASE.include?(word)
  89. word.capitalize
  90. end.join(' ')
  91. end
  92.  
  93. end
  94.  
  95. RSpec.shared_examples 'capitalize' do |message, text, title|
  96. let!(:book) { described_class.new }
  97. let!(:assign_title) { book.title = text }
  98.  
  99. it message do
  100. expect(subject).to eq(title)
  101. end
  102. end
  103.  
  104. RSpec.describe Book do
  105. context 'title' do
  106. subject { book.title }
  107.  
  108. it_behaves_like 'capitalize', 'first letter', 'inferno', 'Inferno'
  109. it_behaves_like 'capitalize', 'every word', 'stuart little', 'Stuart Little'
  110.  
  111. context 'articles' do
  112. it_behaves_like 'capitalize', 'except "the"', 'alexander the great', 'Alexander the Great'
  113. it_behaves_like 'capitalize', 'except "a"', 'to kill a mockingbird', 'To Kill a Mockingbird'
  114. it_behaves_like 'capitalize', 'except "an"', 'to eat an apple a day', 'To Eat an Apple a Day'
  115. end
  116.  
  117. it_behaves_like 'capitalize', 'except conjunctions', 'war and peace', 'War and Peace'
  118. it_behaves_like 'capitalize', 'except prepositions', 'love in the time of cholera', 'Love in the Time of Cholera'
  119. it_behaves_like 'capitalize', 'always for "I"', 'what i wish i knew when i was 20', 'What I Wish I Knew When I Was 20'
  120. it_behaves_like 'capitalize', 'always for first word', 'the man in the iron mask', 'The Man in the Iron Mask'
  121. end
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement