Example #1
0
func TestSpriteHTTP(t *testing.T) {
	in := bytes.NewBufferString(`
$map: sprite-map("*.png", 10px);
div {
  background: sprite($map, "140");
}`)

	ctx := libsass.NewContext()
	ctx.IncludePaths = []string{"../test"}
	ctx.HTTPPath = "http://foo.com"
	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"
	var out bytes.Buffer
	err := ctx.Compile(in, &out)

	if err != nil {
		t.Error(err)
	}
	e := `div {
  background: url("http://foo.com/build/ec0dbb.png") -0px -149px; }
`
	if e != out.String() {
		t.Errorf("got:\n%s\nwanted:\n%s", out.String(), e)
	}

}
Example #2
0
func ExampleSprite() {
	in := bytes.NewBufferString(`
$map: sprite-map("*.png", 10px); // One argument
div {
  background: sprite($map, "140");
}`)

	ctx := libsass.NewContext()

	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"
	var out bytes.Buffer
	err := ctx.Compile(in, &out)
	if err != nil {
		fmt.Println(err)
	}

	io.Copy(os.Stdout, &out)

	// Output:
	// div {
	//   background: url("img/ec0dbb.png") -0px -149px; }

}
Example #3
0
func TestHandle_erroroffset(t *testing.T) {
	in := bytes.NewBufferString(`
$map: sprite-map("*.png", 10px);
div {
  background: sprite($map, "140", 0, 0);
}`)

	ctx := libsass.NewContext()

	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"
	var out bytes.Buffer
	err := ctx.Compile(in, &out)

	e := `Error > stdin:4
error in C function sprite: Please specify unit for offset ie. (2px)

Backtrace:
	stdin:4, in function ` + "`sprite`" + `
	stdin:4

$map: sprite-map("*.png", 10px);
div {
  background: sprite($map, "140", 0, 0);
}
`
	if e != err.Error() {
		t.Errorf("got:\n%s\nwanted:\n%s", err.Error(), e)
	}

}
Example #4
0
func setupCtx(r io.Reader, out io.Writer, cookies ...libsass.Cookie) (*libsass.Context, libsass.UnionSassValue, error) {
	var usv libsass.UnionSassValue
	ctx := libsass.NewContext()
	ctx.OutputStyle = libsass.NESTED_STYLE
	ctx.IncludePaths = make([]string, 0)
	ctx.BuildDir = "../test/build"
	ctx.ImageDir = "../test/img"
	ctx.FontDir = "../test/font"
	ctx.GenImgDir = "../test/build/img"
	ctx.Out = ""

	testSprite(ctx)
	cc := make(chan libsass.UnionSassValue, len(cookies))
	// If callbacks were made, add them to the context
	// and create channels for communicating with them.
	if len(cookies) > 0 {
		cs := make([]libsass.Cookie, len(cookies))
		for i, c := range cookies {
			cs[i] = libsass.Cookie{
				Sign: c.Sign,
				Fn:   wrapCallback(c.Fn, cc),
				Ctx:  ctx,
			}
		}
		usv = <-cc
	}
	err := ctx.Compile(r, out)
	return ctx, usv, err
}
Example #5
0
func TestCompileSpriteMap(t *testing.T) {
	in := bytes.NewBufferString(`
$aritymap: sprite-map("*.png", 0px); // Optional arguments
$map: sprite-map("*.png"); // One argument
$paddedmap: sprite-map("*.png", 1px); // One argument
div {
width: $map;
height: $aritymap;
line-height: $paddedmap;
}`)

	ctx := libsass.NewContext()

	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"
	var out bytes.Buffer
	err := ctx.Compile(in, &out)
	if err != nil {
		t.Error(err)
	}
	exp := `div {
  width: *.png0;
  height: *.png0;
  line-height: *.png1; }
`

	if exp != out.String() {
		t.Errorf("got:\n%s\nwanted:\n%s", out.String(), exp)
	}
}
Example #6
0
func TestFuncSpriteFile(t *testing.T) {
	ctx := libsass.NewContext()
	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"

	// Add real arguments when sass lists can be [un]marshalled
	lst := []interface{}{"*.png", "139"}
	usv, _ := libsass.Marshal(lst)
	usv = SpriteFile(ctx, usv)
	var glob, path string
	err := libsass.Unmarshal(usv, &glob, &path)
	if err != nil {
		t.Error(err)
	}

	if e := "*.png"; e != glob {
		t.Errorf("got: %s wanted: %s", e, glob)
	}

	if e := "139"; e != path {
		t.Errorf("got: %s wanted: %s", e, path)
	}

}
Example #7
0
func TestHTTPHandler(t *testing.T) {
	ctx := libsass.NewContext()
	hh := http.HandlerFunc(HTTPHandler(ctx))
	req, err := http.NewRequest("GET", "", nil)
	req.Header.Set("Origin", "http://foo.com")
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()
	hh.ServeHTTP(w, req)

	if e := 200; w.Code != e {
		t.Errorf("got: %d wanted: %d", w.Code, e)
	}

	resp := decResp(t, w.Body)
	if e := "input is empty"; resp.Error != e {
		t.Errorf("got: %s wanted: %s", resp, e)
	}

	req, err = http.NewRequest("GET", "",
		bytes.NewBufferString(`div { p { color: red; } }`))
	if err != nil {
		t.Error(err)
	}
	w.Body.Reset()
	hh.ServeHTTP(w, req)

	if e := 200; w.Code != e {
		t.Errorf("got: %d wanted: %d", w.Code, e)
	}
	e := `div p {
  color: red; }
`
	resp = decResp(t, w.Body)
	if resp.Contents != e {
		t.Errorf("got: %s wanted: %s", resp.Contents, e)
	}

	ehead := map[string][]string{
		"Access-Control-Allow-Origin":      []string{"http://foo.com"},
		"Access-Control-Allow-Methods":     []string{"POST, GET, OPTIONS, PUT, DELETE"},
		"Access-Control-Allow-Headers":     []string{"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token"},
		"Access-Control-Allow-Credentials": []string{"true"},
	}

	for i, h := range w.Header() {

		if ehead[i][0] != h[0] {
			t.Errorf("got:\n%q\nwanted:\n%q", h, ehead[i])
		}
	}

}
Example #8
0
func TestHTTPHandler_error(t *testing.T) {
	ctx := libsass.NewContext()
	hh := http.HandlerFunc(HTTPHandler(ctx))
	// nil causes panic, is this a problem?
	req, err := http.NewRequest("GET", "",
		bytes.NewBufferString(`div { p { color: darken(); } };`))
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()
	hh.ServeHTTP(w, req)

	if e := 200; w.Code != e {
		t.Errorf("got: %d wanted: %d", w.Code, e)
	}

	e := `Error > stdin:1
required parameter $color is missing in call to function darken
div { p { color: darken(); } };
`
	resp := decResp(t, w.Body)

	if resp.Error != e {
		t.Errorf("got:\n%s\nwanted:\n%s", resp.Error, e)
	}

	req, err = http.NewRequest("GET", "",
		bytes.NewBufferString(`div { p { color: red; } }`))
	if err != nil {
		t.Error(err)
	}
	w.Body.Reset()
	hh.ServeHTTP(w, req)

	if e := 200; w.Code != e {
		t.Errorf("got: %d wanted: %d", w.Code, e)
	}
	e = `div p {
  color: red; }
`
	resp = decResp(t, w.Body)

	if resp.Contents != e {
		t.Errorf("got:\n%s\nwanted:\n%s", resp.Contents, e)
	}

	// Second run shouldn't have an error in it
}
Example #9
0
func BenchmarkSprite(b *testing.B) {
	ctx := libsass.NewContext()
	ctx.BuildDir = "context/test/build"
	ctx.GenImgDir = "context/test/build/img"
	ctx.ImageDir = "context/test/img"
	// Add real arguments when sass lists can be [un]marshalled
	lst := []interface{}{"*.png", libsass.SassNumber{Value: 5, Unit: "px"}}
	usv, _ := libsass.Marshal(lst)

	for i := 0; i < b.N; i++ {
		usv = SpriteMap(ctx, usv)
	}
	// Debug if needed
	// var s string
	// Unmarshal(usv, &s)
	// fmt.Println(s)
}
Example #10
0
func TestFuncSpriteMap(t *testing.T) {
	ctx := libsass.NewContext()
	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"

	// Add real arguments when sass lists can be [un]marshalled
	lst := []interface{}{"*.png", libsass.SassNumber{Value: 5, Unit: "px"}}
	usv, _ := libsass.Marshal(lst)
	usv = SpriteMap(ctx, usv)
	var path string
	err := libsass.Unmarshal(usv, &path)
	if err != nil {
		t.Error(err)
	}

	if e := "*.png5"; e != path {
		t.Errorf("got: %s wanted: %s", path, e)
	}
}
Example #11
0
func TestSpriteMany(t *testing.T) {

	in := bytes.NewBufferString(`
$map: sprite-map("many/*.jpg", 0px);
div {
  background: sprite($map, "bird");
  background: sprite($map, "in");
  background: sprite($map, "pencil");
  background: sprite($map, "rss");
  background: sprite($map, "twitt");
}`)

	ctx := libsass.NewContext()

	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"
	ctx.ImageDir = "../test/img"
	var out bytes.Buffer
	err := ctx.Compile(in, &out)

	if err != nil {
		t.Error(err)
	}

	e := `div {
  background: url("img/617970.png") -0px -0px;
  background: url("img/617970.png") -0px -150px;
  background: url("img/617970.png") -0px -300px;
  background: url("img/617970.png") -0px -450px;
  background: url("img/617970.png") -0px -600px; }
`

	if out.String() != e {
		t.Errorf("got:\n%s\nwanted:\n%s", out.String(), e)
	}
}
Example #12
0
func TestCompileSpritePaddingMap(t *testing.T) {
	in := bytes.NewBufferString(`$map: sprite-map("*.png",10px);
div {
  content: $map;
}`)

	ctx := libsass.NewContext()

	ctx.ImageDir = "../test/img"
	ctx.BuildDir = "../test/build"
	ctx.GenImgDir = "../test/build/img"

	var out bytes.Buffer
	err := ctx.Compile(in, &out)
	if err != nil {
		t.Error(err)
	}
	exp := `div {
  content: *.png10; }
`
	if exp != out.String() {
		t.Errorf("got:\n%s\nwanted:\n%s", out.String(), exp)
	}
}