File: editorconfig_test.go

package info (click to toggle)
golang-gopkg-editorconfig-editorconfig-core-go.v1 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 112 kB
  • sloc: makefile: 3
file content (123 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download | duplicates (2)
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
121
122
123
package editorconfig

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
)

const (
	testFile = "testdata/.editorconfig"
)

func testParse(t *testing.T, ec *Editorconfig) {
	assert.Equal(t, true, ec.Root)
	assert.Equal(t, 3, len(ec.Definitions))

	def := ec.Definitions[0]
	assert.Equal(t, "*", def.Selector)
	assert.Equal(t, EndOfLineLf, def.EndOfLine)
	assert.Equal(t, true, def.InsertFinalNewline)
	assert.Equal(t, CharsetUTF8, def.Charset)
	assert.Equal(t, true, def.TrimTrailingWhitespace)

	def = ec.Definitions[1]
	assert.Equal(t, "*.go", def.Selector)
	assert.Equal(t, IndentStyleTab, def.IndentStyle)
	assert.Equal(t, "4", def.IndentSize)
	assert.Equal(t, 4, def.TabWidth)

	def = ec.Definitions[2]
	assert.Equal(t, "*.{js,css,less,htm,html}", def.Selector)
	assert.Equal(t, IndentStyleSpaces, def.IndentStyle)
	assert.Equal(t, "2", def.IndentSize)
	assert.Equal(t, 2, def.TabWidth)
}

func TestParse(t *testing.T) {
	ec, err := ParseFile(testFile)
	if err != nil {
		t.Errorf("Couldn't parse file: %v", err)
	}

	testParse(t, ec)
}

func TestFilenameMatches(t *testing.T) {
	assertFilenameMatch := func(pattern, name string) {
		assert.Equal(t, true, filenameMatches(pattern, name), "\"%s\" should match \"%s\"", name, pattern)
	}
	assertFilenameNotMatch := func(pattern, name string) {
		assert.Equal(t, false, filenameMatches(pattern, name), "\"%s\" should not match \"%s\"", name, pattern)
	}
	assertFilenameMatch("*", "main.go")
	assertFilenameMatch("*.go", "main.go")
	assertFilenameNotMatch("*.js", "main.go")
	assertFilenameMatch("main.go", "main.go")
	assertFilenameMatch("main.go", "foo/bar/main.go")
	assertFilenameMatch("foo/bar/main.go", "foo/bar/main.go")
	assertFilenameMatch("foo", "foo/main.go")

	assertFilenameMatch("*.{go,css}", "main.go")
	assertFilenameNotMatch("*.{js,css}", "main.go")
	assertFilenameMatch("*.{css,less}", "foo/bar/file.less")

	assertFilenameMatch("{foo}.go", "foo.go")
	assertFilenameMatch("{foo}.go", "bar/baz/foo.go")
	assertFilenameMatch("{}.go", "foo.go")
	assertFilenameMatch("{}.go", "bar.go")
	assertFilenameNotMatch("a{b,c}.go", "ad.go")
	assertFilenameMatch("a{b,c}.go", "ab.go")
	assertFilenameMatch("a{a,b,,d}.go", "ad.go")
	assertFilenameNotMatch("a{a,b,,d}.go", "ac.go")
	assertFilenameNotMatch("a{b,c,d}.go", "a.go")
	assertFilenameMatch("a{b,c,,d}.go", "a.go")

	assertFilenameMatch("a[a-d].go", "ac.go")
	assertFilenameNotMatch("a[a-d].go", "af.go")
}

func TestGetDefinition(t *testing.T) {
	ec, err := ParseFile(testFile)
	if err != nil {
		t.Errorf("Couldn't parse file: %v", err)
	}

	def := ec.GetDefinitionForFilename("main.go")
	assert.Equal(t, IndentStyleTab, def.IndentStyle)
	assert.Equal(t, "4", def.IndentSize)
	assert.Equal(t, 4, def.TabWidth)
	assert.Equal(t, true, def.TrimTrailingWhitespace)
	assert.Equal(t, CharsetUTF8, def.Charset)
	assert.Equal(t, true, def.InsertFinalNewline)
	assert.Equal(t, EndOfLineLf, def.EndOfLine)
}

func TestSave(t *testing.T) {
	ec, err := ParseFile(testFile)
	if err != nil {
		t.Errorf("Couldn't parse file: %v", err)
	}

	tempFile := filepath.Join(os.TempDir(), ".editorconfig")
	defer os.Remove(tempFile)

	err = ec.Save(tempFile)
	assert.Nil(t, err)

	savedEc, err := ParseFile(tempFile)
	assert.Nil(t, err)

	testParse(t, savedEc)
}

func TestPublicTestDefinitionForFilename(t *testing.T) {
	def, err := GetDefinitionForFilename("testdata/root/src/dummy.go")
	assert.Nil(t, err)
	assert.Equal(t, "4", def.IndentSize)
	assert.Equal(t, IndentStyleTab, def.IndentStyle)
	assert.Equal(t, true, def.InsertFinalNewline)
	assert.Equal(t, false, def.TrimTrailingWhitespace)
}