Esempio n. 1
0
func TestIfControlCommand(t *testing.T) {
	input := `if header :contains "Subject" ["keyword1", "keyword2"] { discard :under "test"; stop; }`
	p := Parse("TestIfControlCommand", input)
	file := &ast.File{p.name, p.List}
	commandList := []ast.Command{
		&ast.ControlCommand{
			Name: "if",
			Test: &ast.GenericTest{"header",
				[]ast.Argument{
					ast.TagArgument(":contains"),
					ast.StringArgument([]string{"Subject"}),
					ast.StringArgument([]string{"keyword1", "keyword2"}),
				}},
			Block: []ast.Command{
				&ast.GenericCommand{
					"discard",
					[]ast.Argument{
						ast.TagArgument(":under"),
						ast.StringArgument([]string{"test"}),
					}},
				&ast.StopCommand{},
			},
		},
	}
	expected := &ast.File{
		Name: p.name,
		List: commandList,
	}
	if !ast.Equals(file, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), file.String())
	}
}
Esempio n. 2
0
func parseArguments(p *Parser) []ast.Argument {
	var al []ast.Argument
Loop:
	for {
		switch t := p.next(); {
		case t.Typ == NUMBER:
			al = append(al, ast.NumberArgument(t.Val))
		case t.Typ == TAG:
			al = append(al, ast.TagArgument(t.Val))
		case t.Typ == STRING:
			s, err := strconv.Unquote(t.Val)
			if err != nil {
				p.errorf(err.Error())
				return al
			}
			al = append(al, ast.StringArgument([]string{s}))
		case t.Typ == LEFTBRACKET:
			p.backup()
			al = append(al, ast.StringArgument(parseStrings(p)))
		case atTerminator(t):
			p.backup()
			break Loop
		default:
			p.errorf("Invalid Arguments statement at line %d:%d with token '%s', in file : %s\n", p.lineNumber(), t.Pos, t.Val, p.name)
		}
	}
	return al
}
Esempio n. 3
0
func TestGenericCommand(t *testing.T) {
	input := `header :contains "Subject" "subject keyword";`
	p := Parse("TestGenericCommand", input)
	file := &ast.File{p.name, p.List}
	commandList := []ast.Command{
		&ast.GenericCommand{"header", []ast.Argument{
			ast.TagArgument(":contains"),
			ast.StringArgument([]string{"Subject"}),
			ast.StringArgument([]string{"subject keyword"}),
		}},
	}
	expected := &ast.File{
		Name: p.name,
		List: commandList,
	}
	if !ast.Equals(file, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), file.String())
	}
}