1// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package parser
16
17import (
18	"bytes"
19	"testing"
20)
21
22func TestPatchList(t *testing.T) {
23	expectOverlap := func(err error) {
24		t.Helper()
25		if _, ok := err.(PatchOverlapError); !ok {
26			t.Error("missing PatchOverlapError")
27		}
28	}
29
30	expectOk := func(err error) {
31		t.Helper()
32		if err != nil {
33			t.Error(err)
34		}
35	}
36
37	in := []byte("abcdefghijklmnopqrstuvwxyz")
38
39	patchlist := PatchList{}
40	expectOk(patchlist.Add(0, 3, "ABC"))
41	expectOk(patchlist.Add(12, 15, "MNO"))
42	expectOk(patchlist.Add(24, 26, "Z"))
43	expectOk(patchlist.Add(15, 15, "_"))
44
45	expectOverlap(patchlist.Add(0, 3, "x"))
46	expectOverlap(patchlist.Add(12, 13, "x"))
47	expectOverlap(patchlist.Add(13, 14, "x"))
48	expectOverlap(patchlist.Add(14, 15, "x"))
49	expectOverlap(patchlist.Add(11, 13, "x"))
50	expectOverlap(patchlist.Add(12, 15, "x"))
51	expectOverlap(patchlist.Add(11, 15, "x"))
52	expectOverlap(patchlist.Add(15, 15, "x"))
53
54	if t.Failed() {
55		return
56	}
57
58	buf := new(bytes.Buffer)
59	patchlist.Apply(bytes.NewReader(in), buf)
60	expected := "ABCdefghijklMNO_pqrstuvwxZ"
61	got := buf.String()
62	if got != expected {
63		t.Errorf("expected %q, got %q", expected, got)
64	}
65}
66