Beispiel #1
0
func TestParseDate(t *testing.T) {
	var err error
	now := time.Now()
	deltaDay := 0
	if now.Hour() > 11 || (now.Hour() == 11 && now.Minute() > 40) {
		deltaDay = 1
	}
	expected := time.Date(now.Year(), now.Month(),
		now.Day()+deltaDay, 11, 40, 0,
		0, now.Location())
	var res time.Time

	res, _ = ParseDate("11h40")
	assert.AssertEquals(t, expected, res)

	res, _ = ParseDate("11:40")
	assert.AssertEquals(t, expected, res)

	res, _ = ParseDate("11:40:00")
	assert.AssertEquals(t, expected, res)

	utc, _ := time.LoadLocation("UTC")
	expected = time.Date(2006, 1, 2, 15, 4, 5, 0, utc)

	res, err = ParseDate("2006-01-02T15:04:05Z")
	assert.AssertNotNil(t, err)
	assert.AssertEquals(t, expected, res)
}
Beispiel #2
0
func TestParseDoublePing(t *testing.T) {
	msg := ":[email protected] PRIVMSG #geek :il te reste sora :p\r\nPING :ga.ga.org\r\n"
	res := ParseMessage(msg)
	t.Logf("Parsed message is %#v\n", res)
	assert.AssertEquals(t, len(res), 2)
	assert.AssertEquals(t, res[1], MsgPing{"ga.ga.org"})
}
Beispiel #3
0
func TestParseDouble(t *testing.T) {
	msg1 := MsgPing{"ping"}
	msg2 := MsgPong{"pong"}
	conc := fmt.Sprintf("%s%s", msg1.String(), msg2.String())
	res := ParseMessage(conc)
	assert.AssertEquals(t, res[0], msg1)
	assert.AssertEquals(t, res[1], msg2)
}
Beispiel #4
0
func TestInvalidLexer(t *testing.T) {
	res := gen_lex("4", "4")
	go res.run()

	first := <-res.tokens
	assert.AssertEquals(t, first.tok, INVALID)
	assert.AssertEquals(t, first.val, "4")
}
Beispiel #5
0
func TestSimpleLexer(t *testing.T) {
	res := gen_lex("toto", "NICK lol\r\n")
	go res.run()

	first := <-res.tokens
	assert.AssertEquals(t, first.tok, NICK)
	assert.AssertEquals(t, first.val, "NICK")
	second := <-res.tokens
	assert.AssertEquals(t, second.tok, WORD)
	assert.AssertEquals(t, second.val, "lol")
	third := <-res.tokens
	assert.AssertEquals(t, third.tok, EOF)
}
Beispiel #6
0
func TestParseFiles(t *testing.T) {
	state := defaultBsState()
	state.trainWithHtmlFile("good/first", false)
	t.Logf("State after training is %v\n", state)
	assert.AssertEquals(t, state.GoodWords["test"], 5)
	assert.AssertFalse(t, math.IsNaN(state.BsProba["test"]))
	assert.AssertFloatInferior(t, state.BsProba["test"], 0.5)

	state.trainWithHtmlFile("bad/first", true)
	t.Logf("State after training is %v\n", state)
	assert.AssertEquals(t, state.BadWords["badtest"], 24)
	assert.AssertFalse(t, math.IsNaN(state.BsProba["badtest"]))
	assert.AssertFalse(t, math.IsNaN(state.BsProba["badtest"]))
	assert.AssertFloatSuperior(t, state.BsProba["badtest"], 0.5)
}
Beispiel #7
0
func TestMostProbas(t *testing.T) {
	p1 := prob{"b", 0.5}
	p2 := prob{"a", 0.9}
	p3 := prob{"a", 0.9}
	ps := probs{p1, p2, p3}
	assert.AssertEquals(t, ps.MostSignificantProbas(2), probs{p2, p1})
}
Beispiel #8
0
func TestReload(t *testing.T) {
	state := defaultBsState()
	state.BsConf = BsConf{StateFile: "to", StorageGood: "good", StorageBad: "bad"}

	t.Logf("State before reload is %v\n", state)
	state.processReload()
	t.Logf("State after reload is %v\n", state)
	assert.AssertEquals(t, state.GoodWords["test"], 5)
}
Beispiel #9
0
func TestParsePdf(t *testing.T) {
	file, _ := os.Open("test.pdf")
	parsedUrl, _ := url.Parse("test.pdf")
	content, _ := ioutil.ReadAll(file)
	f := savePdfToText(parsedUrl, content, "/tmp/test_pdf")
	res, _ := os.Open(f)
	parsedText, _ := ioutil.ReadAll(res)
	assert.AssertEquals(t, strings.TrimSpace(string(parsedText)), "Hello World\n\n1")
}
Beispiel #10
0
func TestSaveState(t *testing.T) {
	good := map[string]int{"to": 10}
	bad := map[string]int{"ta": 9}
	probs := map[string]float64{}
	state := BsState{good, bad, probs, testConf}
	state.save()
	loaded := testConf.loadBsState()
	assert.AssertEquals(t, loaded.GoodWords["to"], 10)
}
Beispiel #11
0
func checkUserScores(t *testing.T, first []UserScores, snd []UserScores) {
	if len(first) != len(snd) {
		t.Logf("Invalid size : First %v, snd %v\n", first, snd)
		t.FailNow()
	}

	for k := range first {
		assert.AssertEquals(t, first[k], snd[k])
	}
}
Beispiel #12
0
func TestEnlargeCorpus(t *testing.T) {
	good := map[string]int{"to": 10}
	bad := map[string]int{"ta": 9}
	probs := map[string]float64{}
	state := BsState{good, bad, probs, testConf}

	words := []string{"test"}
	state.enlargeCorpus(words, true)
	assert.AssertEquals(t, state.BadWords["test"], 1)
}
Beispiel #13
0
func TestSimpleBet(t *testing.T) {
	os.Remove("test.db")
	db := InitBase("test.db")
	GetOrCreateBet(db)
	AddUserBet(db, "after", time.Now().Add(time.Hour), false)
	AddUserBet(db, "other", time.Now().Add(time.Hour), false)
	good := time.Now().Add(time.Minute)
	AddUserBet(db, "near", good, false)
	AddUserBet(db, "near2", good, false)

	closestBet := CloserBet(db, time.Now())
	expecetedWinners := []string{"near", "near2"}
	t.Logf("Checking first bet")
	assert.AssertEquals(t, expecetedWinners[0], closestBet[0])
	assert.AssertEquals(t, expecetedWinners[1], closestBet[1])
	t.Logf("Closing first bet")
	CloseBet(db, time.Now())

	scores := GetScores(db)
	expectedScores := []UserScores{{"near", 1},
		{"near2", 1}, {"after", 0}, {"other", 0}}
	checkUserScores(t, expectedScores, scores)

	good2 := time.Now()
	good2 = good2.Add(time.Minute)
	AddUserBet(db, "near", good2, false)
	AddUserBet(db, "other", good2, false)
	AddUserBet(db, "after", good2.Add(time.Hour), false)

	CloseBet(db, good2)

	scores = GetScores(db)
	expectedScores2 := []UserScores{{"near", 2},
		{"near2", 1}, {"other", 1}, {"after", 0}}
	checkUserScores(t, expectedScores2, scores)

	RollbackLastBet(db)
	scores = GetScores(db)
	checkUserScores(t, expectedScores, scores)
}
Beispiel #14
0
func TestConcatenatedPackets(t *testing.T) {
	var token token
	res := gen_lex("toto", "NICK lol\r\nPING bis\r\n")
	go res.run()

	token = <-res.tokens
	assert.AssertEquals(t, token.tok, NICK)
	assert.AssertEquals(t, token.val, "NICK")
	token = <-res.tokens
	assert.AssertEquals(t, token.tok, WORD)
	assert.AssertEquals(t, token.val, "lol")
	token = <-res.tokens
	assert.AssertEquals(t, token.tok, EOF)

	token = <-res.tokens
	assert.AssertEquals(t, token.tok, PING)
	token = <-res.tokens
	assert.AssertEquals(t, token.tok, WORD)
	assert.AssertEquals(t, token.val, "bis")
}
Beispiel #15
0
func testMessageParse(t *testing.T, msg fmt.Stringer) {
	res := ParseMessage(msg.String())
	t.Logf("Parsed message is %q", res)
	assert.AssertEquals(t, res[0], msg)
}
Beispiel #16
0
func TestParseInvalid(t *testing.T) {
	assert.AssertEquals(t, ParseMessage("Invalid")[0], nil)
}
Beispiel #17
0
func TestReverse(t *testing.T) {
	expected := "ƃɐ è ƃɐ"
	r := RotateString("ga è ga")
	t.Logf("Got %q, expected %q\n", r, expected)
	assert.AssertEquals(t, expected, r)
}
Beispiel #18
0
func TestTimezoneTranslate(t *testing.T) {
	utc, _ := time.LoadLocation("UTC")
	expected := time.Date(2006, 1, 2, 15, 4, 5, 0, utc)
	ts, _ := parseTimezoneQuery("Mon, 02 Jan 2006 15:04:05 MST in UTC")
	assert.AssertEquals(t, expected, ts)
}
Beispiel #19
0
func TestMessage(t *testing.T) {
	a := MsgNick{Name: "to"}
	assert.AssertEquals(t, a.String(), "NICK to\r\n")
}
Beispiel #20
0
func TestPiDecimal(t *testing.T) {
	pi, _ := EstimatePi(10)
	assert.AssertEquals(t, "3.142", pi.FloatString(3))
}