
PublisherTest.pudu
Pudu70 lines3.1 KB
1/** @Test.Publisher.Suite — components depending on the publisher seam */2module PuduLangMediator.PublisherTest34import Std.Io as Io5import Std.Test as Test6import PuduLangMediator.Context as Context7import PuduLangMediator.Mediator as Mediator8import PuduLangMediator.Message as Message9import PuduLangMediator as Messaging10import PuduLangMediator.Notification as Notification11import PuduLangMediator.Publisher as Publisher12import PuduLangMediator.Registration as Registration13import PuduLangMediator.Utils.Shared as Shared1415/** @Test.Publisher.Recording — a test double that records what it was asked to publish */16type Recording = { published: Shared.Shared[Array[Str]] }1718impl Publisher.Publisher for Recording {19 20 fn publish[N, E](self: &Self, subject: &Notification.Kind[N, E], notification: N, context: Context.Context) -> Messaging.Outcome[(), E] {21 let line = subject.info.name + " " + show(notification)22 Shared.update(&self.published, |held: Array[Str]| held.push(line))23 Ok(())24 }2526 27 fn broadcast(self: &Self, envelope: Message.Envelope, context: Context.Context) -> Message.Answer {28 let line = envelope.info.name + " " + envelope.summary29 Shared.update(&self.published, |held: Array[Str]| held.push(line))30 Message.unheard(&envelope.info)31 }32}333435fn signUp(publisher: dynamic Publisher.Publisher, joined: &Notification.Kind[Str, Str], name: Str) -> Messaging.Outcome[(), Str] {36 publisher.publish(joined, name, Context.create())37}383940fn main() -> Int {41 let joined: Notification.Kind[Str, Str] = Notification.kind("joined")42 let welcomed = Shared.shared([])43 let mediator = match Mediator.build([44 Registration.notificationHandler(&joined, "welcome", fn(name: Str, _context: Context.Context) -> Messaging.Outcome[(), Str] {45 Shared.update(&welcomed, |held: Array[Str]| held.push(name))46 Ok(())47 })48 ]) {49 case Ok(found) => found50 case Err(invalid) => panic(Mediator.explain(&invalid))51 }52 let recording = Recording{published: Shared.shared([])}53 let real: dynamic Publisher.Publisher = mediator54 let double: dynamic Publisher.Publisher = recording55 let announced = signUp(real, &joined, "ada")56 let recorded = signUp(double, &joined, "grace")57 let broadcast = real.broadcast(Notification.envelope(&joined, "linus"), Context.create())58 let _recordedBroadcast = double.broadcast(Notification.envelope(&joined, "barbara"), Context.create())59 let checks = Test.suite("Publisher", &[60 Test.equals("a mediator stands for a publisher", &(announced, Shared.current(&welcomed)), &(Ok(()), ["ada", "linus"])),61 Test.equals("a mediator broadcasts as a publisher", &Notification.answerOf(&joined, &broadcast), &Some(Ok(()))),62 Test.equals("a double records instead", &(recorded, Shared.current(&recording.published)), &(Ok(()), ["joined \"grace\"", "joined \"barbara\""]))63 ])64 let ran = Test.run(&checks)65 for failure in Test.failuresOf(&ran) {66 let _reported = Io.writeErrorLine(failure)67 }68 Test.report(&ran)69}70