Sha256: 2ef65717ac2ba8915b56371be5136aa26374c61f0f210abf9433bfcd5af0f31f

Contents?: true

Size: 1.33 KB

Versions: 86

Compression:

Stored size: 1.33 KB

Contents

require "../exercise_generator"
require "../exercise_test_case"

class PascalsTriangleGenerator < ExerciseGenerator
  def exercise_name
    "pascals-triangle"
  end

  def test_cases
    test_cases = [] of JSON::Any
    JSON.parse(data)["cases"].each do |group|
      group.each do |a, b|
        test_cases.concat(b) if a.as_s == "cases"
      end
    end

    test_cases.map do |test_case|
      PascalsTriangleTestCase.new(test_case)
    end
  end
end

class PascalsTriangleTestCase < ExerciseTestCase
  private getter description : JSON::Any
  private getter count : JSON::Any
  private getter expected : JSON::Any

  def initialize(test_case)
    @description = test_case["description"]
    @count = test_case["count"]
    @expected = fix_empty_array(test_case["expected"])
  end

  def workload
    if !error?
      "PascalsTriangle.rows(#{count}).should eq(#{expected})"
    else
      <<-WL
      expect_raises(ArgumentError) do
            PascalsTriangle.rows(#{count})
          end
      WL
    end
  end

  def test_name
    if !error?
      "will return the first #{count} row(s)"
    else
      "will raise an Argument error for #{description}"
    end
  end

  private def error?
    expected == -1
  end

  private def fix_empty_array(json)
    if json.to_s.match(/\[\]/)
      JSON.parse("[] of Int32".to_json)
    else
      json
    end
  end
end

Version data entries

86 entries across 86 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.179 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.178 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.177 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.176 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.175 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.174 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.173 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.172 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.171 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.170 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.169 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.167 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.166 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.165 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.164 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.163 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.162 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.161 tracks/crystal/generator/src/generators/pascals-triangle.cr
trackler-2.2.1.160 tracks/crystal/generator/src/generators/pascals-triangle.cr