From: tamouse mailing lists Date: 2013-01-05T11:30:51+09:00 Subject: Re: require "test/unit" --f46d04016c275012b004d28162b4 Content-Type: multipart/alternative; boundary=f46d04016c275012ac04d28162b3 --f46d04016c275012ac04d28162b3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable That's davetron1000 :) On Fri, Jan 4, 2013 at 8:24 PM, D. Deryl Downey wrote= : > I would also recommend you get the book called Build Awesome Command-Line > Applications in Ruby, by David Bryant Copeland. Excellent book! > > tamouse mailing lists > January 4, 2013 9:21 PM > > There is a structure for building command-line applications that may > be something you want to adopt. > > I'm going to point you to davetron1000's gems: methadone and GLI, both > of which are builders for command-line apps (kind of like rails is for > web apps). They provide some nifty things for making nice CLI apps, > but possibly the tl;dr is that you implement the bare minimum in the > part that does the part to take arguments from the command line, and > then pass them to your classes/modules/etc, which are in separate > files. > > Generally, a CLI app structure looks like this: > > my_cli_app/ > Gemfile > Rakefile > bin/ > my_cli_app # this contains the command line interaction part > lib/ > my_cli_app/ # these implement specific features of the module/class > version.rb > classOne.rb > ... > my_cli_app.rb # this implements the module/class > my_cli_app.gemspec # describes how to build this gem > README.rdoc # application documentation that gets bundled in rdoc > spec/ # rspec tests for the app's classes > test/ # Test::Unit tests for app's classes > > (and if you're awesome) > > feature/ # cucumber/aruba tests for the app > > Derrick B. > January 4, 2013 3:07 PM > > You're right, I should not have stated that in that way, but my main > point was directly related to how I thought the OP was adding the test > class into the current code, and not separately. He was receiving an > error, which I also encountered, so I gave my solution. > > Thank you for the clarification on proper test class usage. It should > help me, and the OP, to steer away from such errors by separating the > code. > > sto.mar@web.de > January 4, 2013 2:56 PM > > > Defining the class to be tested in the test file does not make > any sense, since you want to use your class elsewhere, and not only > in tests. You would define it in a separate file and require it > (with `require' or `require_relative'). > > > Derrick B. > January 4, 2013 1:48 PM > > I'm kind of new to using test/unit ( and Ruby! :) ), but I was getting > that same error, too. What I discovered was just adding a test class to > my existing code caused that error until I stripped the code down to > just classes. For example, I wrote a simple Calculator class with four > methods (add, subtract, multiply, divide). I then added code for it to > be interactive. I had to then remove that extra code so that my file > contains only the Calculator class and the TestClass, which included the > test cases, then it worked. > > So, I think (and I am guessing here) that your test files should only > include classes to be tested and the actual testing class. That would > make sense, because when testing code, you are mainly concerned with > static test cases which cover all possible execution paths, so why > bother with user interaction. > > Here are the two files, the original and the one modified for testing: > > ######## Original ################# > class Calculator > def initialize(a, b) > @a, @b =3D a, b > end > def add > @a + @b > end > def sub > @a - @b > end > def mul > @a * @b > end > def div > return "undef" if @b =3D=3D 0 > @a / @b > end > end > > x, y =3D ARGV[0].to_i, ARGV[1].to_i > if ARGV.length =3D=3D 0 > print "Usage calc 3 4\n" > exit > end > c =3D Calculator.new( x, y ) > puts "#{x} plus #{y} equals: #{c.add}" > puts "#{x} minus #{y} equals: #{c.sub}\n" > puts "#{x} times #{y} equals: #{c.mul}\n" > puts "#{x} divided by #{y} equals: #{c.div}\n" > > > ######## For Testing ################# > require 'test/unit' > > class Calculator > def initialize(a, b) > @a, @b =3D a, b > end > def add > @a + @b > end > def sub > @a - @b > end > def mul > @a * @b > end > def div > return "undef" if @b =3D=3D 0 > @a / @b > end > end > > class TestClass < Test::Unit::TestCase > def test_add > c =3D Calculator.new( 12, 3 ) > assert_equal 15, c.add > end > def test_sub > c =3D Calculator.new( 12, 3 ) > assert_equal 9, c.sub > end > def test_mul > c =3D Calculator.new( 12, 3 ) > assert_equal 36, c.mul > end > def test_div > c =3D Calculator.new( 12, 3 ) > assert_equal 4, c.div > end > end > > Mattias A. > December 31, 2012 4:21 AM > Hi Dami=C3=A1n M. Gonz=C3=A1lez! > > Tanks for your reply, i have found below but it do not answer my > question or I do not know how to read/use it.. :/ > > class Test::Unit::TestCase > In Files > lib/minitest/unit.rb > lib/test/unit/parallel.rb > lib/test/unit/testcase.rb > Parent > MiniTest::Unit::TestCase > > Included Modules > Test::Unit::Assertions > Public Class Methods > test_order() click to toggle source > # File lib/test/unit/testcase.rb, line 20 > def self.test_order > :sorted > endPublic Instance Methods > on_parallel_worker?() click to toggle source > # File lib/test/unit/parallel.rb, line 149 > def on_parallel_worker? > true > endrun(runner) click to toggle source > # File lib/test/unit/testcase.rb, line 15 > def run runner > @options =3D runner.options > super runner > endGenerated by RDoc 3.12. > > Generated with the Darkfish Rdoc Generator 3. > > > -- > D. Deryl Downey > > "The bug which you would fright me with I seek" - William Shakespeare - T= he Winter's Tale, Act III, Scene II - A court of Justice. > > > > --f46d04016c275012ac04d28162b3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable That's davetron1000 :)

On Fri, Jan 4,= 2013 at 8:24 PM, D. Deryl Downey <me@daviddwdowney.com> = wrote:
I would also recommend you get the book called Build Awesome Command-Line Applications in Ruby, by David Bryant Copeland. Excellent book!

=20 January 4, 2013= =20 9:21 PM

= There is a=20 structure for building command-line applications that may
be=20 something you want to adopt.

I'm going to point you to=20 davetron1000's gems: methadone and GLI, both
of which are builders= =20 for command-line apps (kind of like rails is for
web apps). They=20 provide some nifty things for making nice CLI apps,
but possibly the=20 tl;dr is that you implement the bare minimum in the
part that does=20 the part to take arguments from the command line, and
then pass them=20 to your classes/modules/etc, which are in separate
files.

General= ly, a CLI app structure looks like this:

my_cli_app/
Gemfile
Rakefile
bin/
my_cli_app # this contains the command line=20 interaction part
lib/
my_cli_app/ # these implement specific features of the module/class
version.rb
classOne.rb
...
my_cli_app.rb # this implements the module/class
=20 my_cli_app.gemspec # describes how to build this gem
README.rdoc #=20 application documentation that gets bundled in rdoc
spec/ # rspec=20 tests for the app's classes
test/ # Test::Unit tests for app's= =20 classes

(and if you're awesome)

feature/ #=20 cucumber/aruba tests for the app

=20 January 4, 2013= =20 3:07 PM

= You're right, I should not have stated that in that way, but my main
point was=20 directly related to how I thought the OP was adding the test
class=20 into the current code, and not separately. He was receiving an
error, which I also encountered, so I gave my solution.

Thank you for=20 the clarification on proper test class usage. It should
help me,=20 and the OP, to steer away from such errors by separating the
code.
<= br>
=20 January 4, 2013= =20 2:56 PM


Defining the class to be tested in the test file does not make
any sense, since you want to use your class elsewhere, and not only
in tests. You would define it in a separate file and require it
(with `require' or `require_relative').


=20 January 4, 2013= =20 1:48 PM

I'm kind of new to using test/unit ( and Ruby! :) ), but I was getting
that same=20 error, too. What I discovered was just adding a test class to
my=20 existing code caused that error until I stripped the code down to
just classes. For example, I wrote a simple Calculator class with four
meth= ods (add, subtract, multiply, divide). I then added code for it to
be=20 interactive. I had to then remove that extra code so that my file
conta= ins only the Calculator class and the TestClass, which included the
test cases, then it worked.

So, I think (and I a= m guessing here) that your test files should only
include classes to be tested and the=20 actual testing class. That would
make sens= e, because when testing=20 code, you are mainly concerned with
static test cases which cover all possible execution paths, so why
bother with user interaction.

H= ere are the two files, the original and the one modified for testing:

#= ####### Original #################
class Calculator
def initialize(a,=20 b)
@a, @b =3D a, b
end
def add
@a + @b
end
def sub
@a - @b
end
def mul
@a * @b
end
def div
return "undef" if @b =3D=3D 0
@a / @b
= end
end

x, y =3D ARGV[0].to_i, ARGV[1].to_i
if ARGV.length =3D=3D 0
print &qu= ot;Usage calc 3 4\n"
exit
end
c =3D Calculator.new( x, y )
puts= =20 "#{x} plus #{y} equals: #{c.add}"
puts "#{x} minus #{y} e= quals:=20 #{c.sub}\n"
puts "#{x} times #{y} equals: #{c.mul}\n"
= puts "#{x}=20 divided by #{y} equals: #{c.div}\n"


######## For Testing=20 #################
require 'test/unit'

class Calculator =20 def initialize(a, b)
@a, @b =3D a, b
end
def add
=20 @a + @b
end
def sub
@a - @b
end
def mul
=20 @a * @b
end
def div
return "undef" if @b =3D= =3D 0
=20 @a / @b
end
end

class TestClass < Test::Unit::TestCase def test_add
c =3D Calculator.new( 12, 3 )
assert_equal=20 15, c.add
end
def test_sub
c =3D Calculator.new( 12, 3 ) assert_equal 9, c.sub
end
def test_mul
c =3D=20 Calculator.new( 12, 3 )
assert_equal 36, c.mul
end
def=20 test_div
c =3D Calculator.new( 12, 3 )
assert_equal 4, c.div<= br> end
end

=20 December 31, 201= 2 4:21 AM
Hi Dami=C3=A1n M. Gonz=C3=A1lez!

Tanks for your reply, i have found below but it do not answer my
question or I do not know how to read/use it.. :/

class=20 Test::Unit::TestCase
In Files
lib/minitest/unit.rb
lib/test/unit/p= arallel.rb
lib/test/unit/testcase.rb
Parent
MiniTest::Unit::TestCa= se

Included Modules
Test::Unit::Assertions
Public Class Methods
test_order() click to toggle source
# File lib/test/unit/testcase.rb, line 20
def self.test_order
:sorted
endPublic Instance Methods
on_parallel_= worker?() click to toggle source
# File lib/test/unit/parallel.rb, line 149
de= f on_parallel_worker?
true
endrun(runner) click to toggle source
= # File lib/test/unit/testcase.rb, line 15
def run runner
@options =3D runner.options
super runner
endGenerated by RDoc 3.12.

= Generated with the Darkfish Rdoc Generator 3.


--
D. Deryl Downey
"The bug which you would fright me with I seek" - Will=
iam Shakespeare - The Winter's Tale, Act III, Scene II - A court of Jus=
tice.



--f46d04016c275012ac04d28162b3-- --f46d04016c275012b004d28162b4 Content-Type: image/jpeg; x-apple-mail-type=stationery; name="postbox-contact.jpg" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: 5f1106fac3bdf29f_0.1.1 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkI CQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQ EBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAZABkDAREA AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5i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Q5pv8Auj+RrhxH8CXq e1hP98peh13hn/kr/hT/ALGDT/8A0fHUZd8BWffGj9va9Q+eP//Z --f46d04016c275012b004d28162b4 Content-Type: image/jpeg; x-apple-mail-type=stationery; name="compose-unknown-contact.jpg" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: 5f1106fac3bdf29f_0.1.2 /9j/4AAQSkZJRgABAQEARwBHAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQECAQEB AQEBAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/2wBDAQEBAQEBAQICAgICAgICAgICAgIC AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/wAARCAAZABkDAREA AhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAABgcICQr/xAA0EAABAwMCAgUKBwAAAAAAAAACAQME BQYRABITIQcUMUF2CBUXIjI2N0JRtVRWkZOV0dL/xAAYAQEAAwEAAAAAAAAAAAAAAAADAAEEAv/E ACQRAAICAAQGAwAAAAAAAAAAAAABAhEDMrHREyExM0FxgfDx/9oADAMBAAIRAxEAPwDuEt+gW/UL et6oVC3rfqNQqFv0OfPn1GhUqfOmzZtKZlS5UqZMaNwzNwiJVIl7eXLCaZIGwBl3TY8epPx2+jy2 ZNPjvkwc9uhW8j7nCPhvOsQliYIeS7cvCpp8o50qwrC4v3lsNSDbdmTEhvs2tahxpfV3WnmbbozJ Ew/gwdadbYExVRXKEKoSdvJcaOSqxE7/AAiX0gXx+a69/JSf9alIlste0VzaNpeFrcT9KKymotyi aZ0KRCnzacoE7Kjzn4gi2KqUh3jqDHDHv4mRUfruTWlMzlVUKIVNp9GguEJnAh0+IZjyAiisgyRD nu5azS8miKqjOTVkKqS/psG37fo1Fbabeg25b8eZPeFJBBJSjMG5HjMeyihnaauZwe4OGiju13GA cpOwBeN+U8/IkGbsiS8b7ryogmbzhbyc9REROfZhERO5ETShjPtvpGqTUyLErytS4siSwx5x2tRH 4hPOI0DkjZtaJtFxuVEbIUUiyeNujlBUJGbJN6nM/Cyf2Hf60YgjvKA+NPSP4gT7axpcPtr51YWJ nYn9dnAQWl722p4ot37yzqnlfp6FrqbwawG8/9k= --f46d04016c275012b004d28162b4--