1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
package libhosty
import (
"net"
"strings"
"testing"
)
func TestLineFormatter(t *testing.T) {
// define custom hostsFileLine with IsCommented
hfl := HostsFileLine{
Number: 0,
Type: 30,
Address: net.ParseIP("1.1.1.1"),
Parts: []string{""},
Hostnames: []string{"my.host.name"},
Raw: "1.1.1.1 my.host.name # This is a host",
Comment: "This is a host",
IsCommented: true,
trimed: "1.1.1.1 my.host.name",
}
// invoke lineFormatter hosts file line
l := lineFormatter(hfl)
// define what we expect
w := "# 1.1.1.1 my.host.name #This is a host"
// check
if !strings.EqualFold(l, w) {
t.Fatalf(`IsCommented=true and Comment: wants '%q' got '%q'`, w, l)
}
// test without IsCommented
hfl.IsCommented = false
l = lineFormatter(hfl)
w = "1.1.1.1 my.host.name #This is a host"
if !strings.EqualFold(l, w) {
t.Fatalf(`IsCommented=false and Comment: wants '%q' got '%q'`, w, l)
}
// test with IsCommented but without comment in line
hfl.IsCommented = true
hfl.Comment = ""
l = lineFormatter(hfl)
w = "# 1.1.1.1 my.host.name"
if !strings.EqualFold(l, w) {
t.Fatalf(`IsCommented=true no Comment: wants '%q' got '%q'`, w, l)
}
// check without IsCommented
hfl.IsCommented = false
hfl.Comment = ""
l = lineFormatter(hfl)
w = "1.1.1.1 my.host.name"
if !strings.EqualFold(l, w) {
t.Fatalf(`IsCommented=false no Comment: wants '%q' got '%q'`, w, l)
}
// define a comment line
hfl = HostsFileLine{
Number: 0,
Type: 20,
Address: []byte{},
Parts: []string{},
Hostnames: []string{},
Raw: "# Comment Line",
Comment: "",
IsCommented: false,
trimed: "",
}
w = "# Comment line"
l = lineFormatter(hfl)
if !strings.EqualFold(l, w) {
t.Fatalf(`Comment: wants '%q' got '%q'`, w, l)
}
}
|