0

興味深い難問があります。Ruby で PSD を解析するためのライブラリを開発中です。また、仲間が同時に JavaScript で PSD を解析するためのライブラリに取り組んでいます。git サブモジュールを介して同じ単体テストを共有したいと考えています。

簡単な JSON DSL を使用して各テストを定義することにしました。単一のテストは次のようになります。

{
  "_name": "Layer should render out",
  "_file": "test/fixtures/layer_out.psd",
  "_exports_to": "test/controls/layer_out_control.png"
}

したがって、適切なテスト ハーネスを構築して、JSON を適切なネイティブ ユニット テストに変換するのは私たち次第です。私は MiniTest を使用して速度を上げてきましたが、いくつかの壁にぶつかっています。

これが私がこれまでに得たものです。テスト ハーネスはTargetPractice当面の間、次のように命名されます。

# run_target_practice.rb

require 'target_practice'

TargetPractice.new(:test) do |test|
  test.pattern = "test/**/*.json"
end

# psd_test.rb

class PSDTest < MiniTest::Unit::TestCase
  attr_accessor :data

  def tests_against_data
    # do some assertions
  end
end

# target_practice.rb

class TargetPractice
  attr_accessor :libs, :pattern

  def initialize(sym)
    @libs = []
    @pattern = ""

    yield self

    run_tests
  end

  def run_tests
     FileList[@pattern].to_a.each do |file|
       test_data = JSON.parse(File.open(file).read)
       test = PSDTest.new(test_data["_name"]) do |t|
         t.data = test_data
       end
     end
  end
end

残念ながら、クラスに留まるのに苦労しyieldています。また、初期化するとすぐにテストが実行されるようです。initializePSDTest

いくつかのオブジェクトを動的に作成しMiniTest::Unit::TestCase、適切なデータ プロパティを設定してから、テストを実行したいと考えています。どんなポインタでも大歓迎です!

4

1 に答える 1

1

ここで物事を少し複雑にしすぎていると思います。必要なのは、パラメーター化された testです。これは、mintest/spec を使用して実装するのは非常に簡単です。

describe "PSD converter" do
  def self.tests(pattern = 'test/**/*.json')
    FileList[pattern].map{|file| JSON.parse(File.read(file))}
  end

  tests.each do |test|
    it "satisfies test: " + test["_name"] do
      # some assertions using test["_file"] and test["_exports_to"]
    end
  end
end
于 2012-04-06T14:06:19.830 に答える