Exemplo n.º 1
0
func TestBinary_math(t *testing.T) {
	const src = `
$a: inspect(1 + 3);
$b: inspect(3/1);
$c: inspect(1/2 + 1/2);
$d: inspect(2*2);
$d: inspect(1*1/2);
$e: inspect(1/2*1/2);
$f: inspect(2*2/2*2);

//$o: inspect(3px + 3px + 3px);
`
	f, err := ParseFile(token.NewFileSet(), "", src, 0|Trace)
	if err != nil {
		t.Fatal(err)
	}

	lits := []string{
		"4",
		"3",
		"1",
		"4",
		"0.5",
		"0.25",
		"4",
	}
	var pos int
	ast.Inspect(f, func(n ast.Node) bool {
		if call, ok := n.(*ast.CallExpr); ok {
			// ident := call.Fun.(*ast.Ident)
			lit := call.Resolved.(*ast.BasicLit)
			val := lits[pos]
			pos++

			if val != lit.Value {
				t.Errorf("useful ident here got: %s wanted %s", lit.Value, val)
			}
		}
		return true
	})

}
Exemplo n.º 2
0
func TestUnitMath(t *testing.T) {
	const src = `
$a: 3px + 3px;
div {
  width: $a;
}
`
	f, err := ParseFile(token.NewFileSet(), "", src, 0)
	if err != nil {
		t.Fatal(err)
	}

	objects := map[string]ast.ObjKind{
		"$color": ast.Var,
		"$list":  ast.Var,
	}
	ast.Inspect(f, func(n ast.Node) bool {
		if spec, ok := n.(*ast.RuleSpec); ok {
			ast.Print(token.NewFileSet(), spec)
		}
		return true
		if ident, ok := n.(*ast.Ident); ok {
			return true
			obj := ident.Obj
			if obj == nil {
				if objects[ident.Name] != ast.Bad {
					t.Errorf("no object for %s", ident.Name)
				}
				return true
			}
			if obj.Name != ident.Name {
				t.Errorf("names don't match: obj.Name = %s, ident.Name = %s", obj.Name, ident.Name)
			}
			kind := objects[ident.Name]
			if obj.Kind != kind {
				t.Errorf("%s: obj.Kind = %s; want %s", ident.Name, obj.Kind, kind)
			}
		}
		return true
	})
}
Exemplo n.º 3
0
func TestObjects(t *testing.T) {
	const src = `
$color: red;
$list: 1 2 $color;
`
	f, err := ParseFile(token.NewFileSet(), "", src, 0)
	if err != nil {
		t.Fatal(err)
	}

	objects := map[string]ast.ObjKind{
		"$color": ast.Var,
		"$list":  ast.Var,
	}

	ast.Inspect(f, func(n ast.Node) bool {
		if ident, ok := n.(*ast.Ident); ok {

			obj := ident.Obj
			if obj == nil {
				if objects[ident.Name] != ast.Bad {
					t.Errorf("no object for %s", ident.Name)
				}
				return true
			}
			if obj.Name != ident.Name {
				t.Errorf("names don't match: obj.Name = %s, ident.Name = %s", obj.Name, ident.Name)
			}
			kind := objects[ident.Name]
			if obj.Kind != kind {
				t.Errorf("%s: obj.Kind = %s; want %s", ident.Name, obj.Kind, kind)
			}
		}
		return true
	})
}