Sha256: eee81a0aed746a34832ab91233ed3815c1e6ae75680f6199bcb72b82a81599c9

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

require "date"
require "spec_helper"

module Garden
  module DefinePlantBetween
    def self.call(plant, plant_range)
      plant.start_planting_on = plant_range.begin
      plant.end_planting_on = plant_range.end
    end
  end

  class Vegetable
    include GraphQL::Define::InstanceDefinable
    lazy_defined_attr_accessor :name, :start_planting_on, :end_planting_on
    accepts_definitions :name, plant_between: DefinePlantBetween, color: GraphQL::Define.assign_metadata_key(:color)

    # definition added later:
    lazy_defined_attr_accessor :height
  end
end

describe GraphQL::Define::InstanceDefinable do
  describe "extending definitions" do
    before do
      Garden::Vegetable.accepts_definitions(:height)
    end

    after do
      Garden::Vegetable.own_dictionary.delete(:height)
    end

    it "accepts after-the-fact definitions" do
      corn = Garden::Vegetable.define do
        name "Corn"
        height 8
      end

      assert_equal "Corn", corn.name
      assert_equal 8, corn.height
    end
  end

  describe "applying custom definitions" do
    it "uses custom callables" do
      tomato = Garden::Vegetable.define do
        name "Tomato"
        plant_between Date.new(2000, 4, 20)..Date.new(2000, 6, 1)
      end

      assert_equal "Tomato", tomato.name
      assert_equal Date.new(2000, 4, 20), tomato.start_planting_on
      assert_equal Date.new(2000, 6, 1), tomato.end_planting_on
    end
  end

  describe ".define with keywords" do
    it "applies definitions from keywords" do
      okra = Garden::Vegetable.define(name: "Okra", plant_between: Date.new(2000, 5, 1)..Date.new(2000, 7, 1))
      assert_equal "Okra", okra.name
      assert_equal Date.new(2000, 5, 1), okra.start_planting_on
      assert_equal Date.new(2000, 7, 1), okra.end_planting_on
    end
  end

  describe "#metadata" do
    it "gets values from definitions" do
      arugula = Garden::Vegetable.define(name: "Arugula", color: :green)
      assert_equal :green, arugula.metadata[:color]
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql-0.19.0 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.15 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.14 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.13 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.12 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.11 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.10 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.9 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.8 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.7 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.6 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.5 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.4 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.3 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.2 spec/graphql/define/instance_definable_spec.rb
graphql-0.18.1 spec/graphql/define/instance_definable_spec.rb