require File.dirname(__FILE__) + '/../../../spec_helper' require 'nokogiri' require 'useless/doc' require 'useless/doc/core/domain' require 'useless/doc/core/api' require 'useless/doc/router' require 'useless/doc/ui/godel' describe Useless::Doc::UI::Godel do describe '.html' do context 'for a Core::Domain instance' do before(:all) do @router = Useless::Doc::Router.default json = load_document('domain.json').read domain = Useless::Doc.load.domain(json) result = Useless::Doc::UI::Godel.new(@router).html(domain) @doc = Nokogiri::HTML(result) end it 'should render the name in the only h1' do h1s = @doc.css('h1') h1s.length.should == 1 h1s.first.content.should == 'useless.io' end it 'should render the domain description in an article p' do description = @doc.css('article.description.domain p') description.length.should == 1 description.first.content.should == 'A collection of useless APIs.' end it 'should not escape the description text' do domain = Useless::Doc::Core::Domain.new description: 'The description' result = Useless::Doc::UI::Godel.new(@router).html(domain) description = Nokogiri::HTML(result).css('article.description.domain p') description.first.inner_html.should == 'The description' end it 'should interpret Markdown for the description' do domain = Useless::Doc::Core::Domain.new description: '*The description*' result = Useless::Doc::UI::Godel.new(@router).html(domain) description = Nokogiri::HTML(result).css('article.description.domain p') description.first.inner_html.should == 'The description' end it 'should render API names as a\'s, with correct doc URLs' do as = @doc.css('a.name') as.length.should == 2 as.map { |a| a.content }.should match_array(['The Jah API', 'The Twonk API']) as.map { |a| a['href'] }.should match_array(['http://jah.doc.useless.io', 'http://twonk.doc.useless.io']) end it 'should render API descriptions as ps within the section' do ps = @doc.css('section > p') ps.length.should == 2 ps.map { |h2| h2.content }.should match_array(['Jah, jah, jah.', 'TWONK!!!']) end end context 'for a Core::API instance' do before(:all) do json = load_document('api.json').read router = Useless::Doc::Router.default api = Useless::Doc.load.api(json) result = Useless::Doc::UI::Godel.new(router).html(api) @doc = Nokogiri::HTML(result) end it 'should render the URL in the only h1' do h1s = @doc.css('h1') h1s.length.should == 1 h1s.first.content.should == 'Twonk API' end it 'should render the API description in an article p' do description = @doc.css('article.description.api p') description.length.should == 1 description.first.content.should == 'Twonk information. Duh.' end it 'should not escape the description text' do domain = Useless::Doc::Core::API.new description: 'The description' result = Useless::Doc::UI::Godel.new(@router).html(domain) description = Nokogiri::HTML(result).css('article.description.api p') description.first.inner_html.should == 'The description' end it 'should interpret Markdown for the description' do domain = Useless::Doc::Core::API.new description: '*The description*' result = Useless::Doc::UI::Godel.new(@router).html(domain) description = Nokogiri::HTML(result).css('article.description.api p') description.first.inner_html.should == 'The description' end it 'should render resouce paths as a\'s' do as = @doc.css('a.path') as.length.should == 2 as.map { |a| a.content }.should match_array(['/twonks/:id', '/twonks/:id/werp']) end it 'should render resouce descriptions as ps within the section' do ps = @doc.css('section > p') ps.length.should == 2 ps.map { |h2| h2.content }.should match_array(['The most critical aspect.', 'That other shit.']) end it 'should add a link for each method, with an appropriate description' do as = @doc.css('table td a') td_content = @doc.css('table td').map { |td| td.content } as.find { |a| a.content == 'GET' }['href'].should == '/twonks/:id#GET' td_content.should include 'Get dat twonk.' as.find { |a| a.content == 'POST' }['href'].should == '/twonks/:id/werp#POST' td_content.should include 'Make dem werps.' end it 'should display concept information' do stages = @doc.css('table.stages').first.content stages.should include 'Concept' stages.should include 'John Henry' end it 'should display specification information' do stages = @doc.css('table.stages').first.content stages.should include 'Specification' stages.should include 'Bill Wever' stages.should include 'In Progress' end it 'should display implementation information' do stages = @doc.css('table.stages').first.content stages.should include 'Implementation' stages.should include 'Bill Wever, John Henry' stages.should include 'Pending' end end context 'for a Core::Resource instance' do before(:each) do json = load_document('resource.json').read router = Useless::Doc::Router.default resource = Useless::Doc.load.resource json result = Useless::Doc::UI::Godel.new(router).html(resource) @doc = Nokogiri::HTML(result) end it 'should render the path within the only h1' do h1s = @doc.css('h1') h1s.length.should == 1 h1s.first.content.should == '/twonks/:id' end it 'should render the methods as h2s' do h2s = @doc.css('h2') h2s.length.should == 2 h2s.map { |h2| h2.content }.should match_array(['GET', 'PUT']) end it 'should render response codes as h3s' do h3s = @doc.css('h3') h3s.length.should == 3 h3s.map { |h3| h3.content }.should match_array(['404 Not Found', '200 OK', '201 Created']) end it 'should contain the method and response code descriptions in ps' do p_content = @doc.css('p').map { |p| p.content } p_content.should include 'The most critical aspect.' p_content.should include 'Retrieve a representation of an individual twonk.' p_content.should include 'A twonk with the specified ID could not be found.' p_content.should include 'The specified twonk was retrieved successfully.' p_content.should include 'The specified twonk was updated successfully.' end it 'should add parameter information to a table' do [ { key: 'id', description: 'The ID of the desired twonk.' }, { key: 'werp', description: 'Self-explanatory.' }, { key: 'id', description: 'The ID of the twonk to be updated.' } ].each do |param| @doc.css('table').find do |table| table.content.match(param[:key]) and table.content.match(param[:description]) end.should_not be_nil end end it 'should add body information to a table' do [ { key: 'name', description: 'The name of the twonk.' }, { key: 'created_by', description: 'The short name of the user who created the twonk.' }, { key: 'hoinked_by', description: 'The ID of the person who hoinked this twonk.' } ].each do |attribute| @doc.css('table').find do |table| table.content.match(attribute[:key]) and table.content.match(attribute[:description]) end.should_not be_nil end end it 'should add header information to a table' do @doc.css('table').find do |table| table.content.match('User-Agent') and table.content.match('The thingy you\'re using.') end.should_not be_nil end it 'should include authentication information in lis' do li_content = @doc.css('li').map { |li| li.content } li_content.should include 'Authentication Required' li_content.should include 'Authentication Not Required' end end end describe '.strip_heredoc' do it 'should strip non-significant whitespace from a heredoc' do string = <<-HEREDOC This is a heredoc This is indented HEREDOC Useless::Doc::UI::Godel.strip_heredoc(string).should == "This is a heredoc\n This is indented\n" end end end