| // 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 ( |
| "fmt" |
| "testing" |
| ) |
| |
| func TestChange(t *testing.T) { |
| var p *Proc |
| if p.Change("foo", "bar") != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| tests := []struct { |
| initial, old, new, outExp string |
| }{ |
| {"a", "a", "b", "b"}, |
| {"aa", "a", "b", "bb"}, |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q)[%d]", tt.old, tt.new, i) |
| t.Run(testname, func(t *testing.T) { |
| p := NewProcFromString(tt.initial).Change(tt.old, tt.new) |
| out := p.replacement |
| if out != tt.outExp { |
| t.Errorf("want %s, got %s", tt.outExp, out) |
| } |
| }) |
| } |
| } |
| |
| func TestDelete(t *testing.T) { |
| var p *Proc |
| if p.Delete() != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo") |
| if p.replacement != "foo" { |
| t.Error("expected p.replacement = 'foo'") |
| } |
| p = p.Delete() |
| if p.replacement != "" { |
| t.Error("expected p.replacement = ''") |
| } |
| } |
| |
| func TestAppend(t *testing.T) { |
| var p *Proc |
| if p.Append("bar") != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo") |
| if p.replacement != "foo" { |
| t.Error("expected p.replacement = 'foo'") |
| } |
| p = p.Append("bar") |
| if p.replacement != "foobar" { |
| t.Error("expected p.replacement = 'foobar'") |
| } |
| } |
| |
| func TestAddIfNeededOperator(t *testing.T) { |
| var p *Proc |
| if p.AddIfNeeded("bar", "bim", "baz") != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| tests := []struct { |
| text, key, loc, addition, expRep string |
| expNil bool |
| }{ |
| {"", "", "", "", "", false}, |
| {"foo", "fo", "baz", "bim", "", true}, // Key found, but location not. |
| {"foo", "fo", "[[[", "bim", "", true}, // Bad regex. |
| {"foo", "[[[", "baz", "bim", "", true}, // Bad regex. |
| {"foo", "bar", "baz", "bim", "foo", false}, // Key not found; no change. |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q, %q)[%d]", tt.text, tt.key, tt.loc, tt.addition, i) |
| t.Run(testname, func(t *testing.T) { |
| p = NewProcFromString(tt.text) |
| p := p.AddIfNeeded(tt.key, tt.loc, tt.addition) |
| if p == nil { |
| if !tt.expNil { |
| t.Error("expected nil Proc") |
| } |
| } else { |
| if tt.expNil { |
| t.Error("expected non-nil Proc") |
| } |
| if p.replacement != tt.expRep { |
| t.Errorf("want replacement: %q, got %q", tt.expRep, p.replacement) |
| } |
| } |
| }) |
| } |
| } |
| func TestRemoveIfNotNeededOperator(t *testing.T) { |
| var p *Proc |
| if p.RemoveIfNotNeeded("bar", "bim") != nil { |
| t.Error("expected nil Proc") |
| } |
| |
| tests := []struct { |
| text, key, remText, expRep string |
| expNil bool |
| }{ |
| {"", "", "", "", false}, |
| {"foo", "bar", "baz", "foo", false}, // No remText; no change. |
| {"foobaz\nbar", "[[[", "baz", "foobaz", true}, // Bad Regex |
| {"foobaz\nbar", "bim", "baz", "foobar", false}, // Key not found, so remove. |
| } |
| |
| for i, tt := range tests { |
| testname := fmt.Sprintf("(%q, %q, %q)[%d]", tt.text, tt.key, tt.remText, i) |
| t.Run(testname, func(t *testing.T) { |
| p = NewProcFromString(tt.text) |
| p := p.RemoveIfNotNeeded(tt.key, tt.remText) |
| if p == nil { |
| if !tt.expNil { |
| t.Error("expected nil Proc") |
| } |
| } else { |
| if tt.expNil { |
| t.Error("expected non-nil Proc") |
| } |
| if p.replacement != tt.expRep { |
| t.Errorf("want replacement: %q, got %q", tt.expRep, p.replacement) |
| } |
| } |
| }) |
| } |
| } |
| |
| func TestReplace(t *testing.T) { |
| var p *Proc |
| if p.Replace("bar") != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo") |
| if p.replacement != "foo" { |
| t.Error("expected p.replacement = 'foo'") |
| } |
| p = p.Replace("bar") |
| if p.replacement != "bar" { |
| t.Error("expected p.replacement = 'bar'") |
| } |
| } |