Esempio n. 1
0
func TestGenerator(t *testing.T) {
	var otto = otto.New()
	var _, err = otto.Run(`
var soy = {};
soy.$$escapeHtml = function(arg) { return arg; };
`)
	if err != nil {
		t.Error(err)
		return
	}

	Funcs["capitalize"] = Func{func(js JSWriter, args []ast.Node) {
		js.Write("(", args[0], ".charAt(0).toUpperCase() + ", args[0], ".slice(1))")
	}, []int{1}}
	defer delete(Funcs, "capitalize")

	soyfile, err := parse.SoyFile("name.soy", `
{namespace test}
{template .funcs}
{let $place: 'world'/}
{capitalize('hel' + 'lo')}, {capitalize($place)}
{/template}`, nil)
	if err != nil {
		t.Error(err)
		return
	}

	var registry = template.Registry{}
	if err = registry.Add(soyfile); err != nil {
		t.Error(err)
		return
	}

	var gen = NewGenerator(&registry)
	var buf bytes.Buffer
	err = gen.WriteFile(&buf, "name.soy")
	if err != nil {
		t.Error(err)
		return
	}

	_, err = otto.Run(buf.String())
	if err != nil {
		t.Error(err)
		return
	}

	output, err := otto.Run(`test.funcs();`)
	if err != nil {
		t.Error(err)
		return
	}
	if output.String() != "Hello, World" {
		t.Errorf("Got %q, expected Hello, World", output.String())
	}
}
Esempio n. 2
0
func initJs(t *testing.T) *otto.Otto {
	var otto = otto.New()
	soyutilsFile, err := os.Open("lib/soyutils.js")
	if err != nil {
		panic(err)
	}
	// remove any non-otto compatible regular expressions
	var soyutilsBuf bytes.Buffer
	var scanner = bufio.NewScanner(soyutilsFile)
	var i = 1
	for scanner.Scan() {
		switch i {
		case 2565, 2579, 2586:
			// skip these regexes
			// soy.esc.$$FILTER_FOR_FILTER_CSS_VALUE_
			// soy.esc.$$FILTER_FOR_FILTER_HTML_ATTRIBUTES_
			// soy.esc.$$FILTER_FOR_FILTER_HTML_ELEMENT_NAME_
		default:
			soyutilsBuf.Write(scanner.Bytes())
			soyutilsBuf.Write([]byte("\n"))
		}
		i++
	}
	// load the soyutils library
	_, err = otto.Run(soyutilsBuf.String())
	if err != nil {
		t.Errorf("soyutils error: %v", err)
		panic(err)
	}
	return otto
}
Esempio n. 3
0
func JsFilter(src string) (f Filter, err error) {
	vm, _, err := otto.Run(src)
	if err != nil {
		return nil, err
	}

	script, err := vm.Compile("", `filter(line)`)
	if err != nil {
		return nil, err
	}

	f = func(line []byte) bool {
		vm.Set("line", line)
		value, err := vm.Run(script)
		if err != nil {
			panic(err)
		}
		res, err := value.ToBoolean()
		if err != nil {
			panic(err)
		}
		return res
	}

	return f, nil
}
Esempio n. 4
0
func TestAddResponseInvalidID(t *testing.T) {
	testPath := "test/response_add.json"
	server := buildResponseAddServer(testPath)
	defer server.Close()

	client = &plurk.PlurkClient{ApiBase: server.URL}

	_, plurkID, _ := otto.Run(`("Invalid")`)
	_, content, _ := otto.Run(`("Hello World")`)
	funcCall := otto.FunctionCall{}
	funcCall.ArgumentList = append(funcCall.ArgumentList, plurkID)
	funcCall.ArgumentList = append(funcCall.ArgumentList, content)

	result := plurk_AddResponse(funcCall)
	success, _ := result.ToBoolean()

	assert.False(t, success)
}
Esempio n. 5
0
func TestLog(t *testing.T) {
	var otto = otto.New()
	_, err := otto.Run(`
var console_output = '';
var console = {};
console.log = function(arg) { console_output += arg; };
var soy = {};
soy.$$escapeHtml = function(arg) { return arg; };
`)
	if err != nil {
		t.Error(err)
		return
	}
	soyfile, err := parse.SoyFile("", `
{namespace test}
{template .log}
{log}Hello {$name}.{/log}
{/template}`, nil)
	if err != nil {
		t.Error(err)
		return
	}
	var buf bytes.Buffer
	err = Write(&buf, soyfile, Options{})
	if err != nil {
		t.Error(err)
		return
	}
	_, err = otto.Run(buf.String())
	if err != nil {
		t.Error(err)
		return
	}
	_, err = otto.Run(`test.log({name: "Rob"});`)
	if err != nil {
		t.Error(err)
		return
	}
	val, _ := otto.Get("console_output")
	if val.String() != "Hello Rob." {
		t.Errorf("got %q", val.String())
	}
}
Esempio n. 6
0
// TestFeaturesJavascript runs the javascript compiled by this implementation
// against that compiled by the reference implementation.
func TestFeaturesJavascript(t *testing.T) {
	rand.Seed(14)
	var registry, err = NewBundle().
		AddGlobalsFile("testdata/FeaturesUsage_globals.txt").
		AddTemplateFile("testdata/simple.soy").
		AddTemplateFile("testdata/features.soy").
		Compile()
	if err != nil {
		t.Error(err)
		return
	}
	var otto = initJs(t)
	for _, soyfile := range registry.SoyFiles {
		var buf bytes.Buffer
		var err = soyjs.Write(&buf, soyfile, soyjs.Options{})
		if err != nil {
			t.Error(err)
			return
		}
		_, err = otto.Run(buf.String())
		if err != nil {
			t.Error(err)
			return
		}
	}

	// Now run all the tests.
	for _, test := range featureTests {
		var jsonData, _ = json.Marshal(test.data)
		var renderStatement = fmt.Sprintf("%s(JSON.parse(%q));",
			"soy.examples.features."+test.name, string(jsonData))
		var actual, err = otto.Run(renderStatement)
		if err != nil {
			t.Errorf("render error: %v\n%v", err, string(jsonData))
			continue
		}

		if actual.String() != test.output {
			t.Errorf("%s\nexpected\n%q\n\ngot\n%q", test.name, test.output, actual.String())
		}
	}
}
Esempio n. 7
0
func TestAddPlurkNoContent(t *testing.T) {
	testPath := "test/plurk_add.json"
	server := buildPlurkAddServer(testPath)
	defer server.Close()

	client = &plurk.PlurkClient{ApiBase: server.URL}

	_, content, _ := otto.Run(`("")`)
	funcCall := otto.FunctionCall{}
	funcCall.ArgumentList = append(funcCall.ArgumentList, content)

	result := plurk_AddPlurk(funcCall)
	success, _ := result.ToBoolean()

	assert.False(t, success)
}
Esempio n. 8
0
func _externalRequire(call otto.FunctionCall, path string) otto.Value {
	d, err := ioutil.ReadFile(path)

	if err != nil {
		// No external module found, let's search our internal path
		return _internalRequire(call, path)
	}

	_, v, err := otto.Run(d)
	if err != nil {
		requireError(path, "external")
		return otto.UndefinedValue()
	}

	return v
}