Class: Entityjs::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/commands/test.rb

Class Method Summary collapse

Class Method Details

.generate(name) ⇒ Object



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
# File 'lib/entityjs/commands/test.rb', line 5

def self.generate(name)
  if !Dirc.game?
    return 2
  end
  
  tests = []

  if name.nil?
    return 4
  end
  
  if name.is_a? Array
    tests = name[1..-1]
    
    name = name.first
  end
  
  #remove extra slash
  if name[0] == '/'
    name = name[1..-1]
  end
  
  filename = name
  if name.index('.').nil?
    filename += '_test.js'
  end
  
  dir = Config.tests_folder
  
  Dirc.create_dir(dir, true)
  
  #create folder if they don't exist
  folders = filename.split('/')
  filename = folders.pop
  
  if folders.size > 0
    folders.each do |i|
      Dirc.create_dir(i, true)
    end
    
  end
  
  if File.exists?(filename)
    return 3
  end
  
  test_name = filename.split('_test.').first
  
  File.open(filename, 'w') do |f|
    
    f.write("module('#{test_name}');")
    
    tests.each do |i|
      f.write("\n\ntest('#{i}', function(){\n\t\n\t\n\t\n});")
    end
    
    f.close
  end
  
  puts "Created test: #{filename}"
  
  Dir.chdir(Dirc.game_root)
  
  return 0
end