Sha256: 1d1fc074c6c5ce3f9a15ff395c57d9d99ef855b380896d81b41f9d58e491ac5f

Contents?: true

Size: 1.97 KB

Versions: 84

Compression:

Stored size: 1.97 KB

Contents

module Test
  module Unit
    module Data
      class << self
        def included(base)
          base.extend(ClassMethods)
        end
      end

      module ClassMethods
        # TODO: WRITE ME.
        def data(*arguments, &block)
          n_arguments = arguments.size
          case n_arguments
          when 0
            data_set = block
          when 1
            data_set = arguments[0]
          when 2
            data_set = {arguments[0] => arguments[1]}
          else
            message = "wrong number arguments(#{n_arguments} for 1..2)"
            raise ArgumentError, message
          end
          current_data = current_attribute(:data)[:value] || []
          attribute(:data, current_data + [data_set])
        end

        # TODO: WRITE ME.
        def load_data(file_name)
          case file_name
          when /\.csv/i
            loader = CSVDataLoader.new(self)
            loader.load(file_name)
          else
            raise ArgumentError, "unsupported file format: <#{file_name}>"
          end
        end

        class CSVDataLoader
          def initialize(test_case)
            @test_case = test_case
          end

          def load(file_name)
            require 'csv'
            header = nil
            CSV.foreach(file_name) do |row|
              if header.nil?
                header = row
                next
              end
              label = nil
              data = {}
              header.each_with_index do |key, i|
                if key == "label"
                  label = row[i]
                else
                  data[key] = normalize_value(row[i])
                end
              end
              @test_case.data(label, data)
            end
          end

          private
          def normalize_value(value)
            Integer(value)
          rescue ArgumentError
            begin
              Float(value)
            rescue ArgumentError
              value
            end
          end
        end
      end
    end
  end
end

Version data entries

84 entries across 68 versions & 6 rubygems

Version Path
rhodes-3.2.0.beta.7 spec/framework_spec/extensions/test-libs/test/unit/data.rb
rhodes-3.2.0.beta.6 spec/framework_spec/extensions/test-libs/test/unit/data.rb
rhodes-3.2.0.beta.5 spec/framework_spec/extensions/test-libs/test/unit/data.rb
test-unit-2.4.0 lib/test/unit/data.rb