diff --git a/pipeline.go b/pipeline.go index 2f71cd3..f452c8e 100644 --- a/pipeline.go +++ b/pipeline.go @@ -79,7 +79,11 @@ func CompareVariants(r *Responsive, ctx *Context) map[string]float64 { scores := make(map[string]float64) for i := range len(imprints) { for j := i + 1; j < len(imprints); j++ { - key := imprints[i].name + ":" + imprints[j].name + left, right := imprints[i].name, imprints[j].name + if right < left { + left, right = right, left + } + key := left + ":" + right scores[key] = imprints[i].imp.Similar(imprints[j].imp) } } diff --git a/pipeline_test.go b/pipeline_test.go index 4061a93..d6637b3 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -142,6 +142,25 @@ func TestCompareVariants(t *testing.T) { } } +func TestCompareVariants_CanonicalKeyOrder(t *testing.T) { + svc, _ := i18n.New() + i18n.SetDefault(svc) + ctx := NewContext() + + r := NewResponsive(). + Variant("mobile", NewLayout("C").C(El("p", Text("Hello")))). + Variant("desktop", NewLayout("C").C(El("p", Text("Hello")))) + + scores := CompareVariants(r, ctx) + + if _, ok := scores["desktop:mobile"]; !ok { + t.Fatalf("CompareVariants should canonicalise pair keys, got %v", scores) + } + if _, ok := scores["mobile:desktop"]; ok { + t.Fatalf("CompareVariants should not emit reversed duplicate keys, got %v", scores) + } +} + func TestCompareVariants_NilResponsive(t *testing.T) { scores := CompareVariants(nil, NewContext()) if len(scores) != 0 {