Sha256: 41707c742e3d7b74e00aa0841fb07f16b0a29bfe447380ba5cf6885c5ba0aaec

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 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_invalid_default
      s = Schema.new :integer, default: '42'

      input = nil

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
schemacop-2.4.4 test/defaults_test.rb
schemacop-2.4.3 test/defaults_test.rb
schemacop-2.4.2 test/defaults_test.rb
schemacop-2.4.1 test/defaults_test.rb