當前位置: 首頁>>代碼示例>>Golang>>正文


Golang gospec.Context類代碼示例

本文整理匯總了Golang中gospec.Context的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Context類的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)
    })
}
開發者ID:runningwild,項目名稱:gorbis,代碼行數:27,代碼來源:vorbis_test.go

示例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")
        }
    })
}
開發者ID:darkhelmet,項目名稱:fetch,代碼行數:8,代碼來源:stopword_test.go

示例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)
        }
    })
}
開發者ID:darkhelmet,項目名稱:fetch,代碼行數:9,代碼來源:simple_test.go

示例4: 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)
        })
    }
}
開發者ID:darkhelmet,項目名稱:fetch,代碼行數:14,代碼來源:stemmer_test.go

示例5: 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))
    })
}
開發者ID:runningwild,項目名稱:gorbis,代碼行數:16,代碼來源:vorbis_test.go

示例6: 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])
        }
    })
}
開發者ID:boggle,項目名稱:gospec,代碼行數:13,代碼來源:fib_test.go

示例7: 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]))
        }
    })
}
開發者ID:runningwild,項目名稱:go-fft,代碼行數:20,代碼來源:fft_test.go

示例8: 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")
    })
}
開發者ID:darkhelmet,項目名稱:fetch,代碼行數:20,代碼來源:double_metaphone_test.go

示例9: 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)
    })
}
開發者ID:runningwild,項目名稱:gorbis,代碼行數:23,代碼來源:foo_test.go

示例10: 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)
        })
    })
}
開發者ID:boggle,項目名稱:gospec,代碼行數:34,代碼來源:stack_test.go

示例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"))
    })
}
開發者ID:orfjackal,項目名稱:gobbler,代碼行數:17,代碼來源:set_test.go

示例12: 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)
//.........這裏部分代碼省略.........
開發者ID:emergenesis,項目名稱:citeplasm-rest-api,代碼行數:101,代碼來源:main_test.go

示例13: 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)))
        }
    })
}
開發者ID:runningwild,項目名稱:go-fft,代碼行數:55,代碼來源:fft_test.go

示例14: ExecutionModelSpec

func ExecutionModelSpec(c gospec.Context) {

    // "Before block", for example common variables for use in all specs.
    commonVariable := ""

    c.Specify("The following child specs modify the same variable", func() {

        // "Before block", for example initialization for this group of specs.
        commonVariable += "x"

        // All sibling specs (specs which are declared within a common parent)
        // are fully isolated from each other. The following three siblings are
        // executed in parallel, each in its own goroutine, and each of them
        // has its own copy of the local variables declared in its parent specs.
        c.Specify("I modify it, but none of my siblings will know it", func() {
            commonVariable += "1"
        })
        c.Specify("Also I modify it, but none of my siblings will know it", func() {
            commonVariable += "2"
        })
        c.Specify("Also I modify it, but none of my siblings will know it", func() {
            commonVariable += "3"
        })

        // "After block", for example tear down of changes to the file system.
        commonVariable += "y"

        // Depending on which of the previous siblings was executed this time,
        // there are three possible values for the variable:
        c.Expect(commonVariable, Satisfies, commonVariable == "x1y" ||
            commonVariable == "x2y" ||
            commonVariable == "x3y")
    })

    c.Specify("You can nest", func() {
        c.Specify("as many specs", func() {
            c.Specify("as you wish.", func() {
                c.Specify("GoSpec does not impose artificial limits, "+
                    "so you can organize your specs freely.", func() {
                })
            })
        })
    })

    c.Specify("The distinction between 'Expect' and 'Assume'", func() {
        // When we have non-trivial test setup code, then it is often useful to
        // explicitly state our assumptions about the state of the system under
        // test, before the body of the test is executed.
        //
        // Otherwise it could happen that the test passes even though the code
        // is broken, or then we get lots of unhelpful error messages from the
        // body of the test, even though the bug was in the test setup.
        //
        // For this use case, GoSpec provides 'Assume' in addition to 'Expect'.
        // Use 'Assume' when the test assumes the correct functioning of some
        // behaviour which is not the focus of the current test:
        //
        // - When an 'Expect' fails, then the child specs are executed normally.
        //
        // - When an 'Assume' fails, then the child specs are NOT executed. This
        //   helps to prevent lots of false alarms from the child specs, when
        //   the real problem was in the test setup.

        // Some very complex test setup code
        input := ""
        for ch := 'a'; ch <= 'c'; ch++ {
            input += string(ch)
        }

        // Uncomment this line to add a bug into the test setup:
        //input += " bug"

        // Uncomment one of the following asserts to see their difference:
        //c.Expect(input, Equals, "abc")
        //c.Assume(input, Equals, "abc")

        c.Specify("When a string is made all uppercase", func() {
            result := strings.ToUpper(input)

            c.Specify("it is all uppercase", func() {
                c.Expect(result, Equals, "ABC")
            })
            c.Specify("its length is not changed", func() {
                c.Expect(len(result), Equals, 3)
            })
        })
    })
}
開發者ID:boggle,項目名稱:gospec,代碼行數:88,代碼來源:execution_model_test.go

示例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")
        }
    })
}
開發者ID:darkhelmet,項目名稱:fetch,代碼行數:27,代碼來源:superstrip_test.go


注:本文中的gospec.Context類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。