-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added a test case and example test for the NewFilePath function
- Loading branch information
1 parent
1675259
commit 95a0e74
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package moibit | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func ExampleNewFilePath() { | ||
directory := "appdata/users" | ||
filename := "userdata.json" | ||
|
||
fp, _ := NewFilePath(directory, filename) | ||
fmt.Println("Path:", fp) | ||
|
||
// Output: | ||
// Path: /appdata/users/userdata.json | ||
} | ||
|
||
func TestNewFilePath(t *testing.T) { | ||
tests := []struct { | ||
input []string | ||
path string | ||
error error | ||
}{ | ||
{[]string{"data", "devices"}, "/data/devices", nil}, | ||
{[]string{"//data", "devices/", "hello.txt"}, "/data/devices/hello.txt", nil}, | ||
{[]string{"data/devices", "hello.txt"}, "/data/devices/hello.txt", nil}, | ||
{[]string{"/data"}, "/data", nil}, | ||
{[]string{"/data", ""}, "/data", nil}, | ||
{[]string{"data", "/devices"}, "/data/devices", nil}, | ||
{[]string{"data/", "/devices/hello.txt"}, "/data/devices/hello.txt", nil}, | ||
{ | ||
[]string{"data", "/devices.txt/hello"}, "/", | ||
errors.New("failed to construct filepath: slash detected after period or missing extension"), | ||
}, | ||
{ | ||
[]string{"data", "/devices.txt/hello.txt"}, "/", | ||
errors.New("failed to construct filepath: multiple periods in final element"), | ||
}, | ||
{ | ||
[]string{"dat.txt", "/devices.txt/hello.txt"}, "/", | ||
errors.New("failed to construct filepath: non-final element '0' contains period"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
fp, err := NewFilePath(test.input...) | ||
|
||
if !reflect.DeepEqual(err, test.error) { | ||
t.Fatalf("error mismatch. expected: %v. got: %v", test.error, err) | ||
} | ||
|
||
if !reflect.DeepEqual(test.path, fp.Path()) { | ||
t.Fatalf("path mismatch. expected: %v. got: %v", test.path, fp.Path()) | ||
} | ||
} | ||
} |