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
|
package libsass
import (
"bytes"
"log"
"os"
"testing"
"github.com/wellington/go-libsass/libs"
)
func TestContextFile(t *testing.T) {
in := bytes.NewBufferString(`div {
span {
color: black;
}
width: 100px;
height: 100px;
}
p {
background: red;
}`)
var out bytes.Buffer
ctx := newContext()
err := ctx.compile(&out, in)
if err != nil {
panic(err)
}
e := `div {
width: 100px;
height: 100px; }
div span {
color: black; }
p {
background: red; }
`
if e != out.String() {
t.Errorf("wanted:\n%s\n"+
"got:\n%s\n", e, out.String())
}
}
func TestContextNilRun(t *testing.T) {
var in, out bytes.Buffer
ctx := newContext()
err := ctx.compile(&out, &in)
if err == nil {
t.Error("No error returned")
}
if e := "No input provided"; e != err.Error() {
t.Errorf("wanted:\n%s\ngot:\n%s", e, err)
}
}
func TestContextRun(t *testing.T) {
in := bytes.NewBufferString(`$red-var: red;
$hex: #00FF00;
div {
background: $hex;
$hex: #00DD00;
font-size: 10pt;
}
`)
var out bytes.Buffer
ctx := newContext()
err := ctx.compile(&out, in)
if err != nil {
panic(err)
}
e := `div {
background: #00FF00;
font-size: 10pt; }
`
if e != out.String() {
t.Errorf("wanted:\n%s\n"+
"got:\n%s\n", e, out.String())
}
}
func TestLibsassError(t *testing.T) {
in := bytes.NewBufferString(`div {
color: red(blue, purple);
}`)
var out bytes.Buffer
ctx := newContext()
ctx.Funcs.Add(Func{
Sign: "foo()",
Fn: TestCallback,
Ctx: &ctx,
})
err := ctx.compile(&out, in)
if err == nil {
t.Error("No error thrown for incorrect arity")
}
if e := "wrong number of arguments (2 for 1) for `red'"; e != ctx.err.Message {
t.Errorf("wanted:%s\ngot:%s\n", e, ctx.err.Message)
}
e := `Error > stdin:2
wrong number of arguments (2 for 1) for ` + "`" + `red'
div {
color: red(blue, purple);
}
`
if e != err.Error() {
t.Errorf("wanted:\n%s\ngot:\n%s\n", e, err)
}
}
func ExampleContext_Compile() {
in := bytes.NewBufferString(`div {
color: red(blue);
background: foo();
}`)
ctx := newContext()
ctx.Funcs.Add(Func{
Sign: "foo()",
Fn: func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error {
res, _ := Marshal("no-repeat")
*rsv = res.Val()
return nil
},
Ctx: &ctx,
})
err := ctx.compile(os.Stdout, in)
if err != nil {
log.Fatal(err)
}
// Output:
// div {
// color: 0;
// background: no-repeat; }
}
func BenchmarkContextCompile(b *testing.B) {
bits := []byte(`div { color: #005500; }`)
big := []byte(`div { color: #005500; } `)
ctx := newContext()
out := bytes.NewBuffer(big)
for i := 0; i < b.N; i++ {
in := bytes.NewBuffer(bits)
out.Reset()
err := ctx.compile(out, in)
if err != nil {
b.Error(err)
}
}
}
|