Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/src/weight.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Weight implements Comparable<Weight> {
/*
* The value of this weight object in grams.
*/
final double _grams;

/// pounds to kg
static const double _pounds_to_kg = 0.45359237;

Weight.fromPounds({double value: 0.0}) : _grams = (value * _pounds_to_kg) * 1000;
Weight.fromGrams({double value: 0.0}) : _grams = value;
Weight.fromKilograms({double value: 0.0}) : _grams = value * 1000;

double get inPounds => _returnValue((_grams / 1000) / _pounds_to_kg);
double get inGrams => _returnValue(_grams);
double get inKiloGrams => _returnValue(_grams / 1000);

double _returnValue(num value) => double.parse(value.toString());

/**
* Compares this Weight to [other], returning zero if the values are equal.
*
* Returns a negative integer if this `Weight` is less than
* [other], or a positive integer if it is heavier.
*
* A negative `Weight` is always considered less than a positive one.
*
*/
int compareTo(Weight other) => _grams.compareTo(other._grams);
}
3 changes: 2 additions & 1 deletion lib/units.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ library units;
export 'src/speed.dart';
export 'src/length.dart';
export 'src/angle.dart';
export 'src/temperature.dart';
export 'src/temperature.dart';
export 'src/weight.dart';
2 changes: 2 additions & 0 deletions test/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import 'angle_test.dart' as angle;
import 'length_test.dart' as length;
import 'speed_test.dart' as speed;
import 'temperature_test.dart' as temperature;
import 'weight_test.dart' as weight;

void main() {
group('angle', angle.main);
group('length', length.main);
group('speed', speed.main);
group('temperature', temperature.main);
group('weight', weight.main);
}
46 changes: 46 additions & 0 deletions test/weight_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:units/units.dart';
import 'package:test/test.dart';

void main() {
group('Weight tests', () {
test('Having -1.0 should match return', () {
expect(new Weight.fromGrams(value: -1.0).inKiloGrams, equals(-0.001));
expect(new Weight.fromGrams(value: -1.0).inPounds,
equals(-0.002204622621848776));
});

test('Having 1 gram should match return', () {
expect(new Weight.fromGrams(value: 1.0).inGrams, equals(1.0));
expect(new Weight.fromGrams(value: 1.0).inKiloGrams, equals(0.001));
expect(new Weight.fromGrams(value: 1.0).inPounds, equals(0.002204622621848776));
});

test('Having 1200 gram should match return', () {
expect(new Weight.fromGrams(value: 1200.0).inGrams, equals(1200));
expect(new Weight.fromGrams(value: 1200.0).inKiloGrams, equals(1.2));
expect(new Weight.fromGrams(value: 1200.0).inPounds, equals(2.6455471462185307));
});
});

group('Weight comparison tests', () {
Weight weight1;
Weight weight2;

setUp(() {
weight1 = Weight.fromGrams(value: 1200.0);
weight2 = Weight.fromPounds(value: 3.0); // ~1.3kg
});

test('Comparing same weight, should return 0 (same weight)', () {
expect(weight1.compareTo(weight1), equals(0));
});

test('Comparing weight, weight 1 is lighter than weight 2', () {
expect(weight1.compareTo(weight2), lessThan(0));
});

test('Comparing weight, weight 2 is heavier than weight 1', () {
expect(weight2.compareTo(weight1), greaterThan(0));
});
});
}