1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package libsass
import (
"bytes"
"strings"
"testing"
)
type ErrorMap struct {
line int
message string
}
func TestError_basic(t *testing.T) {
in := bytes.NewBufferString(`div {
@include invalid-function('');
}`)
out := bytes.NewBuffer([]byte(""))
ctx := newContext()
err := ctx.compile(out, in)
if err == nil {
t.Error("No error returned")
}
e := ErrorMap{2, "no mixin named invalid-function"}
if e.line != ctx.err.Line {
t.Errorf("wanted: %d\ngot: %d", e.line, ctx.err.Line)
}
if !strings.Contains(ctx.err.Message, e.message) {
t.Errorf("%q does not contain %q", ctx.err.Message, e.message)
}
if ctx.errorString != ctx.Error() {
t.Errorf("wanted: %s got: %s", ctx.errorString, ctx.Error())
}
}
func TestError_JSON(t *testing.T) {
in := bytes.NewBufferString(`div {
height: 10px;`)
out := &bytes.Buffer{}
ctx := newContext()
err := ctx.compile(out, in)
e := `Error > stdin:2
Invalid CSS after " height: 10px;": expected "}", was ""
div {
height: 10px;
`
if err == nil {
t.Fatal("no error thrown")
}
if e != err.Error() {
t.Fatalf("got: %s\nwanted: %s", err, e)
}
}
func TestError_unbound(t *testing.T) {
in := bytes.NewBufferString(`div {
background: map-get($sprite,139);
}`)
out := bytes.NewBuffer([]byte(""))
ctx := newContext()
err := ctx.compile(out, in)
if err == nil {
t.Error("No error returned")
}
e := ErrorMap{2, "unbound variable $sprite"}
if e.line != ctx.err.Line {
t.Errorf("wanted:\n%d\ngot:\n%d", e.line, ctx.err.Line)
}
}
func TestError_function(t *testing.T) {
in := bytes.NewBufferString(`// Empty line
@function uniqueFnName($file) {
@return map-get($file,prop);
}
div {
background: uniqueFnName(randfile);
}`)
out := bytes.NewBuffer([]byte(""))
ctx := newContext()
err := ctx.compile(out, in)
if err == nil {
t.Error("No error returned")
}
e := ErrorMap{3, "argument `$map` of `map-get($map, $key)` must be a map"}
if e.line != ctx.err.Line {
t.Errorf("wanted:\n%d\ngot:\n%d", e.line, ctx.err.Line)
}
if !strings.Contains(ctx.err.Message, e.message) {
t.Errorf("%q does not contain %q", ctx.err.Message, e.message)
}
}
func TestError_import(t *testing.T) {
in := bytes.NewBufferString(`span {}
@import "fail";
`)
out := bytes.NewBuffer([]byte(""))
ctx := newContext()
err := ctx.compile(out, in)
if err == nil {
t.Error("No error returned")
}
e := ErrorMap{2, `File to import not found or unreadable: fail.`}
if e.line != ctx.err.Line {
t.Errorf("wanted:\n%d\ngot:\n%d", e.line, ctx.err.Line)
}
if !strings.Contains(ctx.err.Message, e.message) {
t.Errorf("%q does not contain %q", ctx.err.Message, e.message)
}
}
func TestError_processsass(t *testing.T) {
in := []byte(`{
"status": 1,
"file": "stdin",
"line": 3100,
"column": 20,
"message": "error in C function inline-image: format: .svg not supported\nBacktrace:\n\tstdin:3100, in function inline-image\n\tstdin:3100, in mixin printCSSImg\n\tstdin:3117"
}`)
ctx := newContext()
err := ctx.ProcessSassError(in)
if err != nil {
t.Error(err)
}
e := `Error > stdin:3100
error in C function inline-image: format: .svg not supported
Backtrace:
stdin:3100, in function inline-image
stdin:3100, in mixin printCSSImg
stdin:3117`
if e != ctx.Error() {
t.Errorf("got:\n%s\nwanted:\n%s", err.Error(), e)
}
}
func TestError_invalid(t *testing.T) {
ctx := newContext()
err := ctx.ProcessSassError([]byte("/a"))
if len(err.Error()) == 0 {
t.Error("No error thrown on invalid sass json package")
}
}
func TestError_line(t *testing.T) {
ctx := newContext()
ctx.errorString = "Error > stdin:1000"
if e := 1000; e != ctx.ErrorLine() {
t.Errorf("got: %d wanted: %d", ctx.ErrorLine(), e)
}
}
|