Project

General

Profile

Implementing Some Unit Test / Fixing Some Patches etc.

Added by Karl Heinz Marbaise over 15 years ago

Hi,

i have started to implement some unit tests for the trac migration area. Based on my reportings and patches to fix some of the problems.
As first step i have refactored out the code of converting the wiki contents into a stand-alone-class. Based on this class i started to implement the unit test.

Here is my current directory layout:

redmine
  +---- lib
          +-- tasks
                +--- trac
                        +-- trac_migration.rb (The new class)

The unit test i have put under:
redmine
  +---- test
          +-- unit
                +--- lib
                        +-- tasks
                              +--- migration_trac_test.rb (The unit Test for the above class)

So it would be nice if some of those Ruby experts can give me some hints if this is the correct place or if i should change something with the layout.

Here is a little part of the Code for the Unit Test.


require 'test/unit'
require File.dirname(__FILE__) + '/../../../../lib/tasks/trac/trac_migration'

class TracMigrationTest < Test::Unit::TestCase

  def setup
    @trac = Trac.new()
  end

  # The following three test cases are related to issue #2023
  # The construct {{{XYZ}}} is not supported by trac.
  def test_activity_code_tags_10
    assert_equal("{{{XYZ}}}", @trac.wiki("{{{XYZ}}}"))
  end
  # This construct is valid and would produce the expected output in trac.
  # The {{{ must be on a separate line and }}} as well.
  def test_activity_code_tags_20
    assert_equal("<pre>\nXYZ\n</pre>", @trac.wiki("{{{\nXYZ\n}}}"))
  end
  def test_activity_code_tags_30
    assert_equal("<pre>\n<code class=\"java\">\npublic class test\n</code></pre>", @trac.wiki("{{{\n#!java\npublic class test\n}}}"))
  end
....

It would be nice if someone take a look on it....BTW: How is the usual way? Using tabs or spaces ? And how many ? Are there any code guide lines for Redmine?

Here is a code snippet from my implemented class: Any comments are welcome...

#module TracMigration
  class Trac
    TICKET_MAP = []
#    def initialize()
#    end
    def wiki(text)
    # Titles
    text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
    # External Links
    text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
    # Internal Links
    text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
        ....
    end

Naming conventions in Redmine / Ruby ? For the methods too ?

Kind regards
Karl Heinz Marbaise


Replies (3)

RE: Implementing Some Unit Test / Fixing Some Patches etc. - Added by Eric Davis over 15 years ago

Karl Heinz Marbaise wrote:

So it would be nice if some of those Ruby experts can give me some hints if this is the correct place or if i should change something with the layout.

That's fine. It can be moved later.

It would be nice if someone take a look on it....BTW: How is the usual way? Using tabs or spaces ? And how many ?

Ruby is 2 spaces per indent level, no tabs used ever. These links should be helpful for more details:

Are there any code guide lines for Redmine?

Nothing other than following the Ruby and Rails conventions.

Here is a code snippet from my implemented class: Any comments are welcome...
[...]
Naming conventions in Redmine / Ruby ? For the methods too ?

Couple of comments.

  • Indent the body of the methods.
  • Not sure what TICKET_MAP is for but I would guess it's for storing the mapping of Trac to Redmine tickets? If so an instance variable would be better, that way you can have two instances of the Trac class without overwritting data. You can refer it by: @ticket_map or self.ticket_map inside the class and @my_instance_of_the_trac_class.ticket_map on an object.

  class Trac
    # attr_accessor is the method that creates a getter and setter in Ruby.  http://www.ruby-doc.org/docs/UsersGuide/rg/accessors.html
    attr_accessor :ticket_map

    def initialize
      self.ticket_map = [ ]
    end

    def wiki(text)
      # Titles
      text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
      # External Links
      text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
      # Internal Links
      text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
       ....
    end
end

Thanks, let me know if you need anymore help.

Eric

RE: Implementing Some Unit Test / Fixing Some Patches etc. - Added by Karl Heinz Marbaise over 15 years ago

Hi,

first of all thanks for your tips and hints.

So i have continued my work on that....and attached both the unit test and the class itself.

May be you or others can take a look inside and give some hints/comments etc....to improve them....Currently one unit test will fail (I'll fix that).

The ticket_map in the class is not my final state...but it seemed to be a good choice...Hm...we will see...

The next step will be to integrate the class into the migrate_from_trac.rake file and test a real conversion with the refactored code...

Kind regards
Karl Heinz Marbaise

trac_migration.rb (10.5 KB) trac_migration.rb The Class TracWiki
migration_trac_test.rb (6.17 KB) migration_trac_test.rb The Test Case for the class.

RE: Implementing Some Unit Test / Fixing Some Patches etc. - Added by Karl Heinz Marbaise over 15 years ago

I have enhanced my implementation. They current state of implementation can be seen at #2114

Kind regards
Karl Heinz Marbaise

    (1-3/3)