add some node transform specs

This commit is contained in:
leaf corcoran 2020-11-22 17:45:53 -08:00
parent 8ae336bd05
commit da1b28b1da

View File

@ -1,11 +1,107 @@
import with_dev from require "spec.helpers"
describe "moonscript.transform.destructure", ->
local extract_assign_names
local extract_assign_names, split_assign, Block
with_dev ->
{ :extract_assign_names } = require "moonscript.transform.destructure"
{ :extract_assign_names, :split_assign } = require "moonscript.transform.destructure"
{:Block} = require "moonscript.compile"
describe "split_assign #fff", ->
-- {:hello} = world
it "simple assignment", ->
node = {
"assign"
{
{ "table", {
{{"key_literal", "hello"}, {"ref", "hello"}}
}
}
}
{
{"ref", "world"}
}
}
out = split_assign Block!, node
assert.same {
"group", {
{
"group", {
{ "declare", { {"ref", "hello"} } }
{ "assign", { {"ref", "hello"} }, { {"chain", {"ref", "world"}, {"dot", "hello"}} } }
}
}
}
}, out
-- a, {:hello} = one, two
it "multiple assigns", ->
node = {
"assign"
{
{"ref", "a"}
{ "table", {
{{"key_literal", "hello"}, {"ref", "hello"}}
}
}
}
{
{"ref", "one"}
{"ref", "two"}
}
}
out = split_assign Block!, node
assert.same {
"group", {
{"assign", { {"ref", "a"} }, { {"ref", "one"} }}
{
"group", {
{ "declare", { {"ref", "hello"} } }
{ "assign", { {"ref", "hello"} }, { {"chain", {"ref", "two"}, {"dot", "hello"}} } }
}
}
}
}, out
-- {:hello}, a = one, two
it "multiple assigns swapped #ddd", ->
node = {
"assign"
{
{ "table", {
{{"key_literal", "hello"}, {"ref", "hello"}}
}
}
{"ref", "a"}
}
{
{"ref", "one"}
{"ref", "two"}
}
}
out = split_assign Block!, node
assert.same {
"group", {
{
"group", {
{ "declare", { {"ref", "hello"} } }
{ "assign", { {"ref", "hello"} }, { {"chain", {"ref", "one"}, {"dot", "hello"}} } }
}
}
{"assign", { {"ref", "a"} }, { {"ref", "two"} }}
}
}, out
it "extracts names from table destructure", ->
des = {