From 219c4b32b4df42842f25f76e358dcbc7200dfc6d Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sat, 6 Jan 2024 16:26:22 +0100 Subject: [PATCH] Extract custom parser tests to a separate feature spec file --- spec/features/custom_parser_spec.rb | 51 +++++++++++++++++++++++++++++ spec/fixtures/custom_parsers.xml | 4 --- spec/happymapper_spec.rb | 29 ---------------- 3 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 spec/features/custom_parser_spec.rb delete mode 100644 spec/fixtures/custom_parsers.xml diff --git a/spec/features/custom_parser_spec.rb b/spec/features/custom_parser_spec.rb new file mode 100644 index 00000000..2215beef --- /dev/null +++ b/spec/features/custom_parser_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "spec_helper" + +class ParserTest + include HappyMapper + + class Coerce + def self.number_list(val) + val.to_s.split(",").map { _1.strip.to_i } + end + end + + tag "parsertest" + attribute :numbers, Coerce, parser: :number_list + element :strings, self, parser: :string_list + element :bool, String, parser: ->(val) { val.to_s == "1" } + + def self.string_list(val) + val.to_s.split(",").map(&:strip) + end +end + +RSpec.describe "specifying a custom parser for attributes and elements" do + let(:xml) do + <<~XML + + a, b, c + 0 + + XML + end + + it "parses with a singleton method on the type class" do + parsed = ParserTest.parse xml + + expect(parsed.numbers).to eq [1, 2, 3] + end + + it "parses with a singleton method on the model class" do + parsed = ParserTest.parse xml + + expect(parsed.strings).to eq %w(a b c) + end + + it "parses with a proc specified in the element definition" do + parsed = ParserTest.parse xml + + expect(parsed.bool).to be false + end +end diff --git a/spec/fixtures/custom_parsers.xml b/spec/fixtures/custom_parsers.xml deleted file mode 100644 index d4697a4b..00000000 --- a/spec/fixtures/custom_parsers.xml +++ /dev/null @@ -1,4 +0,0 @@ - - a, b, c - 0 - diff --git a/spec/happymapper_spec.rb b/spec/happymapper_spec.rb index 74d3b6e5..145777a2 100644 --- a/spec/happymapper_spec.rb +++ b/spec/happymapper_spec.rb @@ -560,25 +560,6 @@ class Thing end end -class ParserTest - include HappyMapper - - class Coerce - def self.number_list(val) - val.to_s.split(",").map { _1.strip.to_i } - end - end - - tag "parsertest" - attribute :numbers, Coerce, parser: :number_list - element :strings, self, parser: :string_list - element :bool, String, parser: ->(val) { val.to_s == "1" } - - def self.string_list(val) - val.to_s.split(",").map(&:strip) - end -end - describe HappyMapper do describe "being included into another class" do let(:klass) do @@ -1235,14 +1216,4 @@ def self.string_list(val) end end end - - it "parses with custom parser" do - parsed = ParserTest.parse fixture_file("custom_parsers.xml") - - aggregate_failures do - expect(parsed.numbers).to eq [1, 2, 3] - expect(parsed.strings).to eq %w(a b c) - expect(parsed.bool).to be false - end - end end