Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(model): add test cases #4

Merged
merged 4 commits into from
Apr 1, 2023
Merged
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
18 changes: 14 additions & 4 deletions lib/model/alphabet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,23 @@ class Alphabet {
Alphabet.latinNaturalOrder3(),
];

int? valueOfLetter(String letter) =>
int? numericValueOfLetter(String letter) =>
_letterToNumber[removeDiacritics(letter).toUpperCase()];

bool containsLetter(String letter) => valueOfLetter(letter) != null;
bool containsLetter(String letter) => numericValueOfLetter(letter) != null;

int valueOfName(String name) => name.characters.fold(
int numericValueOfName(String name) => name.characters.fold(
0,
(previousValue, letter) => previousValue + (valueOfLetter(letter) ?? 0),
(previousValue, letter) =>
previousValue + (numericValueOfLetter(letter) ?? 0),
);

@override
bool operator ==(Object other) =>
other is Alphabet &&
name == other.name &&
_letterToNumber == other._letterToNumber;

@override
int get hashCode => Object.hash(name, _letterToNumber);
}
9 changes: 8 additions & 1 deletion lib/model/name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ class Name {
this.alphabet = const Alphabet.latinNaturalOrder1(),
});

int get value => alphabet.valueOfName(name);
int get numericValue => alphabet.numericValueOfName(name);

Name copyWith({String? name, Alphabet? alphabet}) => Name(
name ?? this.name,
alphabet: alphabet ?? this.alphabet,
);

@override
bool operator ==(Object other) =>
other is Name && name == other.name && alphabet == other.alphabet;

@override
int get hashCode => Object.hash(name, alphabet.hashCode);
}
4 changes: 2 additions & 2 deletions lib/widgets/alphabet_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _AlphabetDropdownMenuItem extends StatelessWidget {
data: theme.copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: NameSection.colorFromValue(
alphabet.valueOfName(name),
alphabet.numericValueOfName(name),
),
),
),
Expand All @@ -80,7 +80,7 @@ class _AlphabetDropdownMenuItem extends StatelessWidget {
),
const SizedBox(width: 14),
Text(
'${alphabet.valueOfName(name)}',
'${alphabet.numericValueOfName(name)}',
style: TextStyle(
color: theme.colorScheme.primary.withOpacity(0.6),
fontSize: 14,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/name_labels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NameLabels extends StatelessWidget {
else
AlphabetLetter(
letter: character,
value: name.alphabet.valueOfLetter(character),
value: name.alphabet.numericValueOfLetter(character),
isDimmed: !name.alphabet.containsLetter(character),
),
],
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/name_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _NameSectionState extends State<NameSection> {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: NameSection.colorFromValue(name.value),
seedColor: NameSection.colorFromValue(name.numericValue),
),
),
child: Builder(
Expand All @@ -53,7 +53,7 @@ class _NameSectionState extends State<NameSection> {
child: Container(
padding: const EdgeInsetsDirectional.only(end: 36),
width: MediaQuery.of(context).size.width / 4,
child: _NameSectionValue(value: name.value),
child: _NameSectionValue(value: name.numericValue),
),
),
Center(
Expand Down
2 changes: 2 additions & 0 deletions test/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'model/main.dart' as model_test;
import 'utils/main.dart' as utils_test;

void main() {
model_test.main();
utils_test.main();
}
182 changes: 182 additions & 0 deletions test/model/alphabet_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:note_names/model/alphabet.dart';

void main() {
group('Alphabet', () {
group('constructor', () {
test('should create an instance of an Alphabet', () {
const alphabet = Alphabet(
name: 'Alphabet 1',
letterToNumber: {'A': 1, 'B': 2, 'C': 3},
);
expect(alphabet.name, 'Alphabet 1');
expect(alphabet.numericValueOfLetter('C'), 3);
expect(alphabet.numericValueOfName('ABC'), 6);
});
});

group('.latinNaturalOrder()', () {
const alphabet = Alphabet.latinNaturalOrder();

test('should contain the expected letters', () {
expect(alphabet.containsLetter('A'), isTrue);
expect(alphabet.containsLetter('K'), isTrue);
expect(alphabet.containsLetter('O'), isTrue);
expect(alphabet.containsLetter('W'), isTrue);
expect(alphabet.containsLetter('Z'), isTrue);

expect(alphabet.containsLetter(' '), isFalse);
expect(alphabet.containsLetter('.'), isFalse);
});

test('should return the correct numeric value of a letter', () {
expect(alphabet.numericValueOfLetter('A'), 1);
expect(alphabet.numericValueOfLetter('B'), 2);
expect(alphabet.numericValueOfLetter('K'), 10);
expect(alphabet.numericValueOfLetter('O'), 14);
expect(alphabet.numericValueOfLetter('W'), 0);
expect(alphabet.numericValueOfLetter('Z'), 23);

expect(alphabet.numericValueOfLetter(' '), null);
expect(alphabet.numericValueOfLetter('.'), null);
});

test('should return the correct numeric value of a name', () {
expect(alphabet.numericValueOfName('Bach'), 14);
expect(alphabet.numericValueOfName('S.D.G.'), 29);
expect(alphabet.numericValueOfName('JSB'), 29);
expect(alphabet.numericValueOfName('J.S. BACH'), 41);
expect(alphabet.numericValueOfName('Johann Sebastian'), 144);
expect(alphabet.numericValueOfName('Johann Sebastian Bach'), 158);
});
});

group('.latinNaturalOrder1()', () {
const alphabet = Alphabet.latinNaturalOrder1();

test('should contain the expected letters', () {
expect(alphabet.containsLetter('A'), isTrue);
expect(alphabet.containsLetter('K'), isTrue);
expect(alphabet.containsLetter('O'), isTrue);
expect(alphabet.containsLetter('W'), isTrue);
expect(alphabet.containsLetter('Z'), isTrue);

expect(alphabet.containsLetter(' '), isFalse);
expect(alphabet.containsLetter('.'), isFalse);
});

test('should return the correct numeric value of a letter', () {
expect(alphabet.numericValueOfLetter('A'), 1);
expect(alphabet.numericValueOfLetter('B'), 2);
expect(alphabet.numericValueOfLetter('K'), 10);
expect(alphabet.numericValueOfLetter('O'), 14);
expect(alphabet.numericValueOfLetter('W'), 21);
expect(alphabet.numericValueOfLetter('Z'), 24);

expect(alphabet.numericValueOfLetter(' '), null);
expect(alphabet.numericValueOfLetter('.'), null);
});

test('should return the correct numeric value of a name', () {
expect(alphabet.numericValueOfName('Bach'), 14);
expect(alphabet.numericValueOfName('S.D.G.'), 29);
expect(alphabet.numericValueOfName('JSB'), 29);
expect(alphabet.numericValueOfName('J.S. BACH'), 41);
expect(alphabet.numericValueOfName('Johann Sebastian'), 144);
expect(alphabet.numericValueOfName('Johann Sebastian Bach'), 158);
});
});

group('.latinNaturalOrder2()', () {
const alphabet = Alphabet.latinNaturalOrder2();

test('should contain the expected letters', () {
expect(alphabet.containsLetter('A'), isTrue);
expect(alphabet.containsLetter('K'), isTrue);
expect(alphabet.containsLetter('O'), isTrue);
expect(alphabet.containsLetter('W'), isTrue);
expect(alphabet.containsLetter('Z'), isTrue);

expect(alphabet.containsLetter(' '), isFalse);
expect(alphabet.containsLetter('.'), isFalse);
});

test('should return the correct numeric value of a letter', () {
expect(alphabet.numericValueOfLetter('A'), 1);
expect(alphabet.numericValueOfLetter('B'), 2);
expect(alphabet.numericValueOfLetter('K'), 10);
expect(alphabet.numericValueOfLetter('O'), 0);
expect(alphabet.numericValueOfLetter('W'), 20);
expect(alphabet.numericValueOfLetter('Z'), 23);

expect(alphabet.numericValueOfLetter(' '), null);
expect(alphabet.numericValueOfLetter('.'), null);
});

test('should return the correct numeric value of a name', () {
expect(alphabet.numericValueOfName('Bach'), 14);
expect(alphabet.numericValueOfName('S.D.G.'), 28);
expect(alphabet.numericValueOfName('JSB'), 28);
expect(alphabet.numericValueOfName('J.S. BACH'), 40);
expect(alphabet.numericValueOfName('Johann Sebastian'), 127);
expect(alphabet.numericValueOfName('Johann Sebastian Bach'), 141);
});
});

group('.latinNaturalOrder3()', () {
const alphabet = Alphabet.latinNaturalOrder3();

test('should contain the expected letters', () {
expect(alphabet.containsLetter('A'), isTrue);
expect(alphabet.containsLetter('K'), isTrue);
expect(alphabet.containsLetter('O'), isTrue);
expect(alphabet.containsLetter('W'), isTrue);
expect(alphabet.containsLetter('Z'), isTrue);

expect(alphabet.containsLetter(' '), isFalse);
expect(alphabet.containsLetter('.'), isFalse);
});

test('should return the correct numeric value of a letter', () {
expect(alphabet.numericValueOfLetter('A'), 1);
expect(alphabet.numericValueOfLetter('B'), 2);
expect(alphabet.numericValueOfLetter('K'), 0);
expect(alphabet.numericValueOfLetter('O'), 13);
expect(alphabet.numericValueOfLetter('W'), 0);
expect(alphabet.numericValueOfLetter('Z'), 22);

expect(alphabet.numericValueOfLetter(' '), null);
expect(alphabet.numericValueOfLetter('.'), null);
});

test('should return the correct numeric value of a name', () {
expect(alphabet.numericValueOfName('Bach'), 14);
expect(alphabet.numericValueOfName('S.D.G.'), 28);
expect(alphabet.numericValueOfName('JSB'), 28);
expect(alphabet.numericValueOfName('J.S. BACH'), 40);
expect(alphabet.numericValueOfName('Johann Sebastian'), 137);
expect(alphabet.numericValueOfName('Johann Sebastian Bach'), 151);
});
});

group('.alphabets', () {
test('should be a list of Alphabets', () {
expect(Alphabet.alphabets, const TypeMatcher<List<Alphabet>>());
});
});

group('.hashCode', () {
test('should remove duplicates in a Set', () {
final collection = {
// ignore: prefer_const_constructors
Alphabet.latinNaturalOrder(),
// ignore: prefer_const_constructors
Alphabet.latinNaturalOrder1(),
// ignore: prefer_const_constructors
Alphabet.latinNaturalOrder(),
};
expect(collection.length, 2);
});
});
});
}
7 changes: 7 additions & 0 deletions test/model/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'alphabet_test.dart' as alphabet_test;
import 'name_test.dart' as name_test;

void main() {
alphabet_test.main();
name_test.main();
}
67 changes: 67 additions & 0 deletions test/model/name_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:note_names/model/alphabet.dart';
import 'package:note_names/model/name.dart';

void main() {
group('Name', () {
group('constructor', () {
test('should create a new instance of Name', () {
const alphabet = Alphabet(
name: 'Alphabet 1',
letterToNumber: {'A': 1, 'B': 2, 'C': 3},
);
const name = Name('ABC', alphabet: alphabet);
expect(name.name, 'ABC');
expect(name.alphabet, alphabet);
});
});

group('.numericValue', () {
test('should return the numeric value of this name', () {
expect(const Name('ABC').numericValue, 6);

const alphabet = Alphabet(
name: 'Alphabet 1',
letterToNumber: {'A': 2, 'B': 4, 'C': 6},
);
expect(const Name('ABC', alphabet: alphabet).numericValue, 12);
});
});

group('.copyWith', () {
test('should return a new copy of this Name', () {
const name = Name('ABC');
expect(name, name.copyWith());
expect(identical(name, name.copyWith()), isFalse);
expect(identical(name.copyWith(), name.copyWith()), isFalse);
});

test(
'should return a new copy of this SingleBooking with overridden '
'properties',
() {
const alphabet =
Alphabet(name: 'Alphabet 1', letterToNumber: {'Z': 24});
const name = Name('ABC');
final copiedName = name.copyWith(name: 'Z', alphabet: alphabet);
expect(copiedName.name, 'Z');
expect(copiedName.alphabet, alphabet);
},
);
});

group('.hashCode', () {
test('should remove duplicates in a Set', () {
final collection = {
// ignore: prefer_const_constructors
Name('ABC'),
// ignore: prefer_const_constructors
Name('XYZ'),
// ignore: prefer_const_constructors
Name('ABC'),
};
expect(collection.length, 2);
});
});
});
}