require 'erb' module AsProject class TestSuiteGenerator attr_accessor :test_suites, :created_files def initialize(start, suite_name, template_content) remove_test_suites(start) @created_files = [] @test_suites = [] return_to = Dir.pwd Dir.chdir(start) process_dir(Dir.pwd, suite_name) process_test_suites(@test_suites, template_content) finish Dir.chdir(return_to) end def finish puts '>> TestSuites rebuilt from: ' puts Dir.pwd end def remove_test_suites(dir) files = Dir["#{dir}/**/**AllTests.as"] files.each do |file| File.delete file end end def process_dir(dir, suite_name, base='') found_test_case = false suite = TestSuite.new(dir, suite_name) suite.directory = base Dir.foreach(dir) do |file| if(file == '.' || file == '..') next end if(File.directory?(file) && !AsProject.ignore_file?(file)) Dir.chdir(file) if(process_dir(Dir.pwd, suite_name, build_base_name(base, file))) suite.add_test_suite(build_base_name(base, file)) found_test_case = true end Dir.chdir('../') end if(File.fnmatch('*Test.as', file)) suite.add_test_case(build_base_name(base, file)) found_test_case = true end end if(found_test_case) @test_suites << suite end return found_test_case end def build_base_name(base, file) if(base == '') return file end return File.join(base, file) end def process_test_suites(suites, template_content) suites.each do |suite| suite.serialize(template_content) @created_files << suite.file_location end end end class TestSuite attr_accessor :test_cases, :directory, :package def initialize(dir, suite_name) @test_cases = Array.new @directory = dir @suite_name = suite_name end def add_test_case(file) file = file.split('.')[0] test_case = file.split(File::SEPARATOR).join('.') @test_cases.push(test_case) end def add_test_suite(dir) test_case = File.join(dir, @suite_name) add_test_case(test_case) end def package_name @directory.split(File::SEPARATOR).join('.') end def full_suite_name name = @suite_name.split('.')[0] parts = @directory.split(File::SEPARATOR) parts << name return parts.join('.') end def file_location if(@directory.length > 0) return File.join(@directory, @suite_name) else return @suite_name end end def serialize(template_content) @test_cases.sort! parsed = ERB.new(template_content).result(binding) file = File.new(file_location, 'w') file.write(parsed) end def to_s return "Suite for: " + @directory + "\n" end end end