@@ -780,16 +780,15 @@ def three
780780 end
781781 end
782782 describe "api structure with two versions and a namespace" do
783- class TwitterAPI < Grape ::API
784- # version v1
785- version 'v1' , :using => :path
786- get "version" do
783+ before :each do
784+ subject . version 'v1' , :using => :path
785+ subject . get "version" do
787786 api . version
788787 end
789788 # version v2
790- version 'v2' , :using => :path
791- prefix 'p'
792- namespace "n1" do
789+ subject . version 'v2' , :using => :path
790+ subject . prefix 'p'
791+ subject . namespace "n1" do
793792 namespace "n2" do
794793 get "version" do
795794 api . version
@@ -798,22 +797,22 @@ class TwitterAPI < Grape::API
798797 end
799798 end
800799 it "should return versions" do
801- TwitterAPI :: versions . should == [ 'v1' , 'v2' ]
800+ subject . versions . should == [ 'v1' , 'v2' ]
802801 end
803802 it "should set route paths" do
804- TwitterAPI :: routes . size . should >= 2
805- TwitterAPI :: routes [ 0 ] . route_path . should == "/:version/version(.:format)"
806- TwitterAPI :: routes [ 1 ] . route_path . should == "/p/:version/n1/n2/version(.:format)"
803+ subject . routes . size . should >= 2
804+ subject . routes [ 0 ] . route_path . should == "/:version/version(.:format)"
805+ subject . routes [ 1 ] . route_path . should == "/p/:version/n1/n2/version(.:format)"
807806 end
808807 it "should set route versions" do
809- TwitterAPI :: routes [ 0 ] . route_version . should == 'v1'
810- TwitterAPI :: routes [ 1 ] . route_version . should == 'v2'
808+ subject . routes [ 0 ] . route_version . should == 'v1'
809+ subject . routes [ 1 ] . route_version . should == 'v2'
811810 end
812811 it "should set a nested namespace" do
813- TwitterAPI :: routes [ 1 ] . route_namespace . should == "/n1/n2"
812+ subject . routes [ 1 ] . route_namespace . should == "/n1/n2"
814813 end
815814 it "should set prefix" do
816- TwitterAPI :: routes [ 1 ] . route_prefix . should == 'p'
815+ subject . routes [ 1 ] . route_prefix . should == 'p'
817816 end
818817 end
819818 describe "api structure with additional parameters" do
@@ -839,79 +838,91 @@ class TwitterAPI < Grape::API
839838 end
840839
841840 context "desc" do
842- describe "empty api structure" do
843- it "returns an empty array of routes" do
844- subject . desc "grape api"
845- subject . routes . should == [ ]
846- end
847- end
848- describe "single method with a desc" do
849- before ( :each ) do
850- subject . desc "ping method"
851- subject . get :ping do
852- 'pong'
853- end
854- end
855- it "returns route description" do
856- subject . routes [ 0 ] . route_description . should == "ping method"
857- end
858- end
859- describe "single method with a an array of params and a desc hash block" do
860- before ( :each ) do
861- subject . desc "ping method" , { :params => { "x" => "y" } }
862- subject . get "ping/:x" do
863- 'pong'
864- end
865- end
866- it "returns route description" do
867- subject . routes [ 0 ] . route_description . should == "ping method"
868- end
869- end
870- describe "api structure with multiple methods and descriptions" do
871- before ( :each ) do
872- class JitterAPI < Grape ::API
873- desc "first method"
874- get "first" do ; end
875- get "second" do ; end
876- desc "third method"
877- get "third" do ; end
878- end
879- end
880- it "should return a description for the first method" do
881- JitterAPI ::routes [ 0 ] . route_description . should == "first method"
882- JitterAPI ::routes [ 1 ] . route_description . should be_nil
883- JitterAPI ::routes [ 2 ] . route_description . should == "third method"
884- end
885- end
886- describe "api structure with multiple methods, namespaces, descriptions and options" do
887- before ( :each ) do
888- class LitterAPI < Grape ::API
889- desc "first method"
890- get "first" do ; end
891- get "second" do ; end
892- namespace "ns" do
893- desc "ns second" , :foo => "bar"
894- get "second" do ; end
895- end
896- desc "third method" , :details => "details of third method"
897- get "third" do ; end
898- desc "Reverses a string." , { :params =>
899- { "s" => { :desc => "string to reverse" , :type => "string" } }
900- }
901- get "reverse" do
902- params [ :s ] . reverse
903- end
904- end
905- end
906- it "should return a description for the first method" do
907- LitterAPI ::routes [ 0 ] . route_description . should == "first method"
908- LitterAPI ::routes [ 1 ] . route_description . should be_nil
909- LitterAPI ::routes [ 2 ] . route_description . should == "ns second"
910- LitterAPI ::routes [ 2 ] . route_foo . should == "bar"
911- LitterAPI ::routes [ 3 ] . route_description . should == "third method"
912- LitterAPI ::routes [ 4 ] . route_description . should == "Reverses a string."
913- LitterAPI ::routes [ 4 ] . route_params . should == { "s" => { :desc => "string to reverse" , :type => "string" } }
914- end
841+ it "empty array of routes" do
842+ subject . routes . should == [ ]
843+ end
844+ it "empty array of routes" do
845+ subject . desc "grape api"
846+ subject . routes . should == [ ]
847+ end
848+ it "should describe a method" do
849+ subject . desc "first method"
850+ subject . get :first do ; end
851+ subject . routes . length . should == 1
852+ route = subject . routes . first
853+ route . route_description . should == "first method"
854+ route . route_foo . should be_nil
855+ route . route_params . should == { }
856+ end
857+ it "should describe methods separately" do
858+ subject . desc "first method"
859+ subject . get :first do ; end
860+ subject . desc "second method"
861+ subject . get :second do ; end
862+ subject . routes . count . should == 2
863+ subject . routes . map { |route |
864+ { :description => route . route_description , :params => route . route_params }
865+ } . should eq [
866+ { :description => "first method" , :params => { } } ,
867+ { :description => "second method" , :params => { } }
868+ ]
869+ end
870+ it "should reset desc" do
871+ subject . desc "first method"
872+ subject . get :first do ; end
873+ subject . get :second do ; end
874+ subject . routes . map { |route |
875+ { :description => route . route_description , :params => route . route_params }
876+ } . should eq [
877+ { :description => "first method" , :params => { } } ,
878+ { :description => nil , :params => { } }
879+ ]
880+ end
881+ it "should namespace and describe arbitrary parameters" do
882+ subject . namespace "ns" do
883+ desc "ns second" , :foo => "bar"
884+ get "second" do ; end
885+ end
886+ subject . routes . map { |route |
887+ { :description => route . route_description , :foo => route . route_foo , :params => route . route_params }
888+ } . should eq [
889+ { :description => "ns second" , :foo => "bar" , :params => { } } ,
890+ ]
891+ end
892+ it "should include details" do
893+ subject . desc "method" , :details => "method details"
894+ subject . get "method" do ; end
895+ subject . routes . map { |route |
896+ { :description => route . route_description , :details => route . route_details , :params => route . route_params }
897+ } . should eq [
898+ { :description => "method" , :details => "method details" , :params => { } } ,
899+ ]
900+ end
901+ it "should describe a method with parameters" do
902+ subject . desc "Reverses a string." , { :params =>
903+ { "s" => { :desc => "string to reverse" , :type => "string" } }
904+ }
905+ subject . get "reverse" do
906+ params [ :s ] . reverse
907+ end
908+ subject . routes . map { |route |
909+ { :description => route . route_description , :params => route . route_params }
910+ } . should eq [
911+ { :description => "Reverses a string." , :params => { "s" => { :desc => "string to reverse" , :type => "string" } } }
912+ ]
913+ end
914+ it "should symbolize params" do
915+ subject . desc "Reverses a string." , { :params =>
916+ { :s => { :desc => "string to reverse" , :type => "string" } }
917+ }
918+ subject . get "reverse/:s" do
919+ params [ :s ] . reverse
920+ end
921+ subject . routes . map { |route |
922+ { :description => route . route_description , :params => route . route_params }
923+ } . should eq [
924+ { :description => "Reverses a string." , :params => { :s => { :desc => "string to reverse" , :type => "string" } } }
925+ ]
915926 end
916927 end
917928
0 commit comments