/moonbit-skills:moonbit-extract-spec-testfrom moonbit-skills
Extracts formal spec files and test suites from existing MoonBit implementations. Produces spec.mbt with declare stubs and organized valid/invalid test files.
Ships in moonbit-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Validated 2026-10-27 against moon 0.1.20261023 / moonc v0.10.17+d971670cd by compiling and running a full spec + implementation + black-box/white-box test project. Key toolchain facts baked in below:
try?removed,derive(Show)deprecated,raise-style signatures preferred overResult[T, Error], and@pkg.-prefix requirements in black-box tests.
Reverse-engineer a formal API contract (<pkg>_spec.mbt) and comprehensive test suites from an existing MoonBit implementation. This enables spec-driven testing for already-written code.
Identify public API surface:
pub and pub(all) types, functions, traitsExamine test patterns (if tests exist):
*_test.mbt, *_wbtest.mbt)<pkg>_spec.mbt)Create formal API contract:
declare keyword for all public functions/typesderive(Debug, Eq, ToJson) for testability — derive(Show) is deprecated (warning deprecated_syntax); use Debug + debug_inspect() for diagnostic outputT raise E signatures for failable APIs; the toolchain warns result_error_return for Result[T, E] where E is an error type — keep Result only when success/failure must flow as ordinary datadeclare do not have a bodypub(all) for types that need construction in testspub for opaque typesExample spec structure:
///|
/// Error type for parsing operations
pub(all) suberror ParseError {
InvalidFormat(String)
UnexpectedEof
} derive(Debug, Eq, ToJson)
///|
/// Main data type
declare pub(all) type Toml
///|
/// Convert to test JSON format
declare pub fn Toml::to_test_json(self : Toml) -> Json
///|
/// Parse TOML from string
declare pub fn parse(input : StringView) -> Toml raise ParseError
Categorize tests by validity (happy path vs error path):
Valid tests (<pkg>_valid_test.mbt):
Invalid tests (<pkg>_invalid_test.mbt):
For valid input tests — call raising functions directly: a test block propagates errors by default, so an unexpected raise fails the test with the actual error (no try?-style wrapper needed):
///|
test "valid/category/test-name" {
let toml_text =
#|key = "value"
#|
let toml = @pkg.parse(toml_text)
json_inspect(toml.to_test_json(), content={
"key": { "type": "string", "value": "value" }
})
}
For invalid input tests — use try ... catch ... noraise, matching suberror variants exhaustively (a non-exhaustive catch is error partial_match):
///|
test "invalid/category/test-name" {
let toml_text =
#|invalid syntax here
#|
try @pkg.parse(toml_text) catch {
@pkg.ParseError::InvalidFormat(_) => () // expected
@pkg.ParseError::UnexpectedEof => () // expected
} noraise {
toml => fail("Expected parse to fail, got \{toml.to_test_json().stringify(indent=2)}")
}
}
Note: in black-box test files (*_test.mbt) every referenced symbol — including suberror constructors in patterns — needs the @pkg. prefix; unqualified use triggers the test_unqualified_package warning.
Type-check the spec:
moon check
Verified semantics of the spec-only state (moon 0.1.20261023 / moonc v0.10.17): unimplemented declares emit the declaration_unimplemented warning (spec-only check still exits 0), but using an unimplemented declaration anywhere (including tests) is a hard error (exit 1). Implement the declarations in a separate file until the warnings disappear.
Run the tests:
moon test # Run all tests
moon test -u # Update snapshot tests
Verify coverage:
declare keyword (no body needed)to_test_json() for inspection@pkg.function)json_inspect() for complex values<pkg>_valid_test.mbt for happy path, <pkg>_invalid_test.mbt for error path)///| to delimit test blockspub type Config
pub(all) suberror ConfigError {
BadLine(String)
} derive(Debug, Eq)
pub fn Config::parse(text : String) -> Config raise ConfigError {
// implementation
}
pub fn Config::get_value(self : Config, key : String) -> String? {
// implementation
}
///|
declare pub type Config
///|
declare pub fn Config::parse(text : String) -> Config raise ConfigError
///|
declare pub fn Config::get_value(self : Config, key : String) -> String?
Black-box tests import the package under test and use the @pkg. alias for every symbol:
///|
test "parse valid config" {
let cfg = @config.Config::parse("key=value")
inspect(cfg.get_value("key"), content="Some(value)")
}
///|
test "parse invalid config returns error" {
try @config.Config::parse("invalid") catch {
@config.ConfigError::BadLine(line) => inspect(line, content="invalid")
} noraise {
_ => fail("Should have failed to parse")
}
}
(inspect renders Option[String] as Some(value) — String payloads are unquoted in current inspect output; prefer debug_inspect on types that derive(Debug) when exact quoting matters.)
moon check frequently to catch type errors earlymoon test -u when output format changes
try?has been removed from the language (parse error on moonc v0.10.17 with an explicit replacement hint). Use the patterns below instead.
test "call directly" {
let value = function_call(...) // unexpected raise fails the test with the actual error
inspect(value, content="...")
}
try/catch/noraisetry function_call(...) catch {
@pkg.ErrorVariant(payload) => inspect(payload, content="...") // expected error
} noraise {
v => fail("Expected error, got \{Repr(v)}")
}
The catch must be exhaustive over the raised error type (else partial_match).
Resultlet result = try Ok(function_call(...)) catch { e => Err(e) }
match result {
Ok(value) => inspect(value, content="...")
Err(e) => fail("Unexpected error: \{Repr(e)}")
}
json_inspect(value.to_test_json(), content={
"field": { "type": "integer", "value": "42" }
})
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub moonbitlang/skills --plugin moonbit-skills9 skills in this plugin
/make-moonbit-c-bindingsGuides agents through complete, maintainable MoonBit bindings for C/C++ libraries, from upstream source survey through vendoring, safe API design, documentation tests, and ASan validation. Use when creating or hardening MoonBit native FFI bindings, wrapping C APIs, vendoring C sources into native-stub, or turning a C library into a MoonBit package./moonbit-agent-guideGuide for writing, refactoring, and testing MoonBit projects, including exclusive use of `.mbtx` for agent-authored automation. Use when working in MoonBit modules or packages, organizing MoonBit files, using moon tooling (build/check/run/test/doc/ide etc.), or following MoonBit-specific layout, scripting, documentation, and testing conventions./Other plugins' skills for the same job