Skip to content

Commit

Permalink
feat(ast): derive PartialEq and Eq for testing (#259)
Browse files Browse the repository at this point in the history
- only applies when `cfg_attr(test)`
  • Loading branch information
39555 authored Nov 5, 2024
1 parent 39432e4 commit a37e470
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
41 changes: 41 additions & 0 deletions brush-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DISPLAY_INDENT: &str = " ";
/// Represents a complete shell program.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Program {
/// A sequence of complete shell commands.
pub complete_commands: Vec<CompleteCommand>,
Expand All @@ -33,6 +34,7 @@ pub type CompleteCommandItem = CompoundListItem;
/// Indicates whether the preceding command is executed synchronously or asynchronously.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum SeparatorOperator {
/// The preceding command is executed asynchronously.
Async,
Expand All @@ -52,6 +54,7 @@ impl Display for SeparatorOperator {
/// Represents a sequence of command pipelines connected by boolean operators.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct AndOrList {
/// The first command pipeline.
pub first: Pipeline,
Expand All @@ -74,6 +77,7 @@ impl Display for AndOrList {
/// succeeding pipeline.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum AndOr {
/// Boolean AND operator; the embedded pipeline is only to be executed if the
/// preceding command has succeeded.
Expand All @@ -96,6 +100,7 @@ impl Display for AndOr {
/// to the command that follows it.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Pipeline {
/// Indicates whether the result of the overall pipeline should be the logical
/// negation of the result of the pipeline.
Expand Down Expand Up @@ -123,6 +128,7 @@ impl Display for Pipeline {
/// Represents a shell command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Command {
/// A simple command, directly invoking an external command, a built-in command,
/// a shell function, or similar.
Expand Down Expand Up @@ -157,6 +163,7 @@ impl Display for Command {
/// Represents a compound command, potentially made up of multiple nested commands.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CompoundCommand {
/// An arithmetic command, evaluating an arithmetic expression.
Arithmetic(ArithmeticCommand),
Expand Down Expand Up @@ -208,6 +215,7 @@ impl Display for CompoundCommand {
/// An arithmetic command, evaluating an arithmetic expression.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct ArithmeticCommand {
/// The raw, unparsed and unexpanded arithmetic expression.
pub expr: UnexpandedArithmeticExpr,
Expand All @@ -222,6 +230,7 @@ impl Display for ArithmeticCommand {
/// A subshell, which executes commands in a subshell.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct SubshellCommand(pub CompoundList);

impl Display for SubshellCommand {
Expand All @@ -235,6 +244,7 @@ impl Display for SubshellCommand {
/// A for clause, which loops over a set of values.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct ForClauseCommand {
/// The name of the iterator variable.
pub variable_name: String,
Expand Down Expand Up @@ -267,6 +277,7 @@ impl Display for ForClauseCommand {
/// An arithmetic for clause, which loops until an arithmetic condition is reached.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct ArithmeticForClauseCommand {
/// Optionally, the initializer expression evaluated before the first iteration of the loop.
pub initializer: Option<UnexpandedArithmeticExpr>,
Expand Down Expand Up @@ -308,6 +319,7 @@ impl Display for ArithmeticForClauseCommand {
/// pattern-based filters.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CaseClauseCommand {
/// The value being matched on.
pub value: Word,
Expand All @@ -329,6 +341,7 @@ impl Display for CaseClauseCommand {
/// A sequence of commands.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CompoundList(pub Vec<CompoundListItem>);

impl Display for CompoundList {
Expand Down Expand Up @@ -356,6 +369,7 @@ impl Display for CompoundList {
/// An element of a compound command list.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CompoundListItem(pub AndOrList, pub SeparatorOperator);

impl Display for CompoundListItem {
Expand All @@ -369,6 +383,7 @@ impl Display for CompoundListItem {
/// An if clause, which conditionally executes a command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct IfClauseCommand {
/// The command whose execution result is inspected.
pub condition: CompoundList,
Expand Down Expand Up @@ -402,6 +417,7 @@ impl Display for IfClauseCommand {
/// Represents the `else` clause of a conditional command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct ElseClause {
/// If present, the condition that must be met for this `else` clause to be executed.
pub condition: Option<CompoundList>,
Expand Down Expand Up @@ -429,6 +445,7 @@ impl Display for ElseClause {
/// An individual matching case item in a case clause.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CaseItem {
/// The patterns that select this case branch.
pub patterns: Vec<Word>,
Expand Down Expand Up @@ -460,6 +477,7 @@ impl Display for CaseItem {
/// Describes the action to take after executing the body command of a case clause.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CaseItemPostAction {
/// The containing case should be exited.
ExitCase,
Expand All @@ -484,6 +502,7 @@ impl Display for CaseItemPostAction {
/// A while or until clause, whose looping is controlled by a condition.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct WhileOrUntilClauseCommand(pub CompoundList, pub DoGroupCommand);

impl Display for WhileOrUntilClauseCommand {
Expand All @@ -495,6 +514,7 @@ impl Display for WhileOrUntilClauseCommand {
/// Encapsulates the definition of a shell function.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct FunctionDefinition {
/// The name of the function.
pub fname: String,
Expand All @@ -515,6 +535,7 @@ impl Display for FunctionDefinition {
/// Encapsulates the body of a function definition.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct FunctionBody(pub CompoundCommand, pub Option<RedirectList>);

impl Display for FunctionBody {
Expand All @@ -531,6 +552,7 @@ impl Display for FunctionBody {
/// A brace group, which groups commands together.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct BraceGroupCommand(pub CompoundList);

impl Display for BraceGroupCommand {
Expand All @@ -547,6 +569,7 @@ impl Display for BraceGroupCommand {
/// A do group, which groups commands together.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct DoGroupCommand(pub CompoundList);

impl Display for DoGroupCommand {
Expand All @@ -561,6 +584,7 @@ impl Display for DoGroupCommand {
/// Represents the invocation of a simple command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct SimpleCommand {
/// Optionally, a prefix to the command.
pub prefix: Option<CommandPrefix>,
Expand Down Expand Up @@ -607,6 +631,7 @@ impl Display for SimpleCommand {
/// Represents a prefix to a simple command.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CommandPrefix(pub Vec<CommandPrefixOrSuffixItem>);

impl Display for CommandPrefix {
Expand All @@ -625,6 +650,7 @@ impl Display for CommandPrefix {
/// Represents a suffix to a simple command; a word argument, declaration, or I/O redirection.
#[derive(Clone, Default, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CommandSuffix(pub Vec<CommandPrefixOrSuffixItem>);

impl Display for CommandSuffix {
Expand All @@ -643,6 +669,7 @@ impl Display for CommandSuffix {
/// Represents the I/O direction of a process substitution.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum ProcessSubstitutionKind {
/// The process is read from.
Read,
Expand All @@ -662,6 +689,7 @@ impl Display for ProcessSubstitutionKind {
/// A prefix or suffix for a simple command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CommandPrefixOrSuffixItem {
/// An I/O redirection.
IoRedirect(IoRedirect),
Expand Down Expand Up @@ -689,6 +717,7 @@ impl Display for CommandPrefixOrSuffixItem {
/// Encapsulates an assignment declaration.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Assignment {
/// Name being assigned to.
pub name: AssignmentName,
Expand All @@ -711,6 +740,7 @@ impl Display for Assignment {
/// The target of an assignment.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum AssignmentName {
/// A named variable.
VariableName(String),
Expand All @@ -732,6 +762,7 @@ impl Display for AssignmentName {
/// A value being assigned to a variable.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum AssignmentValue {
/// A scalar (word) value.
Scalar(Word),
Expand Down Expand Up @@ -763,6 +794,7 @@ impl Display for AssignmentValue {
/// A list of I/O redirections to be applied to a command.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct RedirectList(pub Vec<IoRedirect>);

impl Display for RedirectList {
Expand All @@ -777,6 +809,7 @@ impl Display for RedirectList {
/// An I/O redirection.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum IoRedirect {
/// Redirection to a file.
File(Option<u32>, IoFileRedirectKind, IoFileRedirectTarget),
Expand Down Expand Up @@ -844,6 +877,7 @@ impl Display for IoRedirect {
/// Kind of file I/O redirection.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum IoFileRedirectKind {
/// Read (`<`).
Read,
Expand Down Expand Up @@ -878,6 +912,7 @@ impl Display for IoFileRedirectKind {
/// Target for an I/O file redirection.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum IoFileRedirectTarget {
/// Path to a file.
Filename(Word),
Expand All @@ -903,6 +938,7 @@ impl Display for IoFileRedirectTarget {
/// Represents an I/O here document.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct IoHereDocument {
/// Whether to remove leading tabs from the here document.
pub remove_tabs: bool,
Expand Down Expand Up @@ -953,6 +989,7 @@ impl Display for TestExpr {
/// An extended test expression.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum ExtendedTestExpr {
/// Logical AND operation on two nested expressions.
And(Box<ExtendedTestExpr>, Box<ExtendedTestExpr>),
Expand Down Expand Up @@ -996,6 +1033,7 @@ impl Display for ExtendedTestExpr {
/// A unary predicate usable in an extended test expression.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum UnaryPredicate {
/// Computes if the operand is a path to an existing file.
FileExists,
Expand Down Expand Up @@ -1084,6 +1122,7 @@ impl Display for UnaryPredicate {
/// A binary predicate usable in an extended test expression.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum BinaryPredicate {
/// Computes if two files refer to the same device and inode numbers.
FilesReferToSameDeviceAndInodeNumbers,
Expand Down Expand Up @@ -1142,6 +1181,7 @@ impl Display for BinaryPredicate {
/// Represents a shell word.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Word {
/// Raw text of the word.
pub value: String,
Expand Down Expand Up @@ -1189,6 +1229,7 @@ impl Word {
/// Encapsulates an unparsed arithmetic expression.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz-testing", derive(arbitrary::Arbitrary))]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct UnexpandedArithmeticExpr {
/// The raw text of the expression.
pub value: String,
Expand Down
Loading

0 comments on commit a37e470

Please sign in to comment.