Example #1
0
func expectParseError(substring string, script string, t *testing.T) {
	_, err := parser.Parse(script)
	if err == nil {
		t.Errorf("Expected parse error containing %q, but there was no error.", substring)
	} else if !strings.Contains(fmt.Sprint(err), substring) {
		t.Errorf("Expected parse error containing %q, but error was %q.", substring, err)
	}
}
Example #2
0
func ExampleParse() {
	var state goscript.State

	input := `{
		Print(10 - -7);
		Print(1 + 4 * 4);
	};`

	program, err := parser.Parse(input)

	if err != nil {
		log.Println("Error parsing: ", err)
	}

	program.Execute(&state)

	// Output:
	// 17
	// 17
}
Example #3
0
func expectPanic(substring string, script string, t *testing.T) {
	defer func() {
		if p := recover(); p != nil {
			if !strings.Contains(fmt.Sprint(p), substring) {
				t.Errorf("Expected panic containing %q, but panic was %q.", substring, p)
			}
		} else {
			t.Errorf("Expected panic containing %q, but no panic.", substring)
		}
	}()

	var state goscript.State

	program, err := parser.Parse(script)
	if err != nil {
		t.Error("Unexpected parse error:", err)
	}

	program.Execute(&state)
}