| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package state |
| |
| import ( |
| "reflect" |
| "testing" |
| ) |
| |
| func TestStalePaths(t *testing.T) { |
| tests := []struct { |
| name string |
| oldPaths []string |
| newPaths []string |
| stalePaths []string |
| }{ |
| { |
| name: "when there are no existing paths", |
| oldPaths: []string{}, |
| newPaths: []string{"a", "b", "c"}, |
| stalePaths: []string{}, |
| }, |
| { |
| name: "when existing paths are removed", |
| oldPaths: []string{"a", "b", "c", "d.md"}, |
| newPaths: []string{"a"}, |
| stalePaths: []string{"b", "c"}, |
| }, |
| { |
| name: "when paths are unchanged", |
| oldPaths: []string{"a", "b", "c"}, |
| newPaths: []string{"a", "b", "c"}, |
| stalePaths: []string{}, |
| }, |
| { |
| name: "when existing paths remain and new ones are added", |
| oldPaths: []string{"a", "b", "c"}, |
| newPaths: []string{"a", "b", "c", "d", "e"}, |
| stalePaths: []string{}, |
| }, |
| { |
| name: "when an old path is a prefix of all new paths", |
| oldPaths: []string{"a", "a/d"}, |
| newPaths: []string{"a/b", "a/c"}, |
| stalePaths: []string{"a/d"}, |
| }, |
| { |
| name: "when an old path is a prefix of at least one new path", |
| oldPaths: []string{"a"}, |
| newPaths: []string{"a/b", "c/d"}, |
| stalePaths: []string{}, |
| }, |
| { |
| name: "when an old path is not a prefix of any new path", |
| oldPaths: []string{"a"}, |
| newPaths: []string{"b/c", "c/d"}, |
| stalePaths: []string{"a"}, |
| }, |
| } |
| |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| paths := stalePaths(tt.oldPaths, tt.newPaths) |
| if !reflect.DeepEqual(paths, tt.stalePaths) { |
| t.Fatalf("wanted:\n%#v\ngot:\n%#v\n", tt.stalePaths, paths) |
| } |
| }) |
| } |
| } |