func TestEndpointsOpen(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_opened_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_closed_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.False(setting.Services[0].Opened(), "") assert.Equal(len(setting.Services[0].Endpoints), 0) }
func TestMiddleware(t *testing.T) { assert := assert.Assert(t) yml := loadFile("conf_middlewares.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Middlewares), 2) service0, _ := setting.GetServiceByID("service0") assert.Equal(service0.Middleware, "base") service1, _ := setting.GetServiceByID("service1") assert.Equal(service1.Middleware, "extend") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Equal(service0.Endpoints[0].Middleware[0], "a.js") assert.Equal(service0.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Nil(service0.Endpoints[1].Middleware) assert.Equal(len(service1.Endpoints[0].Middleware), 2) assert.Equal(service1.Endpoints[0].Middleware[0], "a.js") assert.Equal(service1.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service1.Endpoints[1].Middleware), 2) assert.Equal(service1.Endpoints[1].Middleware[0], "c.js") assert.Equal(service1.Endpoints[1].Middleware[1], "d.js") }
func TestParseTimeUnit(t *testing.T) { assert := assert.Assert(t) cases := []struct { in string want time.Duration }{ {"10", time.Nanosecond * 10}, {"100000", time.Nanosecond * 100 * 1000}, {"1s", time.Second * 1}, {"2s", time.Second * 2}, {"3m", time.Minute * 3}, {"4h", time.Hour * 4}, {"5d", time.Hour * 24 * 5}, {"6M", time.Hour * 24 * 30 * 6}, {"7y", time.Hour * 24 * 365 * 7}, } for _, c := range cases { assert.Equal( func() time.Duration { p, err := parseTimeUnit(c.in) assert.Nil(err) return p }(), c.want, ) } }
func Test_Play_returns_true_when_wins_in_row(t *testing.T) { assert := assert.Assert(t) g := NewGame() players := []int{1, 2} for i := 1; i < 4; i++ { for _, p := range players { won, err := g.Play(i) assert.False(won, "player %v should not have won", p) assert.Nil(err, "error after player %d played", p) } } won, err := g.Play(4) assert.Nil(err, "error when player 1 played final piece") assert.True(won, "player 1 should have won") }
func TestHostnamesInheritFromDefault(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_inherit_from_default.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } ymlOverride := loadFile("config_hostnames_override_default.yml") setting, err = kasi.ParseConfig(ymlOverride) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) for _, h := range []string{"my2.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
func TestEndpoints(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_simple_endpoint1.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 2) }
func TestHostnamesWithString(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_string.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) assert.Equal(setting.Services[0].Hostnames[0], "my0.github.com") }
func Test_Play_returns_true_when_wins_left_diagonal(t *testing.T) { assert := assert.Assert(t) g := NewGame() g.SetRows( "0000000", "0001200", "0012200", "0121200") win, err := g.Play(5) assert.Nil(err, "error after player 1 played final piece") assert.True(win, "player 1 should have won") }
func TestHostnamesWithStrings(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_strings.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
// Make sure I wrote heap game correctly before I try to win it. func TestHeapGame(t *testing.T) { assert := assert.Assert(t) assert.Nil(nil, "good") f := HeapUtilityFunction game := NewHeapGame(1, 2, 3, 4, 5, 6, 7, 8) assert.Equal(f(game), 1) legalMoves := game.LegalMoves() move := legalMoves[0] game1 := game.Copy() over, _ := game1.Play(move) assert.False(over, "game not yet over") assert.NotEqual(game, game1) assert.Equal(f(game1), 2) game2 := game1.Copy() over, _ = game2.Play(1) assert.Equal(f(game2), 5) assert.True(over, "game should be over") }
func TestSplitHostPort(t *testing.T) { assert := assert.Assert(t) cases := []struct { in string want *net.TCPAddr }{ {":80", &net.TCPAddr{Port: 80}}, {"0.0.0.0:80", &net.TCPAddr{Port: 80}}, {"*:80", &net.TCPAddr{Port: 80}}, {"192.168.0.1:80", &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 80}}, } for _, c := range cases { assert.Equal( func() *net.TCPAddr { p, err := splitHostPort(c.in) assert.Nil(err) return p }(), c.want, ) } }