Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix build in jruby && truffleruby #265

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci_jruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rake
env:
JRUBY_OPTS: --profile
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ Gemfile.lock
*.swp
*.swo
bin
# Jetbrains IDEs
.idea
*.iml
/out
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ group :test do
gem "multi_json", require: false
gem "nokogiri", require: false
end
gem 'pry-byebug'
gem 'pry-byebug', platform: :mri

31 changes: 19 additions & 12 deletions test/as_test.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
# 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(:format) { format }

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

it { render(song).must_equal_document output }
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 }
representer!(module: mod) do
property :name, as: ->(*) { self.class.to_s }
end

it { render(song).must_equal_document({"Song" => "Revolution"}) }
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
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_xml_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,7 +1,9 @@
# frozen_string_literal: true

require "test_helper"
require "benchmark"

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

Expand All @@ -25,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!"
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
10 changes: 6 additions & 4 deletions test/binding_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 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
let(:binding) { Binding.new(render_nil_definition) }
Expand All @@ -21,7 +23,7 @@ class BindingTest < MiniTest::Spec
end

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

# return value when value present.
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
22 changes: 13 additions & 9 deletions test/cached_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "test_helper"

class Profiler
Expand Down Expand Up @@ -54,8 +56,8 @@ class AlbumRepresenter < Representable::Decorator
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

Expand All @@ -70,10 +72,10 @@ class AlbumRepresenter < Representable::Decorator
# makes sure options are passed correctly.
_(representer.to_hash(user_options: {volume: 9})).must_equal(
{
"name" => "Live And Dangerous",
"name" => "Live And Dangerous",
"songs" => [
{"title"=>"Jailbreak:{:volume=>9}"}, {"title"=>"Southbound:{:volume=>9}"},
{"title"=>"Emerald:{:volume=>9}"}
{"title" => "Jailbreak:{:volume=>9}"}, {"title" => "Southbound:{:volume=>9}"},
{"title" => "Emerald:{:volume=>9}"}
]
}
) # called in Deserializer/Serializer
Expand All @@ -87,6 +89,7 @@ class AlbumRepresenter < Representable::Decorator

# profiling
it do
skip("TruffleRuby profiler is not implemented yet") if RUBY_ENGINE == "truffleruby"
representer.to_hash

data = Profiler.profile { representer.to_hash }
Expand Down Expand Up @@ -114,11 +117,11 @@ class AlbumRepresenter < Representable::Decorator
describe "deserialization" do
let(:album_hash) do
{
"name" => "Louder And Even More Dangerous",
"name" => "Louder And Even More Dangerous",
"songs" => [
{"title" => "Southbound", "composer" => {"name"=>"Lynott"}},
{"title" => "Jailbreak", "composer" => {"name"=>"Phil Lynott"}},
{"title"=>"Emerald"}
{"title" => "Southbound", "composer" => {"name" => "Lynott"}},
{"title" => "Jailbreak", "composer" => {"name" => "Phil Lynott"}},
{"title" => "Emerald"}
]
}
end
Expand All @@ -141,6 +144,7 @@ class AlbumRepresenter < Representable::Decorator
end

it "xxx" do
skip("TruffleRuby profiler is not implemented yet") if RUBY_ENGINE == "truffleruby"
representer = AlbumRepresenter.new(Model::Album.new)
representer.from_hash(album_hash)

Expand Down
28 changes: 17 additions & 11 deletions test/class_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "test_helper"

class ClassTest < BaseTest
Expand All @@ -13,7 +15,7 @@ def from_hash(doc, *)

describe "class: ClassName, only" do
representer! do
property :song, :class => RepresentingSong # supposed this class exposes #from_hash itself.
property :song, class: RepresentingSong # supposed this class exposes #from_hash itself.
end

it "creates fresh instance and doesn't extend" do
Expand All @@ -25,7 +27,7 @@ def from_hash(doc, *)

describe "class: lambda, only" do
representer! do
property :song, :class => ->(*) { RepresentingSong }
property :song, class: ->(*) { RepresentingSong }
end

it "creates fresh instance and doesn't extend" do
Expand All @@ -38,7 +40,7 @@ def from_hash(doc, *)
# this throws a DeserializationError now.
describe "lambda { nil }" do
representer! do
property :title, :class => nil
property :title, class: nil
end

it do
Expand All @@ -61,9 +63,12 @@ def from_hash(*)
end
end

representer!(:inject => :klass) do
representer!(inject: :klass) do
_klass = klass
property :song, :class => ->(options) { _klass.args = ([options[:fragment], options[:user_options]]); _klass }
property :song, class: ->(options) {
_klass.args = ([options[:fragment], options[:user_options]])
_klass
}
end

it {
Expand All @@ -73,7 +78,7 @@ def from_hash(*)
user_options: {volume: true}
).song.class.args
)
.must_equal([{"name"=>"Captured"}, {:volume=>true}])
.must_equal([{"name" => "Captured"}, {volume: true}])
}
end

Expand All @@ -90,10 +95,11 @@ def from_hash(*)
end
end

representer!(:inject => :klass) do
representer!(inject: :klass) do
_klass = klass
collection :songs, class: ->(options) {
_klass.args = ([options[:fragment], options[:index], options[:user_options]]); _klass
_klass.args = ([options[:fragment], options[:index], options[:user_options]])
_klass
}
end

Expand All @@ -104,7 +110,7 @@ def from_hash(*)
user_options: {volume: true}
).songs.first.class.args
)
.must_equal([{"name"=>"Captured"}, 0, {:volume=>true}])
.must_equal([{"name" => "Captured"}, 0, {volume: true}])
}
end

Expand All @@ -117,8 +123,8 @@ def from_hash(*)
end
end

representer!(:inject => :parser) do
property :song, :class => parser # supposed this class exposes #from_hash itself.
representer!(inject: :parser) do
property :song, class: parser # supposed this class exposes #from_hash itself.
end

it "allows returning arbitrary objects in #from_hash" do
Expand Down
16 changes: 8 additions & 8 deletions test/coercion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class CoercionTest < MiniTest::Spec
it do
_(album.extend(representer).to_hash).must_equal(
{
"title" => "Dire Straits",
"title" => "Dire Straits",
"length" => 41.34,
"band" => {"founded" => 1977},
"songs" => [{"ok" => true}, {"ok" => false}]
"band" => {"founded" => 1977},
"songs" => [{"ok" => true}, {"ok" => false}]
}
)
end
Expand All @@ -43,10 +43,10 @@ class CoercionTest < MiniTest::Spec
album.extend(representer)
album.from_hash(
{
"title" => "Dire Straits",
"title" => "Dire Straits",
"length" => "41.34",
"band" => {"founded" => "1977"},
"songs" => [{"ok" => 1}, {"ok" => 0}]
"band" => {"founded" => "1977"},
"songs" => [{"ok" => 1}, {"ok" => 0}]
}
)

Expand All @@ -60,8 +60,8 @@ class CoercionTest < MiniTest::Spec
representer! do
include Representable::Coercion

property :length, type: Representable::Coercion::Types::Params::Float,
parse_filter: ->(input, _options) { "#{input}.1" }, # happens BEFORE coercer.
property :length, type: Representable::Coercion::Types::Params::Float,
parse_filter: ->(input, _options) { "#{input}.1" }, # happens BEFORE coercer.
render_filter: ->(fragment, *) { "#{fragment}.1" }
end

Expand Down
Loading