// Copyright 2012 Aryan Naraghi (aryan.naraghi@gmail.com) // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package difflib import ( "reflect" "strings" "testing" ) var lcsTests = []struct { seq1 string seq2 string lcs int }{ {"", "", 0}, {"abc", "abc", 3}, {"mzjawxu", "xmjyauz", 4}, {"human", "chimpanzee", 4}, {"Hello, world!", "Hello, world!", 13}, {"Hello, world!", "H e l l o , w o r l d !", 13}, } func TestLongestCommonSubsequenceMatrix(t *testing.T) { for i, test := range lcsTests { seq1 := strings.Split(test.seq1, "") seq2 := strings.Split(test.seq2, "") matrix := longestCommonSubsequenceMatrix(seq1, seq2) lcs := matrix[len(matrix)-1][len(matrix[0])-1] // Grabs the lower, right value. if lcs != test.lcs { t.Errorf("%d. longestCommonSubsequence(%v, %v)[last][last] => %d, expected %d", i, seq1, seq2, lcs, test.lcs) } } } var numEqualStartAndEndElementsTests = []struct { seq1 string seq2 string start int end int }{ {"", "", 0, 0}, {"abc", "", 0, 0}, {"", "abc", 0, 0}, {"abc", "abc", 3, 0}, {"abhelloc", "abbyec", 2, 1}, {"abchello", "abcbye", 3, 0}, {"helloabc", "byeabc", 0, 3}, } func TestNumEqualStartAndEndElements(t *testing.T) { for i, test := range numEqualStartAndEndElementsTests { seq1 := strings.Split(test.seq1, "") seq2 := strings.Split(test.seq2, "") start, end := numEqualStartAndEndElements(seq1, seq2) if start != test.start || end != test.end { t.Errorf("%d. numEqualStartAndEndElements(%v, %v) => (%d, %d), expected (%d, %d)", i, seq1, seq2, start, end, test.start, test.end) } } } var diffTests = []struct { Seq1 string Seq2 string Diff []DiffRecord HtmlDiff string }{ { "", "", []DiffRecord{ {"", Common}, }, `1
1
`,
	},

	{
		"same",
		"same",
		[]DiffRecord{
			{"same", Common},
		},
		`1
same
same
1 `, }, { `one two three `, `one two three `, []DiffRecord{ {"one", Common}, {"two", Common}, {"three", Common}, {"", Common}, }, `1
one
one
1 2
two
two
2 3
three
three
3 4
4
`,
	},

	{
		`one
two
three
`,
		`one
five
three
`,
		[]DiffRecord{
			{"one", Common},
			{"two", LeftOnly},
			{"five", RightOnly},
			{"three", Common},
			{"", Common},
		},
		`1
one
one
1 2
two
five
2 3
three
three
3 4
4
`,
	},

	{
		`Beethoven
Bach
Mozart
Chopin
`,
		`Beethoven
Bach
Brahms
Chopin
Liszt
Wagner
`,

		[]DiffRecord{
			{"Beethoven", Common},
			{"Bach", Common},
			{"Mozart", LeftOnly},
			{"Brahms", RightOnly},
			{"Chopin", Common},
			{"Liszt", RightOnly},
			{"Wagner", RightOnly},
			{"", Common},
		},
		`1
Beethoven
Beethoven
1 2
Bach
Bach
2 3
Mozart
Brahms
3 4
Chopin
Chopin
4
Liszt
5
Wagner
6 5
7
`,
	},

	{
		`adagio
vivace
staccato legato
presto
lento
`,
		`adagio adagio
staccato
staccato legato
staccato
legato
allegro
`,
		[]DiffRecord{
			{"adagio", LeftOnly},
			{"vivace", LeftOnly},
			{"adagio adagio", RightOnly},
			{"staccato", RightOnly},
			{"staccato legato", Common},
			{"presto", LeftOnly},
			{"lento", LeftOnly},
			{"staccato", RightOnly},
			{"legato", RightOnly},
			{"allegro", RightOnly},
			{"", Common},
		},
		`1
adagio
2
vivace
adagio adagio
1
staccato
2 3
staccato legato
staccato legato
3 4
presto
5
lento
staccato
4
legato
5
allegro
6 6
7
`,
	},
}

func TestDiff(t *testing.T) {
	for i, test := range diffTests {
		seq1 := strings.Split(test.Seq1, "\n")
		seq2 := strings.Split(test.Seq2, "\n")

		diff := Diff(seq1, seq2)
		if !reflect.DeepEqual(diff, test.Diff) {
			t.Errorf("%d. Diff(%v, %v) => %v, expected %v",
				i, seq1, seq2, diff, test.Diff)
		}

		htmlDiff := HTMLDiff(seq1, seq2)
		if htmlDiff != test.HtmlDiff {
			t.Errorf("%d. HtmlDiff(%v, %v) => %v, expected %v",
				i, seq1, seq2, htmlDiff, test.HtmlDiff)
		}

	}
}