当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.Expect方法代码示例

本文整理汇总了Golang中gospec.Context.Expect方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Expect方法的具体用法?Golang Context.Expect怎么用?Golang Context.Expect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gospec.Context的用法示例。


在下文中一共展示了Context.Expect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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

示例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: 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

示例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])
		}
	})
}
开发者ID:boggle,项目名称:gospec,代码行数:13,代码来源:fib_test.go

示例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)
		})
	}
}
开发者ID:darkhelmet,项目名称:fetch,代码行数:14,代码来源:stemmer_test.go

示例7: 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

示例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: 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

示例10: 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

示例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: 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"))
	})
}
开发者ID:boggle,项目名称:gospec,代码行数:85,代码来源:expectation_syntax_test.go

示例13: 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

示例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.Expect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。