Exemplo n.º 1
0
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])
}
Exemplo n.º 2
0
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())
}
Exemplo n.º 3
0
func TestRouter_Patch(t *testing.T) {
	router := New()
	assert.Equal(t, 0, len(router.routes["PATCH"]))
	route := router.Patch("/", httpHandlerExample)
	assert.Type(t, "*traffic.Route", route)
	assert.Equal(t, 1, len(router.routes["PATCH"]))
}
Exemplo n.º 4
0
func TestRouter_Delete(t *testing.T) {
	router := New()
	assert.Equal(t, 0, len(router.routes["DELETE"]))
	route := router.Delete("/", httpHandlerExample)
	assert.Type(t, "*traffic.Route", route)
	assert.Equal(t, 1, len(router.routes["DELETE"]))
}
Exemplo n.º 5
0
func TestRouter_Add(t *testing.T) {
	router := New()
	assert.Equal(t, 0, len(router.routes["GET"]))
	route := router.Add(HttpMethod("GET"), "/", httpHandlerExample)
	assert.Type(t, "*traffic.Route", route)
	assert.Equal(t, 1, len(router.routes["GET"]))
}
Exemplo n.º 6
0
func TestNewRoute(t *testing.T) {
	path := "/categories/:category_id/posts/:id"
	route := NewRoute(path, httpHandlerExample, httpHandlerExample2)
	assert.Type(t, "*traffic.Route", route)
	assert.Equal(t, path, route.Path)
	assert.Equal(t, 2, len(route.Handlers))

	expectedPathRegexp := regexp.MustCompile(`\A/categories/(?P<category_id>[^/#?]+)/posts/(?P<id>[^/#?]+)\z`)
	assert.Equal(t, expectedPathRegexp, route.PathRegexp)
}
Exemplo n.º 7
0
func TestRouter_Get(t *testing.T) {
	router := New()
	assert.Equal(t, 0, len(router.routes["GET"]))
	assert.Equal(t, 0, len(router.routes["HEAD"]))
	route := router.Get("/", httpHandlerExample)
	assert.Type(t, "*traffic.Route", route)
	assert.Equal(t, 1, len(router.routes["GET"]))
	assert.Equal(t, 1, len(router.routes["HEAD"]))
	assert.Equal(t, router.routes["GET"][0], router.routes["HEAD"][0])
}
Exemplo n.º 8
0
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())
		}
	})
}
Exemplo n.º 9
0
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)
}
Exemplo n.º 10
0
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())
		}
	})
}
Exemplo n.º 11
0
func TestNew(t *testing.T) {
	router := New()
	assert.Type(t, "*traffic.Router", router)
	assert.Type(t, "map[traffic.HttpMethod][]*traffic.Route", router.routes)

	emptyMap := make(map[HttpMethod][]*Route)
	assert.Equal(t, emptyMap, router.routes)

	assert.Equal(t, 4, len(router.middlewares))
	assert.Type(t, "*traffic.ShowErrorsMiddleware", router.middlewares[0])
	assert.Type(t, "*traffic.LoggerMiddleware", router.middlewares[1])
	assert.Type(t, "*traffic.StaticMiddleware", router.middlewares[2])
	assert.Type(t, "*traffic.RouterMiddleware", router.middlewares[3])

	assert.Equal(t, 0, len(router.env))
}