
PuduLangMediatorTest.pudu
Pudu59 lines3.5 KB
1/** @Test.Root.Suite — outcome helpers, combination, and failure descriptions */2module PuduLangMediatorTest34import Std.Io as Io5import Std.Test as Test6import PuduLangMediator as Messaging789fn main() -> Int {10 let broken = Messaging.Violation{property: "name", message: "must not be empty", code: "NotEmpty"}11 let short = Messaging.Violation{property: "age", message: "must be 18 or more", code: "GreaterThanOrEqual"}12 let failures: Array[Messaging.Failure[Str]] = [13 Messaging.Raised("disk"), Messaging.Unhandled("a"), Messaging.Mismatched("b"), Messaging.Refused("no"),14 Messaging.Invalid([broken, short]), Messaging.Cancelled("stop"), Messaging.Crashed("boom"),15 Messaging.Aggregate([Messaging.Raised("x"), Messaging.Invalid([short])])16 ]17 let succeeded: Messaging.Outcome[Int, Str] = Messaging.succeed(4)18 let refused: Messaging.Outcome[Int, Str] = Messaging.refuse("closed")19 let checks = Test.suite("Messaging", &[20 Test.equals("succeed holds the value", &succeeded, &Ok(4)),21 Test.equals("raise holds the handler's error", &Messaging.raise("bad"), &Err(Messaging.Raised("bad"))),22 Test.equals("refuse holds the reason", &refused, &Err(Messaging.Refused("closed"))),23 Test.equals("lift keeps a value", &Messaging.lift(Ok(2)), &Ok(2)),24 Test.equals("lift raises an error", &Messaging.lift(Err("e")), &Err(Messaging.Raised("e"))),25 Test.equals("raised answers only the handler's error", &failures.map(|found: Messaging.Failure[Str]| Messaging.raised(&found)), &[26 Some("disk"), None, None, None, None, None, None, None27 ]),28 Test.equals("only a cancellation is one", &failures.map(|found: Messaging.Failure[Str]| Messaging.isCancellation(&found)), &[29 false, false, false, false, false, true, false, false30 ]),31 Test.equals("violations are read from aggregates too", &failures.map(|found: Messaging.Failure[Str]| Messaging.violations(&found).length()), &[32 0, 0, 0, 0, 2, 0, 0, 133 ]),34 Test.equals("no failures combine to none", &Messaging.combine([]), &None),35 Test.equals("one failure combines to itself", &Messaging.combine([Messaging.Raised("a")]), &Some(Messaging.Raised("a"))),36 Test.equals("several failures combine flat", &Messaging.combine([Messaging.Raised("a"), Messaging.Aggregate([Messaging.Raised("b"), Messaging.Refused("c")])]), &Some(37 Messaging.Aggregate([Messaging.Raised("a"), Messaging.Raised("b"), Messaging.Refused("c")])38 )),39 Test.equals("an aggregate of one combines to that one", &Messaging.combine([Messaging.Aggregate([Messaging.Refused("only")])]), &Some(Messaging.Refused("only"))),40 Test.equals("every failure is described", &failures.map(|found: Messaging.Failure[Str]| Messaging.describe(&found)), &[41 "The handler failed: \"disk\"",42 "No handler is registered for 'a'.",43 "'b' is registered for another kind of the same name.",44 "The message was refused: no",45 "The message is invalid: name: must not be empty; age: must be 18 or more",46 "The message was cancelled: stop",47 "A handler stopped its thread: boom",48 "2 handlers failed: The handler failed: \"x\"; The message is invalid: age: must be 18 or more"49 ]),50 Test.equals("a value is summarized", &Messaging.summarize(&succeeded), &"Ok(4)"),51 Test.equals("a failure is summarized by its description", &Messaging.summarize(&refused), &"The message was refused: closed")52 ])53 let ran = Test.run(&checks)54 for failure in Test.failuresOf(&ran) {55 let _reported = Io.writeErrorLine(failure)56 }57 Test.report(&ran)58}59