Sha256: 693388600de3942ed453a572d29f37b730682107a248768b5c6298342bc461cf

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'test_helper'

module Schemacop
  class DefaultsTest < Minitest::Test
    def test_basic
      s = Schema.new :integer, default: 42

      input = nil
      output = s.validate!(input)

      assert_equal(42, output)
    end

    def test_hash
      s = Schema.new do
        opt :foo, :string, default: 'bar'
      end

      input = { foo: nil }
      output = s.validate!(input)
      assert_equal({ foo: 'bar' }, output)
    end

    def test_missing_hash_key
      s = Schema.new do
        opt :foo, :string, default: 'bar'
      end

      input = {}
      output = s.validate!(input)
      assert_equal({ foo: 'bar' }, output)
    end

    def test_entire_hash
      s = Schema.new do
        opt :foo, :hash, default: { name: { first: 'Foo', last: 'Bar' } } do
          req :name do
            req :first
            req :last
          end
        end
      end

      input = {}
      output = s.validate!(input)
      assert_equal({ foo: { name: { first: 'Foo', last: 'Bar' } } }, output)
    end

    def test_entire_array
      s = Schema.new do
        opt :foo, :array, default: [{ bar: 42 }] do
          req :bar
        end
      end

      input = {}
      output = s.validate!(input)
      assert_equal({ foo: [{ bar: 42 }] }, output)
    end

    def test_proc
      s = Schema.new do
        opt :year, :integer, default: ->() { Time.now.year }
      end

      input = {}
      output = s.validate!(input)
      assert_equal({ year: Time.now.year }, output)
    end

    def test_nested_proc
      myproc = proc { 42 }

      s = Schema.new do
        opt :myproc, Proc, default: ->() { myproc }
      end

      input = {}
      output = s.validate!(input)
      assert_equal({ myproc: myproc }, output)
    end

    def test_invalid_default
      s = Schema.new :integer, default: '42'

      input = nil

      assert_verr do
        s.validate!(input)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
schemacop-2.4.7 test/defaults_test.rb
schemacop-2.4.6 test/defaults_test.rb
schemacop-2.4.5 test/defaults_test.rb