File: types_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (85 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Validations::Types do
  module TypesSpec
    class FooType
      def self.parse(_); end
    end

    class BarType
      def self.parse; end
    end
  end

  describe '::primitive?' do
    [
      Integer, Float, Numeric, BigDecimal,
      Grape::API::Boolean, String, Symbol,
      Date, DateTime, Time
    ].each do |type|
      it "recognizes #{type} as a primitive" do
        expect(described_class).to be_primitive(type)
      end
    end

    it 'identifies unknown types' do
      expect(described_class).not_to be_primitive(Object)
      expect(described_class).not_to be_primitive(TypesSpec::FooType)
    end
  end

  describe '::structure?' do
    [
      Hash, Array, Set
    ].each do |type|
      it "recognizes #{type} as a structure" do
        expect(described_class).to be_structure(type)
      end
    end
  end

  describe '::special?' do
    [
      JSON, Array[JSON], File, Rack::Multipart::UploadedFile
    ].each do |type|
      it "provides special handling for #{type.inspect}" do
        expect(described_class).to be_special(type)
      end
    end
  end

  describe '::custom?' do
    it 'returns false if the type does not respond to :parse' do
      expect(described_class).not_to be_custom(Object)
    end

    it 'returns true if the type responds to :parse with one argument' do
      expect(described_class).to be_custom(TypesSpec::FooType)
    end

    it 'returns false if the type\'s #parse method takes other than one argument' do
      expect(described_class).not_to be_custom(TypesSpec::BarType)
    end
  end

  describe '::build_coercer' do
    it 'has internal cache variables' do
      expect(described_class.instance_variable_get(:@__cache)).to be_a(Hash)
      expect(described_class.instance_variable_get(:@__cache_write_lock)).to be_a(Mutex)
    end

    it 'caches the result of the build_coercer method' do
      original_cache = described_class.instance_variable_get(:@__cache)
      described_class.instance_variable_set(:@__cache, {})

      a_coercer = described_class.build_coercer(Array[String])
      b_coercer = described_class.build_coercer(Array[String])

      expect(a_coercer.object_id).to eq(b_coercer.object_id)

      described_class.instance_variable_set(:@__cache, original_cache)
    end
  end
end