Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. class CaesarCipherizer
  2.  
  3. def initialize(distance = nil)
  4. @distance = distance.to_i
  5. end
  6.  
  7. def encrypt(text)
  8. text.tr(alphabet, cipher)
  9. end
  10.  
  11. def decrypt(text)
  12. text.tr(cipher, alphabet)
  13. end
  14.  
  15. private
  16. attr_reader :distance
  17.  
  18. def alphabet
  19. Alphabet.to_s
  20. end
  21.  
  22. def cipher
  23. CaesarCipher.new(distance: distance).to_s
  24. end
  25. end
  26.  
  27. class CaesarCipher
  28.  
  29. def initialize(distance: 0)
  30. raise ArgumentError unless distance.is_a? Integer
  31. @distance = distance
  32. end
  33.  
  34. def to_s
  35. Alphabet.to_a.rotate(distance).join
  36. end
  37.  
  38. private
  39. attr_reader :distance
  40. end
  41.  
  42. module Alphabet
  43. def self.to_a
  44. [*('a'..'z'), *('A'..'Z')]
  45. end
  46.  
  47. def self.to_s
  48. to_a.join
  49. end
  50. end
  51.  
  52. #~-~-~-~#
  53.  
  54. require 'minitest/autorun'
  55.  
  56. describe 'CaesarCipherizer' do
  57. subject { CaesarCipherizer.new(distance) }
  58. let(:distance) { nil }
  59.  
  60. describe "#encrypt" do
  61. describe "with no distance specified" do
  62.  
  63. it "should return the original string" do
  64. assert_equal "Hello", subject.encrypt("Hello")
  65. assert_equal "Testing", subject.encrypt("Testing")
  66. end
  67. end
  68.  
  69. describe "with a distance of 1 specified" do
  70. let(:distance) { 1 }
  71.  
  72. it "should return the correct string" do
  73. assert_equal "Ifmmp", subject.encrypt("Hello")
  74. assert_equal "Uftujoh", subject.encrypt("Testing")
  75. end
  76. end
  77.  
  78. describe "with a distance of 5 specified" do
  79. let(:distance) { 5 }
  80.  
  81. it "should return the correct string" do
  82. assert_equal "Mjqqt", subject.encrypt("Hello")
  83. assert_equal "doCDsxq", subject.encrypt("Yjxynsl")
  84. end
  85. end
  86. end
  87.  
  88. describe '.distance' do
  89. describe "with no distance specified" do
  90.  
  91. it "should be inaccessible" do
  92. assert_raises(NoMethodError) { subject.distance }
  93. end
  94. end
  95.  
  96. describe "with a distance of 1 specified" do
  97. let(:distance) { 1 }
  98.  
  99. it "should still be totallyl inaccessible" do
  100. assert_raises(NoMethodError) { subject.distance }
  101. end
  102. end
  103. end
  104. end
  105.  
  106. describe 'CaesarCipher' do
  107. subject { CaesarCipher.new(distance: distance) }
  108.  
  109. let(:distance) { nil }
  110.  
  111. it "should be not give away its distance" do
  112. assert_raises(ArgumentError) { subject.distance }
  113. end
  114.  
  115. describe "#to_s" do
  116. describe "with a hil input" do
  117. it "should repeat back the alphabet" do
  118. assert_equal "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", CaesarCipher.new.to_s
  119. end
  120. end
  121.  
  122. it "should not give away its distance" do
  123. assert_raises(NoMethodError) { CaesarCipher.new.distance }
  124. end
  125.  
  126. describe "with a 0 input" do
  127. let(:distance) { 0 }
  128. it "should repeat back the alphabet" do
  129. assert_equal "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", subject.to_s
  130. end
  131.  
  132. it "should not give away its distance" do
  133. assert_raises(NoMethodError) { subject.distance }
  134. end
  135. end
  136.  
  137. describe "with a 1 input" do
  138. let(:distance) { 1 }
  139. it "should repeat back the alphabet shifted one across" do
  140. assert_equal "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa", subject.to_s
  141. end
  142.  
  143. it "should not give away its distance" do
  144. assert_raises(NoMethodError) { subject.distance }
  145. end
  146. end
  147. end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement