Skip to content

Commit b9dc426

Browse files
committed
Update: make snippet file also non suffix
1 parent 8b53a60 commit b9dc426

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ test: lint
2323
go test -v ./src
2424

2525
.PHONY: integration_test
26-
integration_test: test
27-
(cd ./integration_test && go test -run .)
26+
integration_test: main
27+
(cd ./integration_test && go test -v -run .)
2828

2929
.PHONY: lint
3030
lint:

src/snippets.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"github.com/mattn/go-zglob"
1212
)
1313

14-
const pmySnippetSuffix = "pmy_snippet.txt"
14+
const pmySnippetSuffix = ".txt"
15+
const _pmySnippetSuffix = "pmy_snippet.txt"
1516

1617
// SnippetFile represents one Snippet Json file
1718
// information
@@ -22,11 +23,19 @@ type SnippetFile struct {
2223
}
2324

2425
func (s SnippetFile) isApplicable(relpath string) bool {
25-
return fmt.Sprintf(
26-
"%s_%s",
26+
if s.Relpath == fmt.Sprintf(
27+
"%s%s",
2728
relpath,
2829
pmySnippetSuffix,
29-
) == s.Relpath
30+
) {
31+
return true
32+
}
33+
34+
return s.Relpath == fmt.Sprintf(
35+
"%s_%s",
36+
relpath,
37+
_pmySnippetSuffix,
38+
)
3039
}
3140

3241
// GetAllSnippetFiles get all pmy snippets txt paths
@@ -45,7 +54,7 @@ func GetAllSnippetFiles() []*SnippetFile {
4554
globPattern := fmt.Sprintf(
4655
`%v/**/*%v`,
4756
snippetRoot,
48-
pmySnippetSuffix,
57+
_pmySnippetSuffix,
4958
)
5059
matches, err := zglob.Glob(globPattern)
5160
if err != nil {

src/snippets_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package pmy
2+
3+
import "testing"
4+
5+
// TestIsApplicable tests if applicability of
6+
// snippet is correctly works
7+
func TestIsApplicable(t *testing.T) {
8+
testcases := []struct {
9+
description string
10+
path string
11+
basename string
12+
relpath string
13+
givenRelpath string
14+
expectedRes bool
15+
}{
16+
{
17+
description: "valid pattern",
18+
path: "./snippets/curl/test.txt",
19+
basename: "test.txt",
20+
relpath: "curl/test.txt",
21+
givenRelpath: "curl/test",
22+
expectedRes: true,
23+
},
24+
{
25+
description: "invalid pattern",
26+
path: "./snippets/curl/test.txt",
27+
basename: "test.txt",
28+
relpath: "curl/test.txt",
29+
givenRelpath: "curl/testhoge",
30+
expectedRes: false,
31+
},
32+
//////////////////////////////////////////////////
33+
// !!!!!!!! TODO: deprecated test !!!!!!!!!!
34+
//////////////////////////////////////////////////
35+
{
36+
description: "(deprecated) valid pattern",
37+
path: "./snippets/curl/test_pmy_snippet.txt",
38+
basename: "test_pmy_snippet.txt",
39+
relpath: "curl/test_pmy_snippet.txt",
40+
givenRelpath: "curl/test",
41+
expectedRes: true,
42+
},
43+
{
44+
description: "(deprecated) valid pattern",
45+
path: "./snippets/curl/test_pmy_snippet.txt",
46+
basename: "test_pmy_snippet.txt",
47+
relpath: "curl/testhoge_pmy_snippet.txt",
48+
givenRelpath: "curl/test",
49+
expectedRes: false,
50+
},
51+
}
52+
53+
for _, testcase := range testcases {
54+
target := &SnippetFile{
55+
Path: testcase.path,
56+
Basename: testcase.basename,
57+
Relpath: testcase.relpath,
58+
}
59+
res := target.isApplicable(testcase.givenRelpath)
60+
if !(res == testcase.expectedRes) {
61+
t.Errorf(
62+
"test failed (%v): resulting res %v is not equal to %v",
63+
testcase.description,
64+
res,
65+
testcase.expectedRes,
66+
)
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)