lutaml-model 0.3.29 → 0.3.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -72,6 +72,28 @@ module MixedContentSpec
72
72
  end
73
73
  end
74
74
 
75
+ class Latin < Lutaml::Model::Serializable
76
+ attribute :the, :string
77
+ attribute :from, :string
78
+ attribute :heading, :string
79
+
80
+ xml do
81
+ root "note"
82
+ map_element "to", to: :the
83
+ map_element "from", to: :from
84
+ map_element "heading", to: :heading
85
+ end
86
+ end
87
+
88
+ class Shift < Lutaml::Model::Serializable
89
+ attribute :field, :string, collection: true
90
+
91
+ xml do
92
+ root "root"
93
+ map_element "FieldName", to: :field
94
+ end
95
+ end
96
+
75
97
  class SpecialCharContentWithMixedTrue < Lutaml::Model::Serializable
76
98
  attribute :content, :string
77
99
 
@@ -651,24 +673,185 @@ RSpec.describe "MixedContent" do
651
673
 
652
674
  context "when encoding: nil xml" do
653
675
  let(:expected_encoding_nil_nokogiri_xml) { "&#x2211;computer security&#x220F; type of &#x200B; operation specified &#xB5; by an access right" }
654
- let(:expected_encoding_nil_ox_xml) { "\xE2\x88\x91computer security\xE2\x88\x8F type of \xE2\x80\x8B operation specified \xC2\xB5 by an access right" }
676
+ let(:expected_encoding_nil_ox_xml) { "∑computer security type of operation specified µ by an access right" }
655
677
 
656
678
  it "serializes special char mixed content correctly with encoding: nil to get hexcode" do
657
679
  parsed = MixedContentSpec::HexCode.from_xml(xml)
658
680
  serialized = parsed.to_xml(encoding: nil)
659
681
 
660
- if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
661
- expected_output = expected_encoding_nil_ox_xml
662
- expected_output.force_encoding("ASCII-8BIT")
663
- else
664
- expected_output = expected_encoding_nil_nokogiri_xml
665
- end
682
+ expected_output = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
683
+ expected_encoding_nil_ox_xml
684
+ else
685
+ expected_encoding_nil_nokogiri_xml
686
+ end
666
687
 
667
688
  expect(serialized.strip).to include(expected_output)
668
689
  end
669
690
  end
670
691
  end
671
692
  end
693
+
694
+ context "when use encoding in parsing" do
695
+ context "when use SHIFT-JIS encoding" do
696
+ let(:fixture) { File.read(fixture_path("xml/shift_jis.xml"), encoding: "Shift_JIS") }
697
+
698
+ describe ".from_xml" do
699
+ it "verifies the encoding of file read" do
700
+ expect(fixture.encoding.to_s).to eq("Shift_JIS")
701
+ end
702
+
703
+ it "deserializes SHIFT encoded content correctly with explicit encoding option" do
704
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
705
+
706
+ expected_content = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
707
+ "\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P".force_encoding("Shift_JIS")
708
+ else
709
+ "手書き英字1"
710
+ end
711
+
712
+ expect(parsed.field).to include(expected_content)
713
+ end
714
+
715
+ it "deserializes SHIFT encoded content incorrectly without explicit encoding option" do
716
+ parsed = MixedContentSpec::Shift.from_xml(fixture)
717
+
718
+ expected_content = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
719
+ "\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P".force_encoding("UTF-8")
720
+ else
721
+ "�菑���p���P"
722
+ end
723
+
724
+ expect(parsed.field).to include(expected_content)
725
+ end
726
+ end
727
+
728
+ describe ".to_xml" do
729
+ it "serializes SHIFT-JIS encoding content correctly reading from file" do
730
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
731
+ serialized = parsed.to_xml
732
+
733
+ expect(serialized.strip).to eq(fixture.strip)
734
+ end
735
+
736
+ it "serializes SHIFT encoded content correctly with explicit encoding option both in parsing and deserializing" do
737
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
738
+ serialized = parsed.to_xml(encoding: "UTF-8")
739
+
740
+ expected_xml = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
741
+ "\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P".force_encoding("Shift_JIS")
742
+ else
743
+ "手書き英字1"
744
+ end
745
+
746
+ expect(parsed.field).to include(expected_xml)
747
+ expect(parsed.encoding).to eq("Shift_JIS")
748
+
749
+ expect(serialized).to include("手書き英字1")
750
+ expect(serialized.encoding.to_s).to eq("UTF-8")
751
+ end
752
+
753
+ it "serializes SHIFT encoded content correctly with explicit encoding option" do
754
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
755
+ serialized = parsed.to_xml(encoding: "Shift_JIS")
756
+
757
+ expected_xml = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
758
+ "\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P".force_encoding("Shift_JIS")
759
+ else
760
+ "手書き英字1"
761
+ end
762
+
763
+ expect(parsed.field).to include(expected_xml)
764
+ expect(parsed.encoding).to eq("Shift_JIS")
765
+
766
+ expect(serialized).to include("\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P".force_encoding("Shift_JIS"))
767
+ expect(serialized.encoding.to_s).to eq("Shift_JIS")
768
+ end
769
+
770
+ it "serializes SHIFT encoded content correctly with declaration: true" do
771
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
772
+ serialized = parsed.to_xml(declaration: true, encoding: "Shift_JIS")
773
+
774
+ expected_xml = "<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n<root>\n <FieldName>\x8E\xE8\x8F\x91\x82\xAB\x89p\x8E\x9A\x82P</FieldName>\n <FieldName>123456</FieldName>\n</root>"
775
+
776
+ expect(serialized).to be_equivalent_to(expected_xml)
777
+ expect(serialized.encoding.to_s).to eq("Shift_JIS")
778
+ end
779
+
780
+ it "serializes SHIFT-JIS content incorrectly bcz no encoding provided during parsing" do
781
+ parsed = MixedContentSpec::Shift.from_xml(fixture)
782
+ serialized = parsed.to_xml(encoding: "Shift_JIS")
783
+
784
+ expected_content = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
785
+ "<root>\n <FieldName>\x8E菑\x82\xAB\x89p\x8E\x9A\x82P</FieldName>\n <FieldName>123456</FieldName>\n</root>\n"
786
+ else
787
+ "<root>\n <FieldName>&#65533;&#33745;&#65533;&#65533;&#65533;p&#65533;&#65533;&#65533;P</FieldName>\n <FieldName>123456</FieldName>\n</root>"
788
+ end
789
+
790
+ expect(serialized).to eq(expected_content)
791
+ end
792
+
793
+ it "serializes SHIFT-JIS encoding content correctly reading from string" do
794
+ xml = "<root><FieldName>手書き英字1</FieldName><FieldName>123456</FieldName></root>".encode("Shift_JIS")
795
+ parsed = MixedContentSpec::Shift.from_xml(xml, encoding: "Shift_JIS")
796
+ serialized = parsed.to_xml(encoding: "Shift_JIS")
797
+
798
+ expect(serialized).to be_equivalent_to(xml)
799
+ end
800
+
801
+ it "serializes SHIFT-JIS encoding content correctly" do
802
+ parsed = MixedContentSpec::Shift.from_xml(fixture, encoding: "Shift_JIS")
803
+ serialized = parsed.to_xml(encoding: "Shift_JIS")
804
+
805
+ expect(serialized).to be_equivalent_to(fixture)
806
+ end
807
+ end
808
+ end
809
+
810
+ context "when use LATIN (ISO-8859-1) encoding" do
811
+ let(:fixture) { File.read(fixture_path("xml/latin_encoding.xml"), encoding: "ISO-8859-1") }
812
+
813
+ describe ".from_xml" do
814
+ it "verifies the encoding of file read" do
815
+ expect(fixture.encoding.to_s).to eq("ISO-8859-1")
816
+ end
817
+
818
+ it "deserializes latin encoded content correctly" do
819
+ parsed = MixedContentSpec::Latin.from_xml(fixture, encoding: "ISO-8859-1")
820
+
821
+ expected_content = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
822
+ ["M\xFCller".force_encoding("ISO-8859-1"), "Jos\xE9".force_encoding("ISO-8859-1")]
823
+ else
824
+ ["Müller", "José"]
825
+ end
826
+
827
+ expect(parsed.from).to eq(expected_content[0])
828
+ expect(parsed.the).to eq(expected_content[1])
829
+ end
830
+
831
+ it "deserializes latin encoded content incorrectly" do
832
+ parsed = MixedContentSpec::Latin.from_xml(fixture)
833
+
834
+ expected_content = if adapter_class == Lutaml::Model::XmlAdapter::OxAdapter
835
+ ["M\xFCller", "Jos\xE9"]
836
+ else
837
+ ["M�ller", "Jos�"]
838
+ end
839
+
840
+ expect(parsed.from).to eq(expected_content[0])
841
+ expect(parsed.the).to eq(expected_content[1])
842
+ end
843
+ end
844
+
845
+ describe ".to_xml" do
846
+ it "serializes latin encoded content correctly" do
847
+ parsed = MixedContentSpec::Latin.from_xml(fixture, encoding: "ISO-8859-1")
848
+ serialized = parsed.to_xml
849
+
850
+ expect(serialized.strip).to eq("<note>\n <to>Jos\xE9</to>\n <from>M\xFCller</from>\n <heading>Reminder</heading>\n</note>".force_encoding("ISO-8859-1"))
851
+ end
852
+ end
853
+ end
854
+ end
672
855
  end
673
856
 
674
857
  describe Lutaml::Model::XmlAdapter::NokogiriAdapter do