Ejemplo n.º 1
0
func blacklistGet(t *testing.T, count, offset int, out []string) {
	r, err := http.Get(fmt.Sprintf("http://%s/v1/profanity/blacklist/?lang=%s&count=%d&offset=%d", serverAddr, "en_US", count, offset))

	if err != nil {
		t.Fatalf("error getting: %s", err)
		return
	}

	var res blacklistResponse
	err = json.NewDecoder(r.Body).Decode(&res)

	if err != nil {
		t.Fatal(err)
	}

	log.Printf("res: %v", res)
	expLen := math.IntMin(math.IntMax(res.Total-offset, 0), count)

	if len(res.Blacklist) != expLen {
		t.Fatalf("%d != %d", len(res.Blacklist), expLen)
	}

	if out != nil {
		for i := 0; i < len(res.Blacklist); i++ {
			if res.Blacklist[i] != out[i] {
				t.Fatalf("%s != %s", res.Blacklist[i], out[i])
			}
		}
	}
}
Ejemplo n.º 2
0
func testBackend(t *testing.T, test *TestCase) {
	list := test.list
	words := test.words

	if err := list.Empty(); err != nil {
		t.Fatalf("%s: expected err %v", test, err)
	}

	if cnt, err := list.Count(); cnt != 0 || err != nil {
		t.Fatalf("%s expected 0 got %d, err %v", test, cnt, err)
	}

	if values, err := list.Get(10, 0); len(values) != 0 || err != nil {
		t.Fatalf("%s expected 0 got %d, err %v", test, len(values), err)
	}

	if err := list.Set(words); err != nil {
		t.Fatalf("%s expected nil got %v", test, err)
	}

	if cnt, err := list.Count(); cnt != len(words) || err != nil {
		t.Fatalf("%s expected %d got %d, err %v", test, len(words), cnt, err)
	}

	expCnt := math.IntMin(10, len(words))

	if values, err := list.Get(10, 0); len(values) != expCnt || err != nil {
		t.Fatalf("%s expected %d got %d, err %v", test, expCnt, len(values), err)
	}

	if len(words) > 10 {
		if values, err := list.Get(5, 0); len(values) != 5 || err != nil {
			t.Fatalf("%s expected 1 got %d, err %v", test, len(values), err)
		}

		if values, err := list.Get(5, 5); len(values) != 5 || err != nil {
			t.Fatalf("%s expected 1 got %d, err %v", test, len(values), err)
		}
	}

	if len(words) > 1 {
		if err := list.Delete(words[:1]); err != nil {
			t.Fatalf("%s expected nil got %v", test, err)
		}

		if cnt, err := list.Count(); cnt != len(words)-1 || err != nil {
			t.Fatalf("%s expected %d got %d, err %v", test, len(words)-1, cnt, err)
		}
	}

	if err := list.Empty(); err != nil {
		t.Fatalf("%s: expected err %v", test, err)
	}

	if cnt, err := list.Count(); cnt != 0 || err != nil {
		t.Fatalf("%s expected 0 got %d, err %v", test, cnt, err)
	}
}
Ejemplo n.º 3
0
// Build string replacer from blacklist
func (p *SetReplacer) buildReplacer(words []string) (map[string]string, error) {
	var starindex int
	n := len(words)

	if n == 0 {
		return nil, errors.New("Got empty blacklist")
	}

	repl := make(map[string]string, n)

	for _, w := range words {
		starindex = math.IntMin(len(w), len(starmap)-1)
		repl[strings.ToLower(w)] = starmap[starindex]
	}

	return repl, nil
}
Ejemplo n.º 4
0
// Build string replacer from blacklist
func (p *StringReplacer) buildReplacer(words []string) (*genericReplacer, error) {
	var starindex int
	n := len(words) * 2

	if n == 0 {
		return nil, errors.New("Got empty blacklist")
	}

	repl := make([]string, n)

	for i, w := range words {
		repl[i*2] = w
		starindex = math.IntMin(len(w), len(starmap)-1)
		repl[i*2+1] = starmap[starindex]
	}

	return makeGenericReplacer(repl), nil
}