Skip to content

Commit

Permalink
add undefine_macro API (#67, #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani authored Jun 4, 2024
1 parent 3833adf commit ecded2b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/flgen/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def define_macro(macro, value = nil)
add_macro_definition(k, v, false)
end

def undefine_macro(macro)
remove_macro(macro)
end

def macros
@macros ||= {}
end
Expand Down Expand Up @@ -111,11 +115,19 @@ def add_macro_definition(macro, value, predefined)
end

def add_macro_argument(name, value)
arguments
.delete_if { |argument| argument.type == :define && argument.name == name }
arguments.delete_if { |arg| macro_definition?(arg, name) }
add_compile_argument(Arguments::Define.new(name, value))
end

def remove_macro(macro)
name = macro.to_sym
arguments.reject! { |arg| macro_definition?(arg, name) } && macros.delete(name)
end

def macro_definition?(arg, name)
arg.type == :define && arg.name == name
end

def directory_already_added?(type, path)
arguments
.any? { |argument| argument.type == type && argument.path == path }
Expand Down
8 changes: 5 additions & 3 deletions lib/flgen/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def define_macro(macro, value = nil)
@context.define_macro(macro, value)
end

def undefine_macro(macro)
@context.undefine_macro(macro)
end

def macro?(macro)
@context.macros.key?(macro.to_sym)
end
Expand Down Expand Up @@ -126,9 +130,7 @@ def add_entry(path, from, raise_error, method_name, location)
end

def raise_no_entry_error(path, location, raise_error)
return unless raise_error

raise NoEntryError.new(path, location)
raise_error && (raise NoEntryError.new(path, location))
end

def glob_files(patterns, from, method_name, location)
Expand Down
25 changes: 25 additions & 0 deletions spec/flgen/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,31 @@ module piyo;
end
end

describe '#undefine_macro' do
it '定義済みマクロを無効化する' do
context.define_macro(:FOO)
context.define_macro('BAR=1')
context.define_macro(:BAZ)

context.undefine_macro(:QUX)
context.undefine_macro(:BAZ)
context.undefine_macro(:FOO)

expect(context.macros).to match(BAR: '1')
end

specify '事前定義済みマクロは無効化しない' do
options[:tool] = :vcs

context.define_macro(:FOO)
context.define_macro(:BAR)
context.undefine_macro(:FOO)
context.undefine_macro(:VCS)

expect(context.macros).to match(VCS: true, BAR: true)
end
end

describe '事前定義済みマクロ' do
context '対象ツールがVCSの場合' do
specify ':VCSが定義される' do
Expand Down
12 changes: 12 additions & 0 deletions spec/flgen/file_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,18 @@ def mock_glob(pattens, results)
end
end

describe '#undefine_macro' do
it 'マクロを無効化する' do
file_list = described_class.new(context, path)

expect(context).to receive(:undefine_macro).with(:FOO)
expect(context).to receive(:undefine_macro).with(:BAR)

file_list.undefine_macro(:FOO)
file_list.undefine_macro(:BAR)
end
end

describe '#macro?/#macro_defined?' do
let(:macros) do
[:FOO, :BAR]
Expand Down

0 comments on commit ecded2b

Please sign in to comment.