require File.dirname(__FILE__) + '/../../../spec_helper' require 'nokogiri' require 'useless/doc/ui/godel' require 'useless/doc/serialization/load' describe Useless::Doc::UI::Godel do describe '.resource' do before(:each) do json = load_document('api.json').read api = Useless::Doc::Serialization::Load.api json result = Useless::Doc::UI::Godel.api(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.useless.io' end it 'should render the API description in a top-level p' do description = @doc.css('body > p') description.length.should == 1 description.first.content.should == 'Twonk information. Duh.' end it 'should render resouce paths as h2s' do h2s = @doc.css('h2') h2s.length.should == 2 h2s.map { |h2| h2.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 end describe '.resource' do before(:each) do json = load_document('twonk.json').read resource = Useless::Doc::Serialization::Load.resource json result = Useless::Doc::UI::Godel.resource(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', '200', '201']) 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