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 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 TestParseConfig_UnmarshalError(t *testing.T) { content := `Unmarshal\nError` r := bytes.NewBufferString(content) _, err := parseConfig(r) assert.NotNil(t, err) }
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 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 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 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 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 TestMainHandler_GenerateThumb(t *testing.T) { InitSettings() fixturesPath := "./test_fixtures" settings["PublicPath"] = fixturesPath fileName := "test-100x100.png" filePath := path.Join(fixturesPath, fileName) // we don't have a thumb yet _, err := os.Stat(filePath) assert.NotNil(t, err) recorder := httptest.NewRecorder() request, _ := http.NewRequest("GET", fmt.Sprintf("/%s", fileName), nil) MainHandler(recorder, request) // check if the thumb has been generated assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, "image/jpeg", recorder.HeaderMap.Get("Content-Type")) }
func TestPlate_OpenTemplate_template_not_found(t *testing.T) { p := newPlate("__test-fixtures__", "tmp") _, err := p.openTemplate("random-name") assert.NotNil(t, err) }
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) }