dns/generate_test.go
Joel Sing ed07089f3b Correctly handle $GENERATE modifiers (#703)
* Add a ParseZone test for $GENERATE.

* Add a test for modToPrintf used by $GENERATE.

* Correctly handle $GENERATE modifiers.

As per http://www.zytrax.com/books/dns/ch8/generate.html, the width and type (aka base)
components of a modifier are optional. This means that ${2,0,d}, ${2,0} and ${2} are
valid modifiers, however only the first format was previously permitted. Use default
values for the width and/or type if they are unspecified in the modifier.
2018-06-23 09:12:44 +01:00

40 lines
1.1 KiB
Go

package dns
import (
"testing"
)
func TestGenerateModToPrintf(t *testing.T) {
tests := []struct {
mod string
wantFmt string
wantOffset int
wantErr bool
}{
{"0,0,d", "%0d", 0, false},
{"0,0", "%0d", 0, false},
{"0", "%0d", 0, false},
{"3,2,d", "%02d", 3, false},
{"3,2", "%02d", 3, false},
{"3", "%0d", 3, false},
{"0,0,o", "%0o", 0, false},
{"0,0,x", "%0x", 0, false},
{"0,0,X", "%0X", 0, false},
{"0,0,z", "", 0, true},
{"0,0,0,d", "", 0, true},
}
for _, test := range tests {
gotFmt, gotOffset, err := modToPrintf(test.mod)
switch {
case err != nil && !test.wantErr:
t.Errorf("modToPrintf(%q) - expected nil-error, but got %v", test.mod, err)
case err == nil && test.wantErr:
t.Errorf("modToPrintf(%q) - expected error, but got nil-error", test.mod)
case gotFmt != test.wantFmt:
t.Errorf("modToPrintf(%q) - expected format %q, but got %q", test.mod, test.wantFmt, gotFmt)
case gotOffset != test.wantOffset:
t.Errorf("modToPrintf(%q) - expected offset %d, but got %d", test.mod, test.wantOffset, gotOffset)
}
}
}