Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

passing initial specs

  • Loading branch information...
commit ff523d733d2bd2fa1b3fe71e934683a0c11fab49 1 parent 93461cb
@JoshCheek authored
View
4 Rakefile
@@ -61,7 +61,7 @@ def run_spec(session_num,problem)
if ENV['solved']
problem_dir = "#{sess}/solved/#{problem}.rb"
else
- problem_dir = Dir["#{sess}/challenge/#{problem}_*"].first
+ problem_dir = Dir["#{sess}/challenge/#{problem}_*.rb"].first
end
sh "ruby -c '#{problem_dir}'" # check syntax
sh "rspec -cr '#{problem_dir}' -r enumerator '#{sess}/spec/#{problem}.rb' --fail-fast" # run spec (c for colour, r to require the files, enumerator required for 1.8.6 compatibility)
@@ -78,7 +78,7 @@ end
# each value is a range indicating the problems available for this session
def sessions_with_problems
get_sessions.inject Hash.new do |sessions_with_sections, session|
- challenges = Dir["session#{session}/challenge/*"]
+ challenges = Dir["session#{session}/challenge/*.rb"]
sessions_with_sections[session] = challenges.map { |challenge| challenge[%r([^/]+?$)].to_i }.sort
sessions_with_sections
end
View
3  session6/challenge/1_build_an_app/Gemfile
@@ -0,0 +1,3 @@
+source :rubygems
+gem "sinatra", "~> 1.2.6"
+gem "rack-test", "~> 0.6.0"
View
17 session6/challenge/1_build_an_app/Gemfile.lock
@@ -0,0 +1,17 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ rack (1.3.0)
+ rack-test (0.6.0)
+ rack (>= 1.0)
+ sinatra (1.2.6)
+ rack (~> 1.1)
+ tilt (< 2.0, >= 1.2.2)
+ tilt (1.3.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rack-test (~> 0.6.0)
+ sinatra (~> 1.2.6)
View
33 session6/challenge/1_build_an_app/lib/caesar_cipher.rb
@@ -0,0 +1,33 @@
+module CaesarCipher
+
+ # Hash where you give it a character and it returns the
+ # encrypted or decrypted value (will be built up below)
+ ENCRYPT_MAP = Hash.new { |hash, key| key }
+ DECRYPT_MAP = Hash.new { |hash, key| key }
+
+ # will find first index, so first 'a', but 3 chars after first 'x', will be another 'a'
+ lowercase = ('a'..'z').to_a + ['a', 'b', 'c']
+ uppercase = ('A'..'Z').to_a + ['A', 'B', 'C']
+ numerical = ('0'..'9').to_a + ['0', '1', '2']
+
+ # now, for each group, map the appropriate characters
+ [lowercase, uppercase, numerical].each do |character_set|
+ character_set.each_cons 4 do |char, _, __, char_plus_three|
+ ENCRYPT_MAP[char] = char_plus_three
+ DECRYPT_MAP[char_plus_three] = char
+ end
+ end
+
+ def self.encrypt(message)
+ new_message = ''
+ message.each_char { |crnt_char| new_message << ENCRYPT_MAP[crnt_char] }
+ new_message
+ end
+
+ def self.decrypt(message)
+ original_message = ''
+ message.each_char { |crnt_char| original_message << DECRYPT_MAP[crnt_char] }
+ original_message
+ end
+
+end
View
5 session6/challenge/1_build_an_app/main.rb
@@ -0,0 +1,5 @@
+require "sinatra"
+
+get '/encrypt' do
+
+end
View
25 session6/challenge/1_instructions.rb
@@ -0,0 +1,25 @@
+# You're going to build an app now!
+# You want to be able to send secret messages to your best friend
+# without your pesky little sister being able to read them. So you
+# decide on a Caesar Cipher, for every alphabetic character, you
+# will exchange it with a character three larger, wrapping around:
+# "a" => "d"
+# "z" => "c"
+# "A" => "D"
+# "B" => "E"
+# "0" => "3"
+# "9" => "2"
+#
+# But your best friend is kind of a numbskull, he keeps messing
+# it up. So you decide to write a web application to encrypt
+# the message for him.
+#
+# When he goes to yourapp.com/encrypt, he will be presented
+# with a form that he can type his message into. When he
+# posts it to yourapp.com/encrypted, he will be presented
+# with the encrypted message.
+#
+# When he goes to yourapp.com/decrypt, he will be presented
+# with a form that he can type the encrypted message into.
+# When he posts it to yourapp.com/decrypted, he will be
+# presented with the decrypted message.
View
150 session6/spec/1.rb
@@ -0,0 +1,150 @@
+app_root = File.expand_path "#{File.dirname __FILE__}/../challenge/1_build_an_app"
+$LOAD_PATH << app_root
+
+def unrecoverable_error(message)
+ message = "* #{message} *"
+ border = message.gsub(/./,'*')
+ blank_line = ''
+ puts blank_line, border, message, border, blank_line
+ exit 1
+end
+
+# check necessary preliminaries
+begin
+ require 'main'
+rescue LoadError
+ unrecoverable_error "You should have a main file in in challenge/1_build_an_app/main.rb"
+end
+
+begin
+ Sinatra
+rescue
+ unrecoverable_error "You should have required Sinatra from your main file."
+end
+
+begin
+ require 'rack/test'
+rescue LoadError
+ unrecoverable_error "rack-test should be required and configurable"
+ exit 1
+end
+
+
+# set env for testing
+include Rack::Test::Methods
+set :environment, :test
+
+
+# test actual app
+describe "Your application's library" do
+ specify 'should be in a folder named lib/caesar_cipher.rb' do
+ require('lib/caesar_cipher').should be
+ end
+
+ specify 'Should be in a module named CaesarCipher' do
+ Object.should be_const_defined(:CaesarCipher)
+ CaesarCipher.should be_instance_of Module
+ end
+
+ describe 'CaesarCipher.encrypt' do
+ describe 'with a lowercase message' do
+ specify { CaesarCipher.encrypt('a').should == 'd' }
+ specify { CaesarCipher.encrypt('b').should == 'e' }
+ specify { CaesarCipher.encrypt('x').should == 'a' }
+ specify { CaesarCipher.encrypt('z').should == 'c' }
+ end
+ describe 'with an uppercase message' do
+ specify { CaesarCipher.encrypt('A').should == 'D' }
+ specify { CaesarCipher.encrypt('B').should == 'E' }
+ specify { CaesarCipher.encrypt('X').should == 'A' }
+ specify { CaesarCipher.encrypt('Z').should == 'C' }
+ end
+ describe 'with a numerical message' do
+ specify { CaesarCipher.encrypt('0').should == '3' }
+ specify { CaesarCipher.encrypt('1').should == '4' }
+ specify { CaesarCipher.encrypt('7').should == '0' }
+ specify { CaesarCipher.encrypt('9').should == '2' }
+ end
+ describe 'with non alphanumeric message' do
+ '~!@#$%^&*(){}[];\':",./\\ '.each_char do |char|
+ specify { CaesarCipher.encrypt(char.dup).should == char }
+ end
+ end
+ describe 'with a full message' do
+ specify do
+ CaesarCipher.encrypt("The quick brown fox jumps over the lazy dog.").should ==
+ "Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj."
+ end
+ end
+ end
+
+ describe 'CaesarCipher.decrypt' do
+ describe 'with a lowercase message' do
+ specify { CaesarCipher.decrypt('d').should == 'a' }
+ specify { CaesarCipher.decrypt('e').should == 'b' }
+ specify { CaesarCipher.decrypt('a').should == 'x' }
+ specify { CaesarCipher.decrypt('c').should == 'z' }
+ end
+ describe 'with an uppercase message' do
+ specify { CaesarCipher.decrypt('D').should == 'A' }
+ specify { CaesarCipher.decrypt('E').should == 'B' }
+ specify { CaesarCipher.decrypt('A').should == 'X' }
+ specify { CaesarCipher.decrypt('C').should == 'Z' }
+ end
+ describe 'with a numerical message' do
+ specify { CaesarCipher.decrypt('3').should == '0' }
+ specify { CaesarCipher.decrypt('4').should == '1' }
+ specify { CaesarCipher.decrypt('0').should == '7' }
+ specify { CaesarCipher.decrypt('2').should == '9' }
+ end
+ describe 'with non alphanumeric message' do
+ '~!@#$%^&*(){}[];\':",./\\ '.each_char do |char|
+ specify { CaesarCipher.decrypt(char.dup).should == char }
+ end
+ end
+ describe 'with a full message' do
+ specify do
+ CaesarCipher.decrypt("Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj.").should ==
+ "The quick brown fox jumps over the lazy dog."
+ end
+ end
+ end
+end
+
+
+describe "Your application's Gemfile" do
+ specify { File.should exist "#{app_root}/Gemfile" }
+ it 'should have a source of :rubygems' do
+ File.readlines("#{app_root}/Gemfile").grep(/\bsource\b.*:rubygems\b/).size.should == 1
+ end
+ it 'should have a line for Sinatra' do
+ File.readlines("#{app_root}/Gemfile").grep(/(['"])sinatra\1/).size.should == 1
+ end
+ it 'should have a line for rack-test' do
+ File.readlines("#{app_root}/Gemfile").grep(/(['"])rack-test\1/).size.should == 1
+ end
+end
+
+
+describe "Your application's Gemfile.lock" do
+ it 'should have been installed to a Gemfile.lock' do
+ File.should exist "#{app_root}/Gemfile.lock"
+ end
+ it 'should have a line for Sinatra' do
+ File.readlines("#{app_root}/Gemfile.lock").grep(/\bsinatra\b/).should_not be_empty
+ end
+ it 'should have a line for rack-test' do
+ File.readlines("#{app_root}/Gemfile.lock").grep(/\brack-test\b/).should_not be_empty
+ end
+end
+
+
+describe "Your application's main app" do
+ def app
+ Sinatra::Application
+ end
+ describe "'s '/encrypt' route" do
+ before { get '/encrypt' }
+ specify { last_response.should be_ok }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.