1

I am developing a Ruby gem that has both a pure ruby and a native-compiled component. Pure ruby is there for times where a speed-up is needed, but the native code cannot be compiled. However, I am having trouble testing both the native and pure ruby code in the same test suite.

I have a module which contains the tests, and am doing something like this:

require 'helper'

class BinarySearchNativeTest < Minitest::Test
  require 'binary_search/native'

  include CommonTests
end

I have something similar with a different class name, and a require line to import the pure ruby version of the code.

However, when I run this test suite, depending on the order in which things are loaded, it will use either the native or pure ruby version in both test classes.

Short of renaming methods and other trickery to test this, which will change the code under test, I am at a loss on what to do next. Any suggestions on how to do this, without fracturing the gem into a pure gem and a native gem, which has other problems?

Michael Graff
  • 495
  • 1
  • 4
  • 14
  • 1
    I had same problem, and avoided the issue by having the test suite run whatever was available in the Ruby version I was testing under (i.e. if it could build native extensions I used them, otherwise not). I now rely on Travis CI to ensure coverage of native vs non-native is handled automatically. However, I can also simply delete the built extension and run tests, and have a rake task for that. Here's how I did it: http://stackoverflow.com/questions/17406246/native-extensions-fallback-to-pure-ruby-if-not-supported-on-gem-install My gem which does this is `games_dice` – Neil Slater Dec 28 '13 at 21:19
  • Yea, I thought about doing it that way, so I can keep the pure Ruby and native code in sync, but TDD is very hard when one has to wait for Travis to report a failure. – Michael Graff Dec 29 '13 at 00:12
  • You could have a single task that was along the lines of `[:compile,:test,:delete_compiled_extension,:test]` (where `:delete_compiled_extension` is something you add that removes compiled files). I do that as two separate tasks in my rake file, but no real reason why you cannot combine them. – Neil Slater Dec 29 '13 at 07:50

0 Answers0