예제 #1
0
// hmmmm: interface-wise, this tests that FinderNode.Add() returns an
// object whose CommandString() behaves sensibly... but in
// implementation terms, it's really a test of FuList.CommandString()
func Test_FinderNode_Add_CommandString(t *testing.T) {
	finder1 := NewFinderNode("*.c", "*.h")
	finder2 := NewFinderNode("doc/???.txt")
	finder3 := NewFinderNode()

	sum1, err := finder1.Add(finder2)
	assert.Nil(t, err)
	assert.Equal(t, "'*.c' '*.h' 'doc/???.txt'", sum1.CommandString())

	sum2, err := finder3.Add(sum1)
	assert.Nil(t, err)
	assert.Equal(t, "'*.c' '*.h' 'doc/???.txt'", sum2.CommandString())

	assert.False(t, sum1.Equal(sum2))

	sum2b, err := finder3.Add(sum1)
	assert.Nil(t, err)
	assert.True(t, sum2.Equal(sum2b),
		"expected equal ListNodes:\nsum2  = %T %v\nsum2b = %T %v",
		sum2, sum2, sum2b, sum2b)

	// This is a silly thing to do, and perhaps we should filter out
	// the duplicate patterns... but I don't think so. If the user
	// constructs something silly, we do something silly.
	sum3, err := sum1.Add(sum2)
	assert.Nil(t, err)
	assert.Equal(t,
		"'*.c' '*.h' 'doc/???.txt' '*.c' '*.h' 'doc/???.txt'",
		sum3.CommandString())
}
예제 #2
0
func Test_FuFunction_CheckArgs_minmax(t *testing.T) {
	fn := NewVariadicFunction("bar", 2, 4, nil)
	val := MakeFuString("a")
	args := MakeBasicArgs(nil, []FuObject{val}, nil)
	err := fn.CheckArgs(args)
	assert.Equal(t,
		"function bar() requires at least 2 arguments (got 1)", err.Error())

	// 2 args are good
	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Nil(t, err)

	// 3 args are good
	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Nil(t, err)

	// 4 args are good
	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Nil(t, err)

	// but 5 args is *right out*
	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Equal(t,
		"function bar() takes at most 4 arguments (got 5)", err.Error())
}
예제 #3
0
파일: build_test.go 프로젝트: sbinet/fubsy
// full successful build, then try some incremental rebuilds
func Test_BuildState_BuildTargets_rebuild(t *testing.T) {
	sig := []byte{0}
	db, goal, opts := fullBuild(t, sig)

	// now the rebuild, after marking all nodes unchanged
	graph, executed := setupBuild(true, sig)

	expect := []buildexpect{}
	bstate := NewBuildState(graph, db, opts)
	err := bstate.BuildTargets(goal)
	assert.Nil(t, err)
	assertBuild(t, graph, expect, *executed)

	// again, but this time change one source file (misc.h, forcing
	// rebuilds of misc.o and tool1.o -- but those two will appear
	// unchanged, so we short-circuit the build and do *not* rebuild
	// tool1)
	graph, executed = setupBuild(true, sig)
	graph.Lookup("misc.h").(*dag.StubNode).SetSignature([]byte{1})

	expect = []buildexpect{
		{"tool1.o", dag.BUILT},
		{"misc.o", dag.BUILT},
	}
	bstate = NewBuildState(graph, db, opts)
	err = bstate.BuildTargets(goal)
	assert.Nil(t, err)
	assertBuild(t, graph, expect, *executed)
}
예제 #4
0
func TestMirroring(t *testing.T) {
	if !IsMirrorHostDefined() {
		t.Skip("mirror host is not defined")
	}
	conn := ConnectToTestDb(t)
	if conn == nil {
		return
	}
	defer conn.Close()

	rst, err := conn.Exec("select * from authors")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	assert.Equal(t, 1, len(rst))
	assert.Equal(t, 23, len(rst[0].Rows))
	assert.Equal(t, 23, rst[0].RowsAffected)

	err = failover(conn)
	if err != nil {
		fmt.Printf("failover error %s %s %s\n", err, conn.Error, conn.Message)
		assert.Nil(t, err)
	}
	rst, err = conn.Exec("select * from authors")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	assert.Equal(t, 1, len(rst))
	assert.Equal(t, 23, len(rst[0].Rows))
	assert.Equal(t, 23, rst[0].RowsAffected)
}
예제 #5
0
func Test_FuFunction_CheckArgs_fixed(t *testing.T) {
	val := MakeFuString("a")
	args := MakeBasicArgs(nil, []FuObject{}, nil)
	fn := NewFixedFunction("meep", 0, nil)

	err := fn.CheckArgs(args)
	assert.Nil(t, err)

	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Equal(t,
		"function meep() takes no arguments (got 1)", err.Error())

	fn = NewFixedFunction("foo", 2, nil)
	args.args = args.args[:0]
	err = fn.CheckArgs(args)
	assert.Equal(t,
		"function foo() takes exactly 2 arguments (got 0)", err.Error())

	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Equal(t,
		"function foo() takes exactly 2 arguments (got 1)", err.Error())

	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Nil(t, err)

	args.args = append(args.args, val)
	err = fn.CheckArgs(args)
	assert.Equal(t,
		"function foo() takes exactly 2 arguments (got 3)", err.Error())
}
예제 #6
0
func TestExecute(t *testing.T) {
	conn := ConnectToTestDb(t)
	if conn == nil {
		return
	}
	defer conn.Close()

	rst, err := conn.Exec("select 1")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	assert.Equal(t, 1, len(rst))
	rst, err = conn.Exec("select missing")
	assert.NotNil(t, err)
	assert.Nil(t, rst)
	rst, err = conn.Exec("print 'pero'")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	assert.True(t, strings.Contains(conn.Message, "pero"))
	assert.Equal(t, 1, len(rst))
	assert.Equal(t, 0, len(rst[0].Rows))
	rst, err = conn.Exec("sp_help 'authors'")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	assert.Equal(t, 9, len(rst))
}
예제 #7
0
func TestDuplicateKeys(t *testing.T) {
	worker, err := idworker.NewIdWorker(1, 1)
	assert.Nil(t, err)

	const MAXIDS = 100000

	var ids [MAXIDS]uint64

	start := time.Now()

	for i := 0; i < MAXIDS; i++ {
		id, err := worker.Next()
		assert.Nil(t, err)

		ids[i] = id
	}

	for i := 1; i < MAXIDS; i++ {
		if ids[i-1] > ids[i] {
			t.Errorf("next id %v is smaller then previous one %v", ids[i], ids[i-1])
		}
	}

	end := time.Now()

	delta := float64(end.UnixNano()-start.UnixNano()) / float64(1000*1000)
	t.Logf("Execution time: %fms\n", delta)
	t.Logf("Ids/sec: %f\n", float64(MAXIDS)/delta)
}
예제 #8
0
func TestPathMatchHandler(t *testing.T) {

	pathPattern, _ := paths.NewPathPattern("collection/{id}/name")
	var called bool = false
	h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error {
		called = true
		return nil
	}))

	ctx1 := context_test.MakeTestContextWithPath("/collection/123/name")
	will, _ := h.WillHandle(ctx1)
	assert.True(t, will)
	h.Handle(ctx1)
	assert.True(t, called, "Method should be called")
	assert.Equal(t, "123", ctx1.Data().Get(context.DataKeyPathParameters).(objects.Map).Get("id"))

	ctx2 := context_test.MakeTestContextWithPath("/collection")
	will, _ = h.WillHandle(ctx2)
	assert.False(t, will)
	assert.Nil(t, ctx2.Data().Get(context.DataKeyPathParameters))

	h.BreakCurrentPipeline = true
	shouldStop, handleErr := h.Handle(ctx2)
	assert.Nil(t, handleErr)
	assert.True(t, shouldStop)
	assert.True(t, called, "Handler func should get called")

}
예제 #9
0
func TestDuplicate(t *testing.T) {

	total := 1000 * 1000
	data := make(map[uint64]int)

	sf, err := Default()
	assert.Nil(t, err)

	var id, pre uint64
	for i := 0; i < total; i++ {

		id, err = sf.Next()
		assert.Nil(t, err)

		if id < pre {
			t.Errorf("id %v is samller than previous one %v", id, pre)
		}
		pre = id

		count := data[id]
		if count > 0 {
			t.Errorf("duplicate id %v %d", id, count)
		}
		data[id] = count + 1
	}

	length := len(data)
	t.Logf("map length %v", length)
	if length != total {
		t.Errorf("legth does not match want %v actual %d", total, length)
	}

}
예제 #10
0
func Test_FileNode_Add(t *testing.T) {
	node0 := NewFileNode("foo/bar")
	node1 := NewFileNode("foo/baz")
	obj0 := types.MakeFuString(".c")
	obj1 := types.MakeStringList("a", "b")

	var err error
	var expect types.FuObject
	var actual types.FuObject

	// node + node = list of nodes
	expect = types.MakeFuList(node0, node1)
	actual, err = node0.Add(node1)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))

	// node + string = new node
	expect = NewFileNode("foo/bar.c")
	actual, err = node0.Add(obj0)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))

	// node + list = flattened list
	expect = types.MakeFuList(
		node0, types.MakeFuString("a"), types.MakeFuString("b"))
	actual, err = node0.Add(obj1)
	assert.Nil(t, err)
	assert.True(t, expect.Equal(actual))
}
예제 #11
0
func Test_FuString_Add_strings(t *testing.T) {
	s1 := MakeFuString("hello")
	s2 := MakeFuString("world")
	var result FuObject
	var err error

	// s1 + s1
	result, err = s1.Add(s1)
	assert.Nil(t, err)
	assert.Equal(t, "hellohello", result.(FuString).value)

	// s1 + s2
	result, err = s1.Add(s2)
	assert.Nil(t, err)
	assert.Equal(t, "helloworld", result.(FuString).value)

	// s1 + s2 + s1 + s2
	// (equivalent to ((s1.Add(s2)).Add(s1)).Add(s2), except we have
	// to worry about error handling)
	result, err = s1.Add(s2)
	assert.Nil(t, err)
	result, err = result.Add(s1)
	assert.Nil(t, err)
	result, err = result.Add(s2)
	assert.Nil(t, err)
	assert.Equal(t, "helloworldhelloworld", result.(FuString).value)

	// neither s1 nor s2 is affected by all this adding
	assert.Equal(t, "hello", s1.value)
	assert.Equal(t, "world", s2.value)
}
예제 #12
0
func Test_FuString_ActionExpand(t *testing.T) {
	ns := makeNamespace("foo", "hello", "meep", "blorf")
	input := MakeFuString("meep meep!")
	output, err := input.ActionExpand(ns, nil)
	assert.Nil(t, err)
	assert.Equal(t, input, output)

	input = MakeFuString("meep $foo blah")
	output, err = input.ActionExpand(ns, nil)
	assert.Nil(t, err)
	assert.Equal(t, "meep hello blah", output.ValueString())

	input = MakeFuString("hello ${foo} $meep")
	output, err = input.ActionExpand(ns, nil)
	assert.Nil(t, err)
	assert.Equal(t, "hello hello blorf", output.ValueString())

	ns.Assign("foo", nil)
	output, err = input.ActionExpand(ns, nil)
	assert.Nil(t, err)
	assert.Equal(t, "hello  blorf", output.ValueString())

	ns.Assign("foo", MakeFuString("ping$pong"))
	output, err = input.ActionExpand(ns, nil)
	assert.Equal(t, "undefined variable 'pong' in string", err.Error())
	assert.Nil(t, output)
}
예제 #13
0
func TestTimeSpParams(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_sp_time_sp_params", `@p1 datetime as
    insert into tm (tm) values(@p1)
    select @p1, 123
    return 0`)
	assert.Nil(t, err)

	f := func(tmIn time.Time) {
		var tmOut time.Time
		var i int
		rst, err := conn.ExecSp("test_sp_time_sp_params", tmIn)
		assert.Nil(t, err)
		assert.NotNil(t, rst)
		rst.Next()
		rst.Scan(&tmOut, &i)
		assert.Equal(t, tmIn.UTC(), tmOut.UTC())
		if !tmIn.Equal(tmOut) {
			t.Errorf("%s != %s", tmIn, tmOut)
		}
	}

	f(time.Unix(1404856799, 0))
	f(time.Unix(1404856800, 0))
	f(time.Unix(1404856801, 0))

	f(time.Unix(1404856799, 0).UTC())
	f(time.Unix(1404856800, 0).UTC())
	f(time.Unix(1404856801, 0).UTC())
}
예제 #14
0
func TestGoSqlDbQuery(t *testing.T) {
	db, err := open()
	assert.Nil(t, err)
	rows, err := db.Query("SELECT au_fname, au_lname name FROM authors WHERE au_lname = ? order by au_id", "Ringer")
	assert.Nil(t, err)
	testRingers(t, rows)
}
예제 #15
0
func TestExecSpInputParams2(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_input_params2", "@p1 nvarchar(255), @p2 varchar(255), @p3 nvarchar(255), @p4 nchar(10), @p5 varbinary(10) as select @p1, @p2, @p3, @p4, @p5;  return")
	assert.Nil(t, err)
	want := "£¢§‹›†€"
	wantp2 := "abc"
	wantp3 := "šđčćžabc"
	wantp4 := "šđčćžabcde"
	wantp3 = "FK Ventspils v Nõmme Kalju FC"
	wantp5 := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	rst, err := conn.ExecSp("test_input_params2", want, wantp2, wantp3, wantp4, wantp5)
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	if rst == nil {
		return
	}
	assert.True(t, rst.HasResults())
	var got, gotp2, gotp3, gotp4 string
	var gotp5 []byte
	result := rst.results[0]
	result.Next()
	result.Scan(&got, &gotp2, &gotp3, &gotp4, &gotp5)
	assert.Equal(t, want, got)
	assert.Equal(t, wantp2, gotp2)
	assert.Equal(t, wantp3, gotp3)
	assert.Equal(t, wantp4, gotp4)
	assert.Equal(t, wantp5, gotp5)
	//PrintResults(rst.Results)
}
예제 #16
0
func TestNewDateTypesParam(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_sp_with_datetimeoffset_param", `
    (@p1 datetimeoffset, @p2 date, @p3 time, @p4 datetime2) as
    DECLARE @datetime datetime = @p1;
    SELECT @datetime, @p1, @p2, @p3, @p4
    return `)
	assert.Nil(t, err)
	p1 := "2025-12-10 12:32:10.1237000 +01:00"
	p2 := "2025-12-10"
	p3 := "12:30"
	p4 := "2025-12-10 12:32:10"
	rst, err := conn.ExecSp("test_sp_with_datetimeoffset_param", p1, p2, p3, p4)
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	rst.Next()
	var op1, op2, op3, op4 string
	var dt time.Time
	err = rst.Scan(&dt, &op1, &op2, &op3, &op4)
	assert.Nil(t, err)
	assert.Equal(t, "2025-12-10T12:32:10+01:00", dt.Format(time.RFC3339))
	assert.Equal(t, "2025-12-10 12:32:10.1237000 +01:00", op1)
	assert.Equal(t, "2025-12-10", op2)
	assert.Equal(t, "12:30:00.0000000", op3)
	assert.Equal(t, "2025-12-10 12:32:10.0000000", op4)
}
예제 #17
0
func TestMarshal(t *testing.T) {
	v := &TestStruct{
		IntField: 5,
		String:   "str",
		SubStruct: SubStruct{
			A: true,
			Ptr: &SubStruct{
				A:    false,
				Ints: []int{1, 2},
			},
		},
		Slice: []*SubStruct{
			&SubStruct{A: false}, &SubStruct{A: true},
		},
	}

	scheme := safeDescribe(t, &v)

	result, err := scheme.Marshal(v)
	assert.Nil(t, err)
	assert.Equal(t, result["int_field"], 5)
	assert.Equal(t, result["string"], "str")

	sub1 := result["sub_struct"].(map[string]interface{})
	assert.Equal(t, sub1["a"], true)
	assert.Equal(t, sub1["ints"], []int(nil))

	sub2 := sub1["ptr"].(map[string]interface{})
	assert.Equal(t, sub2["a"], false)
	assert.Equal(t, sub2["ints"], []interface{}{1, 2})

	slice := result["slice"].([]interface{})
	assert.Equal(t, len(slice), 2)
	assert.Equal(t, slice[0].(map[string]interface{})["a"], false)
	assert.Equal(t, slice[1].(map[string]interface{})["a"], true)

	v2 := &MapStruct{
		Map: map[string]*SubStruct{
			"a": &SubStruct{},
			"b": &SubStruct{A: true},
		},
	}

	scheme2 := safeDescribe(t, &v2)

	result, err = scheme2.Marshal(v2)
	assert.Nil(t, err)

	m := result["map"].(map[string]interface{})
	assert.Equal(t, len(m), 2)

	v3 := &OmitEmptyStruct{A: 5}
	scheme3 := safeDescribe(t, &v3)

	result, err = scheme3.Marshal(v3)
	assert.Nil(t, err)

	assert.Equal(t, len(result), 1)
	assert.Equal(t, result["a"], 5)
}
예제 #18
0
func Test_FileNode_Expand(t *testing.T) {
	ns := types.NewValueMap()
	node := NewFileNode("foobar")
	xnode, err := node.ActionExpand(ns, nil)
	assert.Nil(t, err)
	assert.Equal(t, node, xnode)

	err = node.NodeExpand(ns)
	assert.Nil(t, err)
	assert.Equal(t, "foobar", node.Name())

	// test that ActionExpand() follows variable references
	node = NewFileNode("$foo$bar")
	xnode, err = node.ActionExpand(ns, nil)
	assert.Equal(t, "undefined variable 'foo' in string", err.Error())

	// make it so "$foo$bar" expands to "$foo", and ensure that
	// expansion stops there
	// XXX argh: currently this expands to "'$'foo": hmmmmm
	// ns.Assign("foo", types.MakeFuString("$"))
	// ns.Assign("bar", types.MakeFuString("foo"))
	// xnode, err = node.ActionExpand(ns, nil)
	// assert.Nil(t, err)
	// assert.Equal(t, "$foo", xnode.String())
}
예제 #19
0
func Test_mkdir(t *testing.T) {
	cleanup := testutils.Chtemp()
	defer cleanup()

	// mkdir() happily accepts an empty argument list, to allow for
	// cases where a user-defined list becomes the arg list, and it
	// just happens to be empty
	pargs := []types.FuObject{}
	args := RuntimeArgs{
		BasicArgs: types.MakeBasicArgs(nil, pargs, nil),
	}
	result, errs := fn_mkdir(args)
	assert.Nil(t, result)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, []string{}, dirContents("."))

	// easiest case: create a single dir
	pargs = types.MakeStringList("foo").List()
	args.SetArgs(pargs)
	result, errs = fn_mkdir(args)
	assert.Nil(t, result)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, []string{"foo"}, dirContents("."))
	assert.True(t, isDir("foo"))

	// create multiple dirs, including "foo" which already exists
	pargs = types.MakeStringList("meep/meep/meep", "foo", "meep/beep").List()
	args.SetArgs(pargs)
	result, errs = fn_mkdir(args)
	assert.Nil(t, result)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, []string{"foo", "meep"}, dirContents("."))
	assert.True(t, isDir("foo"))
	assert.True(t, isDir("meep/meep"))
	assert.True(t, isDir("meep/meep/meep"))
	assert.True(t, isDir("meep/beep"))

	// now with an error in the middle of the list (*but* we still
	// create the other requested dirs!)
	testutils.TouchFiles("meep/zap")
	pargs = types.MakeStringList("meep/bap", "meep/zap/zip", "foo/bar").List()
	args.SetArgs(pargs)
	result, errs = fn_mkdir(args)
	assert.Nil(t, result)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "mkdir meep/zap: not a directory", errs[0].Error())
	assert.True(t, isDir("meep/bap"))
	assert.True(t, isDir("foo/bar"))

	// finally, with multiple errors
	pargs = append(pargs, types.MakeFuString("meep/zap/blop"))
	args.SetArgs(pargs)
	result, errs = fn_mkdir(args)
	assert.Nil(t, result)
	assert.Equal(t, 2, len(errs))
	assert.Equal(t, "mkdir meep/zap: not a directory", errs[0].Error())
	assert.Equal(t, "mkdir meep/zap: not a directory", errs[1].Error())
}
예제 #20
0
func TestExecSpInputParams(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_input_params", "@p1 int = 0, @p2 int, @p3 as varchar(10), @p4 datetime, @p5 varbinary(10) = null as select @p1 = @p1 + @p2; return @p1")
	assert.Nil(t, err)
	rst, err := conn.ExecSp("test_input_params", 123, 234, "pero", time.Now(), []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
	assert.Nil(t, err)
	assert.False(t, rst.HasResults())
	assert.Equal(t, 357, rst.Status())
}
예제 #21
0
func TestExecSpResults(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_results", " as select 1 one; select 2 two; return 456")
	assert.Nil(t, err)
	rst, err := conn.ExecSp("test_results")
	assert.Nil(t, err)
	assert.Equal(t, 2, len(rst.results))
	assert.Equal(t, 456, rst.Status())
}
예제 #22
0
func TestExecSpReturnValue(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_return_value", " as return 123")
	assert.Nil(t, err)
	rst, err := conn.ExecSp("test_return_value")
	assert.Nil(t, err)
	assert.False(t, rst.HasResults())
	assert.Equal(t, 123, rst.Status())
}
예제 #23
0
func Test_prepareCall(t *testing.T) {
	// this is never going to be called, so it's OK that it's nil
	var fn_dummy func(argsource types.ArgSource) (types.FuObject, []error)
	var dummy1, dummy2 types.FuCallable
	dummy1 = types.NewFixedFunction("dummy1", 0, fn_dummy)
	dummy2 = types.NewFixedFunction("dummy1", 1, fn_dummy)

	rt := minimalRuntime()
	ns := rt.Namespace()
	ns.Assign("dummy1", dummy1)
	ns.Assign("dummy2", dummy2)
	ns.Assign("x", types.MakeFuString("whee!"))

	noargs := []dsl.ASTExpression{}
	onearg := []dsl.ASTExpression{dsl.NewASTString("\"meep\"")}

	var astcall *dsl.ASTFunctionCall
	var callable types.FuCallable
	var args RuntimeArgs
	var errs []error

	// correct (no args) call to dummy1()
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("dummy1"), noargs)
	callable, args, errs = rt.prepareCall(astcall)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, dummy1, callable)
	assert.Equal(t, []types.FuObject{}, args.Args())

	// and to dummy2()
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("dummy2"), onearg)
	callable, args, errs = rt.prepareCall(astcall)
	assert.Equal(t, 0, len(errs))
	assert.Equal(t, dummy2, callable)
	assert.Equal(t, []types.FuObject{types.MakeFuString("meep")}, args.Args())

	// attempt to call dummy2() incorrectly (1 arg, but it's an undefined name)
	astcall = dsl.NewASTFunctionCall(
		dsl.NewASTName("dummy2"),
		[]dsl.ASTExpression{dsl.NewASTName("bogus")})
	callable, _, errs = rt.prepareCall(astcall)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "name not defined: 'bogus'", errs[0].Error())

	// attempt to call non-existent function
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("bogus"), noargs)
	callable, _, errs = rt.prepareCall(astcall)
	assert.Nil(t, callable)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "name not defined: 'bogus'", errs[0].Error())

	// attempt to call something that is not a function
	astcall = dsl.NewASTFunctionCall(dsl.NewASTName("x"), noargs)
	callable, _, errs = rt.prepareCall(astcall)
	assert.Nil(t, callable)
	assert.Equal(t, 1, len(errs))
	assert.Equal(t, "not a function or method: 'x'", errs[0].Error())
}
예제 #24
0
func TestGoSqlDbQueryRow(t *testing.T) {
	db, err := open()
	assert.Nil(t, err)
	row := db.QueryRow("SELECT au_fname, au_lname name FROM authors WHERE au_id = ?", "172-32-1176")
	var firstName, lastName string
	err = row.Scan(&firstName, &lastName)
	assert.Nil(t, err)
	assert.Equal(t, firstName, "Johnson")
	assert.Equal(t, lastName, "White")
}
예제 #25
0
func testRingers(t *testing.T, rows *sql.Rows) {
	var firstName, lastName string
	rows.Next()
	err := rows.Scan(&firstName, &lastName)
	assert.Nil(t, err)
	assert.Equal(t, firstName, "Anne")
	rows.Next()
	err = rows.Scan(&firstName, &lastName)
	assert.Nil(t, err)
	assert.Equal(t, firstName, "Albert")
}
예제 #26
0
func TestGoSqlPrepareQuery(t *testing.T) {
	//t.Skip()
	db, err := open()
	assert.Nil(t, err)
	assert.NotNil(t, db)
	stmt, err := db.Prepare("SELECT au_fname, au_lname name FROM authors WHERE au_lname = ? order by au_id")
	assert.Nil(t, err)
	rows, err := stmt.Query("Ringer")
	assert.Nil(t, err)
	testRingers(t, rows)
}
예제 #27
0
func Test_FuFunction_CheckArgs_unlimited(t *testing.T) {
	fn := NewVariadicFunction("println", 0, -1, nil)
	val := MakeFuString("a")
	args := MakeBasicArgs(nil, []FuObject{val}, nil)

	err := fn.CheckArgs(args)
	assert.Nil(t, err)

	args.args = append(args.args, val, val, val, val)
	err = fn.CheckArgs(args)
	assert.Nil(t, err)
}
예제 #28
0
func Test_println(t *testing.T) {
	cleanup1 := testutils.Chtemp()
	defer cleanup1()

	rfile, err := os.Create("stdout")
	if err != nil {
		panic(err)
	}

	// save a copy of stdout in another fd
	stdout_fd := int(os.Stdout.Fd())
	save_stdout, err := syscall.Dup(stdout_fd)
	if err != nil {
		panic(err)
	}

	// redirect stdout to rfile
	err = syscall.Dup2(int(rfile.Fd()), stdout_fd)
	if err != nil {
		panic(err)
	}

	cleanup2 := func() {
		rfile.Close()
		err = syscall.Dup2(save_stdout, stdout_fd)
		if err != nil {
			panic(err)
		}
		syscall.Close(save_stdout)
	}
	defer cleanup2()

	args := RuntimeArgs{
		BasicArgs: types.MakeBasicArgs(nil, []types.FuObject{}, nil),
	}

	result, errs := fn_println(args)
	assert.Nil(t, result)
	assert.Equal(t, 0, len(errs))
	data, err := ioutil.ReadFile("stdout")
	assert.Nil(t, err)
	assert.Equal(t, "\n", string(data))
	rfile.Truncate(0)
	rfile.Seek(0, 0)

	args.SetArgs(types.MakeStringList("hello", "world").List())
	fn_println(args)
	data, err = ioutil.ReadFile("stdout")
	assert.Nil(t, err)
	assert.Equal(t, "hello world\n", string(data))
	rfile.Truncate(0)
	rfile.Seek(0, 0)
}
예제 #29
0
func Test_translateGlob(t *testing.T) {
	tests := []struct {
		glob string
		re   string
	}{
		{"", "$"},
		{"foo", "foo$"},
		{"foo/bar", "foo/bar$"},

		{"foo?bar", "foo[^/]bar$"},
		{"*.c", "[^/]*\\.c$"},
		{"foo[abc]", "foo[abc]$"},
		{"foo[a-m]*.bop", "foo[a-m][^/]*\\.bop$"},
		{"foo/**/bar", "foo/.*/bar$"},
		{"**/foo/bar", ".*/foo/bar$"},
		{"foo/bar/**", "foo/bar/.*$"},
		{"foo/**/bar/**/baz/**", "foo/.*/bar/.*/baz/.*$"},
	}

	for _, pair := range tests {
		actual, err := translateGlob(pair.glob)
		assert.Nil(t, err)
		assert.Equal(t, pair.re, actual)
	}

	// make sure one of those regexps actually works as intended
	pat, err := translateGlob("foo[a-m]/p*g/*.[ch]")
	assert.Nil(t, err)
	re, err := regexp.Compile("^" + pat)
	assert.Nil(t, err)
	match := []string{
		"foom/pong/bop.c",
		"foog/pig/abc.c",
		"foog/pig/a.c.-af#@0(.h",
		"foob/pg/a_b&.c",
	}
	for _, fn := range match {
		assert.Equal(t, fn, re.FindString(fn))
	}

	nomatch := []string{
		"foo/pong/bop.c",
		"foom/pongx/bop.c",
		"foom/pong/bop.cpp",
		"foom/pong/bop.c/x",
		"fooz/pong/bop.c",
		"foom/pg/bop.d",
	}
	for _, fn := range nomatch {
		assert.Equal(t, "", re.FindString(fn))
	}
}
예제 #30
0
func TestStoredProcedureNotExists(t *testing.T) {
	conn := ConnectToTestDb(t)
	err := createProcedure(conn, "test_sp_not_exists", `as return`)
	assert.Nil(t, err)
	rst, err := conn.ExecSp("test_sp_not_exists")
	assert.Nil(t, err)
	assert.NotNil(t, rst)
	_, err = conn.Exec("drop procedure test_sp_not_exists")
	assert.Nil(t, err)
	rst, err = conn.ExecSp("test_sp_not_exists")
	assert.NotNil(t, err)
	assert.Nil(t, rst)
}