p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Add unit tests for noiseDetail()

Open harshiltewari2004 opened this issue 1 month ago • 2 comments

Increasing access

Tests make noiseDetail()'s behaviour explicit and protect it from regressions, so sketches that rely on it keep working across releases. They also give new contributors a clear reference for how the function is meant to behave.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [x] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Build process
  • [x] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [ ] Other (specify if possible)

Feature enhancement details

noiseDetail() (src/math/noise.js:403-410) has no unit tests. test/unit/math/noise.js:19 notes this:

// ALso need test for noiseSeed and noiseDetail

noiseSeed has since been covered (lines 36-60), but noiseDetail hasn't.

The private getters _getNoiseOctaves() and _getNoiseAmpFalloff() (lines 416-425) make the state directly assertable. Proposed tests:

  • sets octaves and falloff when both are given
  • ignores lod <= 0 and falloff <= 0, keeping the previous values
  • when falloff is omitted it defaults to 0.5, so a previously set falloff is reset rather than kept

Since the suite shares one mockP5Prototype and noiseDetail state persists in the module closure, the new tests would restore the defaults (4 octaves, 0.5 falloff) afterwards so the existing noise() tests aren't affected.

I'd also update the comment at line 19, since noiseSeed is now tested.

Checked against main at 3193703. The strands noiseDetail tests in the WebGL/WebGPU shader suites cover state leaking between shaders, not this function.

I'd like to fix this myself if approved.

harshiltewari2004 avatar Oct 06 '26 19:10 harshiltewari2004

@ksen0 I'd like to work on this if it can be assigned to me. The plan is as described above: tests through _getNoiseOctaves() / _getNoiseAmpFalloff(), restoring the defaults afterwards so the existing noise() tests aren't affected.

harshiltewari2004 avatar Oct 13 '26 16:10 harshiltewari2004

Thanks @harshiltewari2004 it's yours!

ksen0 avatar Oct 13 '26 22:10 ksen0