Skip to content

Commit

Permalink
Add FilePath TestCases
Browse files Browse the repository at this point in the history
- Added a test case and example test for the NewFilePath function
  • Loading branch information
manishmeganathan committed Aug 8, 2022
1 parent 1675259 commit 95a0e74
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions filepath_test.go
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())
}
}
}

0 comments on commit 95a0e74

Please sign in to comment.