Module: Cuprum::Collections::RSpec::Contracts::Scopes::CriteriaContracts::ShouldParseCriteriaFromAHashContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb

Overview

Contract validating the parsing of criteria from a Hash.

Instance Method Summary collapse

Instance Method Details

#apply(example_group) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.



1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
# File 'lib/cuprum/collections/rspec/contracts/scopes/criteria_contracts.rb', line 1774

contract do
  describe 'with nil' do
    let(:error_message) do
      'value must be a Hash with String or Symbol keys'
    end

    it 'should raise an exception' do
      expect { parse_criteria(nil) }
        .to raise_error ArgumentError, error_message
    end
  end

  describe 'with an Object' do
    let(:error_message) do
      'value must be a Hash with String or Symbol keys'
    end

    it 'should raise an exception' do
      expect { parse_criteria(Object.new.freeze) }
        .to raise_error ArgumentError, error_message
    end
  end

  describe 'with a Hash with invalid keys' do
    let(:error_message) do
      'value must be a Hash with String or Symbol keys'
    end
    let(:value)         { { nil => 'invalid' } }

    it 'should raise an exception' do
      expect { parse_criteria(value) }
        .to raise_error ArgumentError, error_message
    end
  end

  describe 'with an empty Hash' do
    let(:value) { {} }

    it { expect(parse_criteria(value)).to be == [] }
  end

  describe 'with a Hash with one key' do
    let(:operators) { Cuprum::Collections::Queries::Operators }
    let(:value) do
      { 'title' => 'A Wizard of Earthsea' }
    end
    let(:expected) do
      [['title', operators::EQUAL, 'A Wizard of Earthsea']]
    end

    it { expect(parse_criteria(value)).to be == expected }
  end

  describe 'with a Hash with many String keys' do
    let(:operators) { Cuprum::Collections::Queries::Operators }
    let(:value) do
      {
        'title'  => 'A Wizard of Earthsea',
        'author' => 'Ursula K. LeGuin',
        'series' => 'Earthsea'
      }
    end
    let(:expected) do
      [
        ['title', operators::EQUAL, 'A Wizard of Earthsea'],
        ['author', operators::EQUAL, 'Ursula K. LeGuin'],
        ['series', operators::EQUAL, 'Earthsea']
      ]
    end

    it { expect(parse_criteria(value)).to be == expected }
  end

  describe 'with a Hash with many Symbol keys' do
    let(:operators) { Cuprum::Collections::Queries::Operators }
    let(:value) do
      {
        title:  'A Wizard of Earthsea',
        author: 'Ursula K. LeGuin',
        series: 'Earthsea'
      }
    end
    let(:expected) do
      [
        ['title', operators::EQUAL, 'A Wizard of Earthsea'],
        ['author', operators::EQUAL, 'Ursula K. LeGuin'],
        ['series', operators::EQUAL, 'Earthsea']
      ]
    end

    it { expect(parse_criteria(value)).to be == expected }
  end
end