| // Copyright 2021 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 codifier |
| |
| import ( |
| "bytes" |
| "fmt" |
| "strings" |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| ) |
| |
| func TestNewProc(t *testing.T) { |
| p := newProc() |
| if p.Store == nil { |
| t.Error("no store in NewProc") |
| } |
| if len(p.Store) != 0 { |
| t.Error("store not size 0 in NewProc") |
| } |
| } |
| |
| func TestRead(t *testing.T) { |
| r := strings.NewReader("foo") |
| |
| var p *Proc |
| p = p.read(r) |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| p = newProc().read(r) |
| if p == nil { |
| t.Error("read error") |
| } |
| if p.original != "foo" { |
| t.Error("read into contents error") |
| } |
| if p.replacement != "foo" { |
| t.Error("read into contents error") |
| } |
| |
| p = p.read(nil) |
| if p != nil { |
| t.Error("nil reader") |
| } |
| } |
| |
| func TestSetFuchsiaDir(t *testing.T) { |
| var p *Proc |
| p = p.setFuchsiaDir("foo") |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| p = newProc().setFuchsiaDir("foo") |
| if p == nil { |
| t.Error("nil proc") |
| } |
| if p.fuchsiaDir != "foo" { |
| t.Error("setFuchsiaDir error") |
| } |
| } |
| |
| func TestSetFilename(t *testing.T) { |
| var p *Proc |
| p = p.setFilename("foo") |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| p = newProc().setFilename("foo") |
| if p == nil { |
| t.Error("nil proc") |
| } |
| if p.filename != "foo" { |
| t.Error("setFilename error") |
| } |
| } |
| |
| func TestOriginalContents(t *testing.T) { |
| var p *Proc |
| p.OriginalContents() |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| p = newProc().read(strings.NewReader("foo")) |
| |
| s := p.OriginalContents() |
| if p == nil { |
| t.Error("nil proc") |
| } |
| if s != "foo" { |
| t.Error("OriginalContents error") |
| } |
| } |
| |
| func TestChangedFilesList(t *testing.T) { |
| var p *Proc |
| _, err := p.ChangedFilesList() |
| if err == nil { |
| t.Error("expected error due to nil Proc") |
| } |
| |
| p = NewProcFromString("foo") |
| s, err := p.ChangedFilesList() |
| if err != nil { |
| t.Error("expected nil error") |
| } |
| if len(s) != 0 { |
| t.Error("ChangedFilesList non-empty") |
| } |
| p.changedFiles = []string{"foo"} |
| s, err = p.ChangedFilesList() |
| if err != nil { |
| t.Error("expected nil error") |
| } |
| if len(s) != 1 { |
| t.Error("ChangedFilesList not length 1") |
| } |
| } |
| |
| func TestWrite(t *testing.T) { |
| var p *Proc |
| b := new(bytes.Buffer) |
| p = p.write(b) |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| p = NewProcFromString("foo").write(nil) |
| if p != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| b = new(bytes.Buffer) |
| p = NewProcFromString("foo").setFilename("bar").write(b) |
| if p == nil { |
| t.Error("write error") |
| } |
| got := b.String() |
| if got != "foo" { |
| t.Errorf("write error: got %q, want %q", got, p.OriginalContents()) |
| } |
| if diff := cmp.Diff(orderedStrings{"bar"}, p.changedFiles); diff != "" { |
| t.Errorf("changedFiles mismatch (-want +got):\n%s", diff) |
| } |
| } |
| |
| func TestPrintItem(t *testing.T) { |
| testList := []struct { |
| key string |
| value interface{} |
| length int |
| want string |
| }{ |
| {"", "", 0, ""}, |
| {"foo", "", 0, ""}, |
| {"foo", "bar", 0, ""}, |
| {"", "", 1, ""}, |
| {"foo", "", 1, ""}, |
| {"foo", "bar", 1, "b"}, |
| {"", "", 20, ""}, |
| {"foo", "", 20, ""}, |
| {"foo", "bar", 20, "bar"}, |
| |
| {"", []string{""}, 0, ""}, |
| {"foo", []string{""}, 0, ""}, |
| {"foo", []string{"bar"}, 0, ""}, |
| {"", []string{""}, 1, ""}, |
| {"foo", []string{""}, 1, ""}, |
| {"foo", []string{"bar"}, 1, "b"}, |
| {"", []string{""}, 20, ""}, |
| {"foo", []string{""}, 20, ""}, |
| {"foo", []string{"bar"}, 20, "bar"}, |
| |
| {"", [][]string{{""}}, 20, " [unknown type [][]string]"}, |
| {"foo", [][]string{{""}}, 20, " [unknown type [][]string]"}, |
| {"foo", [][]string{{"bar"}}, 20, " [unknown type [][]string]"}, |
| } |
| |
| for i, tt := range testList { |
| testname := fmt.Sprintf("(%q, %q, %d)[%d]", tt.key, tt.value, tt.length, i) |
| t.Run(testname, func(t *testing.T) { |
| got := printItem(tt.key, tt.value, tt.length) |
| if got != tt.want { |
| t.Errorf("printItem() want %q, got %q)\n", tt.want, got) |
| } |
| // if diff := cmp.Diff(tt.want, got); diff != "" { |
| // t.Errorf("printItem() mismatch (-want +got):\n%s", diff) |
| // } |
| }) |
| } |
| } |
| |
| func TestCopy(t *testing.T) { |
| var p *Proc |
| if p.copy() != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo") |
| r := NewProcFromString("ffo") |
| p.parent = r |
| p.setFuchsiaDir("baz") |
| p.buildSuccess = true |
| p.testSuccess = true |
| p.setFilename("bar") |
| p.changedFiles = orderedStrings{"a", "b", "c"} |
| p.Store["k1"] = "v1" |
| p.Store["k2"] = "v2" |
| |
| q := p.copy() |
| |
| if q.original != "foo" { |
| t.Error("copy failed on original") |
| } |
| if q.replacement != "foo" { |
| t.Error("copy failed on replacement") |
| } |
| if q.parent != r { |
| t.Error("copy failed on original") |
| } |
| if q.fuchsiaDir != "baz" { |
| t.Error("copy failed on filename") |
| } |
| if q.filename != "bar" { |
| t.Error("copy failed on filename") |
| } |
| // The copied changed files is set to nil. |
| if len(q.changedFiles) != 0 { |
| t.Error("copy failed on changedFiles length") |
| } |
| if len(q.Store) != 2 { |
| t.Error("copy failed on store length") |
| } |
| for k, v := range p.Store { |
| if q.Store[k] != v { |
| t.Error("copy failed on store") |
| } |
| } |
| } |