Created
September 23, 2023 08:39
-
-
Save easylive1989/581d75fad68315f8d7bb2b6b92f0b46e to your computer and use it in GitHub Desktop.
2023鐵人賽_D9_2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CouponInvalidException implements Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoneyNotEnoughException implements Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:equatable/equatable.dart'; | |
class NewYearCoupon extends Equatable { | |
@override | |
List<Object?> get props => []; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:equatable/equatable.dart'; | |
class Product extends Equatable { | |
final double price; | |
const Product(this.price); | |
@override | |
List<Object?> get props => [price]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'product.dart'; | |
class ProductRepository { | |
Future<void> purchase(Product product, coupon) async {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'product_repository.dart'; | |
import 'product.dart'; | |
import 'money_not_enough_exception.dart'; | |
import 'coupon_invalid_exception.dart'; | |
import 'wallet_repository.dart'; | |
import 'new_year_coupon.dart'; | |
import 'time_repository.dart'; | |
class PurchaseProductService { | |
final ProductRepository productRepository; | |
final WalletRepository walletRepository; | |
final TimeRepository timeRepository; | |
PurchaseProductService(this.productRepository, this.walletRepository, this.timeRepository); | |
Future<void> execute(Product product, NewYearCoupon? coupon) async { | |
var now = timeRepository.now(); | |
if (_isUseCoupon(coupon) && !_isFirstOfJanuary(now)) { | |
throw CouponInvalidException(); | |
} | |
var wallet = await walletRepository.get(); | |
if (product.price > wallet.money) { | |
throw MoneyNotEnoughException(); | |
} | |
productRepository.purchase(product, coupon); | |
} | |
bool _isFirstOfJanuary(DateTime now) => now.month == 1 && now.day == 1; | |
bool _isUseCoupon(NewYearCoupon? coupon) => coupon != null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:mockito/annotations.dart'; | |
import 'package:mockito/mockito.dart'; | |
import 'product.dart'; | |
import 'product_repository.dart'; | |
import 'purchase_product_service.dart'; | |
import 'purchase_product_service_test.mocks.dart'; | |
import 'new_year_coupon.dart'; | |
import 'time_repository.dart'; | |
import 'wallet_repository.dart'; | |
import 'wallet.dart'; | |
@GenerateNiceMocks([MockSpec<ProductRepository>(), MockSpec<WalletRepository>(), MockSpec<TimeRepository>()]) | |
main() { | |
test("use coupon when purchase product", () async { | |
var mockProductRepository = MockProductRepository(); | |
var mockWalletRepository = MockWalletRepository(); | |
var mockTimeRepository = MockTimeRepository(); | |
when(mockTimeRepository.now()).thenReturn(DateTime.parse('2023-01-01')); | |
when(mockWalletRepository.get()).thenAnswer((_) async => Wallet(100)); | |
var purchaseProductService = PurchaseProductService(mockProductRepository, mockWalletRepository, mockTimeRepository); | |
await purchaseProductService.execute( | |
const Product(100), | |
NewYearCoupon()); | |
verify( | |
mockProductRepository.purchase( | |
const Product(100), | |
NewYearCoupon()), | |
).called(1); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeRepository { | |
DateTime now() => DateTime.now(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wallet { | |
final double money; | |
Wallet(this.money); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'wallet.dart'; | |
class WalletRepository { | |
Future<Wallet> get() async => Wallet(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment