[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

__FILE__ for requiring file

Jim Freeze

10/30/2006 7:32:00 PM

Hello

I keep wondering if there is a way to have __FILE__ be delayed
in its evaluation. For example, in my tests cases, I usually do the
following so that my test cases are imune to where they are run from:

class TestMyClass < Test::Unit::TestCase

TEST_DIR = File.dirname(__FILE__)
DATA_DIR = File.join(TEST_DIR, "data")

def data_file(file)
File.join(DATA_DIR, file)
end

def test_xyz
open(data_file("fred"))
assert ...
end
end

The problem is I have to add this to every test case since I can't put
data_file inside a module and include it without also having o define
TEST_DIR as a constant of Object inside every testcase.

In other words, I don't want to do:

TEST_DIR = File.dirname(__FILE__)
class TestMyClass < Test::Unit::TestCase
require 'test_helper'
include TestHelper
TestHelper.init __FILE__
end

This just seems a bit verbose.

I guess what I need is something like: __REQUIRING_FILE__
so my TestHelper module could be written as:

module TestHelper
TEST_DIR = File.dirname(__REQUIRING_FILE__)
DATA_DIR = ...as above
end

Has anyone ever needed such a thing? Is there a better way of solving
this problem?

Thanks
--
Jim Freeze

6 Answers

Robert Klemme

10/30/2006 8:05:00 PM

0

Jim Freeze wrote:
> Hello
>
> I keep wondering if there is a way to have __FILE__ be delayed
> in its evaluation. For example, in my tests cases, I usually do the
> following so that my test cases are imune to where they are run from:
>
> class TestMyClass < Test::Unit::TestCase
>
> TEST_DIR = File.dirname(__FILE__)
> DATA_DIR = File.join(TEST_DIR, "data")
>
> def data_file(file)
> File.join(DATA_DIR, file)
> end
>
> def test_xyz
> open(data_file("fred"))
> assert ...
> end
> end
>
> The problem is I have to add this to every test case since I can't put
> data_file inside a module and include it without also having o define
> TEST_DIR as a constant of Object inside every testcase.
>
> In other words, I don't want to do:
>
> TEST_DIR = File.dirname(__FILE__)
> class TestMyClass < Test::Unit::TestCase
> require 'test_helper'
> include TestHelper
> TestHelper.init __FILE__
> end
>
> This just seems a bit verbose.
>
> I guess what I need is something like: __REQUIRING_FILE__
> so my TestHelper module could be written as:
>
> module TestHelper
> TEST_DIR = File.dirname(__REQUIRING_FILE__)
> DATA_DIR = ...as above
> end
>
> Has anyone ever needed such a thing? Is there a better way of solving
> this problem?
>
> Thanks

Untested - just sketching ideas:

mytest.rb:

require 'test/unit'

class TC < Test::Unit::TestCase
def open_data(f,&b)
File.open(File.join(get_dir, f), &b)
end

def get_dir
x = caller(2).shift
if /^(.*):\d+:in / =~ x
File.dirname $1
else
raise "Whatever"
end
end
end


your_test.rb:

class TestFoo < TC
def test_x
open_data("foo") do |io|
io.each_line ...
end
end
end

robert

Louis J Scoras

10/30/2006 8:18:00 PM

0

I think this could get there:

def __REQUIRING_FILE__; caller[1] =~ /([^:]*):/; $1 end

But probably only if you don't call it within any other methods. I'll
have to play around some more.


--
Lou.

Robert Klemme

10/30/2006 8:24:00 PM

0

Louis J Scoras wrote:
> I think this could get there:
>
> def __REQUIRING_FILE__; caller[1] =~ /([^:]*):/; $1 end
>
> But probably only if you don't call it within any other methods. I'll
> have to play around some more.

I played around already :-)

def find_dir
caller.each do |cl|
if %r{^(.*):\d+(?::in )?$} =~ cl
f = $1
return File.dirname(f) if f != __FILE__
end
end
raise "Not found"
end

Regards

robert

Louis J Scoras

10/30/2006 8:40:00 PM

0

On 10/30/06, Robert Klemme <shortcutter@googlemail.com> wrote:

> I played around already :-)
>
> def find_dir
> caller.each do |cl|
> if %r{^(.*):\d+(?::in )?$} =~ cl
> f = $1
> return File.dirname(f) if f != __FILE__
> end
> end
> raise "Not found"
> end
>

Very nice.

I realized after I posted that ':' could be a valid character in a
filename, so my above try at it won't work anyway.

--
Lou.

Louis J Scoras

10/30/2006 8:47:00 PM

0

On 10/30/06, Jim Freeze <jim@freeze.org> wrote:

> module TestHelper
> __REQUIRING_FILE__ = /([^:]*):/.match(caller[1])[1]
>

Just make sure that you won't have any file names with a ':' in them =)


--
Lou.

Ara.T.Howard

10/31/2006 4:00:00 AM

0