本文整理汇总了Golang中gospec.Context.Specify方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Specify方法的具体用法?Golang Context.Specify怎么用?Golang Context.Specify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gospec.Context
的用法示例。
在下文中一共展示了Context.Specify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HuffmanDecodeSpec
func HuffmanDecodeSpec(c gospec.Context) {
c.Specify("Basic huffman decode", func() {
var codebook vorbis.Codebook
codebook.Entries = make([]vorbis.CodebookEntry, 8)
codebook.Entries[0].Length = 2
codebook.Entries[1].Length = 4
codebook.Entries[2].Length = 4
codebook.Entries[3].Length = 4
codebook.Entries[4].Length = 4
codebook.Entries[5].Length = 2
codebook.Entries[6].Length = 3
codebook.Entries[7].Length = 3
codebook.AssignCodewords()
v := []uint8{0x5F, 0x6E, 0x2A, 0x00}
br := vorbis.MakeBitReader(bytes.NewBuffer(v))
c.Expect(codebook.DecodeScalar(br), Equals, 7)
c.Expect(codebook.DecodeScalar(br), Equals, 6)
c.Expect(codebook.DecodeScalar(br), Equals, 5)
c.Expect(codebook.DecodeScalar(br), Equals, 4)
c.Expect(codebook.DecodeScalar(br), Equals, 3)
c.Expect(codebook.DecodeScalar(br), Equals, 2)
c.Expect(codebook.DecodeScalar(br), Equals, 1)
c.Expect(codebook.DecodeScalar(br), Equals, 0)
})
}
示例2: StopwordSpec
func StopwordSpec(c gospec.Context) {
c.Specify("Should ignore stopwords", func() {
stopwords := keys(words)
for token := range channel(stopwords...) {
c.Expect(token.Backing(), Equals, "should never get here")
}
})
}
示例3: SimpleSpec
func SimpleSpec(c gospec.Context) {
c.Specify("Should tokenize a simple string", func() {
s := Build()
tokens := s.Tokenize("Hello world, to all the tests out there!")
for _, str := range []string{"Hello", "world,", "to", "all", "the", "tests", "out", "there!"} {
c.Expect((<-tokens).Backing(), Equals, str)
}
})
}
示例4: NaiveSpec
func NaiveSpec(c gospec.Context) {
in := make([]complex128, 8)
for i := range in {
in[i] = complex(math.Cos(float64(i)/float64(len(in))*2*math.Pi), 0)
}
verify := func(out []complex128, start, stride int, name string) {
c.Specify(name, func() {
for i := start; i < len(out); i += stride {
mag, ang := cmplx.Polar(out[i])
if i == start+stride || i == len(out)+start-stride {
c.Expect(mag, IsWithin(1e-9), float64(len(out)/stride)/2)
if real(out[i]) < 0 {
if ang < 0 {
c.Expect(ang, IsWithin(1e-9), -math.Pi)
} else {
c.Expect(ang, IsWithin(1e-9), math.Pi)
}
} else {
c.Expect(ang, IsWithin(1e-9), 0.0)
}
} else {
c.Expect(mag, IsWithin(1e-9), 0.0)
}
}
})
}
c.Specify("Test basic DFT", func() {
out := make([]complex128, len(in))
fft.DFT(in, out, 0, 1)
verify(out, 0, 1, "Start/Stride 0/1")
in2 := make([]complex128, 2*len(in))
out2 := make([]complex128, 2*len(in))
for i := range in2 {
in2[i] = in[i/2]
}
fft.DFT(in2, out2, 0, 2)
verify(out2, 0, 2, "Start/Stride 0/2")
fft.DFT(in2, out2, 1, 2)
verify(out2, 1, 2, "Start/Stride 1/2")
in5 := make([]complex128, 5*len(in))
out5 := make([]complex128, 5*len(in))
for i := range in5 {
in5[i] = in[i/5]
}
for i := 0; i < 5; i++ {
fft.DFT(in5, out5, i, 5)
verify(out5, i, 5, fmt.Sprintf("Start/Stride %d/%d", i, len(in5)/len(in)))
}
})
}
示例5: FibSpec
func FibSpec(c gospec.Context) {
fib := NewFib().Sequence(10)
c.Specify("The first two Fibonacci numbers are 0 and 1", func() {
c.Expect(fib[0], Equals, 0)
c.Expect(fib[1], Equals, 1)
})
c.Specify("Each remaining number is the sum of the previous two", func() {
for i := 2; i < len(fib); i++ {
c.Expect(fib[i], Equals, fib[i-1]+fib[i-2])
}
})
}
示例6: StemmerSpec
func StemmerSpec(c gospec.Context) {
pairs := map[string]string{
"debugging": "debug",
"laptop": "laptop",
"books": "book",
"debugger": "debugg",
"winery": "winery",
}
for original, stemmed := range pairs {
c.Specify(fmt.Sprintf("Should stem %s to %s", original, stemmed), func() {
c.Expect((<-channel(original)).Backing(), Equals, stemmed)
})
}
}
示例7: BitReaderSpec
func BitReaderSpec(c gospec.Context) {
v := []uint8{1, 1, 3, 0, 0, 9}
// 00001001 00000000 00000000 00000011 00000001 00000001
// 1
// 001 0000000
// 001 00000000 00000000 00000011 00000
// 00001
c.Specify("Bitreader reads from an io.Reader properly", func() {
br := vorbis.MakeBitReader(bytes.NewBuffer(v))
c.Expect(uint32(0x1), Equals, br.ReadBits(1))
c.Expect(uint32(0x80), Equals, br.ReadBits(10))
c.Expect(uint32(0x20000060), Equals, br.ReadBits(32))
c.Expect(uint32(0x1), Equals, br.ReadBits(5))
})
}
示例8: FFTWSpec
func FFTWSpec(c gospec.Context) {
c.Specify("Check agains fftw", func() {
N := 2 * 3 * 5 * 7 * 11
in := make([]complex128, N)
out_fft := make([]complex128, N)
out_fftw := make([]complex128, N)
for i := range in {
in[i] = complex(float64(i/1000-100), float64(i)/10)
}
fftw.PlanDft1d(in, out_fftw, fftw.Forward, fftw.Estimate).Execute()
fft.FFT(in, out_fft)
tolerance := 1e-5
for i := range out_fft {
c.Expect(real(out_fft[i]), IsWithin(tolerance), real(out_fftw[i]))
c.Expect(imag(out_fft[i]), IsWithin(tolerance), imag(out_fftw[i]))
}
})
}
示例9: DoubleMetaphoneSpec
func DoubleMetaphoneSpec(c gospec.Context) {
pairs := map[string]string{
"debugging": "TPKN",
"laptop": "LPTP",
"books": "PKS",
"debugger": "TPKR",
"winery": "ANR",
}
for original, stemmed := range pairs {
c.Specify(fmt.Sprintf("Should encode %s to %s", original, stemmed), func() {
c.Expect((<-channel(original)).Backing(), Equals, stemmed)
})
}
c.Specify("Should return 2 different encodings for Schmidt", func() {
ch := channel("Schmidt")
c.Expect((<-ch).Backing(), Equals, "XMT")
c.Expect((<-ch).Backing(), Equals, "SMT")
})
}
示例10: OggSpec
func OggSpec(c gospec.Context) {
// v := []uint8{1,1,3,0,0,9}
// 00001001 00000000 00000000 00000011 00000001 00000001
// 1
// 001 0000000
// 001 00000000 00000000 00000011 00000
// 00001
// c.Specify("Bitreader reads from an io.Reader properly", func() {
// br := ogg.MakeBitReader(v)
// c.Expect(uint32(0x1), Equals, br.ReadBits(1))
// c.Expect(uint32(0x80), Equals, br.ReadBits(10))
// c.Expect(uint32(0x20000060), Equals, br.ReadBits(32))
// c.Expect(uint32(0x1), Equals, br.ReadBits(5))
// })
c.Specify("Read a file", func() {
f, err := os.Open("metroid.ogg")
c.Assume(err, Equals, nil)
err = ogg.Decode(f)
c.Assume(err, Equals, nil)
})
}
示例11: StringSetSpec
func StringSetSpec(c gospec.Context) {
set := new(StringSet)
c.Specify("An empty set contains nothing", func() {
c.Expect(set.ToArray(), ContainsExactly, Values())
})
c.Specify("Distinct values can be added to a set", func() {
set.Add("a")
set.Add("b")
c.Expect(set.ToArray(), ContainsExactly, Values("a", "b"))
})
c.Specify("Duplicate values cannot be added to a set", func() {
set.Add("a")
set.Add("a")
c.Expect(set.ToArray(), ContainsExactly, Values("a"))
})
}
示例12: StackSpec
func StackSpec(c gospec.Context) {
stack := NewStack()
c.Specify("An empty stack", func() {
c.Specify("is empty", func() {
c.Expect(stack.Empty(), IsTrue)
})
c.Specify("After a push, the stack is no longer empty", func() {
stack.Push("a push")
c.Expect(stack.Empty(), IsFalse)
})
})
c.Specify("When objects have been pushed onto a stack", func() {
stack.Push("pushed first")
stack.Push("pushed last")
c.Specify("the object pushed last is popped first", func() {
poppedFirst := stack.Pop()
c.Expect(poppedFirst, Equals, "pushed last")
})
c.Specify("the object pushed first is popped last", func() {
stack.Pop()
poppedLast := stack.Pop()
c.Expect(poppedLast, Equals, "pushed first")
})
c.Specify("After popping all objects, the stack is empty", func() {
stack.Pop()
stack.Pop()
c.Expect(stack.Empty(), IsTrue)
})
})
}
示例13: ExpectationSyntaxSpec
func ExpectationSyntaxSpec(c gospec.Context) {
c.Specify("Objects can be compared for equality", func() {
c.Expect(1, Equals, 1)
c.Expect("string", Equals, "string")
// There are some shorthands for commonly used comparisons:
c.Expect(true, IsTrue)
c.Expect(false, IsFalse)
c.Expect(nil, IsNil)
var typedNilPointerInsideInterfaceValue *os.File
c.Expect(typedNilPointerInsideInterfaceValue, IsNil)
// Comparing pointer equality is also possible:
p1 := &Point2{1, 2}
p2 := p1
p3 := &Point2{1, 2}
c.Expect(p2, IsSame, p1)
c.Expect(p3, Not(IsSame), p1)
// Comparing floats for equality is not recommended, because
// floats are rarely exactly equal. So don't write like this:
c.Expect(3.141, Equals, 3.141)
// But instead compare using a delta and write like this:
c.Expect(3.141, IsWithin(0.001), 3.1415926535)
// Objects with an "Equals(interface{}) bool" method can be
// compared for equality. See "point.go" for details of how
// the Equals(interface{}) method should be written. Special
// care is needed if the objects are used both as values and
// as pointers.
a1 := Point2{1, 2}
a2 := Point2{1, 2}
c.Expect(a1, Equals, a2)
b1 := &Point3{1, 2, 3}
b2 := &Point3{1, 2, 3}
c.Expect(b1, Equals, b2)
})
c.Specify("All expectations can be negated", func() {
c.Expect(1, Not(Equals), 2)
c.Expect("apples", Not(Equals), "oranges")
c.Expect(new(int), Not(IsNil))
})
c.Specify("Boolean expressions can be stated about an object", func() {
s := "some string"
c.Expect(s, Satisfies, len(s) >= 10 && len(s) <= 20)
c.Expect(s, Not(Satisfies), len(s) == 0)
})
c.Specify("Custom matchers can be defined for commonly used expressions", func() {
c.Expect("first string", HasSameLengthAs, "other string")
})
c.Specify("Arrays/slices, lists, vectors and channels can be tested for containment", func() {
array := []string{"one", "two", "three"}
list := list.New()
list.PushBack("one")
list.PushBack("two")
list.PushBack("three")
vector := new(vector.Vector)
vector.Push("one")
vector.Push("two")
vector.Push("three")
channel := make(chan string, 10)
channel <- "one"
channel <- "two"
channel <- "three"
close(channel)
c.Expect(array, Contains, "one")
c.Expect(list, Contains, "two")
c.Expect(vector, Contains, "three")
c.Expect(channel, Contains, "three")
c.Expect(array, Not(Contains), "four")
c.Expect(list, ContainsAll, Values("two", "one"))
c.Expect(list, ContainsAny, Values("apple", "orange", "one"))
c.Expect(list, ContainsExactly, Values("two", "one", "three"))
c.Expect(list, ContainsInOrder, Values("one", "two", "three"))
c.Expect(list, ContainsInPartialOrder, Values("one", "three"))
})
}
示例14: MainSpec
// MainSpec is the master specification test for the REST server.
func MainSpec(c gospec.Context) {
c.Specify("GET /", func() {
response := GetRequest("/")
c.Specify("returns a status code of 200", func() {
c.Expect(response.Code, Equals, 200)
})
c.Specify("returns a list of available resources", func() {
var msg MessageSuccess
err := json.Unmarshal([]byte(response.Body), &msg)
c.Expect(err, Equals, nil)
c.Expect(msg.Msg, Equals, "success")
c.Expect(len(msg.Results), Equals, 2)
})
})
c.Specify("GET /providers", func() {
c.Specify("returns 401 unauthorized when Authorization is not provided", func() {
response := GetRequest("/providers")
c.Expect(response.Code, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
})
c.Specify("returns 401 unauthorized when Authorization does not contain two arguments", func() {
request := createRequest("GET", "/providers")
request.Header.Add("Authorization", "invalid auth header")
response := do(request)
body := getResponseBody(response)
var msg MessageError
json.Unmarshal([]byte(body), &msg)
c.Expect(response.StatusCode, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
c.Expect(msg.Code, Equals, 401)
})
c.Specify("returns 401 unauthorized when Authorization does not contain GDS", func() {
request := createRequest("GET", "/providers")
request.Header.Add("Authorization", "INVALID onetwothreefour")
response := do(request)
body := getResponseBody(response)
var msg MessageError
json.Unmarshal([]byte(body), &msg)
c.Expect(response.StatusCode, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
c.Expect(msg.Code, Equals, 401)
})
c.Specify("returns 401 unauthorized when Authorization does not have key:signature format", func() {
request := createRequest("GET", "/providers")
request.Header.Add("Authorization", "GDS onetwothreefour")
response := do(request)
body := getResponseBody(response)
var msg MessageError
json.Unmarshal([]byte(body), &msg)
c.Expect(response.StatusCode, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
c.Expect(msg.Code, Equals, 401)
})
c.Specify("returns 401 unauthorized when key is not a valid username", func() {
request := createRequest("GET", "/providers")
request.Header.Add("Authorization", "GDS baduser:signature")
response := do(request)
body := getResponseBody(response)
var msg MessageError
json.Unmarshal([]byte(body), &msg)
c.Expect(response.StatusCode, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
c.Expect(msg.Code, Equals, 401)
})
c.Specify("returns 401 unauthorized when the signature is not valid", func() {
request := createRequest("GET", "/providers")
request.Header.Add("Authorization", "GDS username:signature")
response := do(request)
body := getResponseBody(response)
var msg MessageError
json.Unmarshal([]byte(body), &msg)
c.Expect(response.StatusCode, Equals, 401)
c.Expect(response.Header.Get("WWW-Authenticate"), Not(IsNil))
c.Expect(msg.Code, Equals, 401)
})
c.Specify("returns a list of providers when valid credentials are provided", func() {
response := GetRequestWithAuth("/providers")
c.Expect(response.Code, Equals, 200)
var msg MessageSuccess
err := json.Unmarshal([]byte(response.Body), &msg)
c.Expect(err, Equals, nil)
c.Expect(msg.Msg, Equals, "success")
c.Expect(len(msg.Results), Equals, 3)
//.........这里部分代码省略.........
示例15: SuperstripSpec
func SuperstripSpec(c gospec.Context) {
c.Specify("Should trim whitespace", func() {
c.Expect((<-channel(" foobar ")).Backing(), Equals, "foobar")
})
c.Specify("Should convert to lowercase", func() {
c.Expect((<-channel("LIKEABOSS")).Backing(), Equals, "likeaboss")
})
c.Specify("Should remove unicode characters", func() {
c.Expect((<-channel("Hello 世界")).Backing(), Equals, "hello")
})
c.Specify("Should remove things that aren't letters or numbers", func() {
c.Expect((<-channel("like,./';'[email protected]#$^*boss")).Backing(), Equals, "likeaboss")
})
c.Specify("Should keep numbers", func() {
c.Expect((<-channel("f00b4r")).Backing(), Equals, "f00b4r")
})
c.Specify("Should not emit empty tokens", func() {
for token := range channel("foobar", ",./;'") {
c.Expect(token.Backing(), Equals, "foobar")
}
})
}