Sha256: 3e89425a6e87c22a46515a3208c4b5e5ff6f1ab1a6114466b7b600621a8cb049

Contents?: true

Size: 983 Bytes

Versions: 129

Compression:

Stored size: 983 Bytes

Contents

require 'generator/exercise_case'

class ComplexNumbersCase < Generator::ExerciseCase

  def workload
    indent_text(4, testcase)
  end

  def number
    (respond_to? :input) ? input : z1
  end

  def operation
    case property
    when "add" then " + #{construct_complex(z2)}"
    when "sub" then " - #{construct_complex(z2)}" 
    when "mul" then " * #{construct_complex(z2)}" 
    when "div" then " / #{construct_complex(z2)}" 
    else ".#{property}"
    end
  end

  def testcase
    "expected = #{expected_value}\n" +
    "assert_equal expected, #{construct_complex(number)}#{operation}"
  end

  def construct_complex(value)
    "ComplexNumber.new(#{parse_number(value)})"
  end

  def expected_value
    (expected.is_a? Numeric) ? expected : construct_complex(expected)
  end

  def parse_number(number)
    return number if number.is_a? Numeric
    Array(number).map do |elem|
      (elem.is_a? Numeric) ? elem.to_s : "Math::#{elem.upcase}"
    end.join(", ")
  end

end

Version data entries

129 entries across 129 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.179 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.178 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.177 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.176 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.175 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.174 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.173 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.172 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.171 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.170 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.169 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.167 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.166 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.165 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.164 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.163 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.162 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.161 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb
trackler-2.2.1.160 tracks/ruby/exercises/complex-numbers/.meta/generator/complex_numbers_case.rb