- Notifications
You must be signed in to change notification settings - Fork0
darky/klubok-gleam
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Do notation pipes for functions which easy to mock
Inspired by fp-ts/Effect bind Do-notation, but much more small and simple
Primarly created for easy mocking of functions, which allows to write tons of unit tests
gleam add klubok_gleam
importgleam/option.{None,Some}importgleam/setimportklubok_gleam.{use_klubok2,eff}typeCat{Cat(name:String,age:Int)}fncat_birthday(mock,only){// Make this function pipable// Need to use correct arity by count of `eff` usage, 2 in this exampleuse<-use_klubok2(mock,only)// This function mockableusecat<-eff(fn(){// some IO implementation which fetching catCat("Barsik",5)})// This function mockableuseaged_cat<-eff(fn(){Cat(..cat,age:cat.age+1)})aged_cat}pubfnmain(){// Pass Option.None to mock and only// Should be used on production codecat_birthday(None,None)// Cat("Barsik", 6)// Mock first `eff` with another hardcoded cat// Should be used in unit testscat_birthday(#(fn(){Cat("Marfa",10)}|>Some,None)|>Some,None)// Cat("Marfa", 11)// Call only second `eff` and first IO ignored// Also our birthday fn is mocked and our cat is kiten again//❗`only` is type unsafe and can lead to runtime errors when `Nil` passed instead of actual `eff` valuecat_birthday(#(None,fn(){Cat("Barsik",1)}|>Some)|>Some,set.from_list([1])|>Some)// Cat("Barsik", 1)}
Further documentation can be found athttps://hexdocs.pm/klubok_gleam.
gleam run# Run the projectgleamtest# Run the tests
- Klubok - TS implementation of Klubok
About
Do notation pipes for functions which easy to mock
Topics
Resources
Stars
Watchers
Forks
Packages0
No packages published