|
1 | 1 | package iDB
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - //"os" |
5 |
| - "testing" |
6 |
| - . "github.com/remogatto/prettytest" |
| 4 | + "os" |
| 5 | + //"testing" |
| 6 | + "io/ioutil" |
| 7 | + . "github.com/snowyu/prettytest" |
7 | 8 | )
|
8 | 9 |
|
9 | 10 |
|
10 |
| -type utilsSuite struct { |
| 11 | +type UtilsTestSuite struct { |
11 | 12 | Suite
|
12 | 13 | testfile string
|
13 | 14 | }
|
14 | 15 |
|
15 |
| -func (suite *utilsSuite) Before() { |
| 16 | +func (suite *UtilsTestSuite) Before() { |
16 | 17 | suite.testfile = "mytestfile"
|
| 18 | + result, _ := FileIsExists(suite.testfile) |
| 19 | + if !result { |
| 20 | + ioutil.WriteFile(suite.testfile, nil, 0600) |
| 21 | + } |
| 22 | + suite.Path(suite.testfile) |
17 | 23 | }
|
18 | 24 |
|
19 |
| -func (Suite *utilsSuite) After() { |
20 |
| - os.Remove(Suite.testfile) |
| 25 | +func (suite *UtilsTestSuite) After() { |
| 26 | + os.Remove(suite.testfile) |
21 | 27 | }
|
22 | 28 |
|
23 |
| -const testfile = "mytestfile" |
| 29 | +func (suite *UtilsTestSuite) doTestSetXattr(aKey, aValue string){ |
| 30 | + err := SetXattr(suite.testfile, aKey, aValue) |
| 31 | + suite.Nil(err) |
| 32 | + suite.True(IsXattrExists(suite.testfile, aKey)) |
| 33 | + suite.False(IsXattrExists(suite.testfile, aKey+".myNotExistsTheXKey")) |
| 34 | +} |
24 | 35 |
|
25 |
| -func Test_SetXatrr(t *testing.T){ //test function starts with "Test" and takes a pointer to type testing.T |
26 |
| - err := SetXattr(testfile, "mykey", "myvalue23") |
27 |
| - if (err != nil || !IsXattrExists(testfile, "mykey")) { //try a unit test on function |
28 |
| - t.Error("SetXattr did not work as expected. Error:", err) // log error if it did not work as expected |
29 |
| - } else { |
30 |
| - t.Log("one test passed.") // log some info if you want |
31 |
| - } |
| 36 | +func (suite *UtilsTestSuite) TestSetXattr(){ |
| 37 | + suite.doTestSetXattr("mykey", "myvalue23") |
| 38 | + suite.doTestSetXattr(".dsd.sd", "ajsae@3if34$/dsd") |
32 | 39 | }
|
33 |
| -func Test_GetXatrr(t *testing.T){ |
34 |
| - Test_SetXatrr(t) |
35 |
| - result, err := GetXattr(testfile, "mykey") |
36 |
| - if (result != "myvalue23") { //try a unit test on function |
37 |
| - t.Error("GetXattr did not work as expected. Error:", err) // log error if it did not work as expected |
38 |
| - } else { |
39 |
| - t.Log("one test passed.") // log some info if you want |
| 40 | + |
| 41 | +func InStrings(aStr string, aList []string) bool { |
| 42 | + result := false |
| 43 | + for i := range aList { |
| 44 | + if aList[i] == aStr { |
| 45 | + result = true |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + return result |
| 50 | +} |
| 51 | + |
| 52 | +func (suite *UtilsTestSuite) TestListXattr(){ |
| 53 | + pairs := map[string]string { |
| 54 | + "mykey": "myvalue23", |
| 55 | + "mykey2":"ajsae@3if34$/dsd", |
| 56 | + } |
| 57 | + for k,v := range pairs { |
| 58 | + suite.doTestSetXattr(k, v) |
| 59 | + } |
| 60 | + names, err := ListXattr(suite.testfile) |
| 61 | + suite.Nil(err) |
| 62 | + for k := range pairs { |
| 63 | + suite.True(InStrings(k, names)) |
40 | 64 | }
|
41 | 65 | }
|
42 | 66 |
|
43 |
| -func Test_FileIsExists(t *testing.T){ |
44 |
| - result, err := FileIsExists(testfile) |
45 |
| - if !result { |
46 |
| - t.Error("FileIsExists did not work as expected, Error:", err) |
47 |
| - } else { |
48 |
| - t.Log("one test passed") |
| 67 | +func (suite *UtilsTestSuite) TestGetXatrr(){ |
| 68 | + //suite.TestSetXattr() |
| 69 | + suite.doTestSetXattr("mykey", "myvalue23") |
| 70 | + pairs := map[string]string { |
| 71 | + "mykey": "myvalue23", |
| 72 | + "mykey2":"ajsae@3if34$/dsd", |
49 | 73 | }
|
| 74 | + var result string |
| 75 | + var err error |
| 76 | + for k,v := range pairs { |
| 77 | + suite.doTestSetXattr(k, v) |
| 78 | + result, err = GetXattr(suite.testfile, k) |
| 79 | + suite.Nil(err) |
| 80 | + suite.Equal(result, v) |
| 81 | + } |
| 82 | + result, err = GetXattr(suite.testfile, "myNotExistsTheXKey") |
| 83 | + suite.NotNil(err) |
| 84 | + suite.Equal(result, "") |
| 85 | +} |
| 86 | + |
| 87 | +func (suite *UtilsTestSuite) TestDeleteXatrr(){ |
| 88 | + //suite.TestSetXattr() |
| 89 | + suite.doTestSetXattr("mykey", "myvalue23") |
| 90 | + err := DeleteXattr(suite.testfile, "mykey") |
| 91 | + suite.Nil(err) |
| 92 | + suite.False(IsXattrExists(suite.testfile, "mykey")) |
| 93 | + err = DeleteXattr(suite.testfile, "myNotExistsTheXKey") |
| 94 | + suite.NotNil(err) |
| 95 | +} |
| 96 | + |
| 97 | +func (suite *UtilsTestSuite) TestFileIsExists(){ |
| 98 | + result, err := FileIsExists(suite.testfile) |
| 99 | + suite.True(result) |
| 100 | + suite.Nil(err) |
| 101 | + result, err = FileIsExists("#.NoSuchFileExists##!!") |
| 102 | + suite.False(result) |
| 103 | + suite.Nil(err) |
50 | 104 | }
|
0 commit comments