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.119 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.118 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.117 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.116 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.115 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.114 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.113 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.111 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.110 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.109 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.108 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.107 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.106 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.105 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.104 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.103 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.102 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.101 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.100 tracks/crystal/generator/src/generators/forth.cr
trackler-2.2.1.99 tracks/crystal/generator/src/generators/forth.cr