|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../spec_helper' |
| 4 | +require 'ostruct' |
| 5 | +require OctocatalogDiff::Spec.require_path('/util/util') |
| 6 | + |
| 7 | +describe OctocatalogDiff::Util::Util do |
| 8 | + describe '#object_is_any_of?' do |
| 9 | + it 'should return true when object is one of the classes' do |
| 10 | + object = 42 |
| 11 | + classes = [String, Hash, Integer] |
| 12 | + expect(described_class.object_is_any_of?(object, classes)).to eq(true) |
| 13 | + end |
| 14 | + |
| 15 | + it 'should return false when object is not one of the classes' do |
| 16 | + object = :chickens |
| 17 | + classes = [String, Hash, Integer] |
| 18 | + expect(described_class.object_is_any_of?(object, classes)).to eq(false) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe '#safe_dup' do |
| 23 | + it 'should work with nil' do |
| 24 | + object = nil |
| 25 | + result = described_class.safe_dup(object) |
| 26 | + expect(object).to eq(result) |
| 27 | + end |
| 28 | + |
| 29 | + it 'should work with a string' do |
| 30 | + object = 'boots and cats' |
| 31 | + result = described_class.safe_dup(object) |
| 32 | + expect(object).to eq(result) |
| 33 | + end |
| 34 | + |
| 35 | + it 'should work with a symbol' do |
| 36 | + object = :chickens |
| 37 | + result = described_class.safe_dup(object) |
| 38 | + expect(object).to eq(result) |
| 39 | + end |
| 40 | + |
| 41 | + it 'should work with an integer' do |
| 42 | + object = 42 |
| 43 | + result = described_class.safe_dup(object) |
| 44 | + expect(object).to eq(result) |
| 45 | + end |
| 46 | + |
| 47 | + it 'should work with a hash' do |
| 48 | + object = { 'foo' => 'bar', 'baz' => 'buzz' } |
| 49 | + result = described_class.safe_dup(object) |
| 50 | + expect(object).to eq(result) |
| 51 | + expect(object.object_id).not_to eq(result.object_id) |
| 52 | + end |
| 53 | + |
| 54 | + it 'should work with an array' do |
| 55 | + object = %w[foo bar baz buzz] |
| 56 | + result = described_class.safe_dup(object) |
| 57 | + expect(object).to eq(result) |
| 58 | + expect(object.object_id).not_to eq(result.object_id) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe '#deep_dup' do |
| 63 | + it 'should dupe a hash' do |
| 64 | + hash1 = { 'foo' => 'bar' } |
| 65 | + hash2 = { 'baz' => hash1 } |
| 66 | + hash3 = { 'xxx' => hash2 } |
| 67 | + result = described_class.deep_dup(hash3) |
| 68 | + expect(result['xxx']).to eq(hash2) |
| 69 | + expect(result['xxx'].object_id).not_to eq(hash2.object_id) |
| 70 | + end |
| 71 | + |
| 72 | + it 'should dupe an array' do |
| 73 | + array1 = %w[foo bar baz] |
| 74 | + array2 = ['foo', array1, 'baz'] |
| 75 | + array3 = ['foo', array2, 'baz'] |
| 76 | + result = described_class.deep_dup(array3) |
| 77 | + expect(result[1]).to eq(array2) |
| 78 | + expect(result[1].object_id).not_to eq(array2.object_id) |
| 79 | + end |
| 80 | + |
| 81 | + it 'should dupe a string' do |
| 82 | + obj = 'Hello there' |
| 83 | + result = described_class.deep_dup(obj) |
| 84 | + expect(result).to eq(obj) |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments