Exemplo n.º 1
0
func TestConnect(t *testing.T) {
	c.Connect("127.0.0.1:45678")
	assert.Equal(t, c.Host, "127.0.0.1")
	assert.Equal(t, c.Server, "127.0.0.1:45678")
	waitConnAndClose(t)
	initTestClient()
}
Exemplo n.º 2
0
func TestToFloat64(t *testing.T) {
	var eight interface{} = 8
	assert.Equal(t, ToFloat64(8), 8.00)
	assert.Equal(t, ToFloat64(8.31), 8.31)
	assert.Equal(t, ToFloat64("8.31"), 8.31)
	assert.Equal(t, ToFloat64(eight), 8.0)
}
Exemplo n.º 3
0
func TestMarshal(t *testing.T) {
	SetDefault("port", 1313)
	Set("name", "Steve")

	type config struct {
		Port int
		Name string
	}

	var C config

	err := Marshal(&C)
	if err != nil {
		t.Fatalf("unable to decode into struct, %v", err)
	}

	assert.Equal(t, &C, &config{Name: "Steve", Port: 1313})

	Set("port", 1234)
	err = Marshal(&C)
	if err != nil {
		t.Fatalf("unable to decode into struct, %v", err)
	}
	assert.Equal(t, &C, &config{Name: "Steve", Port: 1234})
}
Exemplo n.º 4
0
func TestAliasInConfigFile(t *testing.T) {
	// the config file specifies "beard".  If we make this an alias for
	// "hasbeard", we still want the old config file to work with beard.
	RegisterAlias("beard", "hasbeard")
	assert.Equal(t, true, Get("hasbeard"))
	Set("hasbeard", false)
	assert.Equal(t, false, Get("beard"))
}
Exemplo n.º 5
0
func TestNick(t *testing.T) {
	c.Nick("test2")
	assert.Equal(t, "test2", c.GetNick())
	assert.Equal(t, "NICK test2\r\n", <-conn.hook)

	c.writeNick("nick")
	assert.Equal(t, "NICK nick\r\n", <-conn.hook)
}
Exemplo n.º 6
0
func TestAddRemoveUser(t *testing.T) {
	channelStore := NewChannelStore()
	channelStore.AddUser("user", "srv", "#chan")
	channelStore.AddUser("user2", "srv", "#chan")
	assert.Equal(t, channelStore.GetUsers("srv", "#chan"), []string{"user", "user2"})
	channelStore.RemoveUser("user", "srv", "#chan")
	assert.Equal(t, []string{"user2"}, channelStore.GetUsers("srv", "#chan"))
}
Exemplo n.º 7
0
func TestRenameUser(t *testing.T) {
	channelStore := NewChannelStore()
	channelStore.AddUser("user", "srv", "#chan1")
	channelStore.AddUser("user", "srv", "#chan2")
	channelStore.RenameUser("user", "new", "srv")
	assert.Equal(t, []string{"new"}, channelStore.GetUsers("srv", "#chan1"))
	assert.Equal(t, []string{"new"}, channelStore.GetUsers("srv", "#chan2"))
}
Exemplo n.º 8
0
func TestIndirectPointers(t *testing.T) {
	x := 13
	y := &x
	z := &y

	assert.Equal(t, ToInt(y), 13)
	assert.Equal(t, ToInt(z), 13)
}
Exemplo n.º 9
0
func TestLevels(t *testing.T) {
	SetStdoutThreshold(LevelError)
	assert.Equal(t, StdoutThreshold(), LevelError)
	SetLogThreshold(LevelCritical)
	assert.Equal(t, LogThreshold(), LevelCritical)
	assert.NotEqual(t, StdoutThreshold(), LevelCritical)
	SetStdoutThreshold(LevelWarn)
	assert.Equal(t, StdoutThreshold(), LevelWarn)
}
Exemplo n.º 10
0
func TestQuit(t *testing.T) {
	c.connected = true
	c.Quit()
	assert.Equal(t, "QUIT\r\n", <-conn.hook)
	_, ok := <-c.quit
	assert.Equal(t, false, ok)

	initTestClient()
}
Exemplo n.º 11
0
func TestWrite(t *testing.T) {
	c.write("test")
	assert.Equal(t, "test\r\n", <-conn.hook)
	c.Write("test")
	assert.Equal(t, "test\r\n", <-conn.hook)
	c.writef("test %d", 2)
	assert.Equal(t, "test 2\r\n", <-conn.hook)
	c.Writef("test %d", 2)
	assert.Equal(t, "test 2\r\n", <-conn.hook)
}
Exemplo n.º 12
0
func TestConnectDefaultPorts(t *testing.T) {
	c.Connect("127.0.0.1")
	assert.Equal(t, "127.0.0.1:6667", c.Server)
	initTestClient()

	c.TLS = true
	c.Connect("127.0.0.1")
	assert.Equal(t, "127.0.0.1:6697", c.Server)
	initTestClient()
}
Exemplo n.º 13
0
func TestMode(t *testing.T) {
	channelStore := NewChannelStore()
	channelStore.AddUser("+user", "srv", "#chan")
	channelStore.SetMode("srv", "#chan", "user", "o", "v")
	assert.Equal(t, []string{"@user"}, channelStore.GetUsers("srv", "#chan"))
	channelStore.SetMode("srv", "#chan", "user", "v", "")
	assert.Equal(t, []string{"+user"}, channelStore.GetUsers("srv", "#chan"))
	channelStore.SetMode("srv", "#chan", "user", "", "v")
	assert.Equal(t, []string{"user"}, channelStore.GetUsers("srv", "#chan"))
}
Exemplo n.º 14
0
func TestMarshalling(t *testing.T) {
	SetConfigType("yaml")
	r := bytes.NewReader(yamlExample)

	marshalReader(r, v.config)
	assert.True(t, InConfig("name"))
	assert.False(t, InConfig("state"))
	assert.Equal(t, "steve", Get("name"))
	assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies"))
	assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, Get("clothing"))
	assert.Equal(t, 35, Get("age"))
}
Exemplo n.º 15
0
func TestReadBufConfig(t *testing.T) {
	v := New()
	v.SetConfigType("yaml")
	v.ReadConfig(bytes.NewBuffer(yamlExample))
	t.Log(v.AllKeys())

	assert.True(t, v.InConfig("name"))
	assert.False(t, v.InConfig("state"))
	assert.Equal(t, "steve", v.Get("name"))
	assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
	assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, v.Get("clothing"))
	assert.Equal(t, 35, v.Get("age"))
}
Exemplo n.º 16
0
func TestRegister(t *testing.T) {
	c.nick = "nick"
	c.Username = "******"
	c.Realname = "rn"
	c.register()
	assert.Equal(t, "NICK nick\r\n", <-conn.hook)
	assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)

	c.Password = "******"
	c.register()
	assert.Equal(t, "PASS pass\r\n", <-conn.hook)
	assert.Equal(t, "NICK nick\r\n", <-conn.hook)
	assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
}
Exemplo n.º 17
0
func TestRemotePrecedence(t *testing.T) {
	initJSON()

	remote := bytes.NewReader(remoteExample)
	assert.Equal(t, "0001", Get("id"))
	marshalReader(remote, v.kvstore)
	assert.Equal(t, "0001", Get("id"))
	assert.NotEqual(t, "cronut", Get("type"))
	assert.Equal(t, "remote", Get("newkey"))
	Set("newkey", "newvalue")
	assert.NotEqual(t, "remote", Get("newkey"))
	assert.Equal(t, "newvalue", Get("newkey"))
	Set("newkey", "remote")
}
Exemplo n.º 18
0
func TestRecv(t *testing.T) {
	buf := &bytes.Buffer{}
	buf.WriteString("CMD\r\n")
	buf.WriteString("PING :test\r\n")
	buf.WriteString("001\r\n")
	c.reader = bufio.NewReader(buf)

	c.ready.Add(1)
	close(c.quit)
	go c.recv()

	assert.Equal(t, "PONG :test\r\n", <-conn.hook)
	assert.Equal(t, &Message{Command: "CMD"}, <-c.Messages)
	initTestClient()
}
Exemplo n.º 19
0
func TestAllKeys(t *testing.T) {
	initConfigs()

	ks := sort.StringSlice{"title", "newkey", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name"}
	dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
	all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[interface{}]interface{}{"trousers": "denim", "jacket": "leather"}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters.batter.type": "Regular", "p_type": "donut"}

	var allkeys sort.StringSlice
	allkeys = AllKeys()
	allkeys.Sort()
	ks.Sort()

	assert.Equal(t, ks, allkeys)
	assert.Equal(t, all, AllSettings())
}
Exemplo n.º 20
0
func TestAutoEnv(t *testing.T) {
	Reset()

	AutomaticEnv()
	os.Setenv("FOO_BAR", "13")
	assert.Equal(t, "13", Get("foo_bar"))
}
Exemplo n.º 21
0
func TestBindPFlags(t *testing.T) {
	flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)

	var testValues = map[string]*string{
		"host":     nil,
		"port":     nil,
		"endpoint": nil,
	}

	var mutatedTestValues = map[string]string{
		"host":     "localhost",
		"port":     "6060",
		"endpoint": "/public",
	}

	for name, _ := range testValues {
		testValues[name] = flagSet.String(name, "", "test")
	}

	err := BindPFlags(flagSet)
	if err != nil {
		t.Fatalf("error binding flag set, %v", err)
	}

	flagSet.VisitAll(func(flag *pflag.Flag) {
		flag.Value.Set(mutatedTestValues[flag.Name])
		flag.Changed = true
	})

	for name, expected := range mutatedTestValues {
		assert.Equal(t, Get(name), expected)
	}

}
Exemplo n.º 22
0
func TestAutoEnvWithPrefix(t *testing.T) {
	Reset()

	AutomaticEnv()
	SetEnvPrefix("Baz")
	os.Setenv("BAZ_BAR", "13")
	assert.Equal(t, "13", Get("bar"))
}
Exemplo n.º 23
0
func TestEnv(t *testing.T) {
	initJSON()

	BindEnv("id")
	BindEnv("f", "FOOD")

	os.Setenv("ID", "13")
	os.Setenv("FOOD", "apple")
	os.Setenv("NAME", "crunk")

	assert.Equal(t, "13", Get("id"))
	assert.Equal(t, "apple", Get("f"))
	assert.Equal(t, "Cake", Get("name"))

	AutomaticEnv()

	assert.Equal(t, "crunk", Get("name"))

}
Exemplo n.º 24
0
func TestEnvPrefix(t *testing.T) {
	initJSON()

	SetEnvPrefix("foo") // will be uppercased automatically
	BindEnv("id")
	BindEnv("f", "FOOD") // not using prefix

	os.Setenv("FOO_ID", "13")
	os.Setenv("FOOD", "apple")
	os.Setenv("FOO_NAME", "crunk")

	assert.Equal(t, "13", Get("id"))
	assert.Equal(t, "apple", Get("f"))
	assert.Equal(t, "Cake", Get("name"))

	AutomaticEnv()

	assert.Equal(t, "crunk", Get("name"))
}
Exemplo n.º 25
0
func TestSetEnvReplacer(t *testing.T) {
	Reset()

	AutomaticEnv()
	os.Setenv("REFRESH_INTERVAL", "30s")

	replacer := strings.NewReplacer("-", "_")
	SetEnvKeyReplacer(replacer)

	assert.Equal(t, "30s", Get("refresh-interval"))
}
Exemplo n.º 26
0
func TestBindPFlag(t *testing.T) {
	var testString = "testing"
	var testValue = newStringValue(testString, &testString)

	flag := &pflag.Flag{
		Name:    "testflag",
		Value:   testValue,
		Changed: false,
	}

	BindPFlag("testvalue", flag)

	assert.Equal(t, testString, Get("testvalue"))

	flag.Value.Set("testing_mutate")
	flag.Changed = true //hack for pflag usage

	assert.Equal(t, "testing_mutate", Get("testvalue"))

}
Exemplo n.º 27
0
func TestBoundCaseSensitivity(t *testing.T) {

	assert.Equal(t, "brown", Get("eyes"))

	BindEnv("eYEs", "TURTLE_EYES")
	os.Setenv("TURTLE_EYES", "blue")

	assert.Equal(t, "blue", Get("eyes"))

	var testString = "green"
	var testValue = newStringValue(testString, &testString)

	flag := &pflag.Flag{
		Name:    "eyeballs",
		Value:   testValue,
		Changed: true,
	}

	BindPFlag("eYEs", flag)
	assert.Equal(t, "green", Get("eyes"))

}
Exemplo n.º 28
0
func TestSlices(t *testing.T) {
	assert.Equal(t, []string{"a", "b"}, ToStringSlice([]string{"a", "b"}))
	assert.Equal(t, []string{"1", "3"}, ToStringSlice([]interface{}{1, 3}))
	assert.Equal(t, []int{1, 3}, ToIntSlice([]int{1, 3}))
	assert.Equal(t, []int{1, 3}, ToIntSlice([]interface{}{1.2, 3.2}))
	assert.Equal(t, []int{2, 3}, ToIntSlice([]string{"2", "3"}))
	assert.Equal(t, []int{2, 3}, ToIntSlice([2]string{"2", "3"}))
}
Exemplo n.º 29
0
func TestToInt(t *testing.T) {
	var eight interface{} = 8
	assert.Equal(t, ToInt(8), 8)
	assert.Equal(t, ToInt(8.31), 8)
	assert.Equal(t, ToInt("8"), 8)
	assert.Equal(t, ToInt(true), 1)
	assert.Equal(t, ToInt(false), 0)
	assert.Equal(t, ToInt(eight), 8)
}
Exemplo n.º 30
0
func TestToString(t *testing.T) {
	var foo interface{} = "one more time"
	assert.Equal(t, ToString(8), "8")
	assert.Equal(t, ToString(8.12), "8.12")
	assert.Equal(t, ToString([]byte("one time")), "one time")
	assert.Equal(t, ToString(template.HTML("one time")), "one time")
	assert.Equal(t, ToString(foo), "one more time")
	assert.Equal(t, ToString(nil), "")
}