I'm using Rdoc 2.58 with Ruby 1.9.2p138. When I generate my rdoc files using the Darkfish format (which is the only option), I'm not getting the source code previews with it. There are no error messages, and the HTML files are being generated for all the classes.
I've tried updating the gem and reading the help files, but I'm not seeing any way to fix this problem.
Is this a known issue? What else can I try?
UPDATE:
example code:
class Iamaclass
def initialize
#name = "superclass"
end
def get_name
#name
end
def proud?
true
end
end
Rdoc 2.5.8 outputs
Parsing sources...
100% [ 1/ 1] someruby.rb
Generating Darkfish...
Files: 1
Classes: 1 ( 1 undocumented)
Constants: 0 ( 0 undocumented)
Modules: 0 ( 0 undocumented)
Methods: 3 ( 3 undocumented)
0.00% documented
Elapsed: 0.0s
source code does not toggle in generated rdoc.
Rdoc 3.5.3 outputs
100% [ 1/ 1] someruby.rb
Generating Darkfish format into /Users/paul/dev/alesrelated/someruby/doc...
Files: 1
Classes: 1 (1 undocumented)
Modules: 0 (0 undocumented)
Constants: 0 (0 undocumented)
Attributes: 0 (0 undocumented)
Methods: 3 (3 undocumented)
Total: 4 (4 undocumented)
0.00% documented
Elapsed: 0.1s
source code does toggle in html output.
You need to add comments to your code, you can try using this for example:
# A description about this class
class Iamaclass
# A description about this method
def initialize
#name = "superclass"
end
# Another description about some other method
def get_name
#name
end
def proud?
true
end
end
After this you can try running the rdoc command again.
Related
While running simplecov on Mac OSX, the resulting coverage makes little sense.
If the following test is run:
rails test test/models/channel_test.rb
> 4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
> Coverage report generated for Minitest to /Volumes/[...]/coverage. 0 / 0 LOC (100.0%) covered.
Yet when running rails test test/models the graphical output indicates for test/models/channel_test.rb
require "test_helper"
class ChannelTest < ActiveSupport::TestCase
test "invalid if name not defined" do
channel = Channel.new(priority: 1, unit_cost: 1, daily_limit: 9999)
assert_not channel.valid?
assert_not channel.save, "Saved the channel without a name"
end
update I presumed it might have been the chosen syntax of the test that might be a flaw - I added a supplemental test and the result is still reported
for the model 3 relevant lines. 0 lines covered and 3 lines missed.
class Channel < ApplicationRecord # red
validates :name, presence: true # red
end # red
Thus the test is passing, but the coverage result is confounding:
a) as a standalone, the coverage count is 0/0, whereas the tests pass
b) what constitutes a miss or conversely coverage by a (the?) test ?
test_helper.rb
require 'simplecov'
SimpleCov.start
ENV['RAILS_ENV'] ||= 'test'
require_relative "../config/environment"
require "rails/test_help"
require 'webmock/minitest'
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors)
fixtures :all
def log_in_as(user, shop)
post user_session_url, params: { user_id: user.id, active_shop_id: #site.shop_id }
end
end
Update 2 As per #BroiSatse suggestion, commenting out parallelize(workers: :number_of_processors)
allows coverage to be measured.
I'm trying to set the logging level for the tests I'm running in test/unit
require 'test/unit'
require 'logger'
class MyMath
def self.adder(a,b)
a+b
end
end
class Tester < Test::Unit::TestCase
$logger = Logger.new($stdout)
def test_adder()
$logger.info "Starting Test"
assert_equal(4, MyMath.adder(2,2) )
$logger.info "Ending Test"
end
end
And the output is
Loaded suite ruby.test
Started
I, [2021-07-23T13:12:26.497311 #49542] INFO -- : Starting Test
I, [2021-07-23T13:12:26.497375 #49542] INFO -- : Ending Test
.
Finished in 0.000358 seconds.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
2793.30 tests/s, 2793.30 assertions/s
How do I get it to NOT print the logging messages? In the Test::Unit::UI::Console::TestRunner code it talks about
# Creates a new TestRunner for running the passed
# suite. If quiet_mode is true, the output while
# running is limited to progress dots, errors and
# failures, and the final result. io specifies
# where runner output should go to; defaults to
# STDOUT.
What is quiet_mode, and how can I set it?
I think your problem is that you need to set the log level of your logger accordingly.
$logger = Logger.new($stdout)
$logger.level = Logger::WARN
https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html
Not sure if you use any framework so you could just set the log level in test like
if ENV["test"]
$logger.level = Logger::WARN
end
My class delegates a method to another object (which I'll call the helper object). I want to include documentation of that method in the delegating class, not just the delegated class. The programmer should not use the helper object, only the main object, so documenting in the helper object isn't very useful.
Consider this example. Rdoc outputs documentation about Node#initialize and Helper#hello. I want to have documentation about Node#hello as if it were just another method. Is there a way to do that?
require 'forwardable'
class Node
extend Forwardable
delegate %w(hello) => :#helper
# Takes no params.
def initialize
#helper = Helper.new()
end
end
class Helper
# Outputs 'hello world'
def hello
puts 'hello world'
end
end
-- update --
I tried yard. The yard command looks like this:
yardoc --no-private --protected app/**/*.rb -
I edited the ruby code to try to add documentation for Node#hello:
class Node
extend Forwardable
delegate %w(hello) => :#helper
# Takes no params.
def initialize
#helper = Helper.new()
end
# #!method hello
# Documentation for Node#hello
end
When I run yardoc, it seems to say that it processes three methods:
Files: 1
Modules: 0 ( 0 undocumented)
Classes: 2 ( 2 undocumented)
Constants: 0 ( 0 undocumented)
Attributes: 0 ( 0 undocumented)
Methods: 3 ( 0 undocumented)
60.00% documented
If I don't have the # #!method directive then it says it only processes two methods:
Files: 1
Modules: 0 ( 0 undocumented)
Classes: 2 ( 2 undocumented)
Constants: 0 ( 0 undocumented)
Attributes: 0 ( 0 undocumented)
Methods: 2 ( 0 undocumented)
50.00% documented
So it seems like it seems that documentation for Node#hello, but it doesn't appear in the actual documentation:
So I'm not sure where to go from there.
On advice from #max, I switched to yard. Using yard, I can create method documentation without any actual method like this:
# #!method hello()
# Outputs "hello world"
delegate %w(hello) => :#helper
It's important to note that the documentation has to be above actual code. It won't work if it's by itself.
Is there a way to make rspec just print number of failed examples without any other information?
I want the output to be like this:
Finished in 2.35 seconds (files took 7.33 seconds to load)
1207 examples, 40 failures, 15 pending
Thank You
Out of the box RSpec does not have a formatter that matches the output you want. However, it is possible to write your own formatter.
Here's is what it would look like for RSpec 3+
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
class SimpleFormatter < RSpec::Core::Formatters::BaseTextFormatter
RSpec::Core::Formatters.register self, :example_passed, :example_pending, :example_failed, :dump_pending, :dump_failures, :dump_summary
def example_passed(message)
# Do nothing
end
def example_pending(message)
# Do nothing
end
def example_failed(message)
# Do nothing
end
def dump_pending(message)
# Do nothing
end
def dump_failures(message)
end
def dump_summary(message)
colorizer = ::RSpec::Core::Formatters::ConsoleCodes
output.puts "\nFinished in #{message.formatted_duration} " \
"(files took #{message.formatted_load_time} to load)\n" \
"#{message.colorized_totals_line(colorizer)}\n"
end
end
# Example from RSpec Core - https://github.com/rspec/rspec-core/blob/bc1482605763cc16efa55c98b8da64a89c8ff5f9/lib/rspec/core/formatters/base_text_formatter.rb
Then from the command line run rspec spec/ -r ./path/to/formatter -f SimpleFormatter.
To avoid having to type out the -r and -f options constantly, you can place them in your .rspec
--require ./path/to/formatter
--format SimpleFormatter
Now just running rspec spec/ will automatically use the formatter we just created.
Method 1:-
test.rb
class Test < Test::Unit::TestCase
def test_sample
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
Method 2:-
test.rb
class Test < Test::Unit::TestCase
require 'helper'
include AssertionHelper
def test_sample
test_assertion
end
end
helper.rb
include Test::Unit::Assertions
module AssertionHelper
def test_assertion
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
Method 3:-
test.rb
class Test < Test::Unit::TestCase
require 'helper'
def test_sample
AssertionHelper.test_assertion()
end
end
helper.rb
include Test::Unit::Assertions
module AssertionHelper
def self.test_assertion
assert_true(test)
assert_equal(a,b)
end
end
Result:-
Finished in 38.329532529 seconds.
1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
When using Method 3, I am getting assertion count as "0" instead of "2".
Is it possible for me to get assertion count as 2 using Method 2 ?
You can pass your current TestCase to your module, like this:
sample_test.rb:
require 'test-unit'
require 'helper'
def a; true ; end
def b; true ; end
def test; true ; end
class SampleTest < Test::Unit::TestCase
def test_sample
AssertionHelper.my_assertion(self)
end
end
helper.rb:
module AssertionHelper
def self.my_assertion(test_case)
test_case.instance_exec do
assert_true(test)
assert_equal(a, b)
end
end
end
Sorry, but I can't reproduce your situation, could you please provide Test::Unit version and your ruby version? Best of all would be your Gemfile with Gemfile.lock. The following setup works for me (I use ruby 2.2.0 and test-unit 3.0.8):
ruby-2.2.0 in ~/projects/test-unit ♥ tree
.
├── Gemfile
├── Gemfile.lock
└── test
├── helper.rb
└── sample_test.rb
1 directory, 4 files
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem 'test-unit', '~> 3.0.8'
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
power_assert (0.2.2)
test-unit (3.0.8)
power_assert
PLATFORMS
ruby
DEPENDENCIES
test-unit (~> 3.0.8)
sample_test.rb:
require 'test-unit'
def a; true ; end
def b; true ; end
def test; true ; end
class SampleTest < Test::Unit::TestCase
require 'helper'
include AssertionHelper
def test_sample
my_assertion
end
end
helper.rb:
module AssertionHelper
def my_assertion
assert_true(test)
assert_equal(a, b)
end
end
Running testrb gives 2 assertions, as expected.
ruby-2.2.0 in ~/projects/test-unit ♥ testrb
Loaded suite .
Started
.
Finished in 0.000828 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
1207.73 tests/s, 2415.46 assertions/s
ruby-2.2.0 in ~/projects/test-unit ♥
UPDATE: This is actually strange that you don't get any error (on your method 3), because I get this: NoMethodError: undefined method 'assert_true' for AssertionHelper:Module and this is true, since AssertionHelper doesn't implement any other methods, you can't run any assert_* methods on it. Just use my code above (your method 2) and you should be fine. If you're still curious what can be done, have a look at Test::Unit::Assertions, there's also a lot of built-in assertions defined, maybe you find that useful.
Or, better, use MiniTest or RSpec, since Test::Unit is deprecated and is left in standard library only for legacy test suites.