x/model: test Display funcs and add GoString
This commit is contained in:
parent
0b65220936
commit
c5768ceffe
@ -188,27 +188,30 @@ func (r Name) MapHash() uint64 {
|
|||||||
return h.Sum64()
|
return h.Sum64()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DisplayModel returns the a display string of the model.
|
||||||
func (r Name) DisplayModel() string {
|
func (r Name) DisplayModel() string {
|
||||||
return r.model
|
return r.model
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Name) DisplayFull() string {
|
// DisplayComplete returns a complete display string of the Name. For any
|
||||||
|
// part that is missing, a ("?") is used. If a complete name is not required
|
||||||
|
// and only the longest possible name is needed, use [Name.String] or
|
||||||
|
// String or check if [Name.Complete] is true before calling.
|
||||||
|
//
|
||||||
|
// It does not include the build.
|
||||||
|
func (r Name) DisplayComplete() string {
|
||||||
return (Name{
|
return (Name{
|
||||||
host: cmp.Or(r.host, "!(MISSING DOMAIN)"),
|
host: cmp.Or(r.host, "?"),
|
||||||
namespace: cmp.Or(r.namespace, "!(MISSING NAMESPACE)"),
|
namespace: cmp.Or(r.namespace, "?"),
|
||||||
model: cmp.Or(r.model, "!(MISSING NAME)"),
|
model: cmp.Or(r.model, "?"),
|
||||||
tag: cmp.Or(r.tag, "!(MISSING TAG)"),
|
tag: cmp.Or(r.tag, "?"),
|
||||||
build: cmp.Or(r.build, "!(MISSING BUILD)"),
|
|
||||||
}).String()
|
}).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisplayCompact returns a compact display string of the Name with only the
|
// GoString implements fmt.GoStringer. It is identical to
|
||||||
// model and tag parts.
|
// [Name.DisplayComplete], but works with fmt.Printf("%#v", name).
|
||||||
func (r Name) DisplayCompact() string {
|
func (r Name) GoString() string {
|
||||||
return (Name{
|
return r.DisplayComplete()
|
||||||
model: r.model,
|
|
||||||
tag: r.tag,
|
|
||||||
}).String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisplayShort returns a short display string of the Name with only the
|
// DisplayShort returns a short display string of the Name with only the
|
||||||
@ -217,7 +220,6 @@ func (r Name) DisplayShort() string {
|
|||||||
return (Name{
|
return (Name{
|
||||||
model: r.model,
|
model: r.model,
|
||||||
tag: r.tag,
|
tag: r.tag,
|
||||||
build: r.build,
|
|
||||||
}).String()
|
}).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,7 +230,6 @@ func (r Name) DisplayLong() string {
|
|||||||
namespace: r.namespace,
|
namespace: r.namespace,
|
||||||
model: r.model,
|
model: r.model,
|
||||||
tag: r.tag,
|
tag: r.tag,
|
||||||
build: r.build,
|
|
||||||
}).String()
|
}).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,57 +156,78 @@ func TestComplete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNameStringVariants(t *testing.T) {
|
func TestNameDisplay(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
in string
|
in string
|
||||||
nameAndTag string
|
wantShort string
|
||||||
nameTagAndBuild string
|
wantLong string
|
||||||
|
wantComplete string
|
||||||
|
wantString string
|
||||||
|
wantModel string
|
||||||
}{
|
}{
|
||||||
{"x/y/z:8n+I", "z:8n", "z:8n+I"},
|
{
|
||||||
{"x/y/z:8n", "z:8n", "z:8n"},
|
name: "Full Name with Build",
|
||||||
|
in: "example.com/library/mistral:latest+Q4_0",
|
||||||
|
wantShort: "mistral:latest",
|
||||||
|
wantLong: "library/mistral:latest",
|
||||||
|
wantComplete: "example.com/library/mistral:latest",
|
||||||
|
wantModel: "mistral",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Complete Name",
|
||||||
|
in: "example.com/library/mistral:latest+Q4_0",
|
||||||
|
wantShort: "mistral:latest",
|
||||||
|
wantLong: "library/mistral:latest",
|
||||||
|
wantComplete: "example.com/library/mistral:latest",
|
||||||
|
wantModel: "mistral",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Short Name",
|
||||||
|
in: "mistral:latest",
|
||||||
|
wantShort: "mistral:latest",
|
||||||
|
wantLong: "mistral:latest",
|
||||||
|
wantComplete: "?/?/mistral:latest",
|
||||||
|
wantModel: "mistral",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Long Name",
|
||||||
|
in: "library/mistral:latest",
|
||||||
|
wantShort: "mistral:latest",
|
||||||
|
wantLong: "library/mistral:latest",
|
||||||
|
wantComplete: "?/library/mistral:latest",
|
||||||
|
wantModel: "mistral",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Case Preserved",
|
||||||
|
in: "Library/Mistral:Latest",
|
||||||
|
wantShort: "Mistral:Latest",
|
||||||
|
wantLong: "Library/Mistral:Latest",
|
||||||
|
wantComplete: "?/Library/Mistral:Latest",
|
||||||
|
wantModel: "Mistral",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range cases {
|
for _, tt := range cases {
|
||||||
t.Run(tt.in, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := ParseName(tt.in)
|
p := ParseName(tt.in)
|
||||||
t.Logf("ParseName(%q) = %#v", tt.in, p)
|
if g := p.DisplayShort(); g != tt.wantShort {
|
||||||
if g := p.DisplayCompact(); g != tt.nameAndTag {
|
t.Errorf("DisplayShort = %q; want %q", g, tt.wantShort)
|
||||||
t.Errorf("ModelAndTag(%q) = %q; want %q", tt.in, g, tt.nameAndTag)
|
|
||||||
}
|
}
|
||||||
if g := p.DisplayShort(); g != tt.nameTagAndBuild {
|
if g := p.DisplayLong(); g != tt.wantLong {
|
||||||
t.Errorf("ModelTagAndBuild(%q) = %q; want %q", tt.in, g, tt.nameTagAndBuild)
|
t.Errorf("DisplayLong = %q; want %q", g, tt.wantLong)
|
||||||
}
|
}
|
||||||
})
|
if g := p.DisplayComplete(); g != tt.wantComplete {
|
||||||
|
t.Errorf("DisplayComplete = %q; want %q", g, tt.wantComplete)
|
||||||
}
|
}
|
||||||
|
if g := p.String(); g != tt.in {
|
||||||
|
t.Errorf("String(%q) = %q; want %q", tt.in, g, tt.in)
|
||||||
}
|
}
|
||||||
|
if g := p.Model(); g != tt.wantModel {
|
||||||
func TestNameFull(t *testing.T) {
|
t.Errorf("Model = %q; want %q", g, tt.wantModel)
|
||||||
const empty = "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/!(MISSING NAME):!(MISSING TAG)+!(MISSING BUILD)"
|
|
||||||
|
|
||||||
cases := []struct {
|
|
||||||
in string
|
|
||||||
wantFull string
|
|
||||||
}{
|
|
||||||
{"", empty},
|
|
||||||
{"ns/mistral:7b+x", "!(MISSING DOMAIN)/ns/mistral:7b+X"},
|
|
||||||
{"ns/mistral:7b+Q4_0", "!(MISSING DOMAIN)/ns/mistral:7b+Q4_0"},
|
|
||||||
{"example.com/x/mistral:latest", "example.com/x/mistral:latest+!(MISSING BUILD)"},
|
|
||||||
{"example.com/x/mistral:latest+Q4_0", "example.com/x/mistral:latest+Q4_0"},
|
|
||||||
|
|
||||||
{"mistral:7b+x", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+X"},
|
|
||||||
{"mistral:7b+q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
|
|
||||||
{"mistral:7b+Q4_0", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:7b+Q4_0"},
|
|
||||||
{"mistral:latest", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:latest+!(MISSING BUILD)"},
|
|
||||||
{"mistral", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:!(MISSING TAG)+!(MISSING BUILD)"},
|
|
||||||
{"mistral:30b", "!(MISSING DOMAIN)/!(MISSING NAMESPACE)/mistral:30b+!(MISSING BUILD)"},
|
|
||||||
}
|
}
|
||||||
|
if g, w := fmt.Sprintf("%#v", p), p.DisplayComplete(); g != w {
|
||||||
for _, tt := range cases {
|
t.Errorf("GoString() = %q; want %q", g, w)
|
||||||
t.Run(tt.in, func(t *testing.T) {
|
|
||||||
p := ParseName(tt.in)
|
|
||||||
t.Logf("ParseName(%q) = %#v", tt.in, p)
|
|
||||||
if g := p.DisplayFull(); g != tt.wantFull {
|
|
||||||
t.Errorf("DisplayFull(%q) = %q; want %q", tt.in, g, tt.wantFull)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user