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.12 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.11 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.10 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.9 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.8 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.7 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.6 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.5 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.4 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.3 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.2 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.1 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.1.0.0 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.55 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.54 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.53 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.52 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.51 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.50 tracks/crystal/src/generator/exercises/forth.cr
trackler-2.0.8.49 tracks/crystal/src/generator/exercises/forth.cr