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


Golang Context.Expect方法代码示例

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


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

示例1: ConcurrencySpec

func ConcurrencySpec(c nanospec.Context) {
	r := NewRunner()
	r.AddSpec(VerySlowDummySpec)

	start := time.Now()
	r.Run()
	end := time.Now()
	totalTime := end.Sub(start).Nanoseconds()

	// If the spec is executed single-threadedly, then it would take
	// at least 4*DELAY to execute. If executed multi-threadedly, it
	// would take at least 2*DELAY to execute, because the first spec
	// needs to be executed fully before the other specs are found, but
	// after that the other specs can be executed in parallel.
	expectedMaxTime := int64(math.Floor(2.9 * DELAY))

	if totalTime > expectedMaxTime {
		c.Errorf("Expected the run to take less than %v ms but it took %v ms",
			expectedMaxTime/MILLISECOND, totalTime/MILLISECOND)
	}

	runCounts := countSpecNames(r.executed)
	c.Expect(runCounts["Child A"]).Equals(1)
	c.Expect(runCounts["Child B"]).Equals(1)
	c.Expect(runCounts["Child C"]).Equals(1)
	c.Expect(runCounts["Child D"]).Equals(1)
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:27,代码来源:concurrency_test.go

示例2: FuncNameSpec

func FuncNameSpec(c nanospec.Context) {

	c.Specify("The name of a function can be retrieved from a function reference", func() {
		name := functionName(dummyFunction)
		c.Expect(name).Equals("gospec.dummyFunction")
	})
	c.Specify("Getting the name of an anonymous functions will fail gracefully", func() {
		name := functionName(func() {})
		//c.Expect(name).Equals("<unknown function>")
		// since weekly.2012-01-15 even anonymous functions have a name
		c.Expect(name).Satisfies(strings.HasPrefix(name, "gospec._func_"))
	})
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:13,代码来源:funcname_test.go

示例3: MatcherMessagesSpec

func MatcherMessagesSpec(c nanospec.Context) {
	spy := new(SpyErrorLogger)
	m := newMatcherAdapter(nil, spy, ExpectFailed)

	c.Specify("Positive expectation failures are reported with the positive message", func() {
		m.Expect(1, DummyEquals, 1)
		c.Expect(spy.LastError()).Equals("")

		m.Expect(1, DummyEquals, 2)
		c.Expect(spy.LastError()).Equals("1 should equal 2")
	})
	c.Specify("Negative expectation failures are reported with the negative message", func() {
		m.Expect(1, Not(DummyEquals), 2)
		c.Expect(spy.LastError()).Equals("")

		m.Expect(1, Not(DummyEquals), 1)
		c.Expect(spy.LastError()).Equals("1 should NOT equal 1")
	})
	c.Specify("Errors in expectations are reported with the error message", func() {
		m.Expect(666, DummyEquals, 1)
		c.Expect(spy.LastError()).Equals("666 illegal value")
	})
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:23,代码来源:matchers_test.go

示例4: LocationSpec

func LocationSpec(c nanospec.Context) {

	c.Specify("Location of the current method can be found", func() {
		loc := currentLocation() // line 15
		c.Expect(loc.FileName()).Equals("location_test.go")
		c.Expect(loc.Line()).Equals(15)
	})
	c.Specify("The line number is that of the call instruction; not where the call will return to", func() {
		// This indirection is needed to reproduce the off-by-one issue, because
		// there must be no instructions on the same line after the call itself.
		var loc *Location
		f := func() {
			loc = callerLocation()
		}
		f() // line 26; call returns to line 27
		c.Expect(loc.Line()).Equals(26)
	})
	c.Specify("Location of the calling method can be found", func() {
		loc := callerLocation()
		c.Expect(loc.FileName()).Equals("context.go")
	})
	c.Specify("The name of the method is provided", func() {
		loc := methodWhereLocationIsCalled()
		c.Expect(loc.Name()).Equals("gospec.methodWhereLocationIsCalled")
	})
	c.Specify("Calls to newLocation are synced with the helper methods", func() {
		c.Expect(newLocation(0).Name()).Equals(currentLocation().Name())
		c.Expect(newLocation(0).File()).Equals(currentLocation().File())
		c.Expect(newLocation(1).Name()).Equals(callerLocation().Name())
		c.Expect(newLocation(1).File()).Equals(callerLocation().File())
	})
	c.Specify("Program Counters can be converted to Locations", func() {
		expectedLine := currentLocation().Line() + 1
		pc, _, _, _ := runtime.Caller(0)
		loc := locationForPC(pc)
		c.Expect(loc.FileName()).Equals("location_test.go")
		c.Expect(loc.Line()).Equals(expectedLine)
	})
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:39,代码来源:location_test.go

示例5: RecoverSpec

func RecoverSpec(c nanospec.Context) {

	c.Specify("When the called function panics", func() {
		err := recoverOnPanic(boom2)

		c.Specify("the cause is returned", func() {
			c.Expect(err.Cause).Equals("boom!")
		})
		c.Specify("the stack trace begins with the panicking line", func() {
			c.Expect(err.StackTrace[0].Name()).Equals(fmt.Sprintf("%v.boom0", pkgPath))
		})
		c.Specify("the stack trace includes all parent functions", func() {
			c.Expect(err.StackTrace[1].Name()).Equals(fmt.Sprintf("%v.boom1", pkgPath))
		})
		c.Specify("the stack trace ends with the called function", func() {
			lastEntry := err.StackTrace[len(err.StackTrace)-1]
			c.Expect(lastEntry.Name()).Equals(fmt.Sprintf("%v.boom2", pkgPath))
		})
		c.Specify("the stack trace line numbers are the line of the call; not where the call will return", func() {
			// For an explanation, see the comments at http://code.google.com/p/go/issues/detail?id=1100
			c.Expect(err.StackTrace[0].Line()).Equals(19)
		})
	})

	c.Specify("When the called function does not panic", func() {
		err := recoverOnPanic(noBoom)

		c.Specify("there is no error", func() {
			c.Expect(err == nil).IsTrue()
		})
	})
}
开发者ID:ghthor,项目名称:gospec,代码行数:32,代码来源:recover_test.go

示例6: ContextSpec

func ContextSpec(c nanospec.Context) {

	c.Specify("When specs are executed", func() {
		result := runSpecWithContext(DummySpecWithTwoChildren, newInitialContext())
		executed := result.executedSpecs
		postponed := result.postponedSpecs

		c.Specify("executed specs are reported", func() {
			c.Expect(len(executed)).Equals(2)
			c.Expect(executed[0].name).Equals("RootSpec")
			c.Expect(executed[1].name).Equals("Child A")
		})
		c.Specify("postponed specs are reported", func() {
			c.Expect(len(postponed)).Equals(1)
			c.Expect(postponed[0].name).Equals("Child B")
		})
	})

	c.Specify("When some of the specs have previously been executed", func() {
		result := runSpecWithContext(DummySpecWithTwoChildren, newExplicitContext([]int{1}))
		executed := result.executedSpecs
		postponed := result.postponedSpecs

		c.Specify("previously executed specs are NOT reported", func() {
			c.Expect(len(executed)).Equals(2)
			c.Expect(executed[0].name).Equals("RootSpec")
			c.Expect(executed[1].name).Equals("Child B")

			c.Expect(len(postponed)).Equals(0)
		})
	})

	c.Specify("Postponed specs are scheduled for execution, until they all have been executed", func() {
		r := NewRunner()
		r.AddSpec(DummySpecWithTwoChildren)
		r.Run()

		runCounts := countSpecNames(r.executed)
		c.Expect(len(runCounts)).Equals(3)
		c.Expect(runCounts["gospec.DummySpecWithTwoChildren"]).Equals(2)
		c.Expect(runCounts["Child A"]).Equals(1)
		c.Expect(runCounts["Child B"]).Equals(1)
	})

	c.Specify("Multiple specs can be executed in one batch", func() {
		r := NewRunner()
		r.AddSpec(DummySpecWithOneChild)
		r.AddSpec(DummySpecWithTwoChildren)
		r.Run()

		runCounts := countSpecNames(r.executed)
		c.Expect(runCounts["gospec.DummySpecWithOneChild"]).Equals(1)
		c.Expect(runCounts["gospec.DummySpecWithTwoChildren"]).Equals(2)
	})
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:55,代码来源:context_test.go

示例7: ExpectationsSpec

func ExpectationsSpec(c nanospec.Context) {

	c.Specify("When a spec has passing expectations or assumptions", func() {
		results := runSpec(func(c Context) {
			c.Expect(1, Equals, 1)
			c.Assume(1, Equals, 1)
			c.Specify("Child", func() {})
		})

		c.Specify("then the spec passes", func() {
			c.Expect(results.FailCount()).Equals(0)
		})
		c.Specify("then its children are executed", func() {
			c.Expect(results.TotalCount()).Equals(2)
		})
	})

	c.Specify("When a spec has failing expectations", func() {
		results := runSpec(func(c Context) {
			c.Expect(1, Equals, 2)
			c.Specify("Child", func() {})
		})

		c.Specify("then the spec fails", func() {
			c.Expect(results.FailCount()).Equals(1)
		})
		c.Specify("then its children are executed", func() {
			c.Expect(results.TotalCount()).Equals(2)
		})
	})

	c.Specify("When a spec has failing assumptions", func() {
		results := runSpec(func(c Context) {
			c.Assume(1, Equals, 2)
			c.Specify("Child", func() {})
		})

		c.Specify("then the spec fails", func() {
			c.Expect(results.FailCount()).Equals(1)
		})
		c.Specify("then its children are NOT executed", func() {
			c.Expect(results.TotalCount()).Equals(1)
		})
	})

	c.Specify("The location of a failed expectation is reported", func() {
		results := runSpec(func(c Context) {
			c.Expect(1, Equals, 2)
		})
		c.Expect(fileOfError(results)).Equals("expectations_test.go")
	})
	c.Specify("The location of a failed assumption is reported", func() {
		results := runSpec(func(c Context) {
			c.Assume(1, Equals, 2)
		})
		c.Expect(fileOfError(results)).Equals("expectations_test.go")
	})
}
开发者ID:rafrombrc,项目名称:gospec,代码行数:58,代码来源:expectations_test.go

示例8: ResultsSpec

func ResultsSpec(c nanospec.Context) {
	results := newResultCollector()

	c.Specify("When results have many root specs", func() {
		results.Update(newSpecRun("RootSpec2", nil, nil, nil))
		results.Update(newSpecRun("RootSpec1", nil, nil, nil))
		results.Update(newSpecRun("RootSpec3", nil, nil, nil))

		c.Specify("then the roots are sorted alphabetically", func() {
			c.Expect(results).Matches(ReportIs(`
- RootSpec1
- RootSpec2
- RootSpec3

3 specs, 0 failures
`))
		})
	})

	c.Specify("When results have many child specs", func() {
		// In tests, when a spec has many children, make sure
		// to pass a common parent instance to all the siblings.
		// Otherwise the parent's numberOfChildren is not
		// incremented and the children's paths will be wrong.

		// use names which would not sort alphabetically
		root := newSpecRun("RootSpec", nil, nil, nil)
		child1 := newSpecRun("one", nil, root, nil)
		child2 := newSpecRun("two", nil, root, nil)
		child3 := newSpecRun("three", nil, root, nil)

		// register in random order
		results.Update(root)
		results.Update(child1)

		results.Update(root)
		results.Update(child3)

		results.Update(root)
		results.Update(child2)

		c.Specify("then the children are sorted by their declaration order", func() {
			c.Expect(results).Matches(ReportIs(`
- RootSpec
  - one
  - two
  - three

4 specs, 0 failures
`))
		})
	})

	c.Specify("Case: zero specs", func() {
		c.Expect(results).Matches(ReportIs(`
0 specs, 0 failures
`))
	})
	c.Specify("Case: spec with no children", func() {
		a1 := newSpecRun("RootSpec", nil, nil, nil)
		results.Update(a1)
		c.Expect(results).Matches(ReportIs(`
- RootSpec

1 specs, 0 failures
`))
	})
	c.Specify("Case: spec with a child", func() {
		a1 := newSpecRun("RootSpec", nil, nil, nil)
		a2 := newSpecRun("Child A", nil, a1, nil)
		results.Update(a1)
		results.Update(a2)
		c.Expect(results).Matches(ReportIs(`
- RootSpec
  - Child A

2 specs, 0 failures
`))
	})
	c.Specify("Case: spec with nested children", func() {
		a1 := newSpecRun("RootSpec", nil, nil, nil)
		a2 := newSpecRun("Child A", nil, a1, nil)
		a3 := newSpecRun("Child AA", nil, a2, nil)
		results.Update(a1)
		results.Update(a2)
		results.Update(a3)
		c.Expect(results).Matches(ReportIs(`
- RootSpec
  - Child A
    - Child AA

3 specs, 0 failures
`))
	})
	c.Specify("Case: spec with multiple nested children", func() {
		runner := NewRunner()
		runner.AddSpec(DummySpecWithMultipleNestedChildren)
		runner.Run()
		c.Expect(runner.Results()).Matches(ReportIs(`
- gospec.DummySpecWithMultipleNestedChildren
//.........这里部分代码省略.........
开发者ID:rafrombrc,项目名称:gospec,代码行数:101,代码来源:results_test.go

示例9: MatchersSpec

func MatchersSpec(c nanospec.Context) {

	c.Specify("Matcher: Equals", func() {
		c.Specify("strings", func() {
			c.Expect(E("apple", Equals, "apple")).Matches(Passes)
			c.Expect(E("apple", Equals, "orange")).Matches(FailsWithMessage(
				"equals “orange”",
				"does NOT equal “orange”"))
		})
		c.Specify("ints", func() {
			c.Expect(E(42, Equals, 42)).Matches(Passes)
			c.Expect(E(42, Equals, 999)).Matches(Fails)
		})
		c.Specify("structs", func() {
			c.Expect(E(DummyStruct{42, 1}, Equals, DummyStruct{42, 2})).Matches(Passes)
			c.Expect(E(DummyStruct{42, 1}, Equals, DummyStruct{999, 2})).Matches(Fails)
		})
		c.Specify("struct pointers", func() {
			c.Expect(E(&DummyStruct{42, 1}, Equals, &DummyStruct{42, 2})).Matches(Passes)
			c.Expect(E(&DummyStruct{42, 1}, Equals, &DummyStruct{999, 2})).Matches(Fails)
		})
	})

	c.Specify("Matcher: IsSame", func() {
		a1 := new(os.File)
		a2 := a1
		b := new(os.File)

		c.Expect(E(a1, IsSame, a2)).Matches(Passes)
		c.Expect(E(a1, IsSame, b)).Matches(FailsWithMessage(
			fmt.Sprintf("is same as “%v”", b),
			fmt.Sprintf("is NOT same as “%v”", b)))

		c.Specify("cannot compare values, but only pointers", func() {
			c.Expect(E(1, IsSame, b)).Matches(GivesError("type error: expected a pointer, but was “1” of type “int”"))
			c.Expect(E(b, IsSame, 1)).Matches(GivesError("type error: expected a pointer, but was “1” of type “int”"))
		})
	})

	c.Specify("Matcher: IsNil", func() {
		c.Expect(E(nil, IsNil)).Matches(Passes)         // interface value nil
		c.Expect(E((*int)(nil), IsNil)).Matches(Passes) // typed pointer nil inside an interface value
		c.Expect(E(new(int), IsNil)).Matches(Fails)
		c.Expect(E(1, IsNil)).Matches(FailsWithMessage(
			"is <nil>",
			"is NOT <nil>"))
	})

	c.Specify("Matcher: IsTrue", func() {
		c.Expect(E(true, IsTrue)).Matches(Passes)
		c.Expect(E(false, IsTrue)).Matches(FailsWithMessage(
			"is <true>",
			"is NOT <true>"))
	})

	c.Specify("Matcher: IsFalse", func() {
		c.Expect(E(false, IsFalse)).Matches(Passes)
		c.Expect(E(true, IsFalse)).Matches(FailsWithMessage(
			"is <false>",
			"is NOT <false>"))
	})

	c.Specify("Matcher: Satisfies", func() {
		value := 42

		c.Expect(E(value, Satisfies, value < 100)).Matches(Passes)
		c.Expect(E(value, Satisfies, value > 100)).Matches(FailsWithMessage(
			"satisfies the criteria",
			"does NOT satisfy the criteria"))
	})

	c.Specify("Matcher: IsWithin", func() {
		value := float64(3.141)
		pi := float64(math.Pi)

		c.Expect(E(value, IsWithin(0.001), pi)).Matches(Passes)
		c.Expect(E(value, IsWithin(0.0001), pi)).Matches(FailsWithMessage(
			"is within 3.141592653589793 ± 0.0001",
			"is NOT within 3.141592653589793 ± 0.0001"))

		c.Specify("cannot compare ints", func() {
			value := int(3)
			pi := float64(math.Pi)

			c.Expect(E(value, IsWithin(0.001), pi)).Matches(GivesError("type error: expected a float, but was “3” of type “int”"))
			c.Expect(E(pi, IsWithin(0.001), value)).Matches(GivesError("type error: expected a float, but was “3” of type “int”"))
		})
	})

	c.Specify("Matcher: Contains", func() {
		values := []string{"one", "two", "three"}

		c.Expect(E(values, Contains, "one")).Matches(Passes)
		c.Expect(E(values, Contains, "two")).Matches(Passes)
		c.Expect(E(values, Contains, "three")).Matches(Passes)

		c.Expect(E(values, Contains, "four")).Matches(FailsWithMessage(
			"contains “four”",
			"does NOT contain “four”"))
	})
//.........这里部分代码省略.........
开发者ID:rafrombrc,项目名称:gospec,代码行数:101,代码来源:matchers_test.go


注:本文中的github.com/orfjackal/nanospec/go/src/nanospec.Context.Expect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。