From e994c00e46d89afc964b3af039d8a035753fbe6e Mon Sep 17 00:00:00 2001 From: Alberto Date: Thu, 11 May 2023 18:17:21 +0200 Subject: [PATCH] chore: minor changes --- example/bin/fraction_example.dart | 4 +- example/lib/src/analyzer.dart | 2 +- example/lib/src/analyzer/analyzer.dart | 13 ----- .../lib/src/analyzer/fraction_analyzer.dart | 32 ------------ .../src/analyzer/mixed_fraction_analyzer.dart | 29 ----------- example/lib/src/console.dart | 49 ------------------- example/lib/src/mixed_fraction_analyzer.dart | 2 +- .../test/analyzer/fraction_analyzer_test.dart | 42 ---------------- .../mixed_fraction_analyzer_test.dart | 43 ---------------- lib/src/types/mixed.dart | 12 +---- lib/src/types/standard.dart | 9 +--- test/types/mixed_test.dart | 8 ++- test/types/standard_test.dart | 2 +- 13 files changed, 14 insertions(+), 233 deletions(-) delete mode 100644 example/lib/src/analyzer/analyzer.dart delete mode 100644 example/lib/src/analyzer/fraction_analyzer.dart delete mode 100644 example/lib/src/analyzer/mixed_fraction_analyzer.dart delete mode 100644 example/lib/src/console.dart delete mode 100644 example/test/analyzer/fraction_analyzer_test.dart delete mode 100644 example/test/analyzer/mixed_fraction_analyzer_test.dart diff --git a/example/bin/fraction_example.dart b/example/bin/fraction_example.dart index b681198..8e8582d 100644 --- a/example/bin/fraction_example.dart +++ b/example/bin/fraction_example.dart @@ -3,7 +3,7 @@ import 'dart:io'; import 'package:fraction_example/fraction_example.dart'; -/// The app's main entrypoint. +/// The application's main entrypoint. void main() { stdout.encoding = utf8; @@ -38,7 +38,7 @@ void main() { } // To keep the console 'awake'. This is very useful on Windows! - stdout.write('Press return to exit...'); + stdout.write('Press any key to exit...'); // ignore: avoid-ignoring-return-values stdin.readLineSync(); diff --git a/example/lib/src/analyzer.dart b/example/lib/src/analyzer.dart index b542c93..5f05964 100644 --- a/example/lib/src/analyzer.dart +++ b/example/lib/src/analyzer.dart @@ -3,7 +3,7 @@ import 'package:fraction/fraction.dart'; /// This base class is used to analyze an [input] string. /// /// The [analyze] method tries to convert [input] into a [Fraction] or -/// [MixedFraction] object. It prints various properties of the fraction. +/// [MixedFraction] object and prints various properties to the console. abstract base class RationalAnalyzer { /// The fraction or mixed fraction input. final String input; diff --git a/example/lib/src/analyzer/analyzer.dart b/example/lib/src/analyzer/analyzer.dart deleted file mode 100644 index bd83a5e..0000000 --- a/example/lib/src/analyzer/analyzer.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'dart:io'; - -/// Parses the input received via [stdin] and analyzes the fraction. -abstract class RationalAnalyzer { - /// The raw input received via [stdin]. - final String input; - - /// Creates a [RationalAnalyzer] instance. - const RationalAnalyzer(this.input); - - /// Parses the [input] and returns the analysis report as a [String]. - String analyze(); -} diff --git a/example/lib/src/analyzer/fraction_analyzer.dart b/example/lib/src/analyzer/fraction_analyzer.dart deleted file mode 100644 index 0034f3e..0000000 --- a/example/lib/src/analyzer/fraction_analyzer.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:fraction/fraction.dart'; -import 'package:fraction_example/src/analyzer/analyzer.dart'; - -/// Tries to convert a [String] into a [Fraction] -class FractionAnalyzer extends RationalAnalyzer { - /// Creates a [FractionAnalyzer] object. - const FractionAnalyzer({required String input}) : super(input); - - @override - String analyze() { - try { - // Parsing the fraction - final fraction = Fraction.fromString(input); - - // Used to incrementally build the results - final buffer = StringBuffer() - ..writeln('\n > ==================== < \n') - ..writeln('Fraction = $fraction') - ..writeln('Decimal: ${fraction.toDouble()}') - ..writeln('Mixed: ${fraction.toMixedFraction()}\n') - ..writeln('Inverse: ${fraction.inverse()}') - ..writeln('Negate: ${fraction.negate()}') - ..writeln('Reduced: ${fraction.reduce()}\n') - ..writeln('Is proper? ${fraction.isProper}') - ..writeln('Is improper? ${fraction.isImproper}'); - - return buffer.toString(); - } on Exception { - return "\n - The fraction you've entered is not correct!\n"; - } - } -} diff --git a/example/lib/src/analyzer/mixed_fraction_analyzer.dart b/example/lib/src/analyzer/mixed_fraction_analyzer.dart deleted file mode 100644 index cb64407..0000000 --- a/example/lib/src/analyzer/mixed_fraction_analyzer.dart +++ /dev/null @@ -1,29 +0,0 @@ -import 'package:fraction/fraction.dart'; -import 'package:fraction_example/src/analyzer/analyzer.dart'; - -/// Tries to convert a [String] into a [MixedFraction] -class MixedFractionAnalyzer extends RationalAnalyzer { - /// Creates a [MixedFractionAnalyzer] object. - const MixedFractionAnalyzer({required String input}) : super(input); - - @override - String analyze() { - try { - // Parsing the fraction - final mixedFraction = MixedFraction.fromString(input); - - // Used to incrementally build the results - final buffer = StringBuffer() - ..writeln('\n > ==================== < \n') - ..writeln('Mixed fraction = $mixedFraction') - ..writeln('Decimal: ${mixedFraction.toDouble()}') - ..writeln('Fraction: ${mixedFraction.toFraction()}\n') - ..writeln('Negate: ${mixedFraction.negate()}') - ..writeln('Reduced: ${mixedFraction.reduce()}'); - - return buffer.toString(); - } on Exception { - return "\n - The mixed fraction you've entered is not correct!\n"; - } - } -} diff --git a/example/lib/src/console.dart b/example/lib/src/console.dart deleted file mode 100644 index 3977daf..0000000 --- a/example/lib/src/console.dart +++ /dev/null @@ -1,49 +0,0 @@ -import 'dart:convert'; -import 'dart:io'; - -import 'package:fraction_example/src/analyzer/fraction_analyzer.dart'; -import 'package:fraction_example/src/analyzer/mixed_fraction_analyzer.dart'; - -/// This class uses standard I/O to process user inputs and prints results to -/// the console. -class Console { - /// Creates a new [Console] instance. - const Console(); - - /// Reads the input and analyzes a fraction or a mixed fraction. - void run() { - stdout.encoding = utf8; - - // Welcome message - stdout - ..writeln('What do you want to analyze?\n') - ..writeln(' 1) Fractions') - ..writeln(' 2) Mixed fractions\n') - ..write('Enter your choice (1 or 2): '); - - final input = stdin.readLineSync() ?? ''; - - if (input == '1') { - stdout.write('Enter the fraction: '); - - final fraction = stdin.readLineSync() ?? ''; - final analyzer = FractionAnalyzer(input: fraction); - - stdout.writeln(analyzer.analyze()); - } else if (input == '2') { - stdout.write('Enter the mixed fraction: '); - - final fraction = stdin.readLineSync() ?? ''; - final analyzer = MixedFractionAnalyzer(input: fraction); - - stdout.writeln(analyzer.analyze()); - } else { - stdout.writeln("\n - You've entered a wrong input!\n"); - } - - // To keep the console 'awake'. This is very useful on Windows! - stdout.write('Press return to exit...'); - // ignore: avoid-ignoring-return-values - stdin.readLineSync(); - } -} diff --git a/example/lib/src/mixed_fraction_analyzer.dart b/example/lib/src/mixed_fraction_analyzer.dart index 4dcd9aa..79fcbc1 100644 --- a/example/lib/src/mixed_fraction_analyzer.dart +++ b/example/lib/src/mixed_fraction_analyzer.dart @@ -1,7 +1,7 @@ import 'package:fraction/fraction.dart'; import 'package:fraction_example/src/analyzer.dart'; -/// Tries to convert a [String] into a [MixedFraction] +/// Tries to convert a [String] into a [MixedFraction]. final class MixedFractionAnalyzer extends RationalAnalyzer { /// Creates a [MixedFractionAnalyzer] object. const MixedFractionAnalyzer({required String input}) : super(input); diff --git a/example/test/analyzer/fraction_analyzer_test.dart b/example/test/analyzer/fraction_analyzer_test.dart deleted file mode 100644 index 1856832..0000000 --- a/example/test/analyzer/fraction_analyzer_test.dart +++ /dev/null @@ -1,42 +0,0 @@ -import 'package:fraction/fraction.dart'; -import 'package:fraction_example/src/analyzer/fraction_analyzer.dart'; -import 'package:test/test.dart'; - -void main() { - group("Testing the 'FractionAnalyzer' class", () { - test('Making sure that valid fractions can be analyzed', () { - const analyzer = FractionAnalyzer( - input: '8/12', - ); - - // Building the expected result - final fraction = Fraction(8, 12); - final buffer = StringBuffer() - ..writeln('\n > ==================== < \n') - ..writeln('Fraction = $fraction') - ..writeln('Decimal: ${fraction.toDouble()}') - ..writeln('Mixed: ${fraction.toMixedFraction()}\n') - ..writeln('Inverse: ${fraction.inverse()}') - ..writeln('Negate: ${fraction.negate()}') - ..writeln('Reduced: ${fraction.reduce()}\n') - ..writeln('Is proper? ${fraction.isProper}') - ..writeln('Is improper? ${fraction.isImproper}'); - - expect( - analyzer.analyze(), - equals(buffer.toString()), - ); - }); - - test('Making sure that invalid fractions report an error', () { - const analyzer = FractionAnalyzer( - input: '', - ); - - expect( - analyzer.analyze(), - equals("\n - The fraction you've entered is not correct!\n"), - ); - }); - }); -} diff --git a/example/test/analyzer/mixed_fraction_analyzer_test.dart b/example/test/analyzer/mixed_fraction_analyzer_test.dart deleted file mode 100644 index 7d45216..0000000 --- a/example/test/analyzer/mixed_fraction_analyzer_test.dart +++ /dev/null @@ -1,43 +0,0 @@ -import 'package:fraction/fraction.dart'; -import 'package:fraction_example/src/analyzer/mixed_fraction_analyzer.dart'; -import 'package:test/test.dart'; - -void main() { - group("Testing the 'MixedFractionAnalyzer' class", () { - test('Making sure that valid fractions can be analyzed', () { - const analyzer = MixedFractionAnalyzer( - input: '6 7/3', - ); - - // Building the expected result - final mixedFraction = MixedFraction( - whole: 6, - numerator: 7, - denominator: 3, - ); - final buffer = StringBuffer() - ..writeln('\n > ==================== < \n') - ..writeln('Mixed fraction = $mixedFraction') - ..writeln('Decimal: ${mixedFraction.toDouble()}') - ..writeln('Fraction: ${mixedFraction.toFraction()}\n') - ..writeln('Negate: ${mixedFraction.negate()}') - ..writeln('Reduced: ${mixedFraction.reduce()}'); - - expect( - analyzer.analyze(), - equals(buffer.toString()), - ); - }); - - test('Making sure that invalid fractions report an error', () { - const analyzer = MixedFractionAnalyzer( - input: '', - ); - - expect( - analyzer.analyze(), - equals("\n - The mixed fraction you've entered is not correct!\n"), - ); - }); - }); -} diff --git a/lib/src/types/mixed.dart b/lib/src/types/mixed.dart index 281d3c2..2cc9a96 100644 --- a/lib/src/types/mixed.dart +++ b/lib/src/types/mixed.dart @@ -33,7 +33,7 @@ import 'package:fraction/src/types/egyptian_converter.dart'; /// ``` /// /// Operators always return new objects. There are extension methods on [num] -/// and [String] that allow you to build [MixedFraction] objects "on the fly". +/// and [String] that allow you to build [MixedFraction] objects "on the fly". /// For example: /// /// ```dart @@ -237,15 +237,7 @@ base class MixedFraction extends Rational { } @override - int get hashCode { - var result = 17; - - result = result * 37 + whole.hashCode; - result = result * 37 + _numerator.hashCode; - result = result * 37 + _denominator.hashCode; - - return result; - } + int get hashCode => Object.hash(whole, numerator, denominator); @override String toString() { diff --git a/lib/src/types/standard.dart b/lib/src/types/standard.dart index 0ffa928..191a123 100644 --- a/lib/src/types/standard.dart +++ b/lib/src/types/standard.dart @@ -306,14 +306,7 @@ base class Fraction extends Rational { } @override - int get hashCode { - var result = 17; - - result = result * 37 + numerator.hashCode; - result = result * 37 + denominator.hashCode; - - return result; - } + int get hashCode => Object.hash(numerator, denominator); @override String toString() { diff --git a/test/types/mixed_test.dart b/test/types/mixed_test.dart index 9cc3728..4e3da33 100644 --- a/test/types/mixed_test.dart +++ b/test/types/mixed_test.dart @@ -189,13 +189,17 @@ void main() { throwsA(isA()), ); expect( - () => MixedFraction.fromString('2 1/1'), - throwsA(isA()), + () => MixedFraction.fromString('2 1/-1'), + throwsA(isA()), ); expect( () => MixedFraction.fromString('2 c/0'), throwsA(isA()), ); + expect( + () => MixedFraction.fromString('1 2/0'), + throwsA(isA()), + ); }, ); diff --git a/test/types/standard_test.dart b/test/types/standard_test.dart index 09f99ea..b91a69a 100644 --- a/test/types/standard_test.dart +++ b/test/types/standard_test.dart @@ -94,7 +94,7 @@ void main() { throwsA(isA()), ); expect( - () => Fraction.fromString('1/-3'), + () => Fraction.fromString('a/3'), throwsA(isA()), ); },