Skip to content

Commit fd2cb65

Browse files
committed
Software Engineering Foundations - RSpec
Software Engineering Foundations - RSpec
1 parent 840cc30 commit fd2cb65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1221
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle exec rspec
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# begin...rescue
2+
# The code in the begin block will execute until an exception is reached.
3+
# Once an exception is reached, the execution will immediately jump to rescue.
4+
5+
num = 0
6+
begin
7+
puts "dividing 10 by #{num}"
8+
ans = 10/num
9+
puts "the answer is #{ans}"
10+
rescue
11+
puts "there was an error in that division"
12+
end
13+
14+
puts "------------" # dividing 10 by 0
15+
puts "finished" # there was an error in that division
16+
# ------------
17+
# finished
18+
19+
# In the event that an exception is never hit in the begin block, then execution will never go to rescue
20+
21+
begin
22+
puts "dividing 10 by #{num}"
23+
ans = 10 / num
24+
puts "the answer is #{ans}"
25+
rescue
26+
puts "There was an error with that division."
27+
end
28+
29+
puts "--------"
30+
puts "finished"
31+
32+
# Output:
33+
# dividing 10 by 5
34+
# the answer is 2
35+
# --------
36+
# finished
37+
38+
# Raise an exception
39+
# Say your method accepts 2 string arguments. You need to raise an exception if either of them is not a string
40+
def format_name(first, last)
41+
if !(first.instance_of?(String) && last.instance_of?(String))
42+
raise "arguments must be strings"
43+
end
44+
first.capitalize + " " + last.capitalize
45+
end
46+
47+
p format_name("NEHA", "gokhale") # "Neha Gokhale"
48+
# p format_name(50, "gokhale") # ./exceptions.rb:42:in `format_name': arguments must be strings (RuntimeError)
49+
50+
51+
# begin...rescue & raise
52+
def format_name(first, last)
53+
if !(first.instance_of?(String) && last.instance_of?(String))
54+
raise "arguments must be strings"
55+
end
56+
57+
first.capitalize + " " + last.capitalize
58+
end
59+
60+
first_name = 42
61+
last_name = "ray"
62+
begin
63+
p format_name(first_name, last_name)
64+
rescue
65+
# do stuff to rescue the "arguments must be strings" exception..
66+
p "there was an exception :("
67+
end
68+
69+
# output:
70+
# "Neha Gokhale"
71+
# "there was an exception :("
72+
73+
Binary file not shown.
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--color
2+
--format documentation
3+
--no-fail-fast
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
gem "byebug"
4+
gem "rspec", "~> 3.2.0"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
byebug (11.0.1)
5+
diff-lcs (1.3)
6+
rspec (3.2.0)
7+
rspec-core (~> 3.2.0)
8+
rspec-expectations (~> 3.2.0)
9+
rspec-mocks (~> 3.2.0)
10+
rspec-core (3.2.3)
11+
rspec-support (~> 3.2.0)
12+
rspec-expectations (3.2.1)
13+
diff-lcs (>= 1.2.0, < 2.0)
14+
rspec-support (~> 3.2.0)
15+
rspec-mocks (3.2.1)
16+
diff-lcs (>= 1.2.0, < 2.0)
17+
rspec-support (~> 3.2.0)
18+
rspec-support (3.2.2)
19+
20+
PLATFORMS
21+
x64-mingw32
22+
23+
DEPENDENCIES
24+
byebug
25+
rspec (~> 3.2.0)
26+
27+
BUNDLED WITH
28+
1.17.2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def add(num1, num2)
2+
num1 + num2
3+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def prime?(num)
2+
return false if num < 2
3+
(2...num).each do |divisor|
4+
return false if num % divisor == 0
5+
end
6+
true
7+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "add"
2+
3+
describe "add method" do
4+
it "should accept two numbers as arguments" do
5+
expect { add(2, 3) }.to_not raise_error
6+
end
7+
8+
it "should return the sum of the two numbers" do
9+
expect(add(2, 3)).to eq(5)
10+
expect(add(10, 12)).to eq(22)
11+
end
12+
end

0 commit comments

Comments
 (0)