File: func_test.go

package info (click to toggle)
golang-github-wellington-go-libsass 0.9.2%2Bgit20181130.4ef5b9d-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,128 kB
  • sloc: cpp: 28,607; ansic: 839; makefile: 44
file content (187 lines) | stat: -rw-r--r-- 3,855 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package libsass

import (
	"bytes"
	"image/color"
	"reflect"
	"testing"
	"time"

	"github.com/wellington/go-libsass/libs"
)

func TestFunc_simpletypes(t *testing.T) {
	in := bytes.NewBufferString(`div {
  background: foo(null, 3px, asdf, false);
}`)

	//var out bytes.Buffer
	ctx := newContext()
	// Communication channel for the C Sass callback function
	ch := make(chan []interface{}, 1)

	ctx.Funcs.Add(Func{

		Sign: "foo($null, $num, $str, $bool)",
		Fn: Handler(func(v interface{}, req SassValue, res *SassValue) error {
			var n interface{}
			var num libs.SassNumber
			var s string
			var b bool
			var intf = []interface{}{n, num, s, b}
			err := Unmarshal(req, &intf)
			if err != nil {
				t.Fatal(err)
			}
			// Send the interface fn arguments to the ch channel
			ch <- intf
			return nil
		}),
		Ctx: &ctx,
	})
	var out bytes.Buffer
	err := ctx.compile(&out, in)
	if err != nil {
		t.Error(err)
	}

	e := []interface{}{
		"<nil>",
		libs.SassNumber{Value: 3.0, Unit: "px"},
		"asdf",
		false,
	}
	var args []interface{}
	select {
	case <-time.After(10 * time.Millisecond):
		t.Fatal("timeout")
	case args = <-ch:
	}
	if !reflect.DeepEqual(e, args) {
		t.Errorf("wanted:\n% #v\ngot:\n% #v", e, args)
	}
}

func TestFunc_colortype(t *testing.T) {
	in := bytes.NewBufferString(`div {
  background: foo(#005500);
}`)

	//var out bytes.Buffer
	ctx := newContext()
	// Communication channel for the C Sass callback function
	ch := make(chan []interface{}, 1)

	ctx.Funcs.Add(Func{
		Sign: "foo($color)",
		Fn: func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error {
			// Send the interface fn arguments to the ch channel
			var infs = []interface{}{color.RGBA{}}
			err := Unmarshal(SassValue{value: usv}, &infs)
			if err != nil {
				t.Fatal(err)
			}
			ch <- infs
			res, _ := Marshal(false)
			*rsv = res.Val()
			return nil
		},
		Ctx: &ctx,
	})
	var out bytes.Buffer
	err := ctx.compile(&out, in)
	if err != nil {
		t.Error(err)
	}

	e := []interface{}{
		color.RGBA{R: 0x0, G: 0x55, B: 0x0, A: 0x1},
	}
	var args []interface{}
	select {
	case <-time.After(100 * time.Millisecond):
		t.Fatal("timeout")
	case args = <-ch:
	}
	if !reflect.DeepEqual(e, args) {
		t.Errorf("wanted:\n% #v\ngot:\n% #v", e, args)
	}

}

func TestFunc_complextypes(t *testing.T) {
	in := bytes.NewBufferString(`div {
	     background: foo((a,b,1mm,#003300));
	   }`)

	var out bytes.Buffer
	ctx := newContext()
	ch := make(chan interface{}, 1)
	ctx.Funcs.Add(Func{
		Sign: "foo($list)",
		Fn: func(v interface{}, usv libs.UnionSassValue, rsv *libs.UnionSassValue) error {
			var sv interface{}
			Unmarshal(SassValue{value: usv}, &sv)
			ch <- sv
			res, _ := Marshal(false)
			*rsv = res.Val()
			return nil
		},
		Ctx: &ctx,
	})
	err := ctx.compile(&out, in)
	if err != nil {
		t.Error(err)
	}

	e := []interface{}{
		"a",
		"b",
		libs.SassNumber{Value: 1, Unit: "mm"},
		color.RGBA{R: 0x0, G: 0x33, B: 0x0, A: 0x1},
	}
	var args interface{}
	select {
	case <-time.After(100 * time.Millisecond):
		t.Fatal("timeout")
	case args = <-ch:
	}
	if !reflect.DeepEqual(e, args) {
		t.Errorf("wanted:\n%#v\ngot:\n% #v", e, args)
	}
}

func TestFunc_customarity(t *testing.T) {
	in := bytes.NewBufferString(`div {
  color: red(blue);
  background: foo(1pt, 2cm);
}`)

	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")
	}
	e := "wrong number of arguments (2 for 0) for `foo'"
	if e != ctx.err.Message {
		t.Errorf("wanted:\n%s\ngot:\n%s\n", e, ctx.err.Message)
	}
	e = `Error > stdin:3
wrong number of arguments (2 for 0) for ` + "`" + `foo'
div {
  color: red(blue);
  background: foo(1pt, 2cm);
}
`
	if e != err.Error() {
		t.Errorf("wanted:\n%s\ngot:\n%s\n", e, err)
	}

}