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!
tamouse mailing lists <tamouse.lists@gmail.com>
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. <lists@ruby-forum.com>
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. <lists@ruby-forum.com>
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. <lists@ruby-forum.com>
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.