// Copyright (c) 2017, Daniel Martí // See LICENSE for licensing information package typedjson_test import ( "bytes" "encoding/json" "flag" "maps" "os" "path/filepath" "slices" "strings" "testing" "github.com/go-quicktest/qt" "mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax/typedjson" ) var update = flag.Bool("u", false, "update output files") func TestRoundtripZsh(t *testing.T) { t.Parallel() // ${a[(r)foo]} produces a FlagsArithm node, which only appears with zsh. src := "echo ${a[(r)foo]}\n" parser := syntax.NewParser(syntax.Variant(syntax.LangZsh)) node, err := parser.Parse(strings.NewReader(src), "") qt.Assert(t, qt.IsNil(err)) var buf bytes.Buffer qt.Assert(t, qt.IsNil(typedjson.Encode(&buf, node))) node2, err := typedjson.Decode(&buf) qt.Assert(t, qt.IsNil(err)) sb := new(strings.Builder) qt.Assert(t, qt.IsNil(syntax.NewPrinter().Print(sb, node2))) qt.Assert(t, qt.Equals(sb.String(), src)) } // allNodeNames lists the name of every [syntax.Node] type, // all of which must be encodable and decodable as a root node. var allNodeNames = []string{ "ArithmCmd", "ArithmExp", "ArrayElem", "ArrayExpr", "Assign", "BinaryArithm", "BinaryCmd", "BinaryTest", "Block", "BraceExp", "CStyleLoop", "CallExpr", "CaseClause", "CaseItem", "CmdSubst", "Comment", "CoprocClause", "DblQuoted", "DeclClause", "ExtGlob", "File", "FlagsArithm", "ForClause", "FuncDecl", "IfClause", "LetClause", "Lit", "ParamExp", "ParenArithm", "ParenTest", "ProcSubst", "Redirect", "SglQuoted", "Stmt", "Subshell", "TestClause", "TestDecl", "TimeClause", "UnaryArithm", "UnaryTest", "WhileClause", "Word", "WordIter", } // TestRoundtripAnyNode checks that any node can be encoded and decoded, // not just [syntax.File], and that every node type is covered. func TestRoundtripAnyNode(t *testing.T) { t.Parallel() tests := []struct { lang syntax.LangVariant src string }{ {syntax.LangBash, ` foo=bar qux >redir <