Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .rspec-opal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--default-path=spec
--format documentation
--color
--require spec_helper
-q oga/setup_opal
-q ll/setup_opal
--opal-opt=-s,tmpdir
6 changes: 6 additions & 0 deletions Opalfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_load_path "lib"

add_gem_dependency "moxml"
add_gem_dependency "equivalent-xml"
add_gem_dependency "toml-rb"
add_gem_dependency "lutaml-xsd"
7 changes: 7 additions & 0 deletions lib/lutaml/model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# frozen_string_literal: true

require "moxml"
if RUBY_ENGINE == 'opal'
require 'corelib/array/pack'
require 'corelib/trace_point'
require 'moxml'
require 'lutaml/model/xml/oga_adapter'
require 'lutaml/model/schema/xml_compiler'
end
require_relative "model/uninitialized_class"
require_relative "model/errors"
require_relative "model/services"
Expand Down
6 changes: 5 additions & 1 deletion lib/lutaml/model/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ def self.cast_from_string!(type)
raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}"
end

def reserved_methods
Lutaml::Model::Serializable.instance_methods - %i[open]
end

def initialize(name, type, options = {})
validate_name!(
name, reserved_methods: Lutaml::Model::Serializable.instance_methods
name, reserved_methods: reserved_methods
)

@name = name
Expand Down
8 changes: 3 additions & 5 deletions lib/lutaml/model/register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ def get_class_without_register(klass_name)
private

def get_type_class(klass_name)
if klass_name.is_a?(String)
Lutaml::Model::Type.const_get(klass_name)
elsif klass_name.is_a?(Symbol)
Lutaml::Model::Type.lookup(klass_name)
end
Lutaml::Model::Type.lookup(klass_name)
rescue UnknownTypeError
Lutaml::Model::Type.const_get(klass_name)
end

def built_in_type?(type)
Expand Down
2 changes: 1 addition & 1 deletion lib/lutaml/model/schema/xml_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "erb"
require "tmpdir"
require "lutaml/xsd"
require "lutaml/xsd" unless RUBY_ENGINE == 'opal'
require_relative "xml_compiler/complex_content_restriction"
require_relative "xml_compiler/attribute_group"
require_relative "xml_compiler/complex_content"
Expand Down
8 changes: 6 additions & 2 deletions lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,12 @@ def empty_object(attr)
def ensure_utf8(value)
case value
when String
value.encode("UTF-8", invalid: :replace, undef: :replace,
replace: "")
if RUBY_ENGINE == 'opal'
value
else
value.encode("UTF-8", invalid: :replace, undef: :replace,
replace: "")
end
when Array
value.map { |v| ensure_utf8(v) }
when ::Hash
Expand Down
5 changes: 3 additions & 2 deletions lib/lutaml/model/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def classify(str)
str = str.sub(/^[a-z\d]*/) { |match| camel_case(match) || match }

str.gsub("::", "/").gsub(%r{(?:_|-|(/))([a-z\d]*)}i) do
word = Regexp.last_match(2)
match1, match2 = Regexp.last_match(1), Regexp.last_match(2)
word = match2
substituted = camel_case(word) || word
Regexp.last_match(1) ? "::#{substituted}" : substituted
match1 ? "::#{substituted}" : substituted
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lutaml/model/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
it "raise exception if option is not allowed" do
expect do
validate_options.call({ foo: "bar" })
end.to raise_error(Lutaml::Model::InvalidAttributeOptionsError, "Invalid options given for `name` [:foo]")
end.to raise_error(Lutaml::Model::InvalidAttributeOptionsError, sym_normal("Invalid options given for `name` [:foo]"))
end

it "raise exception if pattern is given with non string type" do
Expand Down
4 changes: 2 additions & 2 deletions spec/lutaml/model/choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Person < Lutaml::Model::Serializable
expect do
valid_instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("Attributes `[:signed, :unsigned, :watermarked, :encrypted]` count exceeds the upper bound `3`")
expect(error.error_messages.join("\n")).to include(sym_normal("Attributes `[:signed, :unsigned, :watermarked, :encrypted]` count exceeds the upper bound `3`"))
end
end
end
Expand Down Expand Up @@ -160,7 +160,7 @@ class Person < Lutaml::Model::Serializable
expect do
valid_instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to eq("Attributes `[:email, :phone, :check]` count exceeds the upper bound `2`")
expect(error.error_messages.join("\n")).to eq(sym_normal("Attributes `[:email, :phone, :check]` count exceeds the upper bound `2`"))
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/lutaml/model/custom_vobject_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ def initialize(data)

def parse
@data.each_line do |line|
line.chomp!
process_line(line)
process_line(line.chomp)
end
@objects
end
Expand Down
4 changes: 4 additions & 0 deletions spec/lutaml/model/liquefiable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
unless RUBY_ENGINE == 'opal'

require "spec_helper"
require "liquid"
require_relative "../../fixtures/address"
Expand Down Expand Up @@ -257,3 +259,5 @@ class CeramicCollection < Lutaml::Model::Serializable
end
end
end

end
8 changes: 4 additions & 4 deletions spec/lutaml/model/multiple_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ def desc_from_xml(model, values)

context "with Nokogiri adapter" do
it_behaves_like "xml adapter with multiple mappings", Lutaml::Model::Xml::NokogiriAdapter
end
end unless RUBY_ENGINE == 'opal'

context "with Ox adapter" do
it_behaves_like "xml adapter with multiple mappings", Lutaml::Model::Xml::OxAdapter
end
end unless RUBY_ENGINE == 'opal'
end

context "with CustomModel" do
Expand Down Expand Up @@ -331,11 +331,11 @@ def desc_from_xml(model, values)

context "with Nokogiri adapter" do
it_behaves_like "xml adapter with custom methods", Lutaml::Model::Xml::NokogiriAdapter
end
end unless RUBY_ENGINE == 'opal'

context "with Ox adapter" do
it_behaves_like "xml adapter with custom methods", Lutaml::Model::Xml::OxAdapter
end
end unless RUBY_ENGINE == 'opal'
end
end
end
2 changes: 1 addition & 1 deletion spec/lutaml/model/root_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,5 @@ class CeramicCollectionWithoutCollectionTrue < Lutaml::Model::Serializable

describe Lutaml::Model::Toml::TomlRbAdapter do
it_behaves_like "having root mappings", :toml
end
end unless RUBY_ENGINE == 'opal' # Until toml-rb is ported.
end
10 changes: 5 additions & 5 deletions spec/lutaml/model/serializable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def address_from_xml(model, value)

it "raises an error for invalid options" do
expect { RestrictTestClass.restrict(:foo, new_option: :bar) }
.to raise_error(Lutaml::Model::InvalidAttributeOptionsError, "Invalid options given for `foo` [:new_option]")
.to raise_error(Lutaml::Model::InvalidAttributeOptionsError, sym_normal("Invalid options given for `foo` [:new_option]"))
end

it "raises an error if the attribute does not exist" do
Expand Down Expand Up @@ -553,8 +553,8 @@ def address_from_xml(model, value)
end

describe "invalid format handling for XML" do
it_behaves_like "invalid format error", :xml, :nokogiri, :from_xml, :xml
it_behaves_like "invalid format error", :xml, :ox, :from_xml, :xml
it_behaves_like "invalid format error", :xml, :nokogiri, :from_xml, :xml unless RUBY_ENGINE == 'opal'
it_behaves_like "invalid format error", :xml, :ox, :from_xml, :xml unless RUBY_ENGINE == 'opal'
it_behaves_like "invalid format error", :xml, :oga, :from_xml, :xml
end

Expand All @@ -567,10 +567,10 @@ def address_from_xml(model, value)
end

describe "invalid format handling for invalid TOML" do
it_behaves_like "invalid format error", :toml, :toml_rb, :from_toml, :toml
it_behaves_like "invalid format error", :toml, :toml_rb, :from_toml, :toml unless RUBY_ENGINE == 'opal'

# Only test Tomlib if not on problematic platform (Windows Ruby < 3.3)
if RUBY_PLATFORM.include?("mingw") && RUBY_VERSION < "3.3"
if RUBY_ENGINE != 'opal' && RUBY_PLATFORM.include?("mingw") && RUBY_VERSION < "3.3"
# NOTE: Skipped Tomlib case because it causes segmentation fault on
# Windows with Ruby < 3.3
it "skips Tomlib test on Windows Ruby < 3.3 due to segfault risk" do
Expand Down
4 changes: 4 additions & 0 deletions spec/lutaml/model/toml_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
unless RUBY_ENGINE == 'opal'

require "spec_helper"
require "lutaml/model/toml/toml_rb_adapter"
require "lutaml/model/toml/tomlib_adapter"
Expand Down Expand Up @@ -39,3 +41,5 @@
it_behaves_like "a TOML adapter", described_class
end
end

end
4 changes: 3 additions & 1 deletion spec/lutaml/model/type/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
context "with hash" do
let(:value) { { a: 1, b: 2 } }
let(:expected_value) do
if RUBY_VERSION < "3.4.0"
if RUBY_ENGINE == 'opal'
'{"a"=>1, "b"=>2}'
elsif RUBY_VERSION < "3.4.0"
"{:a=>1, :b=>2}"
else
"{a: 1, b: 2}"
Expand Down
4 changes: 2 additions & 2 deletions spec/lutaml/model/xml_adapter/xml_namespace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ class OwnedEnd < Lutaml::Model::Serializable

describe Lutaml::Model::Xml::NokogiriAdapter do
it_behaves_like "an XML namespace parser", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OxAdapter do
it_behaves_like "an XML namespace parser", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OgaAdapter do
it_behaves_like "an XML namespace parser", described_class
Expand Down
4 changes: 2 additions & 2 deletions spec/lutaml/model/xml_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ class Maths < Lutaml::Model::Serializable

describe Lutaml::Model::Xml::NokogiriAdapter do
it_behaves_like "an XML adapter", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OxAdapter do
it_behaves_like "an XML adapter", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OgaAdapter do
it_behaves_like "an XML adapter", described_class
Expand Down
6 changes: 3 additions & 3 deletions spec/lutaml/model/xml_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class Schema < Lutaml::Model::Serializable
end

# Skipping for OX because it does not handle namespaces
context "when overriding child namespace prefix", skip: adapter_class == Lutaml::Model::Xml::OxAdapter do
context "when overriding child namespace prefix", skip: adapter_class.name == "Lutaml::Model::Xml::OxAdapter" do
let(:input_xml) do
<<~XML
<OverrideDefaultNamespacePrefix
Expand Down Expand Up @@ -1315,11 +1315,11 @@ def create_pattern_mapping(array)

describe Lutaml::Model::Xml::NokogiriAdapter do
it_behaves_like "having XML Mappings", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OxAdapter do
it_behaves_like "having XML Mappings", described_class
end
end unless RUBY_ENGINE == 'opal'

describe Lutaml::Model::Xml::OgaAdapter do
it_behaves_like "having XML Mappings", described_class
Expand Down
21 changes: 17 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "liquid"
# require "liquid"
require "rspec/matchers"
require "equivalent-xml"

Expand All @@ -21,15 +21,28 @@ def fixture_path(filename)
end

# configuration example
require_relative "../lib/lutaml/model"
require "lutaml/model"
# require_relative "../lib/lutaml/model/xml/nokogiri_adapter"
# require_relative "../lib/lutaml/model/xml/ox_adapter"
# require_relative "../lib/lutaml/model/toml_adapter/toml_rb_adapter"

Lutaml::Model::Config.configure do |config|
config.xml_adapter_type = :nokogiri
if RUBY_ENGINE == 'opal'
config.xml_adapter_type = :oga
else
config.xml_adapter_type = :nokogiri
end
config.hash_adapter_type = :standard_hash
config.json_adapter_type = :standard_json
config.yaml_adapter_type = :standard_yaml
config.toml_adapter_type = :toml_rb
config.toml_adapter_type = :toml_rb unless RUBY_ENGINE == 'opal'
end

# Create a normalized string version for strings interpolating symbols for Opal
def sym_normal(str)
if RUBY_ENGINE == 'opal'
str.gsub(/:(\w+)/) { "\"#{$1}\"" }
else
str
end
end
Loading