diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/component_manager_spec.rb | 110 | ||||
| -rw-r--r-- | spec/entity_manager_spec.rb | 105 | ||||
| -rw-r--r-- | spec/order_spec.rb | 71 | ||||
| -rw-r--r-- | spec/scene_manager_spec.rb | 26 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 114 | ||||
| -rw-r--r-- | spec/stage_manager_spec.rb | 88 | ||||
| -rw-r--r-- | spec/system_manager_spec.rb | 67 |
7 files changed, 353 insertions, 228 deletions
diff --git a/spec/component_manager_spec.rb b/spec/component_manager_spec.rb index d99d744..9bf9b9e 100644 --- a/spec/component_manager_spec.rb +++ b/spec/component_manager_spec.rb @@ -1,16 +1,19 @@ -require 'felflame' +# frozen_string_literal: true -describe 'Components' do +require_relative '../lib/felflame' - #let :component_manager do +describe 'Components' do + # let :component_manager do # @component_manager ||= FelFlame::Components.new('TestComponents', :param1, param2: 'def') - #end + # end before :all do @component_manager ||= FelFlame::Components.new('TestComponents', :param1, param2: 'def') end before :each do + @orig_stderr = $stderr + $stderr = StringIO.new @ent0 = FelFlame::Entities.new @ent1 = FelFlame::Entities.new @ent2 = FelFlame::Entities.new @@ -20,36 +23,81 @@ describe 'Components' do end after :each do - FelFlame::Entities.each(&:delete) - @component_manager.each(&:delete) + $stderr = @orig_stderr + FelFlame::Entities.reverse_each(&:delete) + @component_manager.reverse_each(&:delete) + end + + it 'can get a single entity' do + @cmp0.entity + $stderr.rewind + $stderr.string.chomp.should eq("This component belongs to NO entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.") + @ent0.add @cmp0 + expect(@cmp0.entity).to eq(@ent0) + @ent1.add @cmp0 + @cmp0.entity + $stderr.rewind + $stderr.string.chomp.should eq("This component belongs to MANY entities but you called the method that is intended for components belonging to a single entity.\nYou may have a bug in your logic.") + end + + it 'responds to array methods' do + expect(@component_manager.respond_to?(:[])).to be true + expect(@component_manager.respond_to?(:each)).to be true + @component_manager.each do |component| + expect(component.respond_to?(:param1)).to be true + end + expect(@component_manager.respond_to?(:filter)).to be true + expect(@component_manager.respond_to?(:first)).to be true + expect(@component_manager.respond_to?(:last)).to be true + expect(@component_manager.respond_to?(:somethingwrong)).to be false + end + + it 'dont respond to missing methods' do + expect { @component_manager.somethingwrong }.to raise_error(NoMethodError) + end + + it 'Component module responds to array methods' do + expect(FelFlame::Components.respond_to?(:[])).to be true + expect(FelFlame::Components.respond_to?(:each)).to be true + FelFlame::Components.each do |component_manager| + expect(component_manager.respond_to?(:addition_triggers)).to be true + end + expect(FelFlame::Components.respond_to?(:filter)).to be true + expect(FelFlame::Components.respond_to?(:first)).to be true + expect(FelFlame::Components.respond_to?(:last)).to be true + expect(FelFlame::Components.respond_to?(:somethingwrong)).to be false + end + + it 'Component module doesnt respond to missing methods' do + expect { FelFlame::Components.somethingwrong }.to raise_error(NoMethodError) end it 'can delete a component' do - component_id = @cmp1.id + # component_id = @cmp1.id @ent0.add @cmp1 - + length = @component_manager.length expect(@cmp1.delete).to be true - expect(@cmp1.id).to be_nil - expect(@component_manager[component_id]).to be_nil + expect(@component_manager.length).to eq(length - 1) + # expect(@cmp1.id).to be_nil + # expect(@component_manager[component_id]).to be_nil expect(@cmp1.entities).to eq([]) end - it 'can iterate over all component managers' do - all_components = FelFlame::Components.constants + it 'can iterate component managers' do + all_components_symbols = FelFlame::Components.constants + all_components = all_components_symbols.map do |symbol| + FelFlame::Components.const_get symbol + end + expect(all_components).to eq(FF::Components.each.to_a) expect(all_components.length).to be > 0 expect(FelFlame::Components.each).to be_an Enumerator - FelFlame::Components.each do |component_manager| - all_components.delete component_manager.to_s.to_sym - end - expect(all_components).to eq([]) end it 'can change params on initialization' do @cmp3 = @component_manager.new(param1: 'ok', param2: 10) - expect(@cmp3.attrs).to eq(param1: 'ok', param2: 10, id: @cmp3.id) + expect(@cmp3.to_h).to eq(param1: 'ok', param2: 10) end - it 'sets default params correctly' do expect(@cmp0.param1).to be_nil expect(@cmp0.param2).to eq('def') @@ -59,28 +107,26 @@ describe 'Components' do expect(@cmp2.param2).to eq('def') end - it 'can read attrs' do - expect(@cmp0.attrs).to eq(param2: 'def', id: 0) - expect(@cmp1.attrs).to eq(param2: 'def', id: 1) - expect(@cmp2.attrs).to eq(param2: 'def', id: 2) + it 'can read attributes' do + expect(@cmp0.to_h).to eq(param2: 'def') + expect(@cmp1.to_h).to eq(param2: 'def') + expect(@cmp2.to_h).to eq(param2: 'def') end it 'can set attrs' do expect(@cmp0.param1 = 4).to eq(4) expect(@cmp1.update_attrs(param1: 3, param2: 'new')).to eq(param1: 3, param2: 'new') - expect(@cmp1.attrs).to eq(param1: 3, param2: 'new', id: 1) + expect(@cmp1.to_h).to eq(param1: 3, param2: 'new') end - it 'can be accessed' do - expect(@cmp0).to eq(@component_manager[0]) - expect(@cmp1).to eq(@component_manager[1]) - expect(@cmp2).to eq(@component_manager[2]) + it 'can be used as a singleton' do + expect(@component_manager.first).to eq(@cmp0) end - it 'can get id from to_i' do - expect(@cmp0.id).to eq(@cmp0.to_i) - expect(@cmp1.id).to eq(@cmp1.to_i) - expect(@cmp2.id).to eq(@cmp2.to_i) + it 'can be accessed' do + expect(@component_manager[0].respond_to?(:param1)).to eq(true) + expect(@component_manager[1].respond_to?(:param1)).to eq(true) + expect(@component_manager[2].respond_to?(:param1)).to eq(true) end it 'cant overwrite exiting component managers' do @@ -89,7 +135,7 @@ describe 'Components' do end it 'can\'t create an attribute when its name is an existing method' do - expect { FelFlame::Components.new('TestComponent2', :id) }.to raise_error(NameError) + # expect { FelFlame::Components.new('TestComponent2', :id) }.to raise_error(NameError) expect { FelFlame::Components.new('TestComponent2', :addition_triggers) }.to raise_error(NameError) expect { FelFlame::Components.new('TestComponent2', :removal_triggers) }.to raise_error(NameError) expect { FelFlame::Components.new('TestComponent2', :attr_triggers) }.to raise_error(NameError) diff --git a/spec/entity_manager_spec.rb b/spec/entity_manager_spec.rb index ef638c0..2068986 100644 --- a/spec/entity_manager_spec.rb +++ b/spec/entity_manager_spec.rb @@ -1,19 +1,18 @@ -require 'felflame' +# frozen_string_literal: true -#class EntitiesTest < Minitest::Test +require_relative '../lib/felflame' -describe 'Entities' do - - #let :component_manager do - # @component_manager ||= FelFlame::Components.new('Test', :param1, param2: 'def') - #end +# class EntitiesTest < Minitest::Test +describe 'Entities' do before :all do + $VERBOSE = nil @component_manager ||= FelFlame::Components.new('TestEntity', :param1, param2: 'def') end - before :each do + @orig_stderr = $stderr + $stderr = StringIO.new @ent0 = FelFlame::Entities.new @ent1 = FelFlame::Entities.new @ent2 = FelFlame::Entities.new @@ -23,8 +22,40 @@ describe 'Entities' do end after :each do - FelFlame::Entities.each(&:delete) - @component_manager.each(&:delete) + $stderr = @orig_stderr + FelFlame::Entities.reverse_each(&:delete) + @component_manager.reverse_each(&:delete) + end + + it 'can get a single component' do + expect { @ent0.component[@component_manager] }.to raise_error(RuntimeError) + @ent0.add @cmp0 + expect(@ent0.component[@component_manager]).to eq(@cmp0) + expect(@ent0.component[@component_manager]).to eq(@ent0.component(@component_manager)) + @ent0.add @cmp1 + @ent0.component[@component_manager] + $stderr.rewind + $stderr.string.chomp.should eq("This entity has MANY of this component but you called the method that is intended for having a single of this component type.\nYou may have a bug in your logic.") + @ent0.components[@component_manager].reverse_each do |component| + @ent0.remove component + end + expect { @ent0.component[@component_manager] }.to raise_error(RuntimeError) + end + + it 'responds to array methods' do + expect(FelFlame::Entities.respond_to?(:[])).to be true + expect(FelFlame::Entities.respond_to?(:each)).to be true + FelFlame::Entities.each do |entity| + expect(entity.respond_to?(:components)).to be true + end + expect(FelFlame::Entities.respond_to?(:filter)).to be true + expect(FelFlame::Entities.respond_to?(:first)).to be true + expect(FelFlame::Entities.respond_to?(:last)).to be true + expect(FelFlame::Entities.respond_to?(:somethingwrong)).to be false + end + + it 'dont respond to missing methods' do + expect { FelFlame::Entities.somethingwrong }.to raise_error(NoMethodError) end it 'won\'t add duplicate entities' do @@ -32,21 +63,15 @@ describe 'Entities' do expect(@ent0.components[@component_manager].count).to eq(2) end - it 'has correct ID\'s' do - expect(@ent0.id).to eq(0) - expect(@ent1.id).to eq(1) - expect(@ent2.id).to eq(2) - end - it 'can be accessed' do - expect(@ent0).to eq(FelFlame::Entities[0]) - expect(@ent1).to eq(FelFlame::Entities[1]) - expect(@ent2).to eq(FelFlame::Entities[2]) + expect(FelFlame::Entities[0].respond_to?(:components)).to eq(true) + expect(FelFlame::Entities[1].respond_to?(:components)).to eq(true) + expect(FelFlame::Entities[2].respond_to?(:components)).to eq(true) end it 'can have components attached' do @ent0.add @cmp0 - expect(@ent0.components[@component_manager][0]).to eq(@cmp0) + expect(@ent0.component[@component_manager]).to eq(@cmp0) @ent1.add @cmp1, @cmp2 expect(@ent1.components[@component_manager].length).to eq(2) @@ -54,38 +79,36 @@ describe 'Entities' do expect(@ent1.components[@component_manager].include?(@cmp2)).to be true end - it 'can get id from to_i' do - expect(@ent0.id).to eq(@ent0.to_i) - expect(@ent1.id).to eq(@ent1.to_i) - expect(@ent2.id).to eq(@ent2.to_i) - end - it 'can have components removed' do @ent0.add @cmp0 - expect(@ent0.remove @cmp0).to be true + expect(@ent0.remove(@cmp0)).to be true expect(@cmp0.entities.empty?).to be true - expect(@ent0.components[@component_manager].empty?).to be true + expect(@ent0.components[@component_manager].nil?).to be true + @ent0.add @cmp0 + @cmp0.delete + expect(@ent0.components[@component_manager].nil?).to be true end it 'can have many components added then removed' do @ent0.add @cmp0, @cmp1, @cmp2 @ent1.add @cmp0, @cmp1 @ent2.add @cmp1, @cmp2 - expect(@ent0.components).to eq({@component_manager => [@cmp0,@cmp1,@cmp2]}) - expect(@cmp0.entities).to eq([@ent0,@ent1]) - expect(@cmp1.entities).to eq([@ent0,@ent1,@ent2]) - expect(@cmp2.entities).to eq([@ent0,@ent2]) + expect(@ent0.components).to eq({ @component_manager => [@cmp0, @cmp1, @cmp2] }) + expect(@cmp0.entities).to eq([@ent0, @ent1]) + expect(@cmp1.entities).to eq([@ent0, @ent1, @ent2]) + expect(@cmp2.entities).to eq([@ent0, @ent2]) @ent1.delete expect(@cmp0.entities).to eq([@ent0]) - expect(@cmp1.entities).to eq([@ent0,@ent2]) - expect(@cmp2.entities).to eq([@ent0,@ent2]) + expect(@cmp1.entities).to eq([@ent0, @ent2]) + expect(@cmp2.entities).to eq([@ent0, @ent2]) @cmp1.delete - expect(@ent0.components).to eq({@component_manager => [@cmp0,@cmp2]}) - @component_manager.each(&:delete) - expect(@component_manager.each.to_a).to eq([]) - expect(@ent0.components).to eq({@component_manager => []}) - expect(@ent2.components).to eq({@component_manager => []}) - FelFlame::Entities.each(&:delete) - expect(FelFlame::Entities.each.to_a).to eq([]) + expect(@ent0.components).to eq({ @component_manager => [@cmp0, @cmp2] }) + @component_manager.reverse_each(&:delete) + expect(@component_manager.length).to eq(0) + expect(@component_manager.empty?).to be true + expect(@ent0.components).to eq({}) + expect(@ent2.components).to eq({}) + FelFlame::Entities.reverse_each(&:delete) + expect(FelFlame::Entities.empty?).to be true end end diff --git a/spec/order_spec.rb b/spec/order_spec.rb new file mode 100644 index 0000000..fd104fc --- /dev/null +++ b/spec/order_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require_relative '../lib/felflame' + +# class EntitiesTest < Minitest::Test + +describe 'Order' do + before :all do + @result = [] + @system0 = FelFlame::Systems.new('System1', priority: 0) do + @result.push 0 + end + @system2 = FelFlame::Systems.new('System3', priority: 2) do + @result.push 2 + end + @system1 = FelFlame::Systems.new('System2', priority: 1) do + @result.push 1 + end + + @scene1 = FelFlame::Scenes.new('Scene0', priority: 1) + @scene0 = FelFlame::Scenes.new('Scene0', priority: 0) + end + + before :each do + end + + after :each do + @result.clear + @scene0.clear + @scene1.clear + @system0.priority = 0 + @system1.priority = 1 + @system2.priority = 2 + @scene0.priority = 0 + @scene1.priority = 1 + end + + it 'can sort Scenes' do + @scene0.add @system0 + @scene1.add @system1 + FelFlame::Order.sort( + @scene1, + @scene0 + ) + expect(@scene1.priority < @scene0.priority).to eq(true) + end + + it 'can sort Systems' do + @scene0.add @system0, @system1, @system2 + FelFlame::Order.sort( + @system2, + @system0, + @system1 + ) + @scene0.call + expect(@result).to eq([2, 0, 1]) + end + + it 'can handle array' do + FelFlame::Order.sort( + @system0, + [ + @system1, + @system2 + ] + ) + expect(@system0.priority < @system1.priority).to eq(true) + expect(@system0.priority < @system2.priority).to eq(true) + expect(@system1.priority == @system2.priority).to eq(true) + end +end diff --git a/spec/scene_manager_spec.rb b/spec/scene_manager_spec.rb index f2aee01..e1984dc 100644 --- a/spec/scene_manager_spec.rb +++ b/spec/scene_manager_spec.rb @@ -1,6 +1,8 @@ -require 'felflame' +# frozen_string_literal: true -#class EntitiesTest < Minitest::Test +require_relative '../lib/felflame' + +# class EntitiesTest < Minitest::Test describe 'Scenes' do before :all do @@ -35,6 +37,9 @@ describe 'Scenes' do it 'can add Systems' do @scene.add @system2, @system3, @system1 + expect(@system1.scenes.length).to eq(1) + expect(@system2.scenes.length).to eq(1) + expect(@system3.scenes.length).to eq(1) expect(@scene.systems).to eq([@system1, @system2, @system3]) end @@ -47,18 +52,23 @@ describe 'Scenes' do it 'can clear Systems' do @scene.add @system2, @system3, @system1 @scene.clear + expect(@system1.scenes.length).to eq(0) + expect(@system2.scenes.length).to eq(0) + expect(@system3.scenes.length).to eq(0) expect(@scene.systems).to eq([]) end - it 'has the correct constant name' do - match = FelFlame::Scenes.new('Match') - expect(FelFlame::Scenes::Match.const_name).to eq('Match') - expect(match.const_name).to eq('Match') - end - it 'can execute Systems in the correct order' do @scene.add @system2, @system3, @system1 @scene.call expect(@cmp.order).to eq([1, 2, 3]) + @cmp.order = [] + @system3.priority = -1 + @scene.call + expect(@cmp.order).to eq([3, 1, 2]) + end + + it 'will return priority when setting priority' do + expect(@scene.priority = 3).to eq(3) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d826a1..797ff0f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause @@ -20,15 +22,15 @@ RSpec.configure do |config| SimpleCov.start do add_filter 'spec' add_filter 'deprecated' - add_group "Ignored Code" do |src_file| + add_group 'Ignored Code' do |src_file| open(src_file.filename).grep(/:nocov:/).any? end SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([ - SimpleCov::Formatter::Console, - SimpleCov::Formatter::HTMLFormatter, - SimpleCov::Formatter::JSONFormatter - #SimpleCovSmallBadge::Formatter - ]) + SimpleCov::Formatter::Console, + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::JSONFormatter + # SimpleCovSmallBadge::Formatter + ]) end # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest @@ -62,55 +64,53 @@ RSpec.configure do |config| # The settings below are suggested to provide a good initial experience # with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed end diff --git a/spec/stage_manager_spec.rb b/spec/stage_manager_spec.rb index ed1125c..16f9080 100644 --- a/spec/stage_manager_spec.rb +++ b/spec/stage_manager_spec.rb @@ -1,40 +1,36 @@ -require 'felflame' +# frozen_string_literal: true -#class EntitiesTest < Minitest::Test +require_relative '../lib/felflame' + +# class EntitiesTest < Minitest::Test describe 'Stage' do before :all do - @component_manager ||= FelFlame::Components.new('TestStage', order: Array.new) - @system2 = FelFlame::Systems.new('StageTest', priority: 50) do - @component_manager.each do |component| - component.order.push 2 - end + @component_manager ||= FelFlame::Components.new('TestStage', order: []) + @system2 = FelFlame::Systems.new('StageTest', priority: 1) do + @component_manager.first.order.push 2 end - @system1 = FelFlame::Systems.new('StageMana', priority: 1) do - @component_manager.each do |component| - component.order.push 1 - end + @system1 = FelFlame::Systems.new('StageMana', priority: 3) do + @component_manager.first.order.push 1 end - @system3 = FelFlame::Systems.new('StageSpell', priority: 100) do - @component_manager.each do |component| - component.order.push 3 - end + @system3 = FelFlame::Systems.new('StageSpell', priority: 2) do + @scene1.add @system1 + @scene2.add @system2 + @scene3.add @system3 + @component_manager.first.order.push 3 end - @scene1 = FelFlame::Scenes.new('TestStage1') - @scene2 = FelFlame::Scenes.new('TestStage2') - @scene3 = FelFlame::Scenes.new('TestStage3') + @scene1 = FelFlame::Scenes.new('TestStage1', priority: 1) + @scene2 = FelFlame::Scenes.new('TestStage2', priority: 2) + @scene3 = FelFlame::Scenes.new('TestStage3', priority: 3) end before :each do @cmp = @component_manager.new - @scene1.add @system1 - @scene2.add @system2 - @scene3.add @system3 end after :each do - FelFlame::Entities.each(&:delete) - @component_manager.each(&:delete) + FelFlame::Entities.reverse_each(&:delete) + @component_manager.reverse_each(&:delete) @scene1.clear @scene2.clear @scene3.clear @@ -43,61 +39,27 @@ describe 'Stage' do it 'can add Scenes' do FelFlame::Stage.add @scene2, @scene1, @scene3 - expect(FelFlame::Stage.scenes).to eq([@scene2, @scene1, @scene3]) - expect(FelFlame::Stage.systems).to eq([@system1, @system2, @system3]) + expect(FelFlame::Stage.scenes).to eq([@scene1, @scene2, @scene3]) end it 'can remove Scenes' do FelFlame::Stage.add @scene1, @scene2, @scene3 FelFlame::Stage.remove @scene1, @scene3 expect(FelFlame::Stage.scenes).to eq([@scene2]) - expect(FelFlame::Stage.systems).to eq([@system2]) end it 'can clear Scenes' do FelFlame::Stage.add @scene1, @scene2, @scene3 FelFlame::Stage.clear expect(FelFlame::Stage.scenes).to eq([]) - expect(FelFlame::Stage.systems).to eq([]) end - it 'can execute Systems in the correct order' do + it 'can call Scenes in correct order' do FelFlame::Stage.add @scene2, @scene1, @scene3 + @scene1.add @system1 + @scene2.add @system2 + @scene3.add @system3 FelFlame::Stage.call - expect(@cmp.order).to eq([1, 2, 3]) - end - - it 'can add Systems to Scenes already added in Stage' do - FelFlame::Stage.add @scene2, @scene1, @scene3 - system2p5 = FelFlame::Systems.new('StageAddingTest', priority: 75) do - @component_manager.each do |component| - component.order.push 2.5 - end - end - @scene2.add system2p5 - @scene3.add system2p5 - FelFlame::Stage.call - expect(@cmp.order).to eq([1,2,2.5,3]) - end - - it 'can remove Systems to Scenes already added in Stage' do - FelFlame::Stage.add @scene2, @scene1, @scene3 - system2p5 = FelFlame::Systems.new('StageAddingTest', priority: 75) do - @component_manager.each do |component| - component.order.push 2.5 - end - end - @scene2.add system2p5 - @scene3.add system2p5 - @scene2.remove @system2 - FelFlame::Stage.call - expect(@cmp.order).to eq([1,2.5,3]) - end - - it 'can have Systems change priority in an existing Stage' do - FelFlame::Stage.add @scene2, @scene1, @scene3 - @system2.priority = 0 - FelFlame::Stage.call - expect(@cmp.order).to eq([2,1,3]) + expect(@component_manager.first.order).to eq([1, 2, 3]) end end diff --git a/spec/system_manager_spec.rb b/spec/system_manager_spec.rb index f9df439..b7177af 100644 --- a/spec/system_manager_spec.rb +++ b/spec/system_manager_spec.rb @@ -1,7 +1,8 @@ -require 'felflame' +# frozen_string_literal: true -describe 'Components' do +require_relative '../lib/felflame' +describe 'Systems' do before :all do @component_manager ||= FelFlame::Components.new('TestSystems', health: 10, whatever: 'imp', mana: 10) @@testitr = 999 @@ -23,10 +24,11 @@ describe 'Components' do end it 'can create a system' do - FelFlame::Systems.new('Test99') do + @@testitr += 1 + sys = FelFlame::Systems.new("Test#{@@testitr}") do 'Works' end - expect(FelFlame::Systems::Test99.call).to eq('Works') + expect(sys.call).to eq('Works') end it 'can be redefined' do @@ -36,26 +38,27 @@ describe 'Components' do expect(@system.call).to eq('very neat') end - it 'can iterate over the sorted systems by priority' do - FelFlame::Systems.new('Test2', priority: 1) {} - FelFlame::Systems.new('Test3', priority: 50) {} - FelFlame::Systems.new('Test4', priority: 7) {} - answer_key = ['Test3', 'Test4', 'Test2'] - test = FelFlame::Systems.each.to_a - # converts the system name to the constant, compares their positions making sure they are sorted - # higher priority should be placed first - expect(test.map(&:const_name).find_index(answer_key[0])).to be <= test.map(&:const_name).find_index(answer_key[1]) - expect(test.map(&:const_name).find_index(answer_key[0])).to be <= test.map(&:const_name).find_index(answer_key[2]) - expect(test.map(&:const_name).find_index(answer_key[1])).to be >= test.map(&:const_name).find_index(answer_key[0]) - expect(test.map(&:const_name).find_index(answer_key[1])).to be <= test.map(&:const_name).find_index(answer_key[2]) - expect(test.map(&:const_name).find_index(answer_key[2])).to be >= test.map(&:const_name).find_index(answer_key[0]) - expect(test.map(&:const_name).find_index(answer_key[2])).to be >= test.map(&:const_name).find_index(answer_key[1]) + it 'responds to array methods' do + expect(FelFlame::Systems.respond_to?(:[])).to be true + expect(FelFlame::Systems.respond_to?(:each)).to be true + FelFlame::Systems.each do |system| + expect(system.respond_to?(:call)).to be true + end + expect(FelFlame::Systems.respond_to?(:filter)).to be true + expect(FelFlame::Systems.respond_to?(:first)).to be true + expect(FelFlame::Systems.respond_to?(:last)).to be true + expect(FelFlame::Systems.respond_to?(:somethingwrong)).to be false + end + + it 'dont respond to missing methods' do + expect { FelFlame::Systems.somethingwrong }.to raise_error(NoMethodError) end it 'can manipulate components' do init1 = 27 init2 = 130 multiple = 3 + iter = 10 first = @component_manager.new(health: init1) second = @component_manager.new(health: init2) @system.redefine do @@ -66,11 +69,21 @@ describe 'Components' do @system.call expect(first.health).to eq(init1 -= multiple) expect(second.health).to eq(init2 -= multiple) - 10.times do + iter.times do @system.call end - expect(first.health).to eq(init1 - (multiple * 10)) - expect(second.health).to eq(init2 - (multiple * 10)) + expect(first.health).to eq(init1 - (multiple * iter)) + expect(second.health).to eq(init2 - (multiple * iter)) + end + + it 'can clear triggers from components and systems' do + @cmp0 = @component_manager.new + @system.trigger_when_added @cmp0 + expect(@cmp0.addition_triggers.length).to eq(1) + expect(@system.addition_triggers.length).to eq(1) + expect(@cmp0.delete).to be true + expect(@cmp0.addition_triggers.length).to eq(0) + expect(@system.addition_triggers.length).to eq(0) end it 'can trigger when a single Component is added' do @@ -219,11 +232,11 @@ describe 'Components' do @entity1 = FelFlame::Entities.new @system.trigger_when_is_changed @cmp0, :whatever @system.trigger_when_is_changed @cmp0, :mana - #expect(@system.attr_triggers).to eq({@cmp0 => [:name, :mana]}) - #expect(@cmp0.attr_triggers).to eq({:name => [@system], :mana => [@system]}) + # expect(@system.attr_triggers).to eq({@cmp0 => [:name, :mana]}) + # expect(@cmp0.attr_triggers).to eq({:name => [@system], :mana => [@system]}) @system.clear_triggers :attr_triggers, :whatever, component_or_manager: @cmp0 - #expect(@system.attr_triggers).to eq({@cmp0 => [:mana]}) - #expect(@cmp0.attr_triggers).to eq({:name => [], :mana => [@system]}) + # expect(@system.attr_triggers).to eq({@cmp0 => [:mana]}) + # expect(@cmp0.attr_triggers).to eq({:name => [], :mana => [@system]}) expect(@cmp0.health).to eq(10) expect(@cmp1.health).to eq(20) @cmp0.whatever = 'something' @@ -326,7 +339,7 @@ describe 'Components' do expect(@cmp0.health).to eq(10) expect(@cmp1.health).to eq(20) end - + it 'can clear addition_trigger, with component' do @cmp0 = @component_manager.new health: 10 @cmp1 = @component_manager.new health: 20 @@ -369,7 +382,7 @@ describe 'Components' do expect(@cmp0.health).to eq(10) expect(@cmp1.health).to eq(20) end - + it 'can clear removal_trigger, with component' do @cmp0 = @component_manager.new health: 10 @cmp1 = @component_manager.new health: 20 |
