github.com/tomkralidis/go-wfs@v0.0.1-0.20180511181554-132fcf63fa2c/util/util_test.go (about) 1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 // The MIT License (MIT) 4 // Copyright (c) 2018 James Lucktaylor 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining a copy 7 // of this software and associated documentation files (the "Software"), to 8 // deal in the Software without restriction, including without limitation the 9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 // sell copies of the Software, and to permit persons to whom the Software is 11 // furnished to do so, subject to the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be included in 14 // all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 22 // USE OR OTHER DEALINGS IN THE SOFTWARE. 23 // 24 /////////////////////////////////////////////////////////////////////////////// 25 package util 26 27 import ( 28 "bytes" 29 "os" 30 "path" 31 "path/filepath" 32 "runtime" 33 "testing" 34 ) 35 36 var baseTestPath string 37 38 func init() { 39 _, thisFilePath, _, _ := runtime.Caller(0) 40 baseTestPath = path.Join(path.Dir(thisFilePath), "test_data/DefaultGpkg") 41 } 42 43 func TestDefaultGpkg(t *testing.T) { 44 cases := []struct { 45 // The name of the sub-directory, under ./test_data/DefaultGpkg/ 46 testSubPath string 47 48 // The full path to the selected file, or an empty string if no file is found 49 dummyFileName string 50 }{ 51 { 52 testSubPath: "1", 53 dummyFileName: filepath.Join(baseTestPath, "1/dummyA.gpkg"), 54 }, 55 { 56 testSubPath: "2", 57 dummyFileName: filepath.Join(baseTestPath, "2/data/dataDummy.gpkg"), 58 }, 59 { 60 testSubPath: "3", 61 dummyFileName: filepath.Join(baseTestPath, "3/test_data/testDataDummy.gpkg"), 62 }, 63 { 64 testSubPath: "4", 65 dummyFileName: "", 66 }, 67 } 68 69 for i, c := range cases { 70 casePath := path.Join(baseTestPath, c.testSubPath) 71 72 if chdirErr := os.Chdir(casePath); chdirErr != nil { 73 t.Errorf("[%d] Could not change working directory to '%s'.", i, casePath) 74 } 75 76 got := DefaultGpkg() 77 78 if got != c.dummyFileName { 79 t.Errorf("[%d] Under '%s', DefaultGpkg() == '%s', wanted '%s'.", i, casePath, got, c.dummyFileName) 80 } 81 } 82 } 83 84 func TestRenderTemplate(t *testing.T) { 85 var tmpl = `<foo>{{ .value }}</foo>` 86 data := map[string]interface{}{"value": "bar"} 87 expected := []byte("<foo>bar</foo>") 88 89 value, _ := RenderTemplate(tmpl, data) 90 91 if bytes.Equal(value, expected) == false { 92 t.Errorf("got %s, wanted %s", value, expected) 93 } 94 }