0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Finished dispenser tests

This commit is contained in:
Matthew Holt 2015-03-21 14:36:32 -06:00
parent 010ac23e8a
commit 5d4726446d
3 changed files with 148 additions and 9 deletions

View file

@ -123,10 +123,10 @@ func (d *dispenser) Args(targets ...*string) bool {
return enough return enough
} }
// RemainingArgs is a convenience function that loads any more arguments // RemainingArgs loads any more arguments (tokens on the same line)
// (tokens on the same line) into a slice and returns them. Open curly // into a slice and returns them. Open curly brace tokens also indicate
// brace tokens indicate the end of arguments (the curly brace is not // the end of arguments, and the curly brace is not included in
// included). // the return value nor is it loaded.
func (d *dispenser) RemainingArgs() []string { func (d *dispenser) RemainingArgs() []string {
var args []string var args []string
@ -149,7 +149,7 @@ func (d *dispenser) ArgErr() error {
if d.Val() == "{" { if d.Val() == "{" {
return d.Err("Unexpected token '{', expecting argument") return d.Err("Unexpected token '{', expecting argument")
} }
return d.Err("Unexpected line break after '" + d.Val() + "' (missing arguments?)") return d.Err("Unexpected line ending after '" + d.Val() + "' (missing arguments?)")
} }
// Err generates a custom parse error with a message of msg. // Err generates a custom parse error with a message of msg.

View file

@ -1,6 +1,7 @@
package config package config
import ( import (
"reflect"
"strings" "strings"
"testing" "testing"
) )
@ -153,6 +154,144 @@ func TestDispenser_NextBlock(t *testing.T) {
assertNextBlock(false, 8, 0) assertNextBlock(false, 8, 0)
} }
func TestDispenser_Args(t *testing.T) {
var s1, s2, s3 string
input := `dir1 arg1 arg2 arg3
dir2 arg4 arg5
dir3 arg6 arg7
dir4`
d := makeTestDispenser("test", input)
d.Next() // dir1
// As many strings as arguments
if all := d.Args(&s1, &s2, &s3); !all {
t.Error("Args(): Expected true, got false")
}
if s1 != "arg1" {
t.Errorf("Args(): Expected s1 to be 'arg1', got '%s'", s1)
}
if s2 != "arg2" {
t.Errorf("Args(): Expected s2 to be 'arg2', got '%s'", s2)
}
if s3 != "arg3" {
t.Errorf("Args(): Expected s3 to be 'arg3', got '%s'", s3)
}
d.Next() // dir2
// More strings than arguments
if all := d.Args(&s1, &s2, &s3); all {
t.Error("Args(): Expected false, got true")
}
if s1 != "arg4" {
t.Errorf("Args(): Expected s1 to be 'arg4', got '%s'", s1)
}
if s2 != "arg5" {
t.Errorf("Args(): Expected s2 to be 'arg5', got '%s'", s2)
}
if s3 != "arg3" {
t.Errorf("Args(): Expected s3 to be unchanged ('arg3'), instead got '%s'", s3)
}
// (quick cursor check just for kicks and giggles)
if d.cursor != 6 {
t.Errorf("Cursor should be 6, but is %d", d.cursor)
}
d.Next() // dir3
// More arguments than strings
if all := d.Args(&s1); !all {
t.Error("Args(): Expected true, got false")
}
if s1 != "arg6" {
t.Errorf("Args(): Expected s1 to be 'arg6', got '%s'", s1)
}
d.Next() // dir4
// No arguments or strings
if all := d.Args(); !all {
t.Error("Args(): Expected true, got false")
}
// No arguments but at least one string
if all := d.Args(&s1); all {
t.Error("Args(): Expected false, got true")
}
}
func TestDispenser_RemainingArgs(t *testing.T) {
input := `dir1 arg1 arg2 arg3
dir2 arg4 arg5
dir3 arg6 { arg7
dir4`
d := makeTestDispenser("test", input)
d.Next() // dir1
args := d.RemainingArgs()
if expected := []string{"arg1", "arg2", "arg3"}; !reflect.DeepEqual(args, expected) {
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
}
d.Next() // dir2
args = d.RemainingArgs()
if expected := []string{"arg4", "arg5"}; !reflect.DeepEqual(args, expected) {
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
}
d.Next() // dir3
args = d.RemainingArgs()
if expected := []string{"arg6"}; !reflect.DeepEqual(args, expected) {
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
}
d.Next() // {
d.Next() // arg7
d.Next() // dir4
args = d.RemainingArgs()
if len(args) != 0 {
t.Errorf("RemainingArgs(): Expected %v, got %v", []string{}, args)
}
}
func TestDispenser_ArgErr_Err(t *testing.T) {
input := `dir1 {
}
dir2 arg1 arg2`
d := makeTestDispenser("test", input)
d.cursor = 1 // {
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "{") {
t.Errorf("ArgErr(): Expected an error message with { in it, but got '%v'", err)
}
d.cursor = 5 // arg2
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "arg2") {
t.Errorf("ArgErr(): Expected an error message with 'arg2' in it; got '%v'", err)
}
err := d.Err("foobar")
if err == nil {
t.Fatalf("Err(): Expected an error, got nil")
}
if !strings.Contains(err.Error(), "test:3") {
t.Errorf("Expected error message with filename:line in it; got '%v'", err)
}
if !strings.Contains(err.Error(), "foobar") {
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
}
}
func makeTestDispenser(filename, input string) dispenser { func makeTestDispenser(filename, input string) dispenser {
return dispenser{ return dispenser{
filename: filename, filename: filename,

View file

@ -53,10 +53,10 @@ type (
// to fill the arguments, then true will be returned. // to fill the arguments, then true will be returned.
Args(...*string) bool Args(...*string) bool
// RemainingArgs is a convenience function that loads any more arguments // RemainingArgs loads any more arguments (tokens on the same line)
// (tokens on the same line) into a slice and returns them. If an open curly // into a slice and returns them. Open curly brace tokens also indicate
// brace token is encountered before the end of the line, that token is // the end of arguments, and the curly brace is not included in
// considered the end of the arguments (and the curly brace is not consumed). // the return value nor is it loaded.
RemainingArgs() []string RemainingArgs() []string
// ArgErr returns an argument error, meaning that another // ArgErr returns an argument error, meaning that another