Loading lib/jsduck/class.rb +11 −4 Original line number Diff line number Diff line Loading @@ -74,14 +74,21 @@ module JsDuck # Utility method that given a package or class name finds the name # of its parent package. def self.package_name(name) parts = name.split(/\./) parts.slice(0, parts.length - 1).join(".") name.slice(0, name.length - self.short_name(name).length - 1) || "" end # Utility method that given full package or class name extracts # the last part of the name. # the "class"-part of the name. # # Because we try to emulate ext-doc, it's not as simple as just # taking the last part. See class_spec.rb for details. def self.short_name(name) name.split(/\./).last parts = name.split(/\./) short = parts.pop while parts.length > 1 && parts.last =~ /^[A-Z]/ short = parts.pop + "." + short end short end end Loading spec/class_spec.rb +60 −0 Original line number Diff line number Diff line Loading @@ -82,4 +82,64 @@ describe JsDuck::Class do end end describe "when full_name like My.package.Cls" do before do @cls = JsDuck::Class.new({:name => "My.package.Cls",}, {}); end it "#package_name contains all parts except the last" do @cls.package_name.should == "My.package" end it "#short_name contains only the last part" do @cls.short_name.should == "Cls" end end describe "when full_name like My.Package.Cls" do before do @cls = JsDuck::Class.new({:name => "My.Package.Cls",}, {}); end it "#package_name contains only first part" do @cls.package_name.should == "My" end it "#short_name contains remaining parts" do @cls.short_name.should == "Package.Cls" end end describe "when full_name has no parts" do before do @cls = JsDuck::Class.new({:name => "Foo",}, {}); end it "#package_name is empty" do @cls.package_name.should == "" end it "#short_name is the same as full_name" do @cls.short_name.should == "Foo" end end describe "when full_name has two uppercase parts" do before do @cls = JsDuck::Class.new({:name => "Foo.Bar",}, {}); end it "#package_name is first part" do @cls.package_name.should == "Foo" end it "#short_name is second part" do @cls.short_name.should == "Bar" end end end spec/tree_spec.rb +48 −0 Original line number Diff line number Diff line Loading @@ -117,3 +117,51 @@ describe JsDuck::Tree do end describe JsDuck::Tree, "lowercase package name" do before do @tree = JsDuck::Tree.new.create([ JsDuck::Class.new({:tagname => :class, :name => "Foo.bar.Baz"}) ]) @root = @tree[:children][0] @middle = @root[:children][0] @leaf = @middle[:children][0] end it "gets root package node" do @root[:cls].should == 'package' end it "gets middle package node" do @middle[:cls].should == 'package' end it "gets leaf class node" do @leaf[:cls].should == 'cls' end end describe JsDuck::Tree, "uppercase package name" do before do @tree = JsDuck::Tree.new.create([ JsDuck::Class.new({:tagname => :class, :name => "Foo.Bar.Baz"}) ]) @root = @tree[:children][0] @middle = @root[:children][0] end it "gets root package node" do @root[:cls].should == 'package' end it "gets middle class node" do @middle[:cls].should == 'cls' end it "gets class name containing package name" do @middle[:text].should == 'Bar.Baz' end end Loading
lib/jsduck/class.rb +11 −4 Original line number Diff line number Diff line Loading @@ -74,14 +74,21 @@ module JsDuck # Utility method that given a package or class name finds the name # of its parent package. def self.package_name(name) parts = name.split(/\./) parts.slice(0, parts.length - 1).join(".") name.slice(0, name.length - self.short_name(name).length - 1) || "" end # Utility method that given full package or class name extracts # the last part of the name. # the "class"-part of the name. # # Because we try to emulate ext-doc, it's not as simple as just # taking the last part. See class_spec.rb for details. def self.short_name(name) name.split(/\./).last parts = name.split(/\./) short = parts.pop while parts.length > 1 && parts.last =~ /^[A-Z]/ short = parts.pop + "." + short end short end end Loading
spec/class_spec.rb +60 −0 Original line number Diff line number Diff line Loading @@ -82,4 +82,64 @@ describe JsDuck::Class do end end describe "when full_name like My.package.Cls" do before do @cls = JsDuck::Class.new({:name => "My.package.Cls",}, {}); end it "#package_name contains all parts except the last" do @cls.package_name.should == "My.package" end it "#short_name contains only the last part" do @cls.short_name.should == "Cls" end end describe "when full_name like My.Package.Cls" do before do @cls = JsDuck::Class.new({:name => "My.Package.Cls",}, {}); end it "#package_name contains only first part" do @cls.package_name.should == "My" end it "#short_name contains remaining parts" do @cls.short_name.should == "Package.Cls" end end describe "when full_name has no parts" do before do @cls = JsDuck::Class.new({:name => "Foo",}, {}); end it "#package_name is empty" do @cls.package_name.should == "" end it "#short_name is the same as full_name" do @cls.short_name.should == "Foo" end end describe "when full_name has two uppercase parts" do before do @cls = JsDuck::Class.new({:name => "Foo.Bar",}, {}); end it "#package_name is first part" do @cls.package_name.should == "Foo" end it "#short_name is second part" do @cls.short_name.should == "Bar" end end end
spec/tree_spec.rb +48 −0 Original line number Diff line number Diff line Loading @@ -117,3 +117,51 @@ describe JsDuck::Tree do end describe JsDuck::Tree, "lowercase package name" do before do @tree = JsDuck::Tree.new.create([ JsDuck::Class.new({:tagname => :class, :name => "Foo.bar.Baz"}) ]) @root = @tree[:children][0] @middle = @root[:children][0] @leaf = @middle[:children][0] end it "gets root package node" do @root[:cls].should == 'package' end it "gets middle package node" do @middle[:cls].should == 'package' end it "gets leaf class node" do @leaf[:cls].should == 'cls' end end describe JsDuck::Tree, "uppercase package name" do before do @tree = JsDuck::Tree.new.create([ JsDuck::Class.new({:tagname => :class, :name => "Foo.Bar.Baz"}) ]) @root = @tree[:children][0] @middle = @root[:children][0] end it "gets root package node" do @root[:cls].should == 'package' end it "gets middle class node" do @middle[:cls].should == 'cls' end it "gets class name containing package name" do @middle[:text].should == 'Bar.Baz' end end