-
Notifications
You must be signed in to change notification settings - Fork 33
test(sprite): add tests for texture reuse, mapping, and missing frames #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
il-sairamg
wants to merge
2
commits into
lightning-js:dev
Choose a base branch
from
il-sairamg:test-coverage/improve-sprite-tests
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import test from 'tape' | ||
| import Sprite from './Sprite.js' | ||
| import symbols from '../lib/symbols.js' | ||
| import { initLog } from '../lib/log.js' | ||
| import { stage, renderer } from '../launch.js' | ||
| import { renderer as engineRenderer } from '../engines/L3/launch.js' | ||
| import element from '../engines/L3/element.js' | ||
| import { EventEmitter } from 'node:events' | ||
|
|
||
| initLog() | ||
|
|
||
| stage.element = element | ||
| Object.assign(renderer, engineRenderer) | ||
| engineRenderer.createNode = () => new EventEmitter() | ||
|
|
||
| function createSprite() { | ||
| return Sprite()({}, { node: { width: 1920, height: 1080 } }) | ||
| } | ||
|
|
||
| test('Sprite - Type', (assert) => { | ||
| assert.equal(typeof Sprite, 'function', 'Sprite should be a function') | ||
| assert.equal(typeof Sprite(), 'function', 'Sprite() should return a function') | ||
| assert.end() | ||
| }) | ||
|
|
||
| test('Sprite - Initialization', (assert) => { | ||
| const sprite = createSprite() | ||
| const props = sprite[symbols.props] | ||
| const state = sprite[symbols.state] | ||
|
|
||
| assert.equal(props.image, undefined, 'image prop should be undefined initially') | ||
| assert.equal(state.spriteTexture, null, 'spriteTexture should be null initially') | ||
| assert.equal(state.currentSrc, null, 'currentSrc should be null initially') | ||
| assert.end() | ||
| }) | ||
|
|
||
| test('Sprite - Texture returns null for invalid inputs', (assert) => { | ||
| const sprite = createSprite() | ||
|
|
||
| sprite[symbols.props].image = undefined | ||
| assert.equal(sprite.texture, null, 'texture should be null when image is undefined') | ||
|
|
||
| sprite[symbols.props].image = null | ||
| assert.equal(sprite.texture, null, 'texture should be null when image is null') | ||
|
|
||
| sprite[symbols.props].image = 'test.png' | ||
| const originalCreateTexture = renderer.createTexture | ||
| delete renderer.createTexture | ||
| assert.equal( | ||
| sprite.texture, | ||
| null, | ||
| 'texture should be null when renderer.createTexture is missing' | ||
| ) | ||
|
|
||
| renderer.createTexture = originalCreateTexture | ||
| assert.end() | ||
| }) | ||
|
|
||
| test('Sprite - Texture creates and reuses ImageTexture', (assert) => { | ||
| const mockTex = {} | ||
| let calls = 0 | ||
| const original = renderer.createTexture | ||
| try { | ||
| renderer.createTexture = () => (calls++, mockTex) | ||
|
|
||
| const sprite = createSprite() | ||
| sprite[symbols.props].image = 'test.png' | ||
| assert.ok(sprite.texture !== null, 'texture should not be null when image is set') | ||
| assert.equal( | ||
| sprite[symbols.state].currentSrc, | ||
| 'test.png', | ||
| 'currentSrc should be set to image path' | ||
| ) | ||
| assert.equal(calls, 1, 'createTexture should be called once for first image') | ||
|
|
||
| sprite[symbols.props].image = 'test2.png' | ||
| sprite.texture | ||
| assert.equal(calls, 2, 'createTexture should be called again when image changes') | ||
| } finally { | ||
| renderer.createTexture = original | ||
| } | ||
| assert.end() | ||
| }) | ||
|
|
||
| test('Sprite - Texture creates SubTexture with map and frame', (assert) => { | ||
| const imgTex = {} | ||
| const subTex = {} | ||
| let imgCalls = 0 | ||
| const original = renderer.createTexture | ||
| try { | ||
| renderer.createTexture = (type) => (type === 'ImageTexture' ? (imgCalls++, imgTex) : subTex) | ||
|
|
||
| const sprite = createSprite() | ||
| sprite[symbols.props].image = 'sheet.png' | ||
| sprite.texture | ||
| const baseTex = sprite[symbols.state].spriteTexture | ||
|
|
||
| // map.frames, map.frame1, missing w/h, manual frame object | ||
| const cases = [ | ||
| { | ||
| map: { frames: { f1: { x: 10, y: 20, w: 50, h: 60 } }, defaults: { w: 100, h: 100 } }, | ||
| frame: 'f1', | ||
| }, | ||
| { map: { f1: { x: 5, y: 10, w: 30, h: 40 } }, frame: 'f1' }, | ||
| { | ||
| map: { frames: { f1: { x: 10, y: 20, w: 50 } }, defaults: { w: 100, h: 100 } }, | ||
| frame: 'f1', | ||
| }, | ||
| { map: null, frame: { x: 15, y: 25, w: 35, h: 45 } }, | ||
| ] | ||
|
|
||
| cases.forEach(({ map, frame }) => { | ||
| sprite[symbols.props].map = map | ||
| sprite[symbols.props].frame = frame | ||
| assert.equal( | ||
| sprite.texture, | ||
| subTex, | ||
| 'texture should be SubTexture when map and frame are set' | ||
| ) | ||
| assert.equal( | ||
| sprite[symbols.state].currentSrc, | ||
| 'sheet.png', | ||
| 'currentSrc should remain unchanged' | ||
| ) | ||
| assert.equal(imgCalls, 1, 'ImageTexture should be created only once') | ||
| assert.equal(sprite[symbols.state].spriteTexture, baseTex, 'spriteTexture should be reused') | ||
| }) | ||
| } finally { | ||
| renderer.createTexture = original | ||
| } | ||
| assert.end() | ||
| }) | ||
|
|
||
| test('Sprite - Texture returns spriteTexture when no frame', (assert) => { | ||
| const mockTex = {} | ||
| const mockSubTex = {} | ||
| let subTexCalls = 0 | ||
| const original = renderer.createTexture | ||
| try { | ||
| renderer.createTexture = (type) => { | ||
| if (type === 'ImageTexture') return mockTex | ||
| if (type === 'SubTexture') { | ||
| subTexCalls++ | ||
| return mockSubTex | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| const sprite = createSprite() | ||
| sprite[symbols.props].image = 'test.png' | ||
|
|
||
| // Case 1: No frame used → must NOT create SubTexture | ||
| sprite[symbols.props].map = null | ||
| sprite[symbols.props].frame = null | ||
| assert.ok(sprite.texture !== null, 'texture should not be null when no frame is set') | ||
| assert.equal(subTexCalls, 0, 'should not create SubTexture when no frame') | ||
|
|
||
| // Case 2: Invalid frame in map.frames → should NOT create SubTexture | ||
| sprite[symbols.props].map = { frames: { f1: { x: 10, y: 20, w: 50, h: 60 } } } | ||
| sprite[symbols.props].frame = 'nonexistent' | ||
| sprite.texture // trigger computation | ||
| assert.equal( | ||
| subTexCalls, | ||
| 0, | ||
| 'should NOT create SubTexture for nonexistent frame in map.frames structure' | ||
| ) | ||
|
|
||
| // Case 3: Invalid frame in direct map → should NOT create additional SubTexture | ||
| sprite[symbols.props].map = { f1: { x: 10, y: 20, w: 50, h: 60 } } | ||
| sprite[symbols.props].frame = 'nonexistent' | ||
| const finalTexture = sprite.texture | ||
| assert.ok(finalTexture !== null, 'texture should not be null') | ||
| assert.equal( | ||
| subTexCalls, | ||
| 0, | ||
| 'should NOT create additional SubTexture for nonexistent frame in direct map' | ||
| ) | ||
| } finally { | ||
| renderer.createTexture = original | ||
| } | ||
| assert.end() | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it necessary for this condition to be nested? or can it be included in the condition one level up?