1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
# frozen_string_literal: true
require File.dirname(__FILE__) + '/spec_helper'
RSpec.describe YARD::CodeObjects::Base do
before { Registry.clear }
it "does not allow empty object name" do
expect { Base.new(:root, '') }.to raise_error(ArgumentError)
end
it "returns a unique instance of any registered object" do
obj = ClassObject.new(:root, :Me)
obj2 = ClassObject.new(:root, :Me)
expect(obj.object_id).to eq obj2.object_id
obj3 = ModuleObject.new(obj, :Too)
obj4 = CodeObjects::Base.new(obj3, :Hello)
obj4.parent = obj
obj5 = CodeObjects::Base.new(obj3, :hello)
expect(obj4.object_id).not_to eq obj5.object_id
end
it "creates a new object if cached object is not of the same class" do
expect(ConstantObject.new(:root, "MYMODULE")).to be_instance_of(ConstantObject)
expect(ModuleObject.new(:root, "MYMODULE")).to be_instance_of(ModuleObject)
expect(ClassObject.new(:root, "MYMODULE")).to be_instance_of(ClassObject)
expect(YARD::Registry.at("MYMODULE")).to be_instance_of(ClassObject)
end
it "simplifies complex namespace paths" do
obj = ClassObject.new(:root, "A::B::C::D")
expect(obj.name).to eq :D
expect(obj.path).to eq "A::B::C::D"
expect(obj.namespace).to eq P("A::B::C")
end
# @bug gh-552
it "simplifies complex namespace paths when path starts with ::" do
obj = ClassObject.new(:root, "::A::B::C::D")
expect(obj.name).to eq :D
expect(obj.path).to eq "A::B::C::D"
expect(obj.namespace).to eq P("A::B::C")
end
it "calls the block again if #new is called on an existing object" do
o1 = ClassObject.new(:root, :Me) do |o|
o.docstring = "DOCSTRING"
end
o2 = ClassObject.new(:root, :Me) do |o|
o.docstring = "NOT_DOCSTRING"
end
expect(o1.object_id).to eq o2.object_id
expect(o1.docstring).to eq "NOT_DOCSTRING"
expect(o2.docstring).to eq "NOT_DOCSTRING"
end
it "allows complex name and converts it to namespace" do
obj = CodeObjects::Base.new(nil, "A::B")
expect(obj.namespace.path).to eq "A"
expect(obj.name).to eq :B
end
it "allows namespace to be nil and not register in the Registry" do
obj = CodeObjects::Base.new(nil, :Me)
expect(obj.namespace).to eq nil
expect(Registry.at(:Me)).to eq nil
end
it "allows namespace to be a NamespaceObject" do
ns = ModuleObject.new(:root, :Name)
obj = CodeObjects::Base.new(ns, :Me)
expect(obj.namespace).to eq ns
end
it "allows :root to be the shorthand namespace of `Registry.root`" do
obj = CodeObjects::Base.new(:root, :Me)
expect(obj.namespace).to eq Registry.root
end
it "does not allow any other types as namespace" do
expect { CodeObjects::Base.new("ROOT!", :Me) }.to raise_error(ArgumentError)
end
it "allows constants to be used as a namespace" do
a = ConstantObject.new(:root, :A)
a.value = "B::C"
b = ClassObject.new(:root, :B)
c = ClassObject.new(b, :C)
klass = ClassObject.new(a, "MyClass")
expect(klass.path).to eq "B::C::MyClass"
end
it "does not allow constants to be used as a namespace if they do not resolve to a valid namespace" do
a = ConstantObject.new(:root, :A)
a.value = "$$INVALID$$"
expect { ClassObject.new(a, "MyClass") }.to raise_error(Parser::UndocumentableError)
end
it "registers itself in the registry if namespace is supplied" do
obj = ModuleObject.new(:root, :Me)
expect(Registry.at(:Me)).to eq obj
obj2 = ModuleObject.new(obj, :Too)
expect(Registry.at(:"Me::Too")).to eq obj2
end
describe "#[]=" do
it "sets any attribute" do
obj = ModuleObject.new(:root, :YARD)
obj[:some_attr] = "hello"
expect(obj[:some_attr]).to eq "hello"
end
it "uses the accessor method if available" do
obj = CodeObjects::Base.new(:root, :YARD)
obj[:source] = "hello"
expect(obj.source).to eq "hello"
obj.source = "unhello"
expect(obj[:source]).to eq "unhello"
end
end
it "sets attributes via attr= through method_missing" do
obj = CodeObjects::Base.new(:root, :YARD)
obj.something = 2
expect(obj.something).to eq 2
expect(obj[:something]).to eq 2
end
it "exists in the parent's #children after creation" do
obj = ModuleObject.new(:root, :YARD)
obj2 = MethodObject.new(obj, :testing)
expect(obj.children).to include(obj2)
end
# @bug gh-1209
it "removes prior defined objects at the same path from namespace's children" do
Registry.clear
obj = ModuleObject.new(:root, :YARD)
ConstantObject.new(obj, :Testing)
ClassObject.new(obj, :Testing)
expect(obj.children.map {|o| o.type.to_sym }).to eq [:class]
expect(Registry.at('YARD::Testing').type).to eq :class
end
it "properly re-indents source starting from 0 indentation" do
obj = CodeObjects::Base.new(nil, :test)
obj.source = <<-eof
def mymethod
if x == 2 &&
5 == 5
3
else
1
end
end
eof
expect(obj.source).to eq "def mymethod\n if x == 2 &&\n 5 == 5\n 3\n else\n 1\n end\nend"
Registry.clear
Parser::SourceParser.parse_string <<-eof
def key?(key)
super(key)
end
eof
expect(Registry.at('#key?').source).to eq "def key?(key)\n super(key)\nend"
Registry.clear
Parser::SourceParser.parse_string <<-eof
def key?(key)
if x == 2
puts key
else
exit
end
end
eof
expect(Registry.at('#key?').source).to eq "def key?(key)\n if x == 2\n puts key\n else\n exit\n end\nend"
end
it "does not add newlines to source when parsing sub blocks" do
Parser::SourceParser.parse_string <<-eof
module XYZ
module ZYX
class ABC
def msg
hello_world
end
end
end
end
eof
expect(Registry.at('XYZ::ZYX::ABC#msg').source).to eq "def msg\n hello_world\nend"
end
it "handles source for 'def x; end'" do
Registry.clear
Parser::SourceParser.parse_string "def x; 2 end"
expect(Registry.at('#x').source).to eq "def x; 2 end"
end
it "sets file and line information" do
Parser::SourceParser.parse_string <<-eof
class X; end
eof
expect(Registry.at(:X).file).to eq '(stdin)'
expect(Registry.at(:X).line).to eq 1
end
it "maintains all file associations when objects are defined multiple times in one file" do
Parser::SourceParser.parse_string <<-eof
class X; end
class X; end
class X; end
eof
expect(Registry.at(:X).file).to eq '(stdin)'
expect(Registry.at(:X).line).to eq 1
expect(Registry.at(:X).files).to eq [['(stdin)', 1], ['(stdin)', 2], ['(stdin)', 3]]
end
it "maintains all file associations when objects are defined multiple times in multiple files" do
3.times do |i|
allow(File).to receive(:read_binary).and_return("class X; end")
Parser::SourceParser.new.parse("file#{i + 1}.rb")
end
expect(Registry.at(:X).file).to eq 'file1.rb'
expect(Registry.at(:X).line).to eq 1
expect(Registry.at(:X).files).to eq [['file1.rb', 1], ['file2.rb', 1], ['file3.rb', 1]]
end
it "prioritizes the definition with a docstring when returning #file" do
Parser::SourceParser.parse_string <<-eof
class X; end
class X; end
# docstring
class X; end
eof
expect(Registry.at(:X).file).to eq '(stdin)'
expect(Registry.at(:X).line).to eq 4
expect(Registry.at(:X).files).to eq [['(stdin)', 4], ['(stdin)', 1], ['(stdin)', 2]]
end
describe "#format" do
it "sends object to Templates.render" do
object = MethodObject.new(:root, :method)
expect(Templates::Engine).to receive(:render).with({:x => 1, :object => object, :type => object.type})
object.format :x => 1
end
it "does not change options object class" do
opts = YARD::Templates::TemplateOptions.new
opts.type = "test"
object = MethodObject.new(:root, :method)
expect(Templates::Engine).to receive(:render).with kind_of(YARD::Templates::TemplateOptions)
object.format(opts)
end
end
describe "#source_type" do
it "defaults to :ruby" do
object = MethodObject.new(:root, :method)
expect(object.source_type).to eq :ruby
end
end
describe "#relative_path" do
it "accepts a string" do
YARD.parse_string "module A; class B; end; class C; end; end"
expect(Registry.at('A::B').relative_path(Registry.at('A::C'))).to eq(
Registry.at('A::B').relative_path('A::C')
)
end
it "returns full class name when objects share a common class prefix" do
YARD.parse_string "module User; end; module UserManager; end"
expect(Registry.at('User').relative_path('UserManager')).to eq 'UserManager'
expect(Registry.at('User').relative_path(Registry.at('UserManager'))).to eq 'UserManager'
end
it "returns the relative path when they share a common namespace" do
YARD.parse_string "module A; class B; end; class C; end; end"
expect(Registry.at('A::B').relative_path(Registry.at('A::C'))).to eq 'C'
YARD.parse_string "module Foo; module A; end; module B; def foo; end end end"
expect(Registry.at('Foo::A').relative_path(Registry.at('Foo::B#foo'))).to eq 'B#foo'
end
it "returns the full path if they don't have a common namespace" do
YARD.parse_string "module A; class B; end; end; module D; class C; end; end"
expect(Registry.at('A::B').relative_path('D::C')).to eq 'D::C'
YARD.parse_string 'module C::B::C; module Apple; end; module Ant; end end'
expect(Registry.at('C::B::C::Apple').relative_path('C::B::C::Ant')).to eq 'Ant'
YARD.parse_string 'module OMG::ABC; end; class Object; end'
expect(Registry.at('OMG::ABC').relative_path('Object')).to eq "Object"
YARD.parse_string("class YARD::Config; MYCONST = 1; end")
expect(Registry.at('YARD::Config').relative_path('YARD::Config::MYCONST')).to eq "MYCONST"
end
it "returns a relative path for class methods" do
YARD.parse_string "module A; def self.b; end; def self.c; end; end"
expect(Registry.at('A.b').relative_path('A.c')).to eq 'c'
expect(Registry.at('A').relative_path('A.c')).to eq 'c'
end
it "returns a relative path for instance methods" do
YARD.parse_string "module A; def b; end; def c; end; end"
expect(Registry.at('A#b').relative_path('A#c')).to eq '#c'
expect(Registry.at('A').relative_path('A#c')).to eq '#c'
end
it "returns full path if relative path is to parent namespace" do
YARD.parse_string "module A; module B; end end"
expect(Registry.at('A::B').relative_path('A')).to eq 'A'
end
it "only returns name for relative path to self" do
YARD.parse_string("class A::B::C; def foo; end end")
expect(Registry.at('A::B::C').relative_path('A::B::C')).to eq 'C'
expect(Registry.at('A::B::C#foo').relative_path('A::B::C#foo')).to eq '#foo'
end
end
describe "#docstring=" do
it "converts string into Docstring when #docstring= is set" do
o = ClassObject.new(:root, :Me)
o.docstring = "DOCSTRING"
expect(o.docstring).to be_instance_of(Docstring)
end
it "sets docstring to docstring of other object if docstring is '(see Path)'" do
ClassObject.new(:root, :AnotherObject) {|x| x.docstring = "FOO" }
o = ClassObject.new(:root, :Me)
o.docstring = '(see AnotherObject)'
expect(o.docstring).to eq "FOO"
end
it "does not copy docstring mid-docstring" do
doc = "Hello.\n(see file.rb)\nmore documentation"
o = ClassObject.new(:root, :Me)
o.docstring = doc
expect(o.docstring).to eq doc
end
it "allows extra docstring after (see Path)" do
ClassObject.new(:root, :AnotherObject) {|x| x.docstring = "FOO" }
o = ClassObject.new(:root, :Me)
o.docstring = Docstring.new("(see AnotherObject)\n\nEXTRA\n@api private", o)
expect(o.docstring).to eq "FOO\n\nEXTRA"
expect(o.docstring).to have_tag(:api)
end
end
describe "#docstring" do
it "returns an empty string if docstring was '(see Path)' and Path is not resolved" do
o = ClassObject.new(:root, :Me)
o.docstring = '(see AnotherObject)'
expect(o.docstring).to eq ""
end
it "returns docstring when object is resolved" do
o = ClassObject.new(:root, :Me)
o.docstring = '(see AnotherObject)'
expect(o.docstring).to eq ""
ClassObject.new(:root, :AnotherObject) {|x| x.docstring = "FOO" }
expect(o.docstring).to eq "FOO"
end
describe "localization" do
it "returns localized docstring" do
fr_locale = YARD::I18n::Locale.new('fr')
allow(fr_locale).to receive(:translate).with('Hello').and_return('Bonjour')
o = ClassObject.new(:root, :Me)
o.docstring = 'Hello'
expect(o.docstring).to eq 'Hello'
allow(Registry).to receive(:locale).with('fr').and_return(fr_locale)
expect(o.docstring('fr')).to eq "Bonjour"
end
it "returns localized docstring tag" do
o = CodeObjects::MethodObject.new(:root, 'Hello#message')
o.docstring.add_tag(Tags::Tag.new('return', 'Hello'))
fr_locale = YARD::I18n::Locale.new('fr')
allow(fr_locale).to receive(:translate).with('Hello').and_return('Bonjour')
allow(Registry).to receive(:locale).with('fr').and_return(fr_locale)
expect(o.docstring('fr').tags.map(&:text)).to eq ['Bonjour']
end
it "returns updated localized docstring" do
fr_locale = YARD::I18n::Locale.new('fr')
allow(Registry).to receive(:locale).with('fr').and_return(fr_locale)
o = ClassObject.new(:root, :Me)
o.docstring = 'Hello'
expect(o.docstring).to eq 'Hello'
allow(fr_locale).to receive(:translate).with('Hello').and_return('Bonjour')
expect(o.docstring('fr')).to eq "Bonjour"
o.docstring = 'World'
allow(fr_locale).to receive(:translate).with('World').and_return('Monde')
expect(o.docstring('fr')).to eq "Monde"
expect(o.docstring).to eq 'World'
end
end
end
describe "#add_file" do
it "only adds a file/line combination once" do
o = ClassObject.new(:root, :Me)
o.add_file('filename', 12)
expect(o.files).to eq [['filename', 12]]
o.add_file('filename', 12)
expect(o.files).to eq [['filename', 12]]
o.add_file('filename', 40) # different line
expect(o.files).to eq [['filename', 12], ['filename', 40]]
end
end
describe "#copy_to" do
it "copies all data to new object" do
YARD.parse_string <<-eof
private
# A docstring
# @return [String] a tag
def foo(a, b, c)
source_code_here
end
eof
foo_c = MethodObject.new(:root, :foo, :class)
Registry.at('#foo').copy_to(foo_c)
expect(foo_c.scope).to eq :class
expect(foo_c.visibility).to eq :private
expect(foo_c.type).to eq :method
expect(foo_c.class).to eq MethodObject
expect(foo_c.path).to eq '::foo'
expect(foo_c.docstring).to eq "A docstring"
expect(foo_c.tag(:return).types).to eq ['String']
expect(foo_c.file).to eq '(stdin)'
expect(foo_c.line).to eq 4
expect(foo_c.source).to match(/source_code_here/)
expect(foo_c.signature).to eq 'def foo(a, b, c)'
expect(foo_c.parameters).to eq [['a', nil], ['b', nil], ['c', nil]]
end
it "returns the copied object" do
YARD.parse_string 'def foo; end'
foo_c = MethodObject.new(:root, :foo, :class)
expect(Registry.at('#foo').copy_to(foo_c)).to eq foo_c
end
it "copies docstring and rewrite tags to new object" do
YARD.parse_string <<-eof
# @return [String] a tag
def foo; end
eof
foo_c = MethodObject.new(:root, :foo, :class)
foo_i = Registry.at('#foo')
foo_i.copy_to(foo_c)
expect(foo_i.tags).not_to eq foo_c.tags
expect(foo_c.tags.first.object).to eq foo_c
end
it "only copies #copyable_attributes" do
foo = MethodObject.new(:root, :foo)
expect(foo).to receive(:copyable_attributes).and_return %w(a b c)
expect(foo).to receive(:instance_variable_get).with('@a').and_return(1)
expect(foo).to receive(:instance_variable_get).with('@b').and_return(2)
expect(foo).to receive(:instance_variable_get).with('@c').and_return(3)
bar = MethodObject.new(:root, :bar)
expect(bar).to receive(:instance_variable_set).with('@a', 1)
expect(bar).to receive(:instance_variable_set).with('@b', 2)
expect(bar).to receive(:instance_variable_set).with('@c', 3)
foo.copy_to(bar)
end
end
end
|