From: "D. Deryl Downey" Date: 2013-01-05T11:24:42+09:00 Subject: Re: require "test/unit" This is a multi-part message in MIME format. --------------090309090205090108000101 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 = a, b > end > def add > @a + @b > end > def sub > @a - @b > end > def mul > @a * @b > end > def div > return "undef" if @b == 0 > @a / @b > end > end > > x, y = ARGV[0].to_i, ARGV[1].to_i > if ARGV.length == 0 > print "Usage calc 3 4\n" > exit > end > c = 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 = a, b > end > def add > @a + @b > end > def sub > @a - @b > end > def mul > @a * @b > end > def div > return "undef" if @b == 0 > @a / @b > end > end > > class TestClass < Test::Unit::TestCase > def test_add > c = Calculator.new( 12, 3 ) > assert_equal 15, c.add > end > def test_sub > c = Calculator.new( 12, 3 ) > assert_equal 9, c.sub > end > def test_mul > c = Calculator.new( 12, 3 ) > assert_equal 36, c.mul > end > def test_div > c = Calculator.new( 12, 3 ) > assert_equal 4, c.div > end > end > > Mattias A. > December 31, 2012 4:21 AM > Hi Dami叩n M. Gonz叩lez! > > 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 = 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 - The Winter's Tale, Act III, Scene II - A court of Justice. --------------090309090205090108000101 Content-Type: multipart/related; boundary="------------090706040109000707070306" --------------090706040109000707070306 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit I would also recommend you get the book called Build Awesome Command-Line Applications in Ruby, by David Bryant Copeland. Excellent book!

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

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.

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').


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 = a, b
end
def add
@a + @b
end
def sub
@a - @b
end
def mul
@a * @b
end
def div
return "undef" if @b == 0
@a / @b
end
end

x, y = ARGV[0].to_i, ARGV[1].to_i
if ARGV.length == 0
print "Usage calc 3 4\n"
exit
end
c = 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 = a, b
end
def add
@a + @b
end
def sub
@a - @b
end
def mul
@a * @b
end
def div
return "undef" if @b == 0
@a / @b
end
end

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

December 31, 2012 4:21 AM
Hi Dami叩n M. Gonz叩lez!

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 = 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 - The Winter's Tale, Act III, Scene II - A court of Justice.


--------------090706040109000707070306 Content-Type: image/jpeg; x-apple-mail-type=stationery; name="postbox-contact.jpg" Content-Transfer-Encoding: base64 Content-ID: Content-Disposition: inline; filename="postbox-contact.jpg" /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgK CgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkL EBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAAR CAAZABkDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl 5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYk NOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk 5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD0jxX40t9M8Op4o1W0stQ1G+t7eTzLm2jk ee4kiViWZlJxkk+gAwMAAV+eVa1SVVu595gsIq6UIo4bwp4q8Ya87Xn9n6L5SMCsY0y3BIPr 8npjjtmodaex7ryqhGGq/M9w8PpZXP2OabR7C3kkKEmO0iR0OeqsqgqR2IORVRq1FJXPExOF jSuuhx//AAvf4nf9Djd/98p/hXp/Wq/8x5H1el/KeJfGHW7mLQ/BsUT7Y5rOEbieA5hjUE/n +teRO/tJH2GRKCg5s0fhVJc3GlWpbX/s93HqvlXYiI2tCQAmQecbiOcc8dMUou2qR7FaDktX oe/2GoG38QnSpJFZ4SpG3oRnIIrpqr3VJ9DxsdRTwymu58+f2qf7wrqsfJ3Pl/xR+0Xq3j+8 tfAt7oJ0WDSESBFmGLhJYkCt5u7GwhkOV4xjB5rtq5R7K9RO7OzKs4hCXs5KyZ3/AMG/jrpf ha/1LT9esLdr025kSVY/NjlOeDgdz+VebXwdSlTVaGqeh9JQzGnWq/VKmklr6oi8Z/tPeM/C 8+n6zpuo7dTAku5jIgZXV2JWJ1PYDBxxjiuvLMJHF8yq7L8zi4oxqwdCFKnvJ/gebf8AC0/2 n/8Aomepf+E3df4V7n9mUfM+F+uz7oyv2wf+TtPiD/2Ef/beOvR6nD0PNND/AOQ5pv8Auj+R rhxH8CXqe1hP98peh13hn/kr/hT/ALGDT/8A0fHUZd8BWffGj9va9Q+eP//Z --------------090706040109000707070306 Content-Type: image/jpeg; x-apple-mail-type=stationery; name="compose-unknown-contact.jpg" Content-Transfer-Encoding: base64 Content-ID: Content-Disposition: inline; filename="compose-unknown-contact.jpg" /9j/4AAQSkZJRgABAQEARwBHAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEC AQEBAQEBAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/2wBDAQEBAQEBAQICAgICAgIC AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/wAAR CAAZABkDAREAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAABgcICQr/xAA0EAABAwMCAgUK BwAAAAAAAAACAQMEBQYRABITIQcUMUF2CBUXIjI2N0JRtVRWkZOV0dL/xAAYAQEAAwEAAAAA AAAAAAAAAAADAAEEAv/EACQRAAICAAQGAwAAAAAAAAAAAAABAhEDMrHREyExM0FxgfDx/9oA DAMBAAIRAxEAPwDuEt+gW/ULet6oVC3rfqNQqFv0OfPn1GhUqfOmzZtKZlS5UqZMaNwzNwiJ VIl7eXLCaZIGwBl3TY8epPx2+jy2ZNPjvkwc9uhW8j7nCPhvOsQliYIeS7cvCpp8o50qwrC4 v3lsNSDbdmTEhvs2tahxpfV3WnmbbozJEw/gwdadbYExVRXKEKoSdvJcaOSqxE7/AAiX0gXx +a69/JSf9alIlste0VzaNpeFrcT9KKymotyiaZ0KRCnzacoE7Kjzn4gi2KqUh3jqDHDHv4mR UfruTWlMzlVUKIVNp9GguEJnAh0+IZjyAiisgyRDnu5azS8miKqjOTVkKqS/psG37fo1Fbab eg25b8eZPeFJBBJSjMG5HjMeyihnaauZwe4OGiju13GAcpOwBeN+U8/IkGbsiS8b7ryogmbz hbyc9REROfZhERO5ETShjPtvpGqTUyLErytS4siSwx5x2tRH4hPOI0DkjZtaJtFxuVEbIUUi yeNujlBUJGbJN6nM/Cyf2Hf60YgjvKA+NPSP4gT7axpcPtr51YWJnYn9dnAQWl722p4ot37y zqnlfp6FrqbwawG8/9k= --------------090706040109000707070306-- --------------090309090205090108000101--