Optional Morphir target profile for moonrockz/gleam-parser
import {
"moonrockz/gleam-parser" @gleam,
"moonrockz/gleam-parser-morphir" @morphir,
}///|
test "mr: retain a Morphir function binding" {
let dialect = @gleam.Dialect::gleam_1_19().with_profile(@morphir.profile())
let config = { ..@gleam.Config::default(), dialect, }
let source = "@external(morphir, \"morphir/sdk/list\", \"length\")\npub fn length(xs: List(a)) -> Int\n"
let result = @gleam.parse_text(source, config~)
assert_eq(result.has_errors(), false)
guard result.module_.definitions[0].definition is Function(function) else {
fail("expected a function")
}
let target = dialect.targets.resolve("morphir").unwrap()
let binding = function.external_for(target).unwrap()
assert_eq(binding.module_, "morphir/sdk/list")
assert_eq(binding.function, "length")
assert_eq(result.module_.definitions[0].target, None)
assert_eq(@gleam.format_parsed(result), source)
assert_eq(
@gleam.decode_extended(@gleam.encode_extended(result)).parsed,
result.parsed(),
)
}///|
test "mr: external types and restrictions stay distinct" {
let dialect = @gleam.Dialect::gleam_1_19().with_profile(@morphir.profile())
let config = { ..@gleam.Config::default(), dialect, }
let result = @gleam.parse_text(
"@target(morphir)\n@external(morphir, \"example/domain\", \"CustomerId\")\npub type CustomerId\n",
config~,
)
assert_eq(result.has_errors(), false)
assert_eq(
result.module_.definitions[0].target.map(t => t.name()),
Some("morphir"),
)
guard result.module_.definitions[0].definition is CustomType(type_) else {
fail("expected an external type")
}
assert_eq(type_.externals.all()[0].external.function, "CustomerId")
assert_eq(
@gleam.parse_text("@target(morphir) pub const x = 1").has_errors(),
true,
)
assert_eq(dialect.knows_target("go"), false)
}///|
test "mr: compose a separately defined target" {
let scala = @gleam.Profile(
"example/scala@1",
[@gleam.Target::extension("scala")][:],
)
let dialect = @gleam.Dialect::gleam_1_19()
.with_profile(@morphir.profile())
.with_profile(scala)
assert_eq(dialect.knows_target("morphir"), true)
assert_eq(dialect.knows_target("scala"), true)
let available = @gleam.Profiles([@morphir.profile(), scala][:])
let config = @gleam.Config::from_toml(
"[parser]\nprofiles = [\"moonrockz/gleam-parser-morphir@0.1.0\", \"example/scala@1\"]\n",
profiles=available,
).to_config(@gleam.Config::default())
assert_eq(config.dialect, dialect)
}test {
let dialect = @gleam.Dialect::gleam_1_19().with_profile(@morphir.profile())
let config = { ..@gleam.Config::default(), dialect, }
let result = @gleam.parse_text(
"@target(morphir) pub const answer = 42",
config~,
)
assert_eq(result.has_errors(), false)
assert_eq(dialect.knows_target("scala"), false)
}fn version() -> String