Class: RubyToggleFile

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

Constant Summary collapse

LIB_RE =
%r!/lib/(.+)\.rb$!
TEST_RE =
%r!/test/(.+/)?test_(.+)\.rb$!
RAILS_MVC2TESTNAME =
{ 'models' => 'unit', 'controllers' => 'functional' }
RAILS_TESTNAME2MVC =
RAILS_MVC2TESTNAME.invert

Instance Method Summary collapse

Instance Method Details

#exist(file) ⇒ Object

private



39
40
41
# File 'lib/ruby_toggle_file.rb', line 39

def exist(file)
  file if File.exist? file
end

#implementation_file(test) ⇒ Object



33
34
35
36
# File 'lib/ruby_toggle_file.rb', line 33

def implementation_file(test)
  m = TEST_RE.match(test)
  run_hooks_with_args_until_success %r/^implementation_file_/, test, m&&(m.pre_match+"/"), m&&m[1], m&&m[2]
end

#implementation_file_00_rails(test, basedir, dir, node) ⇒ Object



75
76
77
78
79
# File 'lib/ruby_toggle_file.rb', line 75

def implementation_file_00_rails(test, basedir, dir, node)
  if m = %r!test/(unit|functional)/(.+)_test.rb$!.match(test)
    "%sapp/%s/%s.rb" % [ m.pre_match, RAILS_TESTNAME2MVC[m[1]], m[2] ]
  end
end

#implementation_file_10_no_match(test, basename, dir, node) ⇒ Object



81
82
83
84
85
# File 'lib/ruby_toggle_file.rb', line 81

def implementation_file_10_no_match(test, basename, dir, node)
  if dir == nil and node == nil and test =~ %r!/test_(.+)\.rb$!
    test.sub("/test_", "/")
  end
end

#implementation_file_20(test, basedir, dir, node) ⇒ Object



87
88
89
# File 'lib/ruby_toggle_file.rb', line 87

def implementation_file_20(test, basedir, dir, node)
  exist("#{basedir}lib/#{dir}#{node}.rb")
end

#implementation_file_30_flat(test, basedir, dir, node) ⇒ Object



91
92
93
# File 'lib/ruby_toggle_file.rb', line 91

def implementation_file_30_flat(test, basedir, dir, node)
  Dir[ "#{basedir}lib/**/#{node}.rb" ].first
end

#implementation_file_99_default(test, basedir, dir, node) ⇒ Object



95
96
97
# File 'lib/ruby_toggle_file.rb', line 95

def implementation_file_99_default(test, basedir, dir, node)
  "#{basedir}lib/#{dir}#{node}.rb"
end

#ruby_toggle_file(file) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ruby_toggle_file.rb', line 12

def ruby_toggle_file(file)
  if File.basename(file) =~ /(?:^test_|_test\.rb$)/
    implementation_file(file)
  else
    test_file(file)
  end
end

#test_file(implementation) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ruby_toggle_file.rb', line 23

def test_file(implementation)
  if m = LIB_RE.match(implementation)
    dir, node = File.split m[1]
    dir = (dir == '.') ? nil : dir+"/"
  else
    dir = node = nil
  end
  run_hooks_with_args_until_success %r/^test_file_/, implementation, m&&(m.pre_match+"/"), dir, node
end

#test_file_00_rails(implementation, basedir, dir, node) ⇒ Object

rails



45
46
47
48
49
# File 'lib/ruby_toggle_file.rb', line 45

def test_file_00_rails(implementation, basedir, dir, node) # rails
  if m = %r!app/(models|controllers)/(.+)\.rb$!.match(implementation)
    "%stest/%s/%s_test.rb" % [ m.pre_match, RAILS_MVC2TESTNAME[m[1]], m[2] ]
  end
end

#test_file_05_rails_lib(implementation, basedir, dir, node) ⇒ Object



51
52
53
54
55
# File 'lib/ruby_toggle_file.rb', line 51

def test_file_05_rails_lib(implementation, basedir, dir, node)
  if basedir and File.directory?( File.join(basedir, "app") )
    "#{basedir}test/unit/test_#{node}.rb"
  end
end

#test_file_10_no_match(implementation, basedir, dir, node) ⇒ Object



57
58
59
60
61
# File 'lib/ruby_toggle_file.rb', line 57

def test_file_10_no_match(implementation, basedir, dir, node)
  if [basedir, dir, node].all?{|x| x.nil?}
    "#{File.dirname(implementation)}/test_#{File.basename(implementation)}"
  end
end

#test_file_20_simple(implementation, basedir, dir, node) ⇒ Object

test/test_NODE.rb



63
64
65
# File 'lib/ruby_toggle_file.rb', line 63

def test_file_20_simple(implementation, basedir, dir, node) # test/test_NODE.rb
  exist "#{basedir}test/test_#{node}.rb"
end

#test_file_30_flat(implementation, basedir, dir, node) ⇒ Object

lib/XXX/NODE.rb -> test/test_NODE.rb



67
68
69
# File 'lib/ruby_toggle_file.rb', line 67

def test_file_30_flat(implementation, basedir, dir, node) # lib/XXX/NODE.rb -> test/test_NODE.rb
  exist "#{basedir}test/test_#{node}.rb" if dir
end

#test_file_99_autotest_default(implementation, basedir, dir, node) ⇒ Object

lib/XXX/NODE.rb -> test/XXX/test_NODE.rb



71
72
73
# File 'lib/ruby_toggle_file.rb', line 71

def test_file_99_autotest_default(implementation, basedir, dir, node) # lib/XXX/NODE.rb -> test/XXX/test_NODE.rb
  "#{basedir}test/#{dir}test_#{node}.rb"
end