Cucumber Messages protocol types for MoonBit with JSON serialization and NDJSON streaming
Dependencies
moon add moonrockz/cucumber-messages| Category | Message Types |
|---|---|
| Source & Parsing | Source, GherkinDocument, ParseError |
| Test Compilation | Pickle |
| Glue Definitions | StepDefinition, Hook, ParameterType |
| Test Run Lifecycle | TestRunStarted, TestRunFinished |
| Test Case Lifecycle | TestCase, TestCaseStarted, TestCaseFinished |
| Step Lifecycle | TestStepStarted, TestStepFinished |
| Hook Lifecycle | TestRunHookStarted, TestRunHookFinished |
| Artifacts | Attachment, ExternalAttachment |
| Metadata | Meta, Suggestion, UndefinedParameterType |
// Deserialize a message from JSON (this example imports the package as @cm)
let json = @json.parse(
"{\"meta\": {\"protocolVersion\": \"34.2.1\", \"implementation\": {\"name\": \"cucumber-moonbit\"}, \"runtime\": {\"name\": \"moonbit\"}, \"os\": {\"name\": \"linux\"}, \"cpu\": {\"name\": \"amd64\"}}}"
) catch { _ => panic() }
let envelope : @cm.Envelope = @json.from_json(json) catch { _ => panic() }
// Serialize a message to JSON
let json_value = envelope.to_json()
// NDJSON streaming: parse multiple messages from newline-delimited JSON
let envelopes = @cm.parse_ndjson(ndjson_string) catch { _ => panic() }
// Serialize envelopes to NDJSON
let ndjson = @cm.envelopes_to_ndjson(envelopes)// Read: feed chunks of any size; a partial last line waits for the next chunk.
let reader = @cm.NdjsonReader::new()
for chunk in chunks {
for envelope in reader.feed(chunk) catch { e => { println(e); [] } } {
handle(envelope)
}
}
// Parse a last line that has no trailing newline.
let last = reader.finish() catch { _ => None }
if last is Some(envelope) {
handle(envelope)
}
// Or read one line at a time; blank lines give None.
let envelope = reader.read_line(line) catch { _ => None }
// Write: one line per envelope to any &Logger (for example StringBuilder).
envelope.write_ndjson(out)pub suberror NdjsonParseError {
NdjsonParseError(line~ : Int, cause~ : Error)
}impl Show for NdjsonParseErrorpub struct Attachment {
body : String
contentEncoding : AttachmentContentEncoding
mediaType : String
fileName : String?
source : Source?
testCaseStartedId : String?
testStepId : String?
url : String?
testRunStartedId : String?
testRunHookStartedId : String?
timestamp : Timestamp?
} derive(ToJson, FromJson)pub enum AttachmentContentEncoding {
Identity
Base64
}impl ToJson for AttachmentContentEncodingimpl FromJson for AttachmentContentEncodingpub struct Duration {
seconds : Int64
nanos : Int
}pub enum Envelope {
Meta(Meta)
Source(Source)
ParseError(ParseError)
TestRunStarted(TestRunStarted)
TestRunFinished(TestRunFinished)
UndefinedParameterType(UndefinedParameterType)
TestCaseStarted(TestCaseStarted)
TestCaseFinished(TestCaseFinished)
TestStepStarted(TestStepStarted)
TestStepFinished(TestStepFinished)
Attachment(Attachment)
ExternalAttachment(ExternalAttachment)
Hook(Hook)
StepDefinition(StepDefinition)
ParameterType(ParameterType)
Suggestion(Suggestion)
TestRunHookStarted(TestRunHookStarted)
TestRunHookFinished(TestRunHookFinished)
GherkinDocument(GherkinDocument)
Pickle(Pickle)
TestCase(TestCase)
UnknownMessage(String, Json)
}pub struct Exception {
type_ : String
message : String?
stackTrace : String?
}pub struct FeatureChild {
rule : Rule?
background : Background?
scenario : Scenario?
} derive(ToJson, FromJson)pub struct Hook {
id : String
sourceReference : SourceReference
name : String?
tagExpression : String?
type_ : HookType?
}pub enum HookType {
BeforeTestRun
AfterTestRun
BeforeTestCase
AfterTestCase
BeforeTestStep
AfterTestStep
}pub enum KeywordType {
Unknown
Context
Action
Outcome
Conjunction
}pub struct NdjsonReader {
// private fields
}fn NdjsonReader::feed(self : NdjsonReader, chunk : String) -> Array[Envelope] raise NdjsonParseErrorfn NdjsonReader::read_line(self : NdjsonReader, line : StringView) -> Envelope? raise NdjsonParseErrorpub struct ParameterType {
id : String
name : String
preferForRegularExpressionMatch : Bool
regularExpressions : Array[String]
useForSnippets : Bool
sourceReference : SourceReference?
} derive(ToJson)impl FromJson for ParameterTypepub(all) struct PickleDocString {
argumentIndex : Int?
mediaType : String?
content : String
} derive(ToJson)impl FromJson for PickleDocStringpub(all) struct PickleStep {
id : String
text : String
astNodeIds : Array[String]
type_ : PickleStepType?
argument : PickleStepArgument?
}impl ToJson for PickleStepimpl FromJson for PickleSteppub(all) struct PickleStepArgument {
docString : PickleDocString?
dataTable : PickleTable?
} derive(ToJson, FromJson)pub(all) enum PickleStepType {
Unknown
Context
Action
Outcome
}impl ToJson for PickleStepTypeimpl FromJson for PickleStepTypepub struct Source {
uri : String
data : String
mediaType : SourceMediaType
} derive(ToJson, FromJson)pub enum SourceMediaType {
GherkinPlain
GherkinMarkdown
}impl ToJson for SourceMediaTypeimpl FromJson for SourceMediaTypepub struct SourceReference {
uri : String?
javaMethod : JavaMethod?
javaStackTraceElement : JavaStackTraceElement?
location : Location?
} derive(ToJson, FromJson)pub struct StepDefinition {
id : String
pattern : StepDefinitionPattern
sourceReference : SourceReference
} derive(ToJson, FromJson)impl ToJson for StepDefinitionPatternimpl FromJson for StepDefinitionPatternpub enum StepDefinitionPatternType {
CucumberExpression
RegularExpression
}impl ToJson for StepDefinitionPatternTypeimpl FromJson for StepDefinitionPatternTypepub struct StepMatchArgumentsList {
stepMatchArguments : Array[StepMatchArgument]
} derive(ToJson, FromJson)impl FromJson for Suggestionpub struct TestRunHookFinished {
testRunHookStartedId : String
result : TestStepResult
timestamp : Timestamp
} derive(ToJson, FromJson)pub struct TestStep {
id : String
hookId : String?
pickleStepId : String?
stepDefinitionIds : Array[String]?
stepMatchArgumentsLists : Array[StepMatchArgumentsList]?
} derive(ToJson, FromJson)pub struct TestStepFinished {
testCaseStartedId : String
testStepId : String
testStepResult : TestStepResult
timestamp : Timestamp
} derive(ToJson, FromJson)pub struct TestStepResult {
duration : Duration
status : TestStepResultStatus
message : String?
exception : Exception?
} derive(ToJson, FromJson)pub enum TestStepResultStatus {
Unknown
Passed
Skipped
Pending
Undefined
Ambiguous
Failed
}impl ToJson for TestStepResultStatusimpl FromJson for TestStepResultStatuspub struct Timestamp {
seconds : Int64
nanos : Int
}Install
Download zipCucumber Messages protocol types for MoonBit with JSON serialization and NDJSON streaming
Dependencies