Skip to content

Commit 86aefc9

Browse files
committed
better local variable test
1 parent c562cde commit 86aefc9

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

spec/spec_helper.rb

+11
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,14 @@
33
RSpec.configure do |config|
44
config.order = 'default'
55
end
6+
7+
def get_variable_from_file(file, variable)
8+
file_scope = binding
9+
file_scope.eval(File.read(file))
10+
11+
begin
12+
return file_scope.local_variable_get(variable)
13+
rescue NameError
14+
raise NameError, "local variable `#{variable}' not defined in #{file}."
15+
end
16+
end

spec/variable_spec.rb

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
# Code your solution in variable.rb
2-
describe "local variables" do
3-
4-
let(:right_answers) { ["greeting = \"Hello World\"", "greeting=\"Hello World\"", "greeting = \'Hello World\'", "greeting=\'Hello World\'"] }
5-
6-
before(:each) do
7-
@content = File.open("variable.rb", "r") { |f| content = f.read }
8-
end
2+
describe "./variable.rb" do
93

104
it "defined a local variable called greeting and set it equal to 'Hello World'" do
11-
expect(right_answers.any? { |answer| @content.match(answer) }).to eq(true)
12-
end
5+
greeting = get_variable_from_file('./variable.rb', "greeting")
136

14-
it "should not be an instance variable" do
15-
expect(@content).to_not include("@")
7+
expect(greeting).to eq("Hello World")
168
end
179

18-
it "should not be an global variable" do
19-
expect(@content).to_not include("$")
20-
end
2110
end

variable.rb

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# Define a local variable called 'greeting',
22
# give it the value of the string "Hello World"
3-
4-

0 commit comments

Comments
 (0)