Class: Moto::TestGenerator
- Inherits:
-
Object
- Object
- Moto::TestGenerator
- Defined in:
- lib/test_generator.rb
Instance Method Summary collapse
-
#create(class_name) ⇒ Object
assuming that target file includes full valid ruby class.
- #create_module_tree(root_module, next_modules) ⇒ Object
-
#generate(test_path_absolute) ⇒ Object
assuming that target file includes only content of method ‘run’ and some magic comments.
-
#generate_for_full_class_code(test_path_absolute) ⇒ Object
Generates objects with tests that are supposed to be executed in this run.
- #generate_for_run_body(test_path_absolute, method_body) ⇒ Object
-
#initialize(app_dir) ⇒ TestGenerator
constructor
A new instance of TestGenerator.
Constructor Details
#initialize(app_dir) ⇒ TestGenerator
Returns a new instance of TestGenerator.
9 10 11 |
# File 'lib/test_generator.rb', line 9 def initialize(app_dir) @app_dir = app_dir end |
Instance Method Details
#create(class_name) ⇒ Object
assuming that target file includes full valid ruby class
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/test_generator.rb', line 14 def create(class_name) class_name = 'MotoApp::Tests::'+class_name a = class_name.underscore.split('/') test_path = (a[1..20]+[a[-1]]).join('/') # TODO: check if this path and constant exists require "#{MotoApp::DIR}/#{test_path}" test_const = class_name.safe_constantize test_const.new end |
#create_module_tree(root_module, next_modules) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/test_generator.rb', line 25 def create_module_tree(root_module, next_modules) return root_module if next_modules.empty? next_module_name = next_modules.shift if root_module.const_defined?(next_module_name.to_sym) m = root_module.const_get(next_module_name.to_sym) else m = Module.new root_module.const_set(next_module_name.to_sym, m) end create_module_tree(m, next_modules) end |
#generate(test_path_absolute) ⇒ Object
assuming that target file includes only content of method ‘run’ and some magic comments
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/test_generator.rb', line 38 def generate(test_path_absolute) method_body = File.read(test_path_absolute) + "\n" full_code = !! method_body.match( /^#\s*FULL_CODE\s+/ ) if full_code generate_for_full_class_code(test_path_absolute) else generate_for_run_body(test_path_absolute, method_body) end end |
#generate_for_full_class_code(test_path_absolute) ⇒ Object
Generates objects with tests that are supposed to be executed in this run.
51 52 53 54 55 56 57 58 |
# File 'lib/test_generator.rb', line 51 def generate_for_full_class_code(test_path_absolute) require test_path_absolute class_name = test_path_absolute.gsub("#{MotoApp::DIR}/",'moto_app/').camelize.chomp('.rb').constantize test_object = class_name.new test_object.static_path = test_path_absolute test_object.evaled = false test_object end |
#generate_for_run_body(test_path_absolute, method_body) ⇒ Object
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 |
# File 'lib/test_generator.rb', line 60 def generate_for_run_body(test_path_absolute, method_body) base = Moto::Test base_class_string = method_body.match( /^#\s*BASE_CLASS:\s(\S+)/ ) unless base_class_string.nil? base_class_string = base_class_string[1].strip a = base_class_string.underscore.split('/') base_test_path = a[1..-1].join('/') require "#{MotoApp::DIR}/lib/#{base_test_path}" base = base_class_string.constantize end # MotoApp::Tests::Login::Short consts = test_path_absolute.camelize.split('Tests::')[1].split('::') consts.pop class_name = consts.pop m = create_module_tree(MotoApp::Tests, consts) cls = Class.new(base) m.const_set(class_name.to_sym, cls) test_object = cls.new test_object.instance_eval( "def run\n #{method_body} \n end" ) test_object.static_path = test_path_absolute test_object.evaled = true test_object end |