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
|
# frozen_string_literal: true
require File.dirname(__FILE__) + '/spec_helper'
RSpec.describe YARD::CodeObjects::MacroObject do
before do
Registry.clear
end
describe ".create" do
def create(*args) MacroObject.create(*args) end
it "creates an object" do
create('foo', '')
obj = Registry.at('.macro.foo')
expect(obj).not_to be nil
end
it "uses identity map" do
obj1 = create('foo', '')
obj2 = create('foo', '')
expect(obj1.object_id).to eq obj2.object_id
end
it "allows specifying of macro data" do
obj = create('foo', 'MACRODATA')
expect(obj.macro_data).to eq 'MACRODATA'
end
context "if a method object is provided" do
it "attaches it" do
obj = create('foo', 'MACRODATA', P('Foo.property'))
expect(obj.method_object).to eq P('Foo.property')
expect(obj).to be_attached
end
end
end
describe ".find" do
before { MacroObject.create('foo', 'DATA') }
it "searches for an object by name" do
expect(MacroObject.find('foo').macro_data).to eq 'DATA'
end
it "accepts Symbol" do
expect(MacroObject.find(:foo).macro_data).to eq 'DATA'
end
end
describe ".find_or_create" do
it "looks up name if @!macro is present and find object" do
macro1 = MacroObject.create('foo', 'FOO')
macro2 = MacroObject.find_or_create('foo', "a b c")
expect(macro1).to eq macro2
end
it "creates new macro if macro by that name does not exist" do
MacroObject.find_or_create('foo', "@!method $1")
expect(MacroObject.find('foo').macro_data).to eq "@!method $1"
end
end
describe ".apply" do
let(:args) { %w(foo a b c) }
def apply(comments)
MacroObject.apply(comments, args)
end
it "only expands macros if @macro is present" do
expect(apply("$1$2$3")).to eq "$1$2$3"
end
it "handles macro text inside block" do
expect(apply("@!macro\n foo$1$2$3\nfoobaz")).to eq "fooabc\nfoobaz"
end
it "appends docstring to existing macro" do
MacroObject.create('name', '$3$2$1')
result = MacroObject.apply("@!macro name\nfoobar", args)
expect(result).to eq "cba\nfoobar"
end
it "uses only non-macro data if docstring is an existing macro" do
data = "@!macro name\n $3$2$1\nEXTRA"
result = MacroObject.apply(data, args, 'SOURCE')
expect(result).to eq "cba\nEXTRA"
expect(MacroObject.apply("@!macro name\nFOO", args)).to eq "cba\nFOO"
end
it "creates macros if they don't exist" do
result = MacroObject.apply("@!macro name\n foo!$1", args, 'SOURCE')
expect(result).to eq "foo!a"
expect(MacroObject.find('name').macro_data).to eq 'foo!$1'
end
it "keeps other tags" do
expect(apply("@!macro\n foo$1$2$3\n@param name foo\nfoo")).to eq(
"fooabc\nfoo\n@param name\n foo"
)
end
end
describe ".expand" do
def expand(comments)
args = %w(foo a b c)
full_line = 'foo :bar, :baz'
MacroObject.expand(comments, args, full_line)
end
it "allows escaping of macro syntax" do
expect(expand("$1\\$2$3")).to eq "a$2c"
end
it "replaces $* with the whole statement" do
expect(expand("$* ${*}")).to eq "foo :bar, :baz foo :bar, :baz"
end
it "replaces $0 with method name" do
expect(expand("$0 ${0}")).to eq "foo foo"
end
it "replaces all $N values with the Nth argument in the method call" do
expect(expand("$1$2$3${3}\nfoobar")).to eq "abcc\nfoobar"
end
it "replaces ${N-M} ranges with N-M arguments (incl. commas)" do
expect(expand("${1-2}x")).to eq "a, bx"
end
it "handles open ended ranges (${N-})" do
expect(expand("${2-}")).to eq "b, c"
end
it "handles negative indexes ($-N)" do
expect(expand("$-1 ${-2-} ${-2--2}")).to eq "c b, c b"
end
it "accepts Docstring objects" do
expect(expand(Docstring.new("$1\n@param name foo"))).to eq "a\n@param name foo"
end
end
describe "#expand" do
it "expands a macro given its data" do
macro = MacroObject.create_docstring('foo', '$1 $2 THREE!')
expect(macro.expand(['NAME', 'ONE', 'TWO'])).to eq "ONE TWO THREE!"
end
end
end
|