<commit_before>package parse

import (
	"fmt"
	"os"
	"reflect"
	"testing"

	"github.com/xiaq/elvish/util"
)

func compoundOfOnePrimary(p *PrimaryNode) *CompoundNode {
	return newCompound(p.Pos, NoSigil, &SubscriptNode{p.Pos, p, nil})
}

func compoundOfBare(p Pos, s string) *CompoundNode {
	return compoundOfOnePrimary(
		&PrimaryNode{p, StringPrimary, newString(p, s, s)})
}

// formWithRedir returns the expected FormNode of a "a" command followed by
// status and output redirections.
func formWithRedir(sr string, rs ...Redir) *FormNode {
	return &FormNode{0, compoundOfBare(0, "a"), newSpaced(1), rs, sr}
}

// formWithOnePrimary returns the expected FormNode of a "a" command followed
// by exactly one primary expression.
func formWithOnePrimary(p *PrimaryNode) *FormNode {
	return &FormNode{0,
		compoundOfBare(0, "a"),
		newSpaced(2, compoundOfOnePrimary(p)), nil, ""}
}

func chunkOfOneForm(f *FormNode) *ChunkNode {
	return newChunk(f.Pos, newPipeline(f.Pos, f))
}

func chunkOfFormWithRedir(sr string, rs ...Redir) *ChunkNode {
	return chunkOfOneForm(formWithRedir(sr, rs...))
}

func chunkOfFormWithOnePrimary(p *PrimaryNode) *ChunkNode {
	return chunkOfOneForm(formWithOnePrimary(p))
}

var parseTests = []struct {
	in     string
	wanted Node
}{
	// Empty chunk
	{"", newChunk(0)},

	// Command with arguments
	{"ls x y", chunkOfOneForm(
		&FormNode{0,
			compoundOfBare(0, "ls"),
			newSpaced(3,
				compoundOfBare(3, "x"),
				compoundOfBare(5, "y")),
			nil, ""})},

	// Multiple pipelines, multiple commands<commit_msg>Add a parse test case<commit_after>package parse

import (
	"fmt"
	"os"
	"reflect"
	"testing"

	"github.com/xiaq/elvish/util"
)

func compoundOfOnePrimary(p *PrimaryNode) *CompoundNode {
	return newCompound(p.Pos, NoSigil, &SubscriptNode{p.Pos, p, nil})
}

func compoundOfBare(p Pos, s string) *CompoundNode {
	return compoundOfOnePrimary(
		&PrimaryNode{p, StringPrimary, newString(p, s, s)})
}

// formWithRedir returns the expected FormNode of a "a" command followed by
// status and output redirections.
func formWithRedir(sr string, rs ...Redir) *FormNode {
	return &FormNode{0, compoundOfBare(0, "a"), newSpaced(1), rs, sr}
}

// formWithOnePrimary returns the expected FormNode of a "a" command followed
// by exactly one primary expression.
func formWithOnePrimary(p *PrimaryNode) *FormNode {
	return &FormNode{0,
		compoundOfBare(0, "a"),
		newSpaced(2, compoundOfOnePrimary(p)), nil, ""}
}

func chunkOfOneForm(f *FormNode) *ChunkNode {
	return newChunk(f.Pos, newPipeline(f.Pos, f))
}

func chunkOfFormWithRedir(sr string, rs ...Redir) *ChunkNode {
	return chunkOfOneForm(formWithRedir(sr, rs...))
}

func chunkOfFormWithOnePrimary(p *PrimaryNode) *ChunkNode {
	return chunkOfOneForm(formWithOnePrimary(p))
}

var parseTests = []struct {
	in     string
	wanted Node
}{
	// Empty chunk
	{"", newChunk(0)},

	// Command with arguments
	{"ls x y", chunkOfOneForm(
		&FormNode{0,
			compoundOfBare(0, "ls"),
			newSpaced(3,
				compoundOfBare(3, "x"),
				compoundOfBare(5, "y")),
			nil, ""})},

	// Wow... such whitespace, much unnecessary, so valid
	{"  ;\n\n  ls   ;\n", newChunk(0,
		newPipeline(7,
			&FormNode{7, compoundOfBare(7, "ls"), newSpaced(12), nil, ""}))},

	// Multiple pipelines, multiple commands
{'fraction_non_alphanumeric': 0.1429886468241792, 'fraction_numerical': 0.009205277692543725, 'mean_word_length': 4.790408525754884, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6468930', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1178, 'n_words': 296}