File: filenamify_test.go

package info (click to toggle)
golang-github-flytam-filenamify 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 84 kB
  • sloc: makefile: 2
file content (120 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package filenamify

import (
	"path/filepath"
	"testing"
)

type inputItem struct {
	str     string
	options Options
}

type exampleItem struct {
	input inputItem
	// output
	output string
}

func newExampleItem(inputStr string, options Options, outputStr string) exampleItem {
	return exampleItem{
		input: inputItem{
			inputStr, options,
		}, output: outputStr,
	}
}

func TestFilenamify(t *testing.T) {
	var output string

	example := []exampleItem{
		newExampleItem("foo/bar", Options{}, "foo!bar"),
		newExampleItem("foo//bar", Options{}, "foo!bar"),
		newExampleItem("//foo//bar//", Options{}, "foo!bar"),
		newExampleItem("foo\\\\\\bar", Options{}, "foo!bar"),
		//---
		newExampleItem("foo/bar", Options{
			Replacement: "🐴🐴",
		}, "foo🐴🐴bar"),
		newExampleItem("////foo////bar////", Options{
			Replacement: "((",
		}, "foo((bar"),
		//--
		newExampleItem("foo\u0000bar", Options{}, "foo!bar"),
		newExampleItem(".", Options{}, "!"),
		newExampleItem("..", Options{}, "!"),
		newExampleItem("./", Options{}, "!"),
		newExampleItem("../", Options{}, "!"),
		newExampleItem("con", Options{}, "con!"),
		newExampleItem("foo/bar/nul", Options{}, "foo!bar!nul"),

		newExampleItem("con", Options{
			Replacement: "🐴🐴",
		}, "con🐴🐴"),
		newExampleItem("c/n", Options{
			Replacement: "o",
		}, "cono"),
		newExampleItem("c/n", Options{
			Replacement: "con",
		}, "cconn"),
	}

	for index, item := range example {
		if output, _ = Filenamify(item.input.str, item.input.options); output != item.output {
			t.Error(index, item.input.str, item.input.options, item.output)
		} else {
			t.Log(index, "pass")
		}
	}

}

func TestFilenamifyV2(t *testing.T) {
	var output string

	input := "c/n"
	expect := "cconn"

	if output, _ = FilenamifyV2(input, func(options *Options) {
		options.Replacement = "con"
	}); output != expect {
		t.Error("expect:", expect, "got:", output)
	} else {
		t.Log("pass")
	}

}

func TestFilenamifyPath(t *testing.T) {
	expect := "foo!bar"
	expect2 := "foohbar"
	inputStr, _ := filepath.Abs("foo:bar")

	if output, _ := Path(inputStr, Options{}); filepath.Base(output) != expect {
		t.Error("TestFilenamifyPath error", filepath.Base(output), expect)
	}

	if output, _ := PathV2(inputStr); filepath.Base(output) != expect {
		t.Error("TestFilenamifyPath error", filepath.Base(output), expect)
	}

	if output, _ := PathV2(inputStr, func(options *Options) {
		options.Replacement = "h"
	}); filepath.Base(output) != expect2 {
		t.Error("TestFilenamifyPath error", filepath.Base(output), expect2)
	}
}

func TestFilenamifyLength(t *testing.T) {
	// Basename length: 152
	const filename = "this/is/a/very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt"

	if output, _ := Filenamify(filepath.Base(filename), Options{}); output != "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_" {
		t.Error("TestFilenamifyLength error")
	}

	if output, _ := Filenamify(filepath.Base(filename), Options{MaxLength: 180}); output != "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt" {
		t.Error("TestFilenamifyLength error")
	}

}