mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 10:59:18 +00:00 
			
		
		
		
	Rework tests (#78)
* Return empty query when Without removes all archetypes * Type replace callback as Any * Add test with ChangeTracker * Rename tests files * Fix indentation * Change CI
This commit is contained in:
		
							parent
							
								
									44780f7f7b
								
							
						
					
					
						commit
						134c41014c
					
				
					 2 changed files with 66 additions and 77 deletions
				
			
		
							
								
								
									
										2
									
								
								.github/workflows/ci.yaml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci.yaml
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -23,7 +23,7 @@ jobs:
 | 
				
			||||||
      - name: Run Unit Tests
 | 
					      - name: Run Unit Tests
 | 
				
			||||||
        id: run_tests
 | 
					        id: run_tests
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
          output=$(./luau tests/world.luau)
 | 
					          output=$(./luau test/tests.luau)
 | 
				
			||||||
          echo "$output"
 | 
					          echo "$output"
 | 
				
			||||||
          if [[ "$output" == *"0 fails"* ]]; then
 | 
					          if [[ "$output" == *"0 fails"* ]]; then
 | 
				
			||||||
            echo "Unit Tests Passed"
 | 
					            echo "Unit Tests Passed"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -515,64 +515,50 @@ TEST("world", function()
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	do CASE "should not find any entities"
 | 
						do CASE "should not find any entities"
 | 
				
			||||||
		local world = jecs.World.new()
 | 
					        local world = jecs.World.new()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		local Hello = world:component()
 | 
					        local Hello = world:component()
 | 
				
			||||||
		local Bob = world:component()
 | 
					        local Bob = world:component()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		local helloBob = world:entity()
 | 
					        local helloBob = world:entity()
 | 
				
			||||||
		world:add(helloBob, jecs.pair(Hello, Bob))
 | 
					        world:add(helloBob, ECS_PAIR(Hello, Bob))
 | 
				
			||||||
		world:add(helloBob, Bob)
 | 
					        world:add(helloBob, Bob)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		local withoutCount = 0
 | 
					        local withoutCount = 0
 | 
				
			||||||
		for _ in world
 | 
					        for _ in world:query(ECS_PAIR(Hello, Bob)):without(Bob) do
 | 
				
			||||||
		    :query(jecs.pair(Hello, Bob))
 | 
					            withoutCount += 1
 | 
				
			||||||
			:without(Bob)
 | 
					 | 
				
			||||||
		do
 | 
					 | 
				
			||||||
			withoutCount += 1
 | 
					 | 
				
			||||||
		end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		CHECK(withoutCount == 0)
 | 
					 | 
				
			||||||
	end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	do CASE "should allow change tracking"
 | 
					 | 
				
			||||||
	    local world = jecs.World.new()
 | 
					 | 
				
			||||||
        local Previous = world:component()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        local ChangeTracker = {}
 | 
					 | 
				
			||||||
        ChangeTracker.__index = ChangeTracker
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        function ChangeTracker.new(component)
 | 
					 | 
				
			||||||
            return setmetatable({
 | 
					 | 
				
			||||||
                addedComponents = {}, -- Map<Entity, T>
 | 
					 | 
				
			||||||
                removedComponents = {}, -- Vec<Entity>
 | 
					 | 
				
			||||||
                component = component,
 | 
					 | 
				
			||||||
                previous = jecs.pair(Previous, component),
 | 
					 | 
				
			||||||
                isTrivial = nil,
 | 
					 | 
				
			||||||
            }, ChangeTracker)
 | 
					 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        local function shallowEq(a, b)
 | 
					        CHECK(withoutCount == 0)
 | 
				
			||||||
            for k, v in a do
 | 
					    end
 | 
				
			||||||
                if b[k] ~= v then
 | 
					end)
 | 
				
			||||||
                    return false
 | 
					
 | 
				
			||||||
                end
 | 
					
 | 
				
			||||||
 | 
					TEST("changetracker", function()
 | 
				
			||||||
 | 
					    local world = jecs.World.new()
 | 
				
			||||||
 | 
					    local Previous = world:component()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    local function shallowEq(a, b)
 | 
				
			||||||
 | 
					        for k, v in a do
 | 
				
			||||||
 | 
					            if b[k] ~= v then
 | 
				
			||||||
 | 
					                return false
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
            return true
 | 
					 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					        return true
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        function ChangeTracker.track(tracker, world, fn)
 | 
					    local function ChangeTracker(world, component)
 | 
				
			||||||
 | 
					        local addedComponents = {}
 | 
				
			||||||
 | 
					        local removedComponents = {}
 | 
				
			||||||
 | 
					        local previous = jecs.pair(Previous, component)
 | 
				
			||||||
 | 
					        local isTrivial = nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        local function track(fn)
 | 
				
			||||||
            local added = false
 | 
					            local added = false
 | 
				
			||||||
            local removed = false
 | 
					            local removed = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            local addedComponents = tracker.addedComponents
 | 
					 | 
				
			||||||
            local removedComponents = tracker.removedComponents
 | 
					 | 
				
			||||||
            local component = tracker.component
 | 
					 | 
				
			||||||
            local previous = tracker.previous
 | 
					 | 
				
			||||||
            local isTrivial = tracker.isTrivial
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            local changes = {}
 | 
					            local changes = {}
 | 
				
			||||||
            function changes:added()
 | 
					            function changes.added()
 | 
				
			||||||
                added = true
 | 
					                added = true
 | 
				
			||||||
                local q = world:query(component):without(previous)
 | 
					                local q = world:query(component):without(previous)
 | 
				
			||||||
                return function()
 | 
					                return function()
 | 
				
			||||||
| 
						 | 
					@ -583,7 +569,6 @@ TEST("world", function()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if isTrivial == nil then
 | 
					                    if isTrivial == nil then
 | 
				
			||||||
                        isTrivial = typeof(data) ~= "table"
 | 
					                        isTrivial = typeof(data) ~= "table"
 | 
				
			||||||
                        tracker.isTrivial = isTrivial
 | 
					 | 
				
			||||||
                    end
 | 
					                    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if not isTrivial then
 | 
					                    if not isTrivial then
 | 
				
			||||||
| 
						 | 
					@ -595,7 +580,7 @@ TEST("world", function()
 | 
				
			||||||
                end
 | 
					                end
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            function changes:changed()
 | 
					            function changes.changed()
 | 
				
			||||||
                local q = world:query(component, previous)
 | 
					                local q = world:query(component, previous)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return function()
 | 
					                return function()
 | 
				
			||||||
| 
						 | 
					@ -616,21 +601,20 @@ TEST("world", function()
 | 
				
			||||||
                        id, new, old = q:next()
 | 
					                        id, new, old = q:next()
 | 
				
			||||||
                    end
 | 
					                    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    print("nil?", id)
 | 
					 | 
				
			||||||
                    addedComponents[id] = new
 | 
					                    addedComponents[id] = new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    return id, old, new
 | 
					                    return id, old, new
 | 
				
			||||||
                end
 | 
					                end
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            function changes:removed()
 | 
					            function changes.removed()
 | 
				
			||||||
                removed = true
 | 
					                removed = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                local q = world:query(tracker.previous):without(tracker.component)
 | 
					                local q = world:query(previous):without(component)
 | 
				
			||||||
                return function()
 | 
					                return function()
 | 
				
			||||||
                    local id = q:next()
 | 
					                    local id = q:next()
 | 
				
			||||||
                    if id then
 | 
					                    if id then
 | 
				
			||||||
                        table.insert(removedComponents, id)
 | 
					                    table.insert(removedComponents, id)
 | 
				
			||||||
                    end
 | 
					                    end
 | 
				
			||||||
                    return id
 | 
					                    return id
 | 
				
			||||||
                end
 | 
					                end
 | 
				
			||||||
| 
						 | 
					@ -638,12 +622,12 @@ TEST("world", function()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            fn(changes)
 | 
					            fn(changes)
 | 
				
			||||||
            if not added then
 | 
					            if not added then
 | 
				
			||||||
                for _ in changes:added() do
 | 
					                for _ in changes.added() do
 | 
				
			||||||
                end
 | 
					                end
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if not removed then
 | 
					            if not removed then
 | 
				
			||||||
                for _ in changes:removed() do
 | 
					                for _ in changes.removed() do
 | 
				
			||||||
                end
 | 
					                end
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -656,16 +640,19 @@ TEST("world", function()
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            track = track
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						do CASE "should allow change tracking"
 | 
				
			||||||
        local Test = world:component()
 | 
					        local Test = world:component()
 | 
				
			||||||
        local TestTracker = ChangeTracker.new(Test)
 | 
					        local TestTracker = ChangeTracker(world, Test)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		local e = world:entity()
 | 
							local e = world:entity()
 | 
				
			||||||
		world:set(e, Test, { foo = 11 })
 | 
							world:set(e, Test, { foo = 11 })
 | 
				
			||||||
    	for e, test in world:query(Test) do
 | 
					 | 
				
			||||||
    	    test.foo = test.foo + 1
 | 
					 | 
				
			||||||
    	end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        TestTracker:track(world, function(changes)
 | 
					        TestTracker.track(function(changes)
 | 
				
			||||||
            local added = 0
 | 
					            local added = 0
 | 
				
			||||||
            local changed = 0
 | 
					            local changed = 0
 | 
				
			||||||
            local removed = 0
 | 
					            local removed = 0
 | 
				
			||||||
| 
						 | 
					@ -684,22 +671,24 @@ TEST("world", function()
 | 
				
			||||||
        end)
 | 
					        end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for e, test in world:query(Test) do
 | 
					        for e, test in world:query(Test) do
 | 
				
			||||||
       	    test.foo = test.foo + 1
 | 
					    	    test.foo = test.foo + 1
 | 
				
			||||||
       	end
 | 
					     	end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        TestTracker:track(world, function(changes)
 | 
					        TestTracker.track(function(changes)
 | 
				
			||||||
            local added = 0
 | 
					            local added = 0
 | 
				
			||||||
            local changed = 0
 | 
					            local changed = 0
 | 
				
			||||||
            local removed = 0
 | 
					            local removed = 0
 | 
				
			||||||
      		for e, data in changes.added() do
 | 
					
 | 
				
			||||||
 | 
					    		for e, data in changes.added() do
 | 
				
			||||||
                added+=1
 | 
					                added+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
      		for e, old, new in changes.changed() do
 | 
					    		for e, old, new in changes.changed() do
 | 
				
			||||||
                changed+=1
 | 
					                changed+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
      		for e in changes.removed() do
 | 
					    		for e in changes.removed() do
 | 
				
			||||||
                removed+=1
 | 
					                removed+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            CHECK(added == 0)
 | 
					            CHECK(added == 0)
 | 
				
			||||||
            CHECK(changed == 1)
 | 
					            CHECK(changed == 1)
 | 
				
			||||||
            CHECK(removed == 0)
 | 
					            CHECK(removed == 0)
 | 
				
			||||||
| 
						 | 
					@ -707,19 +696,19 @@ TEST("world", function()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        world:remove(e, Test)
 | 
					        world:remove(e, Test)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        TestTracker:track(world, function(changes)
 | 
					        TestTracker.track(function(changes)
 | 
				
			||||||
            local added = 0
 | 
					            local added = 0
 | 
				
			||||||
            local changed = 0
 | 
					            local changed = 0
 | 
				
			||||||
            local removed = 0
 | 
					            local removed = 0
 | 
				
			||||||
      		for e, data in changes.added() do
 | 
					       	    for e, data in changes.added() do
 | 
				
			||||||
                added+=1
 | 
					                added+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
      		for e, old, new in changes.changed() do
 | 
					    		for e, old, new in changes.changed() do
 | 
				
			||||||
                changed+=1
 | 
					                changed+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
      		for e in changes.removed() do
 | 
					    		for e in changes.removed() do
 | 
				
			||||||
                removed+=1
 | 
					                removed+=1
 | 
				
			||||||
      		end
 | 
					    		end
 | 
				
			||||||
            CHECK(added == 0)
 | 
					            CHECK(added == 0)
 | 
				
			||||||
            CHECK(changed == 0)
 | 
					            CHECK(changed == 0)
 | 
				
			||||||
            CHECK(removed == 1)
 | 
					            CHECK(removed == 1)
 | 
				
			||||||
		Loading…
	
		Reference in a new issue