func TestParseThumbName(t *testing.T) { path := "foo-20x40.png" name, geometry, err := ParseThumbName(path) assert.Nil(t, err) assert.Equal(t, "foo", name) assert.Equal(t, 20, geometry.Width) assert.Equal(t, 40, geometry.Height) path = "foo-xyz-20x40.png" name, geometry, err = ParseThumbName(path) assert.Nil(t, err) assert.Equal(t, "foo-xyz", name) assert.Equal(t, 20, geometry.Width) assert.Equal(t, 40, geometry.Height) path = "foo20x40.png" //bad filename name, geometry, err = ParseThumbName(path) assert.NotNil(t, err) assert.Equal(t, "", name) assert.Equal(t, 0, geometry.Width) assert.Equal(t, 0, geometry.Height) }
func TestTranslate(t *testing.T) { defer resetLocales() en := NewLocale("en") en.Add("greeting", "hello") en.Add("greeting2", "hello2") AddLocale(en) it := NewLocale("it") it.Add("greeting", "ciao") AddLocale(it) var value string var err error err = SetLocale("en") assert.Nil(t, err) assert.Equal(t, en, currentLocale) value, err = Translate("greeting") assert.Nil(t, err) assert.Equal(t, "hello", value) value, err = Translate("greeting2") assert.Nil(t, err) assert.Equal(t, "hello2", value) err = SetLocale("it") assert.Nil(t, err) assert.Equal(t, it, currentLocale) value, err = Translate("greeting") assert.Nil(t, err) assert.Equal(t, "ciao", value) value, err = Translate("greeting2") assert.NotNil(t, err) assert.Type(t, "*i18n.TranslationMissing", err) err = SetLocale("es") assert.NotNil(t, err) assert.Type(t, "*i18n.LocaleMissing", err) assert.True(t, currentLocale == nil) value, err = Translate("greeting") assert.NotNil(t, err) assert.Equal(t, "Current locale is not set", err.Error()) }
func TestLocale_T(t *testing.T) { locale := NewLocale("en") locale.Add("greeting", "hello %s") value, err := locale.T("greeting", "world") assert.Nil(t, err) assert.Equal(t, "hello world", value) }
func TestPlate_Execute(t *testing.T) { defer os.RemoveAll("__test-data__") p := newPlate("__test-fixtures__", "__test-data__") p.execute("go", []string{"program-name", "outpath", "World"}...) paths := map[string]string{ "__test-data__/main.go": `package main import "fmt" func main() { fmt.Println("Hello World") }`, "__test-data__/main_test.go": `package main import "testing" func TestFoo(t *testing.T) { }`, "__test-data__/config/app.config": `config file`, } for path, expectedContent := range paths { f, err := os.Open(path) assert.Nil(t, err) content, err := ioutil.ReadAll(f) if err != nil { logPkg.Fatal(err) } assert.Equal(t, expectedContent, string(content)) } }
func TestNewFinder(t *testing.T) { f, err := newFinder("http://example.com") expectedURL, _ := url.Parse("http://example.com") assert.Nil(t, err) assert.Equal(t, expectedURL, f.URL) }
func TestBundler_Bundle(t *testing.T) { defer testRemoveTestDataFolder() b := testNewBundler() b.bundle() tests := map[string]string{ "application": ` function file1(){return"file1"} function file2(){return"file2"}`, "admin": ` function admin1(){return"admin1"} function admin2(){return"admin2"} function editor(){return"editor"}`, } for filename, expectedContent := range tests { h := md5.New() io.Copy(h, bytes.NewBufferString(expectedContent)) path := fmt.Sprintf("%s/%s-%x.js", b.outputPath, filename, h.Sum(nil)) f, err := os.Open(path) content, err := ioutil.ReadAll(f) assert.Nil(t, err) assert.Equal(t, expectedContent, string(content)) } }
func TestParseConfig(t *testing.T) { content := `javascripts: application: - file-1.js - file-2.js admin: - file-3.js - file-4.js ` r := bytes.NewBufferString(content) c, err := parseConfig(r) assert.Nil(t, err) assert.Equal(t, 2, len(c.Javascripts)) assert.Type(t, "[]string", c.Javascripts["application"]) assert.Equal(t, 2, len(c.Javascripts["application"])) assert.Equal(t, "file-1.js", c.Javascripts["application"][0]) assert.Equal(t, "file-2.js", c.Javascripts["application"][1]) assert.Type(t, "[]string", c.Javascripts["admin"]) assert.Equal(t, 2, len(c.Javascripts["admin"])) assert.Equal(t, "file-3.js", c.Javascripts["admin"][0]) assert.Equal(t, "file-4.js", c.Javascripts["admin"][1]) }
func TestSetPort(t *testing.T) { resetGlobalEnv() assert.Nil(t, GetVar("port")) SetPort(80) assert.Equal(t, 80, GetVar("port")) resetGlobalEnv() }
func TestSetHost(t *testing.T) { resetGlobalEnv() assert.Nil(t, GetVar("host")) SetHost("1.2.3.4") assert.Equal(t, "1.2.3.4", GetVar("host")) resetGlobalEnv() }
func TestGetVar(t *testing.T) { resetGlobalEnv() env["foo-2"] = "bar-2" assert.Equal(t, "bar-2", GetVar("foo-2")) assert.Nil(t, GetVar("os_foo")) os.Setenv("TRAFFIC_OS_FOO", "bar") assert.Equal(t, "bar", GetVar("os_foo")) resetGlobalEnv() }
func TestThumbFromPath(t *testing.T) { path := "/path/to/image/foo-20x40.png" thumb, err := ThumbFromPath(path) assert.Nil(t, err) assert.Equal(t, path, thumb.Path) assert.Equal(t, "/path/to/image", thumb.Dir) assert.Equal(t, "foo", thumb.Name) assert.Equal(t, ".png", thumb.Ext) assert.Equal(t, "/path/to/image/foo.png", thumb.MainPath) }
func TestClient_newRequest(t *testing.T) { c := New(&Environment{ SearchApiKey: "foo", DisplayApiKey: "bar", }) v := url.Values{} v.Set("p1", "v1") r, err := c.newRequest("/foo", v) assert.Nil(t, err) expectedURI := "http://test.lyricfind.com/api_service/foo?output=json&p1=v1" assert.Equal(t, expectedURI, r.URL.String()) }
func TestPlate_OpenTemplate(t *testing.T) { p := newPlate("__test-fixtures__", "tmp") mainTpl, err := p.openTemplate("go") assert.NotNil(t, mainTpl) assert.Nil(t, err) assert.Equal(t, 6, len(mainTpl.Templates())) names := []string{"main.go", "main_test.go", "config/app.config", "bin/test_command.sh"} for _, name := range names { tpl := mainTpl.Lookup(name) assert.NotNil(t, tpl) } }
func TestClient_Search(t *testing.T) { c := New(&Environment{ SearchApiKey: "foo", DisplayApiKey: "bar", Live: true, }) fc := newFakeHttpClient("search") c.httpClient = fc sr, err := c.Search(IPAddress("1.2.3.4"), &SearchOptions{Artist: "artist-foo"}) assert.Nil(t, err) assert.Equal(t, 2, len(sr.Tracks)) assert.Equal(t, "/search.do", fc.request.URL.Path) }
func TestClient_Lyric(t *testing.T) { c := New(&Environment{ SearchApiKey: "foo", DisplayApiKey: "bar", Live: true, }) fc := newFakeHttpClient("lyric") c.httpClient = fc sr, err := c.Lyric(IPAddress("1.2.3.4"), "test client", 1234) assert.Nil(t, err) assert.Equal(t, "Lorem Ipsum", sr.Track.Title) assert.Equal(t, "/lyric.do", fc.request.URL.Path) }
func TestLoad(t *testing.T) { defer resetLocales() files := map[string]string{ "en.conf": ` [en] greeting = "hello" `, "it.conf": ` [it] greeting = "ciao" `, "other_en.conf": ` [en] # add more translations for locale "en" greeting2 = "hello2" `, "other_it.conf": ` [it] # add more translations for locale "it" greeting2 = "ciao2" `, "es.conf": `# blank file`, } withTestData(files, func(folderPath string) { err := Load(filepath.Join(folderPath, "*.conf")) assert.Nil(t, err) assert.Equal(t, 2, len(locales)) tests := [][]string{ []string{"en", "greeting", "hello"}, []string{"en", "greeting2", "hello2"}, []string{"it", "greeting", "ciao"}, []string{"it", "greeting2", "ciao2"}, } for _, items := range tests { code := items[0] key := items[1] value := items[2] locale := GetLocale(code) assert.NotNil(t, locale) assert.Type(t, "*i18n.Locale", locale) assert.Equal(t, code, locale.Code) tr := locale.Translations[key] assert.Equal(t, value, tr.Value()) } }) }
func TestParseGeometry(t *testing.T) { geometry, err := ParseGeometry("200x300") assert.Nil(t, err) assert.Equal(t, 200, geometry.Width) assert.Equal(t, 300, geometry.Height) geometry, err = ParseGeometry("200x") assert.Nil(t, err) assert.Equal(t, 200, geometry.Width) assert.Equal(t, 0, geometry.Height) geometry, err = ParseGeometry("x300") assert.Nil(t, err) assert.Equal(t, 0, geometry.Width) assert.Equal(t, 300, geometry.Height) geometry, err = ParseGeometry("x") assert.NotNil(t, err) assert.Type(t, "main.ErrorBadGeometry", err) geometry, err = ParseGeometry("bad-geometry") assert.NotNil(t, err) assert.Type(t, "main.ErrorBadGeometry", err) }
func TestSetLocale(t *testing.T) { defer resetLocales() locale := NewLocale("en") AddLocale(locale) var err error err = SetLocale("en") assert.Nil(t, err) assert.Equal(t, locale, currentLocale) err = SetLocale("foo") assert.NotNil(t, err) }
func TestBundler_InitOutputPath(t *testing.T) { defer testRemoveTestDataFolder() b := testNewBundler() _, err := os.Stat(testDataFolder) if err == nil { t.Errorf("%s followed should must be removed before runnning tests", testDataFolder) } b.initOutputPath() fi, err := os.Stat(b.outputPath) assert.Nil(t, err) assert.True(t, fi.IsDir()) }
func TestLoadFile(t *testing.T) { defer resetLocales() files := map[string]string{ "en.conf": ` greeting: hello [en_GB] greeting: hello GB [en_US] greeting: hello US [en_GB] greeting2: hello 2 GB `, } withTestData(files, func(folderPath string) { err := loadFile(filepath.Join(folderPath, "en.conf")) assert.Nil(t, err) assert.Equal(t, 3, len(locales)) tests := [][]string{ []string{"en", "greeting", "hello"}, []string{"en_GB", "greeting", "hello GB"}, []string{"en_GB", "greeting2", "hello 2 GB"}, []string{"en_US", "greeting", "hello US"}, } for _, items := range tests { code := items[0] key := items[1] value := items[2] locale := GetLocale(code) assert.NotNil(t, locale) assert.Type(t, "*i18n.Locale", locale) assert.Equal(t, code, locale.Code) tr := locale.Translations[key] assert.Equal(t, value, tr.Value()) } }) }
func TestClient_newLyricRequest(t *testing.T) { c := New(&Environment{ SearchApiKey: "foo", DisplayApiKey: "bar", }) r, err := c.newLyricRequest(IPAddress("1.2.3.4"), "Test Client", 1234) assert.Nil(t, err) v := r.URL.Query() expected := url.Values{ "output": []string{"json"}, "apikey": []string{"bar"}, "reqtype": []string{"default"}, "trackid": []string{"amg:1234"}, "ipaddress": []string{"1.2.3.4"}, "useragent": []string{"Test Client"}, } assert.Equal(t, expected, v) }
func TestClient_newSearchRequest(t *testing.T) { c := New(&Environment{ SearchApiKey: "foo", DisplayApiKey: "bar", }) r, err := c.newSearchRequest(IPAddress("1.2.3.4"), &SearchOptions{Artist: "artist-foo"}) assert.Nil(t, err) v := r.URL.Query() expected := url.Values{ "output": []string{"json"}, "apikey": []string{"foo"}, "reqtype": []string{"default"}, "searchtype": []string{"track"}, "artist": []string{"artist-foo"}, "ipaddress": []string{"1.2.3.4"}, "displaykey": []string{"bar"}, } assert.Equal(t, expected, v) }
func TestMinifier_min(t *testing.T) { original := `// is.js // (c) 2001 Douglas Crockford // 2001 June 3 // is // The -is- object is used to identify the browser. Every browser edition // identifies itself, but there is no standard way of doing it, and some of // the identification is deceptive. This is because the authors of web // browsers are liars. For example, Microsoft's IE browsers claim to be // Mozilla 4. Netscape 6 claims to be version 5. // Warning: Do not use this awful, awful code or any other thing like it. // Seriously. var is = { ie: navigator.appName == 'Microsoft Internet Explorer', java: navigator.javaEnabled(), ns: navigator.appName == 'Netscape', ua: navigator.userAgent.toLowerCase(), version: parseFloat(navigator.appVersion.substr(21)) || parseFloat(navigator.appVersion), win: navigator.platform == 'Win32' } is.mac = is.ua.indexOf('mac') >= 0; if (is.ua.indexOf('opera') >= 0) { is.ie = is.ns = false; is.opera = true; } if (is.ua.indexOf('gecko') >= 0) { is.ie = is.ns = false; is.gecko = true; }` expected := ` var is={ie:navigator.appName=='Microsoft Internet Explorer',java:navigator.javaEnabled(),ns:navigator.appName=='Netscape',ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=='Win32'} is.mac=is.ua.indexOf('mac')>=0;if(is.ua.indexOf('opera')>=0){is.ie=is.ns=false;is.opera=true;} if(is.ua.indexOf('gecko')>=0){is.ie=is.ns=false;is.gecko=true;}` output := bytes.NewBufferString("") m := newMinifier(bytes.NewBufferString(original), output) err := m.min() assert.Nil(t, err) assert.Equal(t, expected, string(output.Bytes())) m = newMinifier(bytes.NewBufferString(`var x = 0; /*`), output) err = m.min() assert.NotNil(t, err) assert.Equal(t, errorUnterminatedComment, err) m = newMinifier(bytes.NewBufferString(`var x = "unterminated`), output) err = m.min() assert.NotNil(t, err) assert.Equal(t, errorUnterminatedStringLiteral, err) m = newMinifier(bytes.NewBufferString(`var x = /[1-2/`), output) err = m.min() assert.NotNil(t, err) assert.Equal(t, errorUnterminatedSetInRegexpLiteral, err) m = newMinifier(bytes.NewBufferString(`var x = /hello`), output) err = m.min() assert.NotNil(t, err) assert.Equal(t, errorUnterminatedRegexpLiteral, err) }