diff --git a/spec/classes/embedded_converter_spec.cr b/spec/classes/embedded_converter_spec.cr index 4b7ec07..780bc92 100644 --- a/spec/classes/embedded_converter_spec.cr +++ b/spec/classes/embedded_converter_spec.cr @@ -5,6 +5,7 @@ include Nodes describe EmbeddedConverter do context "when the mediaResource has an iframeSrc value" do it "returns an EmbeddedContent node" do + store = GistStore.new paragraph = PostResponse::Paragraph.from_json <<-JSON { "text": "", @@ -25,7 +26,7 @@ describe EmbeddedConverter do } JSON - result = EmbeddedConverter.convert(paragraph) + result = EmbeddedConverter.convert(paragraph, store) result.should eq( EmbeddedContent.new( @@ -40,6 +41,7 @@ describe EmbeddedConverter do context "when the mediaResource has a blank iframeSrc value" do context "and the href is unknown" do it "returns an EmbeddedLink node" do + store = GistStore.new paragraph = PostResponse::Paragraph.from_json <<-JSON { "text": "", @@ -60,7 +62,7 @@ describe EmbeddedConverter do } JSON - result = EmbeddedConverter.convert(paragraph) + result = EmbeddedConverter.convert(paragraph, store) result.should eq(EmbeddedLink.new(href: "https://example.com")) end @@ -68,6 +70,7 @@ describe EmbeddedConverter do context "and the href is gist.github.com" do it "returns an GithubGist node" do + store = GistStore.new paragraph = PostResponse::Paragraph.from_json <<-JSON { "text": "", @@ -88,10 +91,13 @@ describe EmbeddedConverter do } JSON - result = EmbeddedConverter.convert(paragraph) + result = EmbeddedConverter.convert(paragraph, store) result.should eq( - GithubGist.new(href: "https://gist.github.com/user/someid") + GithubGist.new( + href: "https://gist.github.com/user/someid", + gist_store: store + ) ) end end diff --git a/spec/classes/gist_scanner_spec.cr b/spec/classes/gist_scanner_spec.cr new file mode 100644 index 0000000..a5648e6 --- /dev/null +++ b/spec/classes/gist_scanner_spec.cr @@ -0,0 +1,102 @@ +require "../spec_helper" + +describe GistScanner do + it "returns gist ids from paragraphs" do + iframe = PostResponse::IFrame.new( + PostResponse::MediaResource.new( + href: "https://gist.github.com/user/123ABC", + iframeSrc: "", + iframeWidth: 0, + iframeHeight: 0 + ) + ) + paragraphs = [ + PostResponse::Paragraph.new( + text: "Check out this gist:", + type: PostResponse::ParagraphType::P, + markups: [] of PostResponse::Markup, + iframe: nil, + layout: nil, + metadata: nil + ), + PostResponse::Paragraph.new( + text: "", + type: PostResponse::ParagraphType::IFRAME, + markups: [] of PostResponse::Markup, + iframe: iframe, + layout: nil, + metadata: nil + ), + ] + + result = GistScanner.new(paragraphs).scan + + result.should eq(["https://gist.github.com/user/123ABC"]) + end + + it "returns ids without the file parameters" do + iframe = PostResponse::IFrame.new( + PostResponse::MediaResource.new( + href: "https://gist.github.com/user/123ABC?file=example.txt", + iframeSrc: "", + iframeWidth: 0, + iframeHeight: 0 + ) + ) + paragraphs = [ + PostResponse::Paragraph.new( + text: "", + type: PostResponse::ParagraphType::IFRAME, + markups: [] of PostResponse::Markup, + iframe: iframe, + layout: nil, + metadata: nil + ), + ] + + result = GistScanner.new(paragraphs).scan + + result.should eq(["https://gist.github.com/user/123ABC"]) + end + + it "returns a unique list of ids" do + iframe1 = PostResponse::IFrame.new( + PostResponse::MediaResource.new( + href: "https://gist.github.com/user/123ABC?file=example.txt", + iframeSrc: "", + iframeWidth: 0, + iframeHeight: 0 + ) + ) + iframe2 = PostResponse::IFrame.new( + PostResponse::MediaResource.new( + href: "https://gist.github.com/user/123ABC?file=other.txt", + iframeSrc: "", + iframeWidth: 0, + iframeHeight: 0 + ) + ) + paragraphs = [ + PostResponse::Paragraph.new( + text: "", + type: PostResponse::ParagraphType::IFRAME, + markups: [] of PostResponse::Markup, + iframe: iframe1, + layout: nil, + metadata: nil + ), + PostResponse::Paragraph.new( + text: "", + type: PostResponse::ParagraphType::IFRAME, + markups: [] of PostResponse::Markup, + iframe: iframe2, + layout: nil, + metadata: nil + ), + ] + + result = GistScanner.new(paragraphs).scan + + result.should eq(["https://gist.github.com/user/123ABC"]) + end +end diff --git a/spec/classes/gist_store_spec.cr b/spec/classes/gist_store_spec.cr new file mode 100644 index 0000000..182d425 --- /dev/null +++ b/spec/classes/gist_store_spec.cr @@ -0,0 +1,58 @@ +require "../spec_helper" + +describe GistStore do + describe "#store_gist_file" do + describe "adds the gist file to the gist id" do + it "calls the github client" do + store = GistStore.new + file = GistFile.new( + filename: "filename", + content: "content", + raw_url: "raw_url" + ) + + store.store_gist_file("1", file) + + store.store["1"].should eq([file]) + end + end + end + + describe "the gist does not exist in the store" do + it "returns a MissingGistFile" do + missing_file = MissingGistFile.new(id: "1", filename: "filename") + store = GistStore.new + + file = store.get_gist_files(id: "1", filename: "filename") + + file.should eq([missing_file]) + end + end + + describe "when a filename is given" do + it "returns the GistFile for that filename" do + store = GistStore.new + file1 = GistFile.new("one", "", "") + file2 = GistFile.new("two", "", "") + store.store["1"] = [file1, file2] + + gists = store.get_gist_files(id: "1", filename: "one") + + gists.should eq([file1]) + gists.should_not contain([file2]) + end + end + + describe "when a filename is NOT given" do + it "returns all GistFiles" do + store = GistStore.new + file1 = GistFile.new("one", "", "") + file2 = GistFile.new("two", "", "") + store.store["1"] = [file1, file2] + + gists = store.get_gist_files(id: "1", filename: nil) + + gists.should eq([file1, file2]) + end + end +end diff --git a/spec/classes/paragraph_converter_spec.cr b/spec/classes/paragraph_converter_spec.cr index c3e30cb..e66b824 100644 --- a/spec/classes/paragraph_converter_spec.cr +++ b/spec/classes/paragraph_converter_spec.cr @@ -4,6 +4,7 @@ include Nodes describe ParagraphConverter do it "converts a simple structure with no markups" do + gist_store = GistStore.new paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON [ { @@ -18,12 +19,13 @@ describe ParagraphConverter do JSON expected = [Heading3.new(children: [Text.new(content: "Title")] of Child)] - result = ParagraphConverter.new.convert(paragraphs) + result = ParagraphConverter.new.convert(paragraphs, gist_store) result.should eq expected end it "converts a simple structure with a markup" do + gist_store = GistStore.new paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON [ { @@ -54,12 +56,13 @@ describe ParagraphConverter do ] of Child), ] - result = ParagraphConverter.new.convert(paragraphs) + result = ParagraphConverter.new.convert(paragraphs, gist_store) result.should eq expected end it "groups