-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe_test.go
60 lines (54 loc) · 1.12 KB
/
recipe_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"hash/crc32"
"regexp"
"testing"
)
func TestRandSecure(t *testing.T) {
num, err := cryptoRandSecure(int64(^(uint64(1) << 63)))
if err != nil {
t.Fatal(err)
}
t.Log(num)
}
func TestRandSecure_Limit_Max_1(t *testing.T) {
num, err := cryptoRandSecure(1)
if err != nil {
t.Fatal(err)
}
if num > 1 {
t.Error("didn't expect nimber bigger then 1")
}
}
func TestUniqueRecipeID_No_Title(t *testing.T) {
id, err := uniqueRecipeID("")
if err == nil {
t.Fatal("error was expected")
return
}
if err != errRecipeTitleNotSet {
t.Errorf("expected '%s' but got '%s'", errRecipeTitleNotSet, err)
}
if id != "" {
t.Error("in case of error empty id is expected")
}
}
func TestUniqueRecipeID(t *testing.T) {
title := "raspberry pi"
id, err := uniqueRecipeID(title)
if err != nil {
t.Fatal("error was expected")
return
}
t.Log(id)
// use regex to match id
var crc = crc32.ChecksumIEEE([]byte(title))
r, err := regexp.Compile(fmt.Sprintf("%x-[0-9 a-z].*-[0-9 a-z].*", crc))
if err != nil {
t.Fatal(err)
}
if !r.MatchString(id) {
t.Errorf("'%s' is not in expected format", id)
}
}