From 296c6ce45dcd6d961123e953b3e06d6297b87fbd Mon Sep 17 00:00:00 2001 From: Sebastian Roth Date: Mon, 2 Jul 2018 11:55:28 +0800 Subject: [PATCH] Add G/KG/Pounds to a new weight comparison class --- lib/src/weight.dart | 30 ++++++++++++++++++++++++++++ lib/units.dart | 3 ++- test/test_all.dart | 2 ++ test/weight_test.dart | 46 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 lib/src/weight.dart create mode 100644 test/weight_test.dart diff --git a/lib/src/weight.dart b/lib/src/weight.dart new file mode 100644 index 0000000..ab52b77 --- /dev/null +++ b/lib/src/weight.dart @@ -0,0 +1,30 @@ +class Weight implements Comparable { + /* + * 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); +} diff --git a/lib/units.dart b/lib/units.dart index 8811b8c..104451a 100644 --- a/lib/units.dart +++ b/lib/units.dart @@ -7,4 +7,5 @@ library units; export 'src/speed.dart'; export 'src/length.dart'; export 'src/angle.dart'; -export 'src/temperature.dart'; \ No newline at end of file +export 'src/temperature.dart'; +export 'src/weight.dart'; \ No newline at end of file diff --git a/test/test_all.dart b/test/test_all.dart index 0a99666..eba6394 100644 --- a/test/test_all.dart +++ b/test/test_all.dart @@ -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); } \ No newline at end of file diff --git a/test/weight_test.dart b/test/weight_test.dart new file mode 100644 index 0000000..a6c452d --- /dev/null +++ b/test/weight_test.dart @@ -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)); + }); + }); +}