func TestArgMultipleRequired(t *testing.T) { a := newArgGroup() a.Arg("a", "").Required().String() a.Arg("b", "").Required().String() a.init() _, err := a.parse(Tokenize([]string{})) assert.Error(t, err) _, err = a.parse(Tokenize([]string{"A"})) assert.Error(t, err) _, err = a.parse(Tokenize([]string{"A", "B"})) assert.NoError(t, err) }
func TestPublish(t *testing.T) { // test nil event exp := `[7,"http://example.com/api/test",null]` comparePublish(t, exp, "http://example.com/api/test", nil) // test simple event exp = `[7,"http://example.com/api/testing:thing","this is an event"]` comparePublish(t, exp, "http://example.com/api/testing:thing", "this is an event") // test complex event obj := testObj{"the test", 17.3, []int{1, 2, 3}} exp = `[7,"http://www.example.com/doc#thing",{"name":"the test","value":17.3,"list":[1,2,3]}]` comparePublish(t, exp, "http://www.example.com/doc#thing", obj) // test with excludeMe exp = `[7,"http://example.com/api/testing:thing","this is an event",true]` comparePublish(t, exp, "http://example.com/api/testing:thing", "this is an event", true) // test with exclude list exp = `[7,"http://example.com/api/testing:thing","this is an event",["bob","john"]]` comparePublish(t, exp, "http://example.com/api/testing:thing", "this is an event", []string{"bob", "john"}) // test with eligible list exp = `[7,"http://example.com/api/testing:thing","this is an event",[],["sam","fred"]]` comparePublish(t, exp, "http://example.com/api/testing:thing", "this is an event", []string{}, []string{"sam", "fred"}) // test bad uri _, err := createPublish("asdfasdf", "bad uri") assert.Error(t, err) }
func TestCall(t *testing.T) { // zero call arguments exp := `[2,"123456","http://example.com/testRPC"]` compareCall(t, exp, "123456", "http://example.com/testRPC") // nil call argument exp = `[2,"654321","http://example.com/otherRPC",null]` compareCall(t, exp, "654321", "http://example.com/otherRPC", nil) // one call argument exp = `[2,"a1b2c3d4","http://example.com/dosomething/rpc","call arg"]` compareCall(t, exp, "a1b2c3d4", "http://example.com/dosomething/rpc", "call arg") // more call arguments exp = `[2,"abcdefg","http://example.com/rpc","arg1","arg2"]` compareCall(t, exp, "abcdefg", "http://example.com/rpc", "arg1", "arg2") // complex call argument exp = `[2,"1234","http://example.com/rpc",{"name":"george","value":14.98,"list":[1,3,5]},"astring"]` obj := testObj{Name: "george", Value: 14.98, List: []int{1, 3, 5}} compareCall(t, exp, "1234", "http://example.com/rpc", obj, "astring") // test bad uri _, err := createCall("abcd", "httpnopenopenope") assert.Error(t, err) }
func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) { c := New("test", "test") c.Arg("arg", "help").String() c.Command("cmd", "help") _, err := c.Parse([]string{}) assert.Error(t, err) }
func TestInvalidDefaultArgValueErrors(t *testing.T) { c := New("test", "test") cmd := c.Command("cmd", "cmd") cmd.Arg("arg", "arg").Default("one").Int() _, err := c.Parse([]string{"cmd"}) assert.Error(t, err) }
func TestInvalidArgsDefaultCanBeOverridden(t *testing.T) { a := newArgGroup() a.Arg("a", "").Default("invalid").Bool() assert.NoError(t, a.init()) tokens := Tokenize([]string{}) _, err := a.parse(tokens) assert.Error(t, err) }
func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) { c := New("test", "test") cmd := c.Command("cmd", "") cmd.Arg("a", "a").String() cmd.Arg("b", "b").Required().String() _, err := c.Parse([]string{}) assert.Error(t, err) }
func TestBinaryDecodeToValueErrors(t *testing.T) { b := []byte{1, 0, 0, 0} var v uint32 err := Unmarshal(b, v) assert.Error(t, err) err = Unmarshal(b, &v) assert.NoError(t, err) assert.Equal(t, uint32(1), v) }
func TestInvalidFlagDefaultCanBeOverridden(t *testing.T) { fg := newFlagGroup() f := fg.Flag("a", "").Default("invalid") f.Bool() assert.NoError(t, fg.init()) tokens := Tokenize([]string{}) _, err := fg.parse(tokens, false) assert.Error(t, err) }
func TestParseExistingFile(t *testing.T) { p := parserMixin{} v := p.ExistingFile() err := p.value.Set("/etc/hosts") assert.NoError(t, err) assert.Equal(t, "/etc/hosts", *v) err = p.value.Set("/etc/hostsDEFINITELYMISSING") assert.Error(t, err) }
func TestNegateNonBool(t *testing.T) { fg := newFlagGroup() f := fg.Flag("b", "") f.Int() fg.init() tokens := Tokenize([]string{"--no-b"}) _, err := fg.parse(tokens, false) assert.Error(t, err) }
func TestRequiredFlags(t *testing.T) { c := New("test", "test") c.Flag("a", "a").String() c.Flag("b", "b").Required().String() _, err := c.Parse([]string{"--a=foo"}) assert.Error(t, err) _, err = c.Parse([]string{"--b=foo"}) assert.NoError(t, err) }
func TestEvalBadExpr(t *testing.T) { assert.Equal(t, 1, Init()) r, err := Eval("x+") assert.Nil(t, r) assert.Error(t, err) assert.Panics(t, func() { EvalOrDie("+1*(") }) }
func TestTooManyArgs(t *testing.T) { a := New("test", "test") a.Arg("a", "").String() assert.NoError(t, a.init()) context := Tokenize([]string{"a", "b"}) _, err := a.parse(context) assert.NoError(t, err) assert.Equal(t, Tokens{&Token{TokenArg, "b"}}, context.Tokens) _, err = a.Parse([]string{"a", "b"}) assert.Error(t, err) }
func TestRequiredFlag(t *testing.T) { fg := newFlagGroup() fg.Flag("a", "").Required().Bool() assert.NoError(t, fg.init()) tokens := Tokenize([]string{"--a"}) tokens, err := fg.parse(tokens, false) assert.NoError(t, err) tokens = Tokenize([]string{}) tokens, err = fg.parse(tokens, false) assert.Error(t, err) }
func TestTooManyArgs(t *testing.T) { a := New("test", "test") a.Arg("a", "").String() assert.NoError(t, a.init()) toks := Tokenize([]string{"a", "b"}) toks, _, err := a.parse(toks) assert.NoError(t, err) assert.Equal(t, tokens{&token{TokenArg, "b"}}, toks) _, err = a.Parse([]string{"a", "b"}) assert.Error(t, err) }
func TestPrefix(t *testing.T) { exp := `[1,"prefix","http://www.example.com/api/start"]` msg, err := createPrefix("prefix", "http://www.example.com/api/start") if err != nil { t.Errorf("error creating prefix message: %s", err) } assert.Equal(t, exp, string(msg)) // test bad uri _, err = createPrefix("prefix", "httpppppppp") assert.Error(t, err) }
func TestRegexPath_Error(t *testing.T) { pattern := `[0-9]++` matcherFunc := RegexPath(pattern) var ctx context.Context ctx = context_test.MakeTestContextWithPath("adam[23]") _, err := matcherFunc(ctx) assert.Error(t, err) }
func TestUnsubscribe(t *testing.T) { exp := `[6,"http://example.com/something"]` msg, err := createUnsubscribe("http://example.com/something") if err != nil { t.Errorf("error creating unsubscribe message: %s", err) } assert.Equal(t, exp, string(msg)) // test bad uri _, err = createUnsubscribe("qwerty") assert.Error(t, err) }
func TestSubscribe(t *testing.T) { exp := `[5,"http://example.com/simple"]` msg, err := CreateSubscribe("http://example.com/simple") if err != nil { t.Errorf("error creating subscribe message: %s", err) } assert.Equal(t, exp, string(msg)) // test bad uri _, err = CreateSubscribe("qwerty") assert.Error(t, err) }
func TestAcquireRelease(t *testing.T) { oldnow := nowfunc defer func() { nowfunc = oldnow }() virtualtime := time.Now() nowfunc = func() time.Time { return virtualtime } var err error var c *connection cpI, err := NewConnectionPool(localEndpointPool, keyspace, PoolOptions{Size: 1, Grace: 1}) if err != nil { t.Fatal("Error connecting to Cassandra:", err) } cp := cpI.(*connectionPool) n := cp.nodes[0] assert.Equal(t, len(n.available.l), 1) assert.NoError(t, err) c, err = cp.acquire() assert.Equal(t, len(n.available.l), 0) assert.NoError(t, err) cp.release(c) assert.Equal(t, len(n.available.l), 1) assert.NoError(t, err) c, err = cp.acquire() assert.Equal(t, len(n.available.l), 0) assert.NoError(t, err) n.blacklist() assert.Equal(t, len(n.available.l), 0) assert.NoError(t, err) c, err = cp.acquire() assert.Equal(t, len(n.available.l), 0) assert.Error(t, err) assert.Nil(t, c) virtualtime = virtualtime.Add(time.Second * 2) c, err = cp.acquire() assert.Equal(t, len(n.available.l), 0) assert.NoError(t, err) }
func TestEvent(t *testing.T) { // test nil event exp := `[8,"http://example.com/api/test",null]` compareEvent(t, exp, "http://example.com/api/test", nil) // test simple event exp = `[8,"http://example.com/api/testing:thing","this is an event"]` compareEvent(t, exp, "http://example.com/api/testing:thing", "this is an event") // test complex event obj := testObj{"the test", 17.3, []int{1, 2, 3}} exp = `[8,"http://www.example.com/doc#thing",{"name":"the test","value":17.3,"list":[1,2,3]}]` compareEvent(t, exp, "http://www.example.com/doc#thing", obj) // test bad uri _, err := createEvent("asdfasdf", "bad uri") assert.Error(t, err) }
func TestArgRemainderErrorsWhenNotLast(t *testing.T) { a := newArgGroup() a.Arg("test", "").Strings() a.Arg("test2", "").String() assert.Error(t, a.init()) }
func TestUnmarshalFieldNoField(t *testing.T) { mE := MustNewMapping(&everythingComp{}) var s string err := mE.UnmarshalField("NoSuchFIeld", []byte{}, &s) assert.Error(t, err) }
func TestMarshalFieldNoField(t *testing.T) { mE := MustNewMapping(&everythingComp{}) bv, err := mE.MarshalField("NoSuchFIeld", "keyhere") assert.Error(t, err) assert.Nil(t, bv) }
func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) { a := New("test", "") a.Arg("opts", "").Required().Strings() _, err := a.Parse([]string{"hello", "-world"}) assert.Error(t, err) }
func TestInvalidDefaultFlagValueErrors(t *testing.T) { c := New("test", "test") c.Flag("foo", "foo").Default("a").Int() _, err := c.Parse([]string{}) assert.Error(t, err) }