Ejemplo n.º 1
0
func TestJqSimpleProgram(t *testing.T) {
	state, err := jq.New()

	if err != nil {
		t.Errorf("Error initializing state_state: %v", err)
	}
	defer state.Close()

	input, err := jq.JvFromJSONString("{\"a\": 123}")
	if err != nil {
		t.Error(err)
	}

	cIn, cOut, cErrs := state.Start(".a", jq.JvArray())
	outputs, errs := feedJq(input, cIn, cOut, cErrs)

	if len(errs) > 0 {
		t.Errorf("Expected no errors, but got %#v", errs)
	}

	if l := len(outputs); l != 1 {
		t.Errorf("Got %d outputs (%#v), expected %d", l, outputs, 1)
	} else if val := outputs[0].ToGoVal(); val != 123 {
		t.Errorf("Got %#v, expected %#v", val, 123)
	}
}
Ejemplo n.º 2
0
func TestJqCompileError(t *testing.T) {
	state, err := jq.New()

	if err != nil {
		t.Errorf("Error initializing jq_state: %v", err)
	}
	defer state.Close()

	const program = "a b"
	cIn, cOut, cErr := state.Start(program, jq.JvArray())
	_, errors := feedJq(nil, cIn, cOut, cErr)

	// JQ might (and currently does) report multiple errors. One of them will
	// contain our input program. Check for that but don't be overly-specific
	// about the string or order of errors

	gotErrors := false
	for _, err := range errors {
		gotErrors = true
		if strings.Contains(err.Error(), program) {
			// t.Pass("Found the error we expected: %#v\n",
			return
		}
	}

	if !gotErrors {
		t.Fatal("Errors were expected but none seen")
	}
	t.Fatal("No error containing the program source found")
}
Ejemplo n.º 3
0
// New creates a nwq JqRepl
//
// If stdin is not a tty then it will re-open the controlling tty ("/dev/tty"
// on unix) to be able to run in interactive mode
func New() (*JqRepl, error) {
	repl := JqRepl{
		promptTemplate: promptTemplate,
	}

	cfg, err := repl.readlineReplConfig()
	if err != nil {
		return nil, err
	}

	repl.reader, err = readline.NewEx(cfg)
	if err != nil {
		return nil, err
	}

	repl.libJq, err = jq.New()
	if err != nil {
		repl.reader.Close()
		return nil, err
	}

	repl.results = jq.JvArray()

	return &repl, nil
}
Ejemplo n.º 4
0
func TestJqRuntimeError(t *testing.T) {
	state, err := jq.New()

	if err != nil {
		t.Errorf("Error initializing state_state: %v", err)
	}
	defer state.Close()

	input, err := jq.JvFromJSONString(`{"a": 123}`)
	if err != nil {
		t.Error(err)
	}

	cIn, cOut, cErrs := state.Start(".[0]", jq.JvArray())
	_, errors := feedJq(input, cIn, cOut, cErrs)

	if l := len(errors); l != 1 {
		t.Errorf("Got %d errors (%#v), expected %d", l, errors, 1)
	}
}
Ejemplo n.º 5
0
func makeProgramArgs(history *jq.Jv) *jq.Jv {
	// Create this structure:
	// programArgs = [
	//		{"name": "out", "value": history },
	//    {"name": "_", "value": history[-1] },
	//    {"name": "__", "value": history[-2] },
	// ]

	arg := jq.JvObject().ObjectSet(jvStringName.Copy(), jvStringOut.Copy()).ObjectSet(jvStringValue.Copy(), history.Copy())
	res := jq.JvArray().ArrayAppend(arg)

	len := history.Copy().ArrayLength()

	if len >= 1 {
		arg = jq.JvObject().ObjectSet(jvStringName.Copy(), jvStringUnderscore.Copy()).ObjectSet(jvStringValue.Copy(), history.Copy().ArrayGet(len-1))
		res = res.ArrayAppend(arg)
	}
	if len >= 2 {
		arg = jq.JvObject().ObjectSet(jvStringName.Copy(), jvStringDunderscore.Copy()).ObjectSet(jvStringValue.Copy(), history.Copy().ArrayGet(len-2))
		res = res.ArrayAppend(arg)
	}

	return res
}