Created
September 23, 2023 08:34
-
-
Save easylive1989/bd7d59b1675f7c608a43cee7408edfde to your computer and use it in GitHub Desktop.
2023鐵人賽_D9_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 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'; | |
class PurchaseProductService { | |
final ProductRepository productRepository; | |
final WalletRepository walletRepository; | |
PurchaseProductService(this.productRepository, this.walletRepository); | |
Future<void> execute(Product product, NewYearCoupon? coupon) async { | |
var now = DateTime.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 'wallet_repository.dart'; | |
@GenerateNiceMocks([MockSpec<ProductRepository>(), MockSpec<WalletRepository>()]) | |
main() { | |
test("use coupon when purchase product", () { | |
var mockProductRepository = MockProductRepository(); | |
var mockWalletRepository = MockWalletRepository(); | |
var purchaseProductService = PurchaseProductService(mockProductRepository, mockWalletRepository); | |
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 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