| // Copyright 2021 The Fuchsia Authors. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package compare |
| |
| import ( |
| "testing" |
| |
| "github.com/texttheater/golang-levenshtein/levenshtein" |
| ) |
| |
| func TestLevenshteinComparator(t *testing.T) { |
| t.Parallel() |
| |
| tests := []struct { |
| name string |
| s1 string |
| s2 string |
| expectedScore float64 |
| }{ |
| { |
| name: "substitution", |
| s1: "[9/10] ACTION\n\n//path/to/binary(//build/foobar:x64)", |
| s2: "[8/11] ACTION\n\n//path/to/binary(//build/foobar:x64)", |
| // ratio = (len(s1) + len(s2) - levDist) / len(s1) + len(s2). |
| // Each substition costs 2. |
| expectedScore: float64(51+51-4) / (51 + 51), |
| }, |
| { |
| name: "addition and subtraction", |
| s1: "[9/10] ACTION\n\n//path/to/binary(//build/foobar:x64)", |
| s2: "[8/11] ACTION\n\n//path/to/binary(//build/foo:x64)\n\nFAILED: binary exe.unstripped/binary", |
| // Each addition or subtraction costs 1. |
| expectedScore: float64(51+86-45) / (51 + 86), |
| }, |
| // The order of inputs should not influence the score. |
| { |
| name: "reordering", |
| s1: "[8/11] ACTION\n\n//path/to/binary(//build/foo:x64)\n\nFAILED: binary exe.unstripped/binary", |
| s2: "[9/10] ACTION\n\n//path/to/binary(//build/foobar:x64)", |
| expectedScore: float64(51+86-45) / (51 + 86), |
| }, |
| } |
| |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| lc := LevenshteinComparator{Opts: levenshtein.DefaultOptions} |
| score := lc.Compare(test.s1, test.s2) |
| if score != test.expectedScore { |
| t.Fatalf("expected score %0.3f, got %0.3f", test.expectedScore, score) |
| } |
| }) |
| } |
| } |