Newbie Q: Self-Testing Ruby Files (Double Assignment to Constants)

D

ded

I like to make class files self-testing and can do so with the
following arrangement:

==================== Junk.rb ==========================
Class Junk
CONST = 'a value'
def initialize
end
end

# Unit tests
if __FILE__ == $0
require 'test/TestJunk.rb'
end

===================== test/TestJunk.rb ==================
require 'test/unit'
require 'Junk' unless defined?(CONST)

class TestJunk < Test::Unit::TestCase
def test_sometest
assert(<<some assertion>>, "Some message")
end
end
==========================================================

The thought is that I can run Junk.rb or TestJunk.rb as a top-level
script and have the unit tests run either way.

This works, EXCEPT if I run Junk.rb it complains that I have already
assigned to CONST, so it is apparently loading Junk.rb twice.

I tried to avoid these with the if defined?(CONST) construct, but ot
does not help.

Questions:

1. I thought the 'require' method would load the file only if it was
not loaded already. If so, how could two requires result in two
executions of the CONST = 'some value' line? There's something I'm not
getting here.

2. Why doesn't the "if defined?(CONST)" thingy help? I would think it
is unnecessary given my assumption in 1. I'm definitely confused.

Ruby-Nuby in need of guidance.

Thanks.
 
A

Austin Ziegler

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D test/Test=
Junk.rb =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
require 'test/unit'
- require 'Junk' unless defined?(CONST)
+ require 'Junk' unless defined?(Junk::Const)

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
D

David A. Black

Hi --

I like to make class files self-testing and can do so with the
following arrangement:

==================== Junk.rb ==========================
Class Junk

class, not Class :)
CONST = 'a value'
def initialize
end
end

# Unit tests
if __FILE__ == $0
require 'test/TestJunk.rb'
end

===================== test/TestJunk.rb ==================
require 'test/unit'
require 'Junk' unless defined?(CONST)

You've defined CONST inside Junk, but you're testing for it at the top
level. Try this:

require 'Junk' unless defined?(Junk::CONST)


David
 
D

ded

Thanks, both.

But shouldn't 'require' not need that sort of test? Should it not know
that the module was already loaded?
 
A

Austin Ziegler

Thanks, both.
=20
But shouldn't 'require' not need that sort of test? Should it not know
that the module was already loaded?

It shouldn't. Using ruby 1.8.2 (2004-12-25) [i386-mswin32], I didn't
experience any warnings to the effect you suggested.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top