Skip to content

Commit

Permalink
Quantity.== test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dkin-om committed Aug 31, 2021
1 parent aa27ff9 commit 86be5f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/src/quantities/base/base_quantity.dart
Expand Up @@ -17,7 +17,7 @@ abstract class BaseQuantity<BQ extends BaseQuantity<BQ>> extends Quantity<BQ> {
Dimension<BQ> get dimension => Dimension.base(baseDimension);

@override
List<Object> get props => <Object>[siValue, baseDimension];
List<Object> get props => <Object>[siValue.toDouble(), baseDimension];

@override
num valueIn(String unitSymbol) =>
Expand Down
4 changes: 2 additions & 2 deletions lib/src/quantities/quantity.dart
Expand Up @@ -24,7 +24,7 @@ abstract class Quantity<Q extends Quantity<Q>> with EquatableMixin {
Dimension<Q> get dimension => _dimension!;

@override
List<Object> get props => <Object>[siValue, dimension];
List<Object> get props => <Object>[siValue.toDouble(), dimension];

/// Returns the value of this quantity in [unitSymbol] unit
num valueIn(String unitSymbol) => dimension.convert(siValue, to: unitSymbol);
Expand All @@ -48,7 +48,7 @@ abstract class Quantity<Q extends Quantity<Q>> with EquatableMixin {
bool operator <=(Q q) => this < q || this == q;

@override
String toString() => '$siValue $dimension';
String toString() => '$siValue ${dimension.siUnitSymbol}';
}

/// Superclass of all quantities' dimensions
Expand Down
17 changes: 13 additions & 4 deletions test/converter_test.dart
Expand Up @@ -12,14 +12,15 @@ import 'quantities/speed_testcase.dart';
void main() {
testAdditions();
testSubtractions();
testEQs();
testConverters();
}

void testAdditions() {
addTestCases.forEach((List<dynamic> testCase) {
final Quantity<dynamic> sum = testCase[0] + testCase[1];

test('sum.siValue -> ${testCase[2]}', () {
test('sum.siValue == ${testCase[2]}', () {
expect(sum.siValue, fractionalCloseTo(testCase[2], 1e-6));
});
});
Expand All @@ -29,12 +30,20 @@ void testSubtractions() {
subtractTestCases.forEach((List<dynamic> testCase) {
final Quantity<dynamic> difference = testCase[0] - testCase[1];

test('difference.siValue -> ${testCase[2]}', () {
test('difference.siValue == ${testCase[2]}', () {
expect(difference.siValue, fractionalCloseTo(testCase[2], 1e-6));
});
});
}

void testEQs() {
eqTestCases.forEach((List<Quantity<dynamic>> testCase) {
test('${testCase[0]} == ${testCase[1]}', () {
expect(testCase[0], equals(testCase[1]));
});
});
}

void testConverters() {
testConverter(lengthConverter);
testConverter(temperatureConverter);
Expand All @@ -50,12 +59,12 @@ void testConverter(Map<String, dynamic> converter) {
final dynamic quantity = testCase['quantity'];

group('$quantity', () {
test('siValue -> ${testCase['siValue']}', () {
test('siValue == ${testCase['siValue']}', () {
expect(quantity.siValue, fractionalCloseTo(testCase['siValue'], 1e-6));
});

testCase['valueIn'].forEach((String unitSymbol, num value) {
test('valueIn($unitSymbol) -> $value', () {
test('valueIn($unitSymbol) == $value', () {
expect(quantity.valueIn(unitSymbol), fractionalCloseTo(value, 1e-6));
});
});
Expand Down
22 changes: 22 additions & 0 deletions test/quantities/quantity_testcase.dart
Expand Up @@ -35,3 +35,25 @@ final List<List<dynamic>> subtractTestCases = <List<dynamic>>[
<dynamic>[Speed(2.997924, 'm/s'), Speed(3.353083, 'mi/h'), 1.498962],
<dynamic>[Speed(1125, 'ft/s'), Speed(644, 'kn'), 11.597778],
];

final List<List<Quantity<dynamic>>> eqTestCases = <List<Quantity<dynamic>>>[
// Length
<Length>[Length(2.5, 'm'), Length(250, 'cm')],
<Length>[Length(31.415926, 'Mm'), Length(3.1415926e17, 'Å')],

// Time
<Time>[Time(100, 'h'), Time(3.6e5, 's')],
<Time>[Time(32, 'y'), Time(11687.76, 'd')],

// Temperature
<Temperature>[Temperature(55, 'K'), Temperature(99, 'R')],
<Temperature>[Temperature(-40, 'F'), Temperature(233.15, 'K')],

// Area
<Area>[Area(0.75, 'km2'), Area(7.5e5, 'm2')],
<Area>[Area(3e4, 'm2'), Area(3, 'ha')],

// Speed
<Speed>[Speed(60, 'km/h'), Speed(50 / 3, 'm/s')],
<Speed>[Speed(2.5, 'm/s'), Speed(9, 'km/h')],
];

0 comments on commit 86be5f3

Please sign in to comment.