Sha256: 1347def5c6df4c5b5fe280737ffea9e8d3898eb199a3b2ccc1cb03386370718e

Contents?: true

Size: 1.13 KB

Versions: 185

Compression:

Stored size: 1.13 KB

Contents

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

class ForthGenerator < ExerciseGenerator
  def exercise_name
    "forth"
  end

  def test_cases
    test_cases = [] of JSON::Any
    JSON.parse(data).each do |k, v|
      v.each do |a, b|
        test_cases.concat(b) if a.as_s == "cases"
      end
    end
    test_cases.map do |test_case|
      ForthTestCase.new(test_case)
    end
  end
end

class ForthTestCase < ExerciseTestCase
  private getter input : JSON::Any | String
  private getter description : JSON::Any
  private getter expected : JSON::Any?

  def initialize(test_case)
    @input = test_case["input"].as_a.join
    @description = test_case["description"]
    @expected = fix_empty_array(test_case["expected"]?)
  end

  def workload
    if expected
      "Forth.evaluate(#{input.inspect}).should eq(#{expected})"
    else
      <<-WL
      expect_raises do
            Forth.evaluate(#{input.inspect})
          end
      WL
    end
  end

  def test_name
    description
  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

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.52 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.51 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.50 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.49 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.48 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.47 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.46 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.45 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.44 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.43 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.42 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.41 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.40 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.39 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.38 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.37 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.36 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.34 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.33 tracks/crystal/src/generator/exercises/forth.cr