
CatalogTest.pudu
Pudu48 lines2.7 KB
1/** @Test.Catalog.Suite — gathering routes, counting registrations, and finding problems */2module PuduLangMediator.CatalogTest34import Std.HashMap as HashMap5import Std.Io as Io6import Std.Test as Test7import PuduLangMediator.Catalog as Catalog8import PuduLangMediator.Compose as Compose9import PuduLangMediator.Context as Context10import PuduLangMediator.Domain.Order as Order11import PuduLangMediator as Messaging12import PuduLangMediator.Publishing as Publishing13import PuduLangMediator.Request as Request14import PuduLangMediator.Utils.Erasure as Erasure151617fn main() -> Int {18 let lookup: Request.Kind[Int, Int, Str] = Request.kind("lookup")19 let twin: Request.Kind[Int, Int, Str] = Request.kind("lookup")20 let catalog = Catalog.empty(Publishing.Sequential{}, Catalog.ForUnhandled)21 let addBehavior = fn(route: Request.Route[Int, Int, Str]) -> Request.Route[Int, Int, Str] {22 Request.Route{..route, behaviors: route.behaviors.push(fn(_request: Int, context: Context.Context, next: Request.Next[Int, Str]) -> Messaging.Outcome[Int, Str] { next(context) })}23 }24 let once = Catalog.touch(&catalog.requests, &lookup.info, &lookup.routes, Request.route(), addBehavior, 0, 1, Compose.requestFinish(lookup))25 let twice = Catalog.touch(&once, &lookup.info, &lookup.routes, Request.route(), addBehavior, 0, 1, Compose.requestFinish(lookup))26 let collided = Catalog.touch(&twice, &twin.info, &twin.routes, Request.route(), addBehavior, 1, 0, Compose.requestFinish(twin))27 let entry = HashMap.get(&collided, &"lookup")28 let routed = match entry {29 case Some(found) => Erasure.unpack(&lookup.routes, &found.route)30 case None => None31 }32 let marked = Catalog.marked(catalog, Order.Behaviors, "lookup")33 let checks = Test.suite("Catalog", &[34 Test.equals("a fresh catalog has nothing", &(HashMap.size(&catalog.requests), catalog.assembly.marks.length()), &(0, 0)),35 Test.equals("registrations accumulate on one route", &(match routed { case Some(route) => route.behaviors.length() case None => 0 }), &2),36 Test.equals("handlers and components are counted", &(match entry { case Some(found) => (found.handlers, found.components, found.collisions) case None => (0, 0, 0) }), &(0, 2, 1)),37 Test.equals("a mark records the layer and the kind", &marked.assembly.marks, &[Order.Closed(Order.Behaviors, "lookup")]),38 Test.equals("problems name the collision and the missing handler", &Catalog.problems(&Catalog.Catalog{..catalog, requests: collided}), &[39 "'lookup' names two different kinds", "'lookup' has behaviors or processors but no handler"40 ])41 ])42 let ran = Test.run(&checks)43 for failure in Test.failuresOf(&ran) {44 let _reported = Io.writeErrorLine(failure)45 }46 Test.report(&ran)47}48