Sha256: ffdb4f2de91b529c1cc1dc499b82d5e7b2f3a207c88e9437a83d72943f4c3764

Contents?: true

Size: 1.15 KB

Versions: 86

Compression:

Stored size: 1.15 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)["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|
      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(Exception) 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

86 entries across 86 versions & 1 rubygems

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