Created
February 11, 2024 14:47
-
-
Save serrodcal/7ef4c5556280e17cc6600ec447ccec83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
mixin Electric { | |
double _batteryCapacity = 0.0, _battery = 0.0; | |
void engineStart() { | |
assert(_battery > 0.05, 'No _battery, please charge'); | |
_battery -= (_battery * 0.05); | |
} | |
void charge(double energy) { | |
assert(energy > 0, 'Energy cannot be less than or equal to 0'); | |
assert(energy <= (_batteryCapacity - _battery), | |
'No enough energy, please charge energy'); | |
_batteryCapacity += energy; | |
} | |
double batteryLevel() => _battery; | |
double batteryCapacity() => _batteryCapacity; | |
} | |
mixin Combustion { | |
double _fuelTankCapacity = 0.0, _fuel = 0.0; | |
void engineStart() { | |
assert(_fuel > 0.02, 'No fuel, please reFuel'); | |
_fuel -= (_fuel * 0.02); | |
} | |
void reFuel(double fuel) { | |
assert(fuel > 0, 'Fuel cannot be less than or equal to 0'); | |
assert( | |
_fuel <= (_fuelTankCapacity - _fuel), 'No enough fuel, please refuel'); | |
_fuel += fuel; | |
} | |
double fuelLevel() => _fuel; | |
double fuelTankCapacity() => _fuelTankCapacity; | |
} | |
abstract class Vehicle { | |
final String _brand, _model; | |
final int _maxPassenger, _passengers; | |
Vehicle({ | |
required String brand, | |
required String model, | |
required int maxPassenger, | |
required int passengers, | |
}) : assert( | |
brand.isNotEmpty, 'brand must be a non-empty string, and not null'), | |
assert( | |
model.isNotEmpty, 'model must be a non-empty string, and not null'), | |
assert(passengers > 0 && passengers <= maxPassenger, | |
'maxPassenger must be more than 0 and less than $maxPassenger'), | |
_brand = brand, | |
_model = model, | |
_maxPassenger = maxPassenger, | |
_passengers = passengers; | |
String get brand => _brand; | |
String get model => _model; | |
int get maxPassenger => _maxPassenger; | |
int get passengers => _passengers; | |
} | |
abstract class MotorBike extends Vehicle { | |
MotorBike({ | |
required String brand, | |
required String model, | |
int maxPassenger = 2, | |
required int passengers, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(maxPassenger == 2, 'maxPassenger must be 2'); | |
} | |
String toString() => | |
'MotorBike(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers)'; | |
} | |
class CombustionMotorBike extends Vehicle with Combustion { | |
CombustionMotorBike({ | |
required String brand, | |
required String model, | |
int maxPassenger = 2, | |
required int passengers, | |
double fuel = 0.0, | |
double fuelTankCapacity = 0.0, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(fuel > 0 && fuel <= fuelTankCapacity, | |
'fuel must be more than 0 and less than fuelTankCapacity'); | |
assert(fuelTankCapacity > 0, 'fuelTankCapacity must be more than 0'); | |
this._fuel = fuel; | |
this._fuelTankCapacity = fuelTankCapacity; | |
} | |
String toString() => | |
'CombustionMotorBike(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers, fuel: $_fuel, fuelTankCapacity: $_fuelTankCapacity)'; | |
} | |
class ElectricMotorbike extends Vehicle with Electric { | |
ElectricMotorbike({ | |
required String brand, | |
required String model, | |
int maxPassenger = 2, | |
required int passengers, | |
double battery = 0.0, | |
double batteryCapacity = 0.0, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(battery > 0 && battery <= batteryCapacity, | |
'battery must be more than 0 and less than batteryCapacity'); | |
assert(batteryCapacity > 0, 'battery must be more than 0'); | |
this._battery = battery; | |
this._batteryCapacity = batteryCapacity; | |
} | |
String toString() => | |
'ElectricMotorBike(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers, battery: $_battery, batteryCapacity: $_batteryCapacity)'; | |
} | |
abstract class Car extends Vehicle { | |
int? _numOfDoors; | |
Car({ | |
required String brand, | |
required String model, | |
int maxPassenger = 6, | |
required int passengers, | |
int numOfDoors = 3, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(maxPassenger >= 4 && maxPassenger <= 6, | |
'maxPassenger must be in range [4,6]'); | |
assert(numOfDoors >= 3 && numOfDoors <= 5, | |
'numOfDoors must be in range [3,5]'); | |
_numOfDoors = numOfDoors; | |
} | |
int get numOfDoors => _numOfDoors ?? 3; | |
String toString() => | |
'Car(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers, numOfDoors: $_numOfDoors)'; | |
} | |
class CombustionCar extends Car with Combustion { | |
CombustionCar({ | |
required String brand, | |
required String model, | |
int maxPassenger = 4, | |
required int passengers, | |
int numOfDoors = 3, | |
double fuel = 0.0, | |
double fuelTankCapacity = 0.0, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(fuel > 0.0 && fuel <= fuelTankCapacity, | |
'fuel must be more than 0 and less than fuelTankCapacity'); | |
assert(fuel > 0.0, 'fuel must be more than 0'); | |
_numOfDoors = numOfDoors; | |
this._fuel = fuel; | |
this._fuelTankCapacity = fuelTankCapacity; | |
} | |
String toString() => | |
'CombustionCar(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers, numOfDoors: $_numOfDoors, fuel: $_fuel, fuelTankCapacity: $_fuelTankCapacity)'; | |
} | |
class ElectricCar extends Car with Electric { | |
ElectricCar({ | |
required String brand, | |
required String model, | |
int maxPassenger = 4, | |
required int passengers, | |
int numOfDoors = 3, | |
double battery = 0.0, | |
double batteryCapacity = 0.0, | |
}) : super( | |
brand: brand, | |
model: model, | |
maxPassenger: maxPassenger, | |
passengers: passengers, | |
) { | |
assert(battery > 0.0 && battery <= batteryCapacity, | |
'battery must be more than 0 and less than batteryCapacity'); | |
assert(battery > 0.0, 'battery must be more than 0'); | |
_numOfDoors = numOfDoors; | |
this._battery = battery; | |
this._batteryCapacity = batteryCapacity; | |
} | |
String toString() => | |
'ElectricCar(brand: $_brand, model: $_model, maxPassenger: $_maxPassenger, passengers: $_passengers, numOfDoors: $_numOfDoors, battery: $_battery, batteryCapacity: $_batteryCapacity)'; | |
} |
This file contains hidden or 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 'app.dart'; | |
void main(List<String> args) { | |
// Happy path tests | |
print('Happy path tests:'); | |
CombustionMotorBike motorBike1 = testCreateCombustionMotorbike(); | |
print(motorBike1.toString()); | |
ElectricMotorbike motorBike2 = testCreateElectricMotorbike(); | |
print(motorBike2.toString()); | |
CombustionCar car1 = testCreateCombustionCar(); | |
print(car1.toString()); | |
ElectricCar electricCar1 = testCreateElectricCar(); | |
print(electricCar1.toString()); | |
print('\nHappy path tests passed!'); | |
// Assertions tests | |
assert(testCombustionMotorbikeBrandisEmptyAssertion() == true, | |
'Brand is empty assertion failed'); | |
assert(testCombustionMotorbikeModelisEmptyAssertion() == true, | |
'Model is empty assertion failed'); | |
assert(testCombustionMotorbikeMaxPassengerNoMoreThan2Assertion() == true, | |
'Max passenger is more than 2 assertion failed'); | |
assert(testCombustionMotorbikeHasNoLessThan0FuelAssertion() == true, | |
'Fuel is less than 0 assertion failed'); | |
assert( | |
testCombustionMotorbikeHasNoMoreFuelThanFuelTankCapacityFuelAssertion() == | |
true, | |
'Fuel is more than fuel tank capacity assertion failed'); | |
assert( | |
testCombustionMotorbikeHasNoLessThan0FuelTankCapacityAssertion() == true, | |
'Fuel tank capacity is less than 0 assertion failed'); | |
print('\nAll tests passed!'); | |
} | |
// Happy path tests | |
CombustionMotorBike testCreateCombustionMotorbike() { | |
CombustionMotorBike combustionMotorBike = CombustionMotorBike( | |
brand: 'Honda', | |
model: 'CBR 1000 RR', | |
passengers: 2, | |
fuel: 10.0, | |
fuelTankCapacity: 22.0, | |
); | |
return combustionMotorBike; | |
} | |
ElectricMotorbike testCreateElectricMotorbike() { | |
ElectricMotorbike electricMotorbike = ElectricMotorbike( | |
brand: 'Electyum', | |
model: 'Super Soco CU Mini', | |
passengers: 2, | |
battery: 60.0, | |
batteryCapacity: 100.0, | |
); | |
return electricMotorbike; | |
} | |
CombustionCar testCreateCombustionCar() { | |
CombustionCar combustionCar = CombustionCar( | |
brand: 'SEAT', | |
model: 'León', | |
passengers: 1, | |
maxPassenger: 5, | |
numOfDoors: 5, | |
fuel: 22.0, | |
fuelTankCapacity: 58.0, | |
); | |
return combustionCar; | |
} | |
ElectricCar testCreateElectricCar() { | |
ElectricCar electricCar = ElectricCar( | |
brand: 'Tesla', | |
model: 'Model 3', | |
passengers: 4, | |
numOfDoors: 5, | |
battery: 65.0, | |
batteryCapacity: 100.0, | |
); | |
return electricCar; | |
} | |
// Assertions tests | |
bool testCombustionMotorbikeBrandisEmptyAssertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: '', | |
model: 'Modelo', | |
passengers: 2, | |
fuel: 10.0, | |
fuelTankCapacity: 20, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} | |
bool testCombustionMotorbikeModelisEmptyAssertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: 'Moto', | |
model: '', | |
passengers: 2, | |
fuel: 10.0, | |
fuelTankCapacity: 20, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} | |
bool testCombustionMotorbikeMaxPassengerNoMoreThan2Assertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: 'Moto', | |
model: 'Modelo', | |
passengers: 3, | |
fuel: 10.0, | |
fuelTankCapacity: 20, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} | |
bool testCombustionMotorbikeHasNoLessThan0FuelAssertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: 'Moto', | |
model: 'Modelo', | |
passengers: 2, | |
fuel: -1, | |
fuelTankCapacity: 20, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} | |
bool testCombustionMotorbikeHasNoMoreFuelThanFuelTankCapacityFuelAssertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: 'Moto', | |
model: 'Modelo', | |
passengers: 2, | |
fuel: 21, | |
fuelTankCapacity: 20, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} | |
bool testCombustionMotorbikeHasNoLessThan0FuelTankCapacityAssertion() { | |
try { | |
CombustionMotorBike _ = CombustionMotorBike( | |
brand: 'Moto', | |
model: 'Modelo', | |
passengers: 2, | |
fuel: 10, | |
fuelTankCapacity: 0, | |
); | |
return false; | |
} catch (e) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment