Mats
Mats

Reputation: 303

Ruby: Strange string comparison assertion behaviour

Can anyone explain what is happening here? I have this simple class with some static methods, and I want to test them.

yaqueline/build/converters/asciidocconverter.rb

# encoding: UTF-8
require 'asciidoctor'

module Yaqueline
  module Build
    module Converters

      class AsciiDocConverter < Converter

        class << self

          def matches path
            path =~ /\.(asciidoc|adoc|ascii|ad)$/
          end

          def convert content
             html = Asciidoctor.convert content, to_file: false, safe: :safe
              html = get_guts_out_of_body html
              puts "asciidoc #{html}"
              html
            end

            def get_guts_out_of_body html
              if html =~ /<body>/
                puts "get guts: #{html}"
                return html.match(%r{(?<=<body>).*(?=</body>)})
              end
              html
            end

          end # class << self

        end # class

      end
    end
  end

and the test in test/build/converters/asciidocconverter_test.rb:

# encoding: utf-8
require 'helper'
require 'yaqueline/build/converters/asciidocconverter'

class TestAsciidocConverter < Test::Unit::TestCase

  should "be able to get body html from a document" do
value = %q{SUCCESS}
html = %Q{
     <html>
       <head>
     <title>Hej värld</title>
       </head>
       <body>#{value}</body>
     </html>}
guts = Yaqueline::Build::Converters::AsciiDocConverter.get_guts_out_of_body html
puts "guts was '#{guts}'"
assert value.eql?(guts), "guts was '#{guts}', expected '#{value}'"
  end

end

When running the test with

$ rake test TEST=test/build/converters/asciidocconverter_test.rb

The results looks good to me:

Started
get guts: 
     <html>
       <head>
     <title>Hej värld</title>
       </head>
       <body>SUCCESS</body>
     </html>
guts was 'SUCCESS'
F
===============================================================================================================================================================================
Failure:
  guts was 'SUCCESS', expected 'SUCCESS'.
  <false> is not true.
test: AsciidocConverter should be able to get body html from a document. (TestAsciidocConverter)
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:37:in `block in <class:TestAsciidocConverter>'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `instance_exec'
/Users/mats/src/examples/yaqueline/test/build/converters/asciidocconverter_test.rb:39:in `block in create_test_from_should_hash'
===============================================================================================================================================================================

but the assertion fails which seems odd to me and I'll need some help.

I'm running ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15] and my Gemfilelooks like

# Add dependencies required to use your gem here.
# Example:
#   gem "activesupport", ">= 2.3.5"

gem 'mercenary'
gem 'safe_yaml'
gem 'kramdown'
gem 'colorator'
gem 'pathutil'
gem 'nokogiri'
gem 'sass'
gem 'listen', '~> 3.0'
gem 'asciidoctor'
gem 'tilt'
gem 'erubis'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
  gem "rdoc", "~> 3.12"
  gem "bundler", "~> 1.0"
  gem "juwelier", "~> 2.1.0"
  gem "simplecov", ">= 0"
  gem 'rubocop', '~> 0.48.1', require: false
  gem 'thin' # or whatever I end up with
  gem 'minitest'
  gem 'test-unit'
  gem 'shoulda'
end

Maybe this helps to realize hat test harness I'm using.

Can anyone see the mistake or explain what's going on?

Cheers

Upvotes: 0

Views: 115

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

Inspect the types of values being compared. One of them is not a string. (Thus, it can't be equal to a string).

 guts = html.match(%r{(?<=<body>).*(?=</body>)})
 guts # => #<MatchData "SUCCESS">
 guts.to_s # => "SUCCESS"

Upvotes: 2

Related Questions