Skip to content

Commit

Permalink
New Style/HashConversion cop
Browse files Browse the repository at this point in the history
  • Loading branch information
nesaulov committed Apr 19, 2021
1 parent 8b535c1 commit fffea50
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
14 changes: 7 additions & 7 deletions spec/builder_spec.rb
Expand Up @@ -31,38 +31,38 @@ def multi_struct
let(:carrier) { nil }

context 'valid schema is passed' do
let(:schema) { Hash[foo: String, bar: Array] }
let(:schema) { { foo: String, bar: Array } }

it 'returns hash with correct values' do
is_expected.to eq(foo: 'A string', bar: [1, 2, 4])
end

context 'with hash as a type' do
let(:schema) { Hash[foo: String, baz: Hash] }
let(:schema) { { foo: String, baz: Hash } }

it 'returns hash with correct values' do
is_expected.to eq(foo: 'A string', baz: { key: :value })
end
end

context 'with nested values' do
let(:schema) { Hash[foo: String, nested: { bar: Array, again: { baz: Hash } }] }
let(:schema) { { foo: String, nested: { bar: Array, again: { baz: Hash } } } }

it 'returns hash with correct values' do
is_expected.to eq(foo: 'A string', nested: { bar: [1, 2, 4], again: { baz: { key: :value } } })
end
end

context 'with nested objects' do
let(:schema) { Hash[foo: String, struct: { foo: Integer, bar: Array }] }
let(:schema) { { foo: String, struct: { foo: Integer, bar: Array } } }

it 'invokes nested methods on the object' do
is_expected.to eq(foo: 'A string', struct: { foo: 42, bar: [1] })
end
end

context 'with multi-nested objects' do
let(:schema) { Hash[foo: String, multi_struct: { foo: Integer, bar: { baz: Array } }] }
let(:schema) { { foo: String, multi_struct: { foo: Integer, bar: { baz: Array } } } }

it 'invokes nested methods on the objects' do
is_expected.to eq(foo: 'A string', multi_struct: { foo: 42, bar: { baz: [1] } })
Expand All @@ -72,7 +72,7 @@ def multi_struct

context 'invalid schema is passed' do
context 'with undefined method' do
let(:schema) { Hash[not_a_method: String] }
let(:schema) { { not_a_method: String } }
let(:message) { /undefined method `not_a_method'.* You have probably defined a key in the schema that doesn't have a corresponding method/ } # rubocop:disable Layout/LineLength

it 'raises UndefinedMethodError' do
Expand All @@ -81,7 +81,7 @@ def multi_struct
end

context 'with invalid types' do
let(:schema) { Hash[foo: Integer, bar: String] }
let(:schema) { { foo: Integer, bar: String } }

it 'raises TypeError' do
expect { result }
Expand Down
2 changes: 1 addition & 1 deletion spec/copier_spec.rb
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Surrealist::Copier do
describe '#deep_copy' do
let(:object) { Hash[animal: { kind: 'dog', name: 'Rusty' }] }
let(:object) { { animal: { kind: 'dog', name: 'Rusty' } } }

it_behaves_like 'hash is cloned deeply and it`s structure is not changed' do
let(:copy) { Surrealist::Copier.deep_copy(object) }
Expand Down
8 changes: 4 additions & 4 deletions spec/hash_utils_spec.rb
Expand Up @@ -5,29 +5,29 @@
subject(:camelized_hash) { described_class.camelize_hash(hash) }

context 'not nested hash' do
let(:hash) { Hash[snake_key: 'some value'] }
let(:hash) { { snake_key: 'some value' } }

it { expect(camelized_hash.keys.first).to eq(:snakeKey) }
end

context 'nested hash' do
let(:hash) { Hash[snake_key: { nested_key: { one_more_level: true } }] }
let(:hash) { { snake_key: { nested_key: { one_more_level: true } } } }

it 'camelizes hash recursively' do
expect(camelized_hash).to eq(snakeKey: { nestedKey: { oneMoreLevel: true } })
end
end

context 'mixed symbols and string' do
let(:hash) { Hash[snake_key: { 'nested_key' => { 'one_more_level': true } }] }
let(:hash) { { snake_key: { 'nested_key' => { 'one_more_level': true } } } }

it 'camelizes hash recursively' do
expect(camelized_hash).to eq(snakeKey: { 'nestedKey' => { 'oneMoreLevel': true } })
end
end

context 'array as hash key' do
let(:hash) { Hash[['some_key'] => 'value'] }
let(:hash) { { ['some_key'] => 'value' } }

it { expect(camelized_hash.keys.first).to eq(['some_key']) }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/multiple_serializers_spec.rb
Expand Up @@ -42,13 +42,13 @@ def initialize(id, title, author)

describe 'single item' do
context 'default' do
let(:expectation) { Hash[id: 1, title: 'Ruby is dead', author: { name: 'John' }] }
let(:expectation) { { id: 1, title: 'Ruby is dead', author: { name: 'John' } } }

it { expect(post.surrealize).to eq(expectation.to_json) }
end

context 'specific' do
let(:expectation) { Hash[id: 1, title: 'Ruby is dead'] }
let(:expectation) { { id: 1, title: 'Ruby is dead' } }

it { expect(post.surrealize(for: :short)).to eq(expectation.to_json) }
it { expect(post.surrealize(serializer: ShortPostSerializer)).to eq(expectation.to_json) }
Expand Down
2 changes: 1 addition & 1 deletion spec/schema_definer_spec.rb
Expand Up @@ -6,7 +6,7 @@ class Person; include Surrealist; end
let(:instance) { Person }

context 'when hash is passed' do
let(:schema) { Hash[a: 1, b: {}] }
let(:schema) { { a: 1, b: {} } }

before { described_class.call(instance, schema) }

Expand Down
2 changes: 1 addition & 1 deletion spec/wrapper_spec.rb
Expand Up @@ -37,7 +37,7 @@ def no_args_provided?
end
end

let(:object) { Hash[a: 3, nested_thing: { key: :value }] }
let(:object) { { a: 3, nested_thing: { key: :value } } }
let(:klass) { 'SomeClass' }
let(:error) { "Can't wrap schema in root key - class name was not passed" }

Expand Down

0 comments on commit fffea50

Please sign in to comment.