Skip to content

Commit

Permalink
fix deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Jan 1, 2023
1 parent 21d9655 commit 07dc894
Show file tree
Hide file tree
Showing 61 changed files with 2,200 additions and 1,912 deletions.
45 changes: 26 additions & 19 deletions test/as_test.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
require "test_helper"
# frozen_string_literal: true

require 'test_helper'

class AsTest < MiniTest::Spec
for_formats(
:hash => [Representable::Hash, {"title" => "Wie Es Geht"}, {"title" => "Revolution"}]
hash: [Representable::Hash, { 'title' => 'Wie Es Geht' }, { 'title' => 'Revolution' }]
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
) do |format, mod, input, output|
let(:song) { representer.prepare(Song.new("Revolution")) }
let(:song) { representer.prepare(Song.new('Revolution')) }
let(:format) { format }

describe "as: with :symbol" do
representer!(:module => mod) do
property :name, :as => :title
describe 'as: with :symbol' do
representer!(module: mod) do
property :name, as: :title
end

it { render(song).must_equal_document output }
it { _(parse(song, input).name).must_equal "Wie Es Geht" }
it { assert_equal_document render(song), output }
it { _(parse(song, input).name).must_equal 'Wie Es Geht' }
end

describe "as: with lambda" do
representer!(:module => mod) do
property :name, :as => ->(*) { self.class.to_s }
describe 'as: with lambda' do
representer!(module: mod) do
property :name, as: ->(*) { self.class.to_s }
end

it { render(song).must_equal_document({"Song" => "Revolution"}) }
it { _(parse(song, {"Song" => "Wie Es Geht"}).name).must_equal "Wie Es Geht" }
it { assert_equal_document(render(song), { 'Song' => 'Revolution' }) }
it { _(parse(song, { 'Song' => 'Wie Es Geht' }).name).must_equal 'Wie Es Geht' }
end

describe "lambda arguments" do
describe 'lambda arguments' do
representer! do
property :name, :as => ->(options) { options[:user_options].inspect }
property :name, as: ->(options) { options[:user_options].inspect }
end

it { render(song, user_options: {volume: 1}).must_equal_document({"{:volume=>1}" => "Revolution"}) }
it { _(parse(song, {"{:volume=>1}" => "Wie Es Geht"}, user_options: {volume: 1}).name).must_equal "Wie Es Geht" }
it { assert_equal_document(render(song, user_options: { volume: 1 }), { '{:volume=>1}' => 'Revolution' }) }
it {
_(parse(song, { '{:volume=>1}' => 'Wie Es Geht' }, user_options: { volume: 1 }).name).must_equal 'Wie Es Geht'
}
end
end
end
Expand All @@ -54,7 +58,10 @@ class AsXmlTest < MiniTest::Spec
end

it do
skip
_(representer.new(Album.new(Band.new("Offspring"))).to_xml).must_equal ""
assert_equal "<album>
<combo>
<name>Offspring</name>
</combo>
</album>", representer.new(Album.new(Band.new('Offspring'))).to_xml
end
end
10 changes: 6 additions & 4 deletions test/benchmarking.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "test_helper"
require "benchmark"
# frozen_string_literal: true

require 'test_helper'
require 'benchmark'

SONG_PROPERTIES = 1000.times.collect do |i|
"property_#{i}"
Expand Down Expand Up @@ -35,12 +37,12 @@ def random_song
album = OpenStruct.new(songs: 100.times.collect { random_song })

times << Benchmark.measure do
puts "================ next!"
puts '================ next!'
album.extend(AlbumRepresenter).to_json
end
end

puts times.join("")
puts times.join('')

# 100 songs, 100 attrs
# 0.050000 0.000000 0.050000 ( 0.093157)
Expand Down
24 changes: 13 additions & 11 deletions test/binding_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "test_helper"
# frozen_string_literal: true

require 'test_helper'

class BindingTest < MiniTest::Spec
Binding = Representable::Binding
let(:render_nil_definition) { Representable::Definition.new(:song, :render_nil => true) }
let(:render_nil_definition) { Representable::Definition.new(:song, render_nil: true) }

describe "#skipable_empty_value?" do
describe '#skipable_empty_value?' do
let(:binding) { Binding.new(render_nil_definition) }

# don't skip when present.
it { _(binding.skipable_empty_value?("Disconnect, Disconnect")).must_equal false }
it { _(binding.skipable_empty_value?('Disconnect, Disconnect')).must_equal false }

# don't skip when it's nil and render_nil: true
it { _(binding.skipable_empty_value?(nil)).must_equal false }
Expand All @@ -17,21 +19,21 @@ class BindingTest < MiniTest::Spec
it { _(Binding.new(Representable::Definition.new(:song)).skipable_empty_value?(nil)).must_equal true }

# don't skip when nil and :render_nil undefined.
it { _(Binding.new(Representable::Definition.new(:song)).skipable_empty_value?("Fatal Flu")).must_equal false }
it { _(Binding.new(Representable::Definition.new(:song)).skipable_empty_value?('Fatal Flu')).must_equal false }
end

describe "#default_for" do
let(:definition) { Representable::Definition.new(:song, :default => "Insider") }
describe '#default_for' do
let(:definition) { Representable::Definition.new(:song, default: 'Insider') }
let(:binding) { Binding.new(definition) }

# return value when value present.
it { _(binding.default_for("Black And Blue")).must_equal "Black And Blue" }
it { _(binding.default_for('Black And Blue')).must_equal 'Black And Blue' }

# return false when value false.
it { _(binding.default_for(false)).must_equal false }

# return default when value nil.
it { _(binding.default_for(nil)).must_equal "Insider" }
it { _(binding.default_for(nil)).must_equal 'Insider' }

# return nil when value nil and render_nil: true.
it { _(Binding.new(render_nil_definition).default_for(nil)).must_be_nil }
Expand All @@ -41,8 +43,8 @@ class BindingTest < MiniTest::Spec
_(
Binding.new(
Representable::Definition.new(
:song, :render_nil => true,
:default => "The Quest"
:song, render_nil: true,
default: 'The Quest'
)
).default_for(nil)
).must_be_nil
Expand Down
96 changes: 49 additions & 47 deletions test/cached_test.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
require "test_helper"
# frozen_string_literal: true

require 'test_helper'

class Profiler
def self.profile(&block)
case RUBY_ENGINE
when "ruby"
require "ruby-prof"

output = StringIO.new
profile_result = RubyProf.profile(&block)
printer = RubyProf::FlatPrinter.new(profile_result)
printer.print(output)
output.string
when "jruby"
require "jruby/profiler"

output_stream = java.io.ByteArrayOutputStream.new
print_stream = java.io.PrintStream.new(output_stream)
profile_result = JRuby::Profiler.profile(&block)
printer = JRuby::Profiler::FlatProfilePrinter.new(profile_result)
printer.printProfile(print_stream)
output_stream.toString
when 'ruby'
require 'ruby-prof'

output = StringIO.new
profile_result = RubyProf.profile(&block)
printer = RubyProf::FlatPrinter.new(profile_result)
printer.print(output)
output.string
when 'jruby'
require 'jruby/profiler'

output_stream = java.io.ByteArrayOutputStream.new
print_stream = java.io.PrintStream.new(output_stream)
profile_result = JRuby::Profiler.profile(&block)
printer = JRuby::Profiler::FlatProfilePrinter.new(profile_result)
printer.printProfile(print_stream)
output_stream.toString
end
end
end
Expand Down Expand Up @@ -51,29 +53,29 @@ class AlbumRepresenter < Representable::Decorator
collection :songs, decorator: SongRepresenter, class: Model::Song
end

describe "serialization" do
describe 'serialization' do
let(:album_hash) do
{
"name" => "Louder And Even More Dangerous",
"songs" => [{"title"=>"Southbound:{:volume=>10}"}, {"title"=>"Jailbreak:{:volume=>10}"}]
'name' => 'Louder And Even More Dangerous',
'songs' => [{ 'title' => 'Southbound:{:volume=>10}' }, { 'title' => 'Jailbreak:{:volume=>10}' }]
}
end

let(:song) { Model::Song.new("Jailbreak") }
let(:song2) { Model::Song.new("Southbound") }
let(:album) { Model::Album.new("Live And Dangerous", [song, song2, Model::Song.new("Emerald")]) }
let(:song) { Model::Song.new('Jailbreak') }
let(:song2) { Model::Song.new('Southbound') }
let(:album) { Model::Album.new('Live And Dangerous', [song, song2, Model::Song.new('Emerald')]) }
let(:representer) { AlbumRepresenter.new(album) }

it do
# album2 = Model::Album.new("Louder And Even More Dangerous", [song2, song])

# makes sure options are passed correctly.
_(representer.to_hash(user_options: {volume: 9})).must_equal(
_(representer.to_hash(user_options: { volume: 9 })).must_equal(
{
"name" => "Live And Dangerous",
"songs" => [
{"title"=>"Jailbreak:{:volume=>9}"}, {"title"=>"Southbound:{:volume=>9}"},
{"title"=>"Emerald:{:volume=>9}"}
'name' => 'Live And Dangerous',
'songs' => [
{ 'title' => 'Jailbreak:{:volume=>9}' }, { 'title' => 'Southbound:{:volume=>9}' },
{ 'title' => 'Emerald:{:volume=>9}' }
]
}
) # called in Deserializer/Serializer
Expand Down Expand Up @@ -101,24 +103,24 @@ class AlbumRepresenter < Representable::Decorator
# 3 nested decorator is instantiated for 3 Songs, though.
_(data).must_match(/3\s*(<Class::)?Representable::Decorator>?[\#.]prepare/m)
# no Binding is instantiated at runtime.
_(data).wont_match "Representable::Binding#initialize"
_(data).wont_match 'Representable::Binding#initialize'
# 2 mappers for Album, Song
# data.must_match "2 Representable::Mapper::Methods#initialize"
# title, songs, 3x title, composer
_(data).must_match(/8\s*Representable::Binding[#.]render_pipeline/m)
_(data).wont_match "render_functions"
_(data).wont_match "Representable::Binding::Factories#render_functions"
_(data).wont_match 'render_functions'
_(data).wont_match 'Representable::Binding::Factories#render_functions'
end
end

describe "deserialization" do
describe 'deserialization' do
let(:album_hash) do
{
"name" => "Louder And Even More Dangerous",
"songs" => [
{"title" => "Southbound", "composer" => {"name"=>"Lynott"}},
{"title" => "Jailbreak", "composer" => {"name"=>"Phil Lynott"}},
{"title"=>"Emerald"}
'name' => 'Louder And Even More Dangerous',
'songs' => [
{ 'title' => 'Southbound', 'composer' => { 'name' => 'Lynott' } },
{ 'title' => 'Jailbreak', 'composer' => { 'name' => 'Phil Lynott' } },
{ 'title' => 'Emerald' }
]
}
end
Expand All @@ -129,18 +131,18 @@ class AlbumRepresenter < Representable::Decorator
AlbumRepresenter.new(album).from_hash(album_hash)

_(album.songs.size).must_equal 3
_(album.name).must_equal "Louder And Even More Dangerous"
_(album.songs[0].title).must_equal "Southbound"
_(album.songs[0].composer.name).must_equal "Lynott"
_(album.songs[1].title).must_equal "Jailbreak"
_(album.songs[1].composer.name).must_equal "Phil Lynott"
_(album.songs[2].title).must_equal "Emerald"
_(album.name).must_equal 'Louder And Even More Dangerous'
_(album.songs[0].title).must_equal 'Southbound'
_(album.songs[0].composer.name).must_equal 'Lynott'
_(album.songs[1].title).must_equal 'Jailbreak'
_(album.songs[1].composer.name).must_equal 'Phil Lynott'
_(album.songs[2].title).must_equal 'Emerald'
_(album.songs[2].composer).must_be_nil

# TODO: test options.
end

it "xxx" do
it 'xxx' do
representer = AlbumRepresenter.new(Model::Album.new)
representer.from_hash(album_hash)

Expand All @@ -151,9 +153,9 @@ class AlbumRepresenter < Representable::Decorator
# MRI and JRuby has different output formats. See note above.
_(data).must_match(/5\s*(<Class::)?Representable::Decorator>?[#.]prepare/)
# a total of 5 properties in the object graph.
_(data).wont_match "Representable::Binding#initialize"
_(data).wont_match 'Representable::Binding#initialize'

_(data).wont_match "parse_functions" # no pipeline creation.
_(data).wont_match 'parse_functions' # no pipeline creation.
_(data).must_match(/10\s*Representable::Binding[#.]parse_pipeline/)
# three mappers for Album, Song, composer
# data.must_match "3 Representable::Mapper::Methods#initialize"
Expand Down
Loading

0 comments on commit 07dc894

Please sign in to comment.