|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'stdlib::sort_by' do |
| 6 | + it { is_expected.not_to be_nil } |
| 7 | + |
| 8 | + describe 'raise exception with inappropriate parameters' do |
| 9 | + it { is_expected.to run.with_params.and_raise_error(ArgumentError, Regexp.new('expects 1 argument, got none')) } |
| 10 | + it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, Regexp.new('expects a block')) } |
| 11 | + it { is_expected.to run.with_params(:undef).and_raise_error(ArgumentError, Regexp.new("rejected: parameter 'ary' expects an Array value, got Undef")) } |
| 12 | + it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, Regexp.new("rejected: parameter 'ary' expects an Array value, got Boolean")) } |
| 13 | + it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, Regexp.new("rejected: parameter 'ary' expects an Array value, got Integer")) } |
| 14 | + it { is_expected.to run.with_params({}).with_lambda { 1 }.and_raise_error(ArgumentError, Regexp.new('block expects between 1 and 2 arguments, got none')) } |
| 15 | + end |
| 16 | + |
| 17 | + # Puppet's each iterator considers Integers, Strings, Arrays and Hashes to be Iterable. |
| 18 | + unordered_array = ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] |
| 19 | + ordered_array = ['The', 'brown', 'dog', 'fox', 'jumps', 'lazy', 'over', 'quick', 'the'] |
| 20 | + unordered_hash = { 'The' => 'quick', 'brown' => 'fox', 'jumps' => 'over', 'the' => 'lazy', 'dog' => '.' } |
| 21 | + ordered_hash = { 'dog' => '.', 'brown' => 'fox', 'the' => 'lazy', 'jumps' => 'over', 'The' => 'quick' } |
| 22 | + unordered_string = 'The quick brown fox jumps over the lazy dog.' |
| 23 | + ordered_string = ' .Tabcdeeefghhijklmnoooopqrrstuuvwxyz' |
| 24 | + |
| 25 | + describe 'with sane input' do |
| 26 | + it 'does sort Array' do |
| 27 | + expect(subject).to run \ |
| 28 | + .with_params(unordered_array) \ |
| 29 | + .with_lambda { |e| e } \ |
| 30 | + .and_return(ordered_array) |
| 31 | + end |
| 32 | + |
| 33 | + it 'does sort Hash by entry' do |
| 34 | + expect(subject).to run \ |
| 35 | + .with_params(unordered_hash) \ |
| 36 | + .with_lambda { |e| e[1] } \ |
| 37 | + .and_return(ordered_hash) |
| 38 | + end |
| 39 | + |
| 40 | + it 'does sort Hash by key-value pairs' do |
| 41 | + expect(subject).to run \ |
| 42 | + .with_params(unordered_hash) \ |
| 43 | + .with_lambda { |_, v| v } \ |
| 44 | + .and_return(ordered_hash) |
| 45 | + end |
| 46 | + |
| 47 | + it 'does sort String' do |
| 48 | + expect(subject).to run \ |
| 49 | + .with_params(unordered_string) \ |
| 50 | + .with_lambda { |e| e } \ |
| 51 | + .and_return(ordered_string) |
| 52 | + end |
| 53 | + end |
| 54 | +end |
0 commit comments