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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/generators/unit_test_generator.rb', line 3
def create_unit_tests
require 'json'
require 'reflector'
class_name = args[0]
file_name = class_name.downcase
puts "criando testes unitarios para #{class_name}..."
is_controller = /.*Controller$/.match?(class_name)
class_type = "model"
first_line = File.open("#{Rails.root}/tmp/#{class_name}.rb", &:readline)
sample_object = JSON.parse(first_line)["current_state"]
executed_methods = []
executed_arguments = {}
if is_controller
class_type = "controller"
template = %Q{require 'rails_helper'
RSpec.describe #{class_name}, :type => :#{class_type} do
}
File.readlines("#{Rails.root}/tmp/#{class_name}.rb").each do |line|
json_string = JSON.parse(line.strip)
puts "\nReflecting..."
reflector = Reflect::Reflector.new
klass = json_string["klass"]
meth = json_string["method"]
cs = json_string["current_state"]
args = json_string["args"]
class_and_method_name = "#{klass}::#{meth}"
if executed_methods.include?(class_and_method_name)
if executed_arguments[class_and_method_name.to_sym].include?(args)
else
executed_arguments[class_and_method_name.to_sym].push(args)
end
else
executed_methods.push(class_and_method_name)
executed_arguments[class_and_method_name.to_sym] = []
puts "Executando #{klass}::#{meth} com argumentos #{args} no objeto #{sample_object}"
reflection = reflector.reflect(klass, meth, sample_object, args)
puts "Resultado: #{reflection}"
print "\n"
template_aux = %Q{
describe 'GET #{meth}' do
it 'returns a successful response' do
get :#{meth}
expect(response).to be_successful
end
it 'renders the #{meth} template' do
get :#{meth}
expect(response).to render_template("#{meth}")
end
end
}
template.concat(template_aux)
end
end
template.concat(%Q{
end
})
else
template = %Q{require 'rails_helper'
RSpec.describe #{class_name}, :type => :#{class_type} do
let(:#{file_name}) { #{class_name}.new(#{sample_object}) }
it "is valid with valid attributes" do
expect(#{file_name}).to be_valid
end
}
File.readlines("#{Rails.root}/tmp/#{class_name}.rb").each do |line|
json_string = JSON.parse(line.strip)
puts "\nReflecting..."
reflector = Reflect::Reflector.new
klass = json_string["klass"]
meth = json_string["method"]
cs = json_string["current_state"]
args = json_string["args"]
class_and_method_name = "#{klass}::#{meth}"
if executed_methods.include?(class_and_method_name)
if executed_arguments[class_and_method_name.to_sym].include?(args)
else
executed_arguments[class_and_method_name.to_sym].push(args)
end
else
executed_methods.push(class_and_method_name)
executed_arguments[class_and_method_name.to_sym] = []
puts "Executando #{klass}::#{meth} com argumentos #{args} no objeto #{sample_object}"
reflection = reflector.reflect(klass, meth, sample_object, args)
puts "Resultado: #{reflection}"
print "\n"
template_aux = %Q{
describe '##{meth}' do
it "should return valid string for method #{meth}" do
expect(#{class_name.downcase}.#{meth}(#{args.join(', ')})).to eq "#{reflection}"
end
end
}
template.concat(template_aux)
end
end
template.concat(%Q{
end
})
end
create_file "spec/#{class_type}s/#{file_name.sub('controller', '_controller')}_spec.rb", template
puts "Testes criados."
end
|