diff --git a/shard.yml b/shard.yml index c19ca00..64132a9 100644 --- a/shard.yml +++ b/shard.yml @@ -29,4 +29,4 @@ dependencies: development_dependencies: lucky_flow: github: luckyframework/lucky_flow - version: ~> 0.7.3 \ No newline at end of file + version: ~> 0.7.3 diff --git a/spec/classes/iframe_media_resolver_spec.cr b/spec/classes/iframe_media_resolver_spec.cr new file mode 100644 index 0000000..ed167a8 --- /dev/null +++ b/spec/classes/iframe_media_resolver_spec.cr @@ -0,0 +1,27 @@ +require "../spec_helper" + +include Nodes + +describe IFrameMediaResolver do + around_each do |example| + original_client = IFrameMediaResolver.http_client + IFrameMediaResolver.http_client = FakeMediumClient + example.run + IFrameMediaResolver.http_client = original_client + end + + it "returns a url of the embedded page" do + iframe = PostResponse::IFrame.from_json <<-JSON + { + "mediaResource": { + "id": "d4515fff7ecd02786e75fc8997c94bbf" + } + } + JSON + resolver = IFrameMediaResolver.new(iframe: iframe) + + result = resolver.fetch_href + + result.should eq("https://example.com") + end +end diff --git a/spec/classes/markup_converter_spec.cr b/spec/classes/markup_converter_spec.cr new file mode 100644 index 0000000..a87a2b2 --- /dev/null +++ b/spec/classes/markup_converter_spec.cr @@ -0,0 +1,99 @@ +require "../spec_helper" + +include Nodes + +describe MarkupConverter do + it "returns just text with no markups" do + json = <<-JSON + { + "text": "Hello, world", + "type": "P", + "markups": [], + "href": null, + "iframe": null, + "layout": null, + "metadata": null + } + JSON + paragraph = PostResponse::Paragraph.from_json(json) + + result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups) + + result.should eq([Text.new(content: "Hello, world")]) + end + + it "returns just text with multiple markups" do + json = <<-JSON + { + "text": "strong and emphasized only", + "type": "P", + "markups": [ + { + "title": null, + "type": "STRONG", + "href": null, + "start": 0, + "end": 6, + "rel": null, + "anchorType": null + }, + { + "title": null, + "type": "EM", + "href": null, + "start": 11, + "end": 21, + "rel": null, + "anchorType": null + } + ], + "href": null, + "iframe": null, + "layout": null, + "metadata": null + } + JSON + paragraph = PostResponse::Paragraph.from_json(json) + + result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups) + + result.should eq([ + Strong.new(children: [Text.new(content: "strong")] of Child), + Text.new(content: " and "), + Emphasis.new(children: [Text.new(content: "emphasized")] of Child), + Text.new(content: " only"), + ]) + end + + it "returns just text with a code markup" do + json = <<-JSON + { + "text": "inline code", + "type": "P", + "markups": [ + { + "title": null, + "type": "CODE", + "href": null, + "start": 7, + "end": 11, + "rel": null, + "anchorType": null + } + ], + "href": null, + "iframe": null, + "layout": null, + "metadata": null + } + JSON + paragraph = PostResponse::Paragraph.from_json(json) + + result = MarkupConverter.convert(text: paragraph.text, markups: paragraph.markups) + + result.should eq([ + Text.new(content: "inline "), + Code.new(children: [Text.new(content: "code")] of Child), + ]) + end +end diff --git a/spec/classes/paragraph_converter_spec.cr b/spec/classes/paragraph_converter_spec.cr new file mode 100644 index 0000000..14faef2 --- /dev/null +++ b/spec/classes/paragraph_converter_spec.cr @@ -0,0 +1,271 @@ +require "../spec_helper" + +include Nodes + +describe ParagraphConverter do + around_each do |example| + original_client = IFrameMediaResolver.http_client + IFrameMediaResolver.http_client = FakeMediumClient + example.run + IFrameMediaResolver.http_client = original_client + end + + it "converts a simple structure with no markups" do + paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON + [ + { + "text": "Title", + "type": "H3", + "markups": [], + "href": null, + "iframe": null, + "layout": null, + "metadata": null + } + ] + JSON + expected = [Heading3.new(children: [Text.new(content: "Title")] of Child)] + + result = ParagraphConverter.new.convert(paragraphs) + + result.should eq expected + end + + it "converts a simple structure with a markup" do + paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON + [ + { + "text": "inline code", + "type": "P", + "markups": [ + { + "name": null, + "title": null, + "type": "CODE", + "href": null, + "start": 7, + "end": 11, + "rel": null, + "anchorType": null + } + ], + "href": null, + "iframe": null, + "layout": null, + "metadata": null + } + ] + JSON + expected = [ + Paragraph.new(children: [ + Text.new(content: "inline "), + Code.new(children: [Text.new(content: "code")] of Child), + ] of Child) + ] + + result = ParagraphConverter.new.convert(paragraphs) + + result.should eq expected + end + + it "groups