From 1502d72d62e4932f617ed1c9558b9fab4a2d252d Mon Sep 17 00:00:00 2001 From: chrisklus Date: Mon, 7 Jan 2019 15:38:37 -0700 Subject: [PATCH] Add RangeWithValue tests, see https://github.com/phetsims/dot/issues/77 --- js/RangeTests.js | 2 +- js/RangeWithValueTests.js | 62 +++++++++++++++++++++++++++++++++++++++ js/dot-tests.js | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 js/RangeWithValueTests.js diff --git a/js/RangeTests.js b/js/RangeTests.js index 5b1dadf..d3f66f4 100644 --- a/js/RangeTests.js +++ b/js/RangeTests.js @@ -1,4 +1,4 @@ -// Copyright 2018, University of Colorado Boulder +// Copyright 2019, University of Colorado Boulder /** * Range tests diff --git a/js/RangeWithValueTests.js b/js/RangeWithValueTests.js new file mode 100644 index 0000000..ae8e813 --- /dev/null +++ b/js/RangeWithValueTests.js @@ -0,0 +1,62 @@ +// Copyright 2019, University of Colorado Boulder + +/** + * RangeWithValue tests + * + * @author Chris Klusendorf (PhET Interactive Simulations) + */ + +define( require => { + 'use strict'; + + // modules + const RangeWithValue = require( 'DOT/RangeWithValue' ); + + QUnit.module( 'RangeWithValue' ); + + QUnit.test( 'constructor', assert => { + assert.ok( new RangeWithValue( 1, 10, 2 ), 'valid range with value' ); + assert.ok( new RangeWithValue( 1, 10, 10 ), 'valid range with value' ); + window.assert && assert.throws( () => { + new RangeWithValue( 1, 10, 11 ); // eslint-disable-line + }, 'invalid range with value, default value is out of range' ); + window.assert && assert.throws( () => { + new RangeWithValue( 1, 10 ); // eslint-disable-line + }, 'invalid range with value, default value is required' ); + } ); + + QUnit.test( 'methods', assert => { + + // test a valid and invalid setMin() + const rangeWithValue1 = new RangeWithValue( 1, 10, 3 ); + rangeWithValue1.setMin( 2 ); + assert.equal( rangeWithValue1.min, 2, 'setMin() succeeds when min < defaultValue < max' ); + window.assert && assert.throws( () => { rangeWithValue1.setMin( 4 ); }, 'setMin() fails when defaultValue < min < max' ); + + // test a valid and invalid setMax() + const rangeWithValue2 = new RangeWithValue( 1, 10, 8 ); + rangeWithValue2.setMax( 9 ); + assert.equal( rangeWithValue2.max, 9, 'setMax() succeeds when max > defaultValue > min' ); + window.assert && assert.throws( () => { rangeWithValue2.setMax( 7 ); }, 'setMax() fails when defaultValue > max > min' ); + + // test a true and false equals() + const rangeWithValue3 = new RangeWithValue( 1, 10, 5 ); + assert.ok( + rangeWithValue3.equals( new RangeWithValue( 1, 10, 5 ) ), + 'equals() succeeds when rangeWithValue1 === rangeWithValue2' + ); + assert.notOk( + rangeWithValue3.equals( new RangeWithValue( 1, 10, 6 ) ), + 'equals() fails when rangeWithValue1 !== rangeWithValue2' + ); + + // test a valid and invalid setMinMax() + const rangeWithValue5 = new RangeWithValue( 1, 10, 5 ); + rangeWithValue5.setMinMax( 2, 9 ); + assert.ok( rangeWithValue5.equals( new RangeWithValue( 2, 9, 5 ) ), 'setMinMax() succeeds when min < defaultValue < max' ); + window.assert && assert.throws( () => { + rangeWithValue5.setMinMax( 3, 4 ); + }, 'setMinMax() fails when default value is out of range' + ); + } ); +} ); \ No newline at end of file diff --git a/js/dot-tests.js b/js/dot-tests.js index 5ff3b04..f6c3a9e 100644 --- a/js/dot-tests.js +++ b/js/dot-tests.js @@ -17,6 +17,7 @@ define( function( require ) { require( 'DOT/MatrixOps3Tests' ); require( 'DOT/OpenRangeTests' ); require( 'DOT/RangeTests' ); + require( 'DOT/RangeWithValueTests' ); require( 'DOT/UtilTests' ); require( 'DOT/Transform3Tests' ); require( 'DOT/LinearFunctionTests' );