func Test_CreateDeleteFile(t *testing.T) {
	test_dir := os.TempDir()
	file_store, err := New(test_dir)
	if err != nil {
		assert.Fail(t, fmt.Sprintf("file store failed to create: %s", err))
	}
	full_test_path := ""
	// Create file
	test_file := `invalid/\:jargon`
	file, err := file_store.OpenFile(test_file, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
	assert.True(t, err == nil, fmt.Sprintf("%s", err))
	if file != nil {
		t.Logf("File generated \"%s\" from \"%s\"", file.Name(), test_file)
		assert.Equal(t, test_file, file.Original_Name(),
			fmt.Sprintf("File mapping name mismatch, original name %s, original name on file %s", test_file, file.Original_Name()))
		full_test_path = file.Name()
		file.Close()
	}

	// Delete file
	err = file_store.DeleteFile(test_file)
	assert.True(t, err == nil, fmt.Sprintf("%s", err))
	_, exist_err := os.Stat(full_test_path)
	assert.True(t, exist_err != nil)

	// Cleanup in failure case
	if err != nil {
		os.Remove(full_test_path)
	}
}
Exemplo n.º 2
0
func Test_diffDelta(t *testing.T) {
	dmp := New()
	// Convert a diff into delta string.
	diffs := []Diff{
		Diff{DiffEqual, "jump"},
		Diff{DiffDelete, "s"},
		Diff{DiffInsert, "ed"},
		Diff{DiffEqual, " over "},
		Diff{DiffDelete, "the"},
		Diff{DiffInsert, "a"},
		Diff{DiffEqual, " lazy"},
		Diff{DiffInsert, "old dog"}}

	text1 := dmp.DiffText1(diffs)
	assert.Equal(t, "jumps over the lazy", text1)

	delta := dmp.DiffToDelta(diffs)
	assert.Equal(t, "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta)

	// Convert delta string into a diff.
	_seq1, err := dmp.DiffFromDelta(text1, delta)
	assertDiffEqual(t, diffs, _seq1)

	// Generates error (19 < 20).
	_, err = dmp.DiffFromDelta(text1+"x", delta)
	if err == nil {
		t.Fatal("diff_fromDelta: Too long.")
	}

	// Generates error (19 > 18).
	_, err = dmp.DiffFromDelta(text1[1:], delta)
	if err == nil {
		t.Fatal("diff_fromDelta: Too short.")
	}

	// Generates error (%xy invalid URL escape).
	_, err = dmp.DiffFromDelta("", "+%c3%xy")
	if err == nil {
		assert.Fail(t, "diff_fromDelta: expected Invalid URL escape.")
	}

	// Generates error (invalid utf8).
	_, err = dmp.DiffFromDelta("", "+%c3xy")
	if err == nil {
		assert.Fail(t, "diff_fromDelta: expected Invalid utf8.")
	}

	// Test deltas with special characters.
	diffs = []Diff{
		Diff{DiffEqual, "\u0680 \x00 \t %"},
		Diff{DiffDelete, "\u0681 \x01 \n ^"},
		Diff{DiffInsert, "\u0682 \x02 \\ |"}}
	text1 = dmp.DiffText1(diffs)
	assert.Equal(t, "\u0680 \x00 \t %\u0681 \x01 \n ^", text1)

	delta = dmp.DiffToDelta(diffs)
	// Lowercase, due to UrlEncode uses lower.
	assert.Equal(t, "=7\t-7\t+%DA%82 %02 %5C %7C", delta)

	_res1, err := dmp.DiffFromDelta(text1, delta)
	if err != nil {
		t.Fatal(err)
	}
	assertDiffEqual(t, diffs, _res1)

	// Verify pool of unchanged characters.
	diffs = []Diff{
		Diff{DiffInsert, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "}}
	text2 := dmp.DiffText2(diffs)
	assert.Equal(t, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ", text2, "diff_text2: Unchanged characters.")

	delta = dmp.DiffToDelta(diffs)
	assert.Equal(t, "+A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ", delta, "diff_toDelta: Unchanged characters.")

	// Convert delta string into a diff.
	_res2, _ := dmp.DiffFromDelta("", delta)
	assertDiffEqual(t, diffs, _res2)
}