Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CaesarCipherizer
- def initialize(distance = nil)
- @distance = distance.to_i
- end
- def encrypt(text)
- text.tr(alphabet, cipher)
- end
- def decrypt(text)
- text.tr(cipher, alphabet)
- end
- private
- attr_reader :distance
- def alphabet
- Alphabet.to_s
- end
- def cipher
- CaesarCipher.new(distance: distance).to_s
- end
- end
- class CaesarCipher
- def initialize(distance: 0)
- raise ArgumentError unless distance.is_a? Integer
- @distance = distance
- end
- def to_s
- Alphabet.to_a.rotate(distance).join
- end
- private
- attr_reader :distance
- end
- module Alphabet
- def self.to_a
- [*('a'..'z'), *('A'..'Z')]
- end
- def self.to_s
- to_a.join
- end
- end
- #~-~-~-~#
- require 'minitest/autorun'
- describe 'CaesarCipherizer' do
- subject { CaesarCipherizer.new(distance) }
- let(:distance) { nil }
- describe "#encrypt" do
- describe "with no distance specified" do
- it "should return the original string" do
- assert_equal "Hello", subject.encrypt("Hello")
- assert_equal "Testing", subject.encrypt("Testing")
- end
- end
- describe "with a distance of 1 specified" do
- let(:distance) { 1 }
- it "should return the correct string" do
- assert_equal "Ifmmp", subject.encrypt("Hello")
- assert_equal "Uftujoh", subject.encrypt("Testing")
- end
- end
- describe "with a distance of 5 specified" do
- let(:distance) { 5 }
- it "should return the correct string" do
- assert_equal "Mjqqt", subject.encrypt("Hello")
- assert_equal "doCDsxq", subject.encrypt("Yjxynsl")
- end
- end
- end
- describe '.distance' do
- describe "with no distance specified" do
- it "should be inaccessible" do
- assert_raises(NoMethodError) { subject.distance }
- end
- end
- describe "with a distance of 1 specified" do
- let(:distance) { 1 }
- it "should still be totallyl inaccessible" do
- assert_raises(NoMethodError) { subject.distance }
- end
- end
- end
- end
- describe 'CaesarCipher' do
- subject { CaesarCipher.new(distance: distance) }
- let(:distance) { nil }
- it "should be not give away its distance" do
- assert_raises(ArgumentError) { subject.distance }
- end
- describe "#to_s" do
- describe "with a hil input" do
- it "should repeat back the alphabet" do
- assert_equal "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", CaesarCipher.new.to_s
- end
- end
- it "should not give away its distance" do
- assert_raises(NoMethodError) { CaesarCipher.new.distance }
- end
- describe "with a 0 input" do
- let(:distance) { 0 }
- it "should repeat back the alphabet" do
- assert_equal "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", subject.to_s
- end
- it "should not give away its distance" do
- assert_raises(NoMethodError) { subject.distance }
- end
- end
- describe "with a 1 input" do
- let(:distance) { 1 }
- it "should repeat back the alphabet shifted one across" do
- assert_equal "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa", subject.to_s
- end
- it "should not give away its distance" do
- assert_raises(NoMethodError) { subject.distance }
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement