Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Added TimeOfDay and Color adapters & tests! #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions lib/src/adapters/color_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

class ColorAdapter extends TypeAdapter<Color> {
@override
Color read(BinaryReader reader) {
final colorValue = reader.readInt();
return Color(colorValue);
}

@override
void write(BinaryWriter writer, Color obj) {
writer.writeInt(obj.value);
}

@override
int get typeId => 200;
}
25 changes: 25 additions & 0 deletions lib/src/adapters/time_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

class TimeAdapter extends TypeAdapter<TimeOfDay> {
@override
TimeOfDay read(BinaryReader reader) {
final string = reader.readString();
final hourMinute =
string.substring(string.indexOf('(') + 1, string.length - 1).split(':');
final hour = hourMinute[0];
final minute = hourMinute[1];
return TimeOfDay(
hour: int.parse(hour),
minute: int.parse(minute),
);
}

@override
void write(BinaryWriter writer, TimeOfDay obj) {
writer.writeString(obj.toString());
}

@override
int get typeId => 201;
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
mockito:

pedantic: ">=1.8.0 <2.0.0"
29 changes: 29 additions & 0 deletions test/color_adapter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/src/adapters/color_adapter.dart';
import 'package:mockito/mockito.dart';

import 'mocks.dart';

void main() {
group('ColorAdapter', () {
test('.read()', () {
const color = Color(0xFF000000);
final BinaryReader binaryReader = BinaryReaderMock();
when(binaryReader.readInt()).thenReturn(color.value);

final readColor = ColorAdapter().read(binaryReader);
verify(binaryReader.readInt());
expect(readColor, readColor);
});

test('.write()', () {
const color = Color(0xFF000000);
final BinaryWriter binaryWriter = BinaryWriterMock();

ColorAdapter().write(binaryWriter, color);
verify(binaryWriter.writeInt(color.value));
});
});
}
6 changes: 6 additions & 0 deletions test/mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';

class BinaryReaderMock extends Mock implements BinaryReader {}

class BinaryWriterMock extends Mock implements BinaryWriter {}
29 changes: 29 additions & 0 deletions test/time_adapter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/src/adapters/time_adapter.dart';
import 'package:mockito/mockito.dart';

import 'mocks.dart';

void main() {
group('TimeOfDayAdapter', () {
test('.read()', () {
final currentTime = TimeOfDay.now();
final BinaryReader binaryReader = BinaryReaderMock();
when(binaryReader.readString()).thenReturn(currentTime.toString());

final time = TimeAdapter().read(binaryReader);
verify(binaryReader.readString());
expect(time, time);
});

test('.write()', () {
final currentTime = TimeOfDay.now();
final BinaryWriter binaryWriter = BinaryWriterMock();

TimeAdapter().write(binaryWriter, currentTime);
verify(binaryWriter.writeString(currentTime.toString()));
});
});
}