Skip to content

Commit

Permalink
use 2.7 format
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Jan 1, 2023
1 parent f40d0b0 commit 992870a
Show file tree
Hide file tree
Showing 58 changed files with 1,630 additions and 1,495 deletions.
24 changes: 12 additions & 12 deletions test/as_test.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
# frozen_string_literal: true

require 'test_helper'
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
describe "as: with :symbol" do
representer!(module: mod) do
property :name, as: :title
end

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

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

it { assert_equal_document(render(song), { '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 }
end

it { assert_equal_document(render(song, user_options: { volume: 1 }), { '{:volume=>1}' => 'Revolution' }) }
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'
_(parse(song, {"{:volume=>1}" => "Wie Es Geht"}, user_options: {volume: 1}).name).must_equal "Wie Es Geht"
}
end
end
Expand All @@ -62,6 +62,6 @@ class AsXmlTest < MiniTest::Spec
<combo>
<name>Offspring</name>
</combo>
</album>", representer.new(Album.new(Band.new('Offspring'))).to_xml
</album>", representer.new(Album.new(Band.new("Offspring"))).to_xml
end
end
14 changes: 7 additions & 7 deletions test/benchmarking.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'test_helper'
require 'benchmark'
require "test_helper"
require "benchmark"

SONG_PROPERTIES = 1000.times.collect do |i|
SONG_PROPERTIES = Array.new(1000) do |i|
"property_#{i}"
end

Expand All @@ -27,22 +27,22 @@ module AlbumRepresenter
end

def random_song
attrs = Hash[SONG_PROPERTIES.collect { |n| [n, n] }]
attrs = SONG_PROPERTIES.to_h { |n| [n, n] }
OpenStruct.new(attrs)
end

times = []

3.times.each do
album = OpenStruct.new(songs: 100.times.collect { random_song })
album = OpenStruct.new(songs: Array.new(100) { 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
18 changes: 9 additions & 9 deletions test/binding_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

require 'test_helper'
require "test_helper"

class BindingTest < MiniTest::Spec
Binding = Representable::Binding
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 @@ -19,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 @@ -44,7 +44,7 @@ class BindingTest < MiniTest::Spec
Binding.new(
Representable::Definition.new(
:song, render_nil: true,
default: 'The Quest'
default: "The Quest"
)
).default_for(nil)
).must_be_nil
Expand Down
106 changes: 53 additions & 53 deletions test/cached_test.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# frozen_string_literal: true

require 'test_helper'
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 'truffleruby'
require 'truffleruby-tool'

output = StringIO.new
TruffleRubyTool.profile(&block)
output.string
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 "truffleruby"
require "truffleruby-tool"

output = StringIO.new
TruffleRubyTool.profile(&block)
output.string
end
end
end
Expand Down Expand Up @@ -59,29 +59,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 @@ -109,24 +109,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 @@ -137,18 +137,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 @@ -159,9 +159,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 992870a

Please sign in to comment.