Skip to content

Instantly share code, notes, and snippets.

View petrosDemetrakopoulos's full-sized avatar

Petros Demetrakopoulos petrosDemetrakopoulos

View GitHub Profile
@petrosDemetrakopoulos
petrosDemetrakopoulos / retain_cycle_unit_test.swift
Created December 15, 2020 10:10
Swift: Unit testing for retain cycles
class testsForVehicle: XCTestCase {
override func setUp() {
super.setUp()
}
func testVehicleIsNotRetained() {
var sut: Vehicle? = Vehicle()
let wheel1 = Wheel(vehicle: sut!)
sut!.add(wheel1)
@petrosDemetrakopoulos
petrosDemetrakopoulos / Vehicle_wheels_retain_cycles.swift
Created December 15, 2020 10:13
Swift: Unit testing for retain cycles
class Vehicle {
private var wheels = [Wheel]()
func add(_ wheel : Wheel) {
wheels.append(wheel)
}
}
class Wheel {
private var vehicle : Vehicle
@petrosDemetrakopoulos
petrosDemetrakopoulos / reverse_string_python.py
Created January 7, 2021 08:40
Reversing a string in python
print("Hello"[::-1])
enum Pizza {
case preparePizza(id: Int, size: Int?, type: PizzaType, doughType: DoughType)
case addedExtras(id: Int, extras: [String:Int])
case bakedPizza(id: Int)
case deliveredPizza(id: Int)
init(id: Int, size: Int?, type: PizzaType, doughType: DoughType) {
self = .preparePizza(id: id, size: size, type: type, doughType: doughType)
}
}
@petrosDemetrakopoulos
petrosDemetrakopoulos / Pizza.swift
Last active January 11, 2021 19:03
Pizza struct
struct Pizza {
let id: Int
let size: Int
let type: PizzaType
let doughType: DoughType
let hasExtraIngredients: Bool
let extraIngredients: [String: Int] //Where the key is the ingredient and the value is the amount
let isBaked: Bool
let isDelivered: Bool
}
mutating func addExtras(id: Int, extras: [String:Int]) {
switch self {
case .preparePizza(id: let id, size: _, type: _, doughType: _):
self = .addedExtras(id: id, extras: extras)
case .addedExtras(id: let id, extras: _):
self = .addedExtras(id: id, extras: extras)
case .bakedPizza(id: let id):
print("Cannot add extras to Piza \(id) at this phase")
case .deliveredPizza(id: _):
print("Pizza \(id) must be baked before delivery")
@petrosDemetrakopoulos
petrosDemetrakopoulos / getters.swift
Last active January 11, 2021 19:41
Computed properties
var isDelivered: Bool {
switch self {
case .preparePizza(id: _, size: _, type: _, doughType: _):
return false
case .addedExtras(id: _, extras: _):
return false
case .bakedPizza(id: _):
return false
case .deliveredPizza(id: _):
return true
var napolitana = Pizza(id: 0, size: 8, type: .napolitana, doughType: .thin)
print(napolitana.currentState)
napolitana.bakePizza()
print(napolitana.currentState)
napolitana.deliverPizza()
print(napolitana.currentState)
var csrf = require('csurf');
var app = express();
app.use(csrf());
app.use(function(req, res, next) {
res.locals._csrf = req.csrfToken();
next();
});
<html>
<form method="post" action=“changeEmail">
<input type="hidden" name="_csrf" value="_csrf">
<input type="email" name=“newEmail">
</form>
</html>