-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_spec.rb
More file actions
93 lines (75 loc) · 2.4 KB
/
Copy pathsolution_spec.rb
File metadata and controls
93 lines (75 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'rspec'
require 'rspec/expectations'
require 'code_breaker'
VARIABLES = [:objects, :question_marks].freeze
def code_lines
%q{[['solution::code']]}
end
def statements
@statements ||= Array(CodeBreaker.parse(code_lines))
end
def assignment?(statement)
statement.is_a?(Hash) && statement.keys.first == :lvasgn
end
def brackets_statement
@brackests_statement ||= statements.select do |statement|
assignment?(statement) && statement[:lvasgn].first == VARIABLES.first
end
end
def new_statement
@new_statement ||= statements.select do |statement|
assignment?(statement) && statement[:lvasgn].first == VARIABLES.last
end
end
def array_assigned?(variable)
!!defined?(variable) && variable.is_a?(Array)
end
def array_defined_with_new?(variable, statement)
array_assigned?(variable) &&
statement.first[:lvasgn][1][0..1] == [{ const: :Array }, :new]
end
describe 'Your code' do
[['solution::code']]
VARIABLES.each do |name|
it "defines a variable with name \"#{name}\"" do
expect(local_variables.include?(name)).to be true
end
if local_variables.include?(name)
it "assigns an Array to the variable \"#{name}\"" do
expect(eval(name.to_s)).to be_an Array
end
end
end
if (local_variables & VARIABLES).sort == VARIABLES.sort
describe 'assings an Array to "objects" which' do
it 'is created by using []' do
expect(array_assigned?(objects)).to be true
expect(brackets_statement.first[:lvasgn].last.keys.first).to eq :array
end
if array_assigned?(objects)
before { @values = brackets_statement.first[:lvasgn].last[:array] }
it 'holds a Float' do
expect(@values.include?(Float)).to be true
end
it 'holds a Boolean' do
included = @values.include?(TrueClass) || @values.include?(FalseClass)
expect(included).to be true
end
it 'holds an empty Array' do
empty_array = { array: [] }
expect(@values.include?(empty_array)).to be true
end
end
end
describe 'assings an Array to "question_marks" which' do
it 'is created by using #new' do
expect(array_defined_with_new?(question_marks, new_statement)).to be true
end
if array_defined_with_new?(question_marks, new_statement)
it 'holds three times "?"' do
expect(question_marks).to eq ['?'] * 3
end
end
end
end
end