From d34a20c73c6c13115d5444d8d40233840d0617a7 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 09:49:53 +0900 Subject: [PATCH] add link#href, #text test --- spec/page/link_spec.cr | 17 +++++++++++++++++ spec/spec_helper.cr | 2 +- src/mechanize/page/link.cr | 14 +++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 spec/page/link_spec.cr diff --git a/spec/page/link_spec.cr b/spec/page/link_spec.cr new file mode 100644 index 0000000..e42db51 --- /dev/null +++ b/spec/page/link_spec.cr @@ -0,0 +1,17 @@ +require "../spec_helper" + +describe "Mechanize Page Link test" do + it "returns href" do + agent = Mechanize.new + page = agent.get("http://example.com/link") + link = page.links.first + link.href.should eq "http://example.com/" + end + + it "returns text" do + agent = Mechanize.new + page = agent.get("http://example.com/link") + link = page.links.first + link.text.should eq "link text" + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 6000e77..a6d8251 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -29,7 +29,7 @@ WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY page_title - ページのリンク + link text BODY diff --git a/src/mechanize/page/link.cr b/src/mechanize/page/link.cr index 469c7ac..d302e0c 100644 --- a/src/mechanize/page/link.cr +++ b/src/mechanize/page/link.cr @@ -1,15 +1,15 @@ class MechanizeCr::PageContent::Link - property node : Lexbor::Node - property page : Page - property mech : Mechanize + getter node : Lexbor::Node + getter page : Page + getter mech : Mechanize + getter href : String + getter text : String def initialize(node, mech, page) @node = node @page = page @mech = mech - # @attributes = node - # @href = node['href'] - # @text = nil - # @uri = nil + @href = node.fetch("href", "") + @text = node.inner_text end end