Class: Moto::TestGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/test_generator.rb

Instance Method Summary collapse

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
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
# File 'lib/test_generator.rb', line 51

def generate_for_full_class_code(test_path_absolute)
  require test_path_absolute

  test_object = nil

  # Checking if it's possible to create test based on provided path. In case something is wrong with
  # modules structure in class itself Moto::Test will be instantized with raise injected into its run()
  # so we can have proper reporting and summary even if the test doesn't execute.
  begin
    class_name = test_path_absolute.gsub("#{MotoApp::DIR}/",'moto_app/').camelize.chomp('.rb').constantize
    test_object = class_name.new
  rescue NameError
    class_name = Moto::Test
    test_object = class_name.new

    class << test_object
      attr_accessor :custom_name
    end

    test_object.custom_name = test_path_absolute.gsub("#{MotoApp::DIR}/",'moto_app/').camelize.chomp('.rb')

    class << test_object
      def run
        raise "ERROR: Invalid module structure: #{custom_name}"
      end
    end

  end

  test_object.static_path = test_path_absolute
  test_object.evaled = false
  test_object
end

#generate_for_run_body(test_path_absolute, method_body) ⇒ Object



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
# File 'lib/test_generator.rb', line 85

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}/#{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