# encoding: utf-8 # # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun . # Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit. # require "spec_helper" describe Mbrao::ParsingEngines::PlainText do subject{ Mbrao::ParsingEngines::PlainText.new } let(:sample_metadata) { < {a: "b"}}) end it "should return a default value if parsing failed" do expect(subject.parse_metadata("---\n\"yaml:", {default: "DEFAULT"})).to eq("DEFAULT") end it "should raise an exception if parsing failed and no default is available" do expect { subject.parse_metadata("---\n\"yaml:") }.to raise_error(::Mbrao::Exceptions::InvalidMetadata) end end describe "#filter_content" do def parse_content(content) content.split("\n").map(&:strip).select {|l| l.present? } end it "should return the original content if locales contains *" do expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), "*"))).to eq(["START", "IT, EN", "MIDDLE", "!IT and !ES", "EN in !IT", "!IT", "END"]) end it "should use default locale if nothing is specified" do ::Mbrao::Parser.locale = :it expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content)))).to eq(["START", "IT, EN", "MIDDLE", "END"]) end it "should ignore unclosed tag, trying to close the leftmost start tag" do expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, "{{content: it}}\n{{content: en}}NO{{/content}}")))).to eq(["{{content: en}}NO"]) end it "should correctly filter by tag" do expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), "en"))).to eq(["START", "IT, EN", "MIDDLE", "!IT and !ES", "EN in !IT", "!IT", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), "it"))).to eq(["START", "IT, EN", "MIDDLE", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), "es"))).to eq(["START", "MIDDLE", "!IT", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), "fr"))).to eq(["START", "MIDDLE", "!IT and !ES", "!IT", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), ["it", "en"]))).to eq(["START", "IT, EN", "MIDDLE", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), ["es", "en"]))).to eq(["START", "IT, EN", "MIDDLE", "EN in !IT", "!IT", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), ["fr", "en"]))).to eq(["START", "IT, EN", "MIDDLE", "!IT and !ES", "EN in !IT", "!IT", "END"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample_nested_content), ["fr", "es", "en"]))).to eq(["START", "IT, EN", "MIDDLE", "EN in !IT", "!IT", "END"]) end it "should use different tags" do sample = "[content-it]{{content: !it}}IT[/content]\nOK" expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample), "it", {content_tags: ["[content-%ARGS%]", "[/content]"]}))).to eq(["{{content: !it}}IT", "OK"]) expect(parse_content(subject.filter_content(::Mbrao::Content.create(nil, sample), "en", {content_tags: ["[content-%ARGS%]", "[/content]"]}))).to eq(["OK"]) end end end