CodeSampleX

示例

github.com/stretchr/testify v1.7.0: suite.Suite

已验证示例 — golang github.com/stretchr/testify v1.7.0: suite.Suite. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: suite.Suite stores and returns the…

sha256:2c18e083d87475a51aab28226fb02ab1f866ae91a9959382c65155692ad91142

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-06

案例

HOW
目标
verify github.com/stretchr/testify/suite.Suite in pkg:golang/github.com/stretchr/testify@v1.7.0
包
符号
  • github.com/stretchr/testify/suite.Suite
环境
go 1.26.6
创建时间
2026-09-06T06:51:13Z

契约

  1. suite.Suite stores and returns the active testing context via SetT and T
  2. suite.Suite initializes assert and require helpers bound to the testing context upon SetT
  3. suite.Suite enables direct assertion calls through embedded assert.Assertions
  4. suite.Suite.Run executes a subtest with an isolated child context and restores the parent context
  5. suite.Run executes all exported Test methods on a type embedding suite.Suite

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract_test.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/stretchr/testify/suite.Suite in pkg:golang/github.com/stretchr/testify@v1.7.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/stretchr/testify@v1.7.0
Demonstrate these symbols/APIs:
  - github.com/stretchr/testify/suite.Suite

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:19013c199bf18a2447e1ff945e03c5aea99ae0671091af9e4cd2630a96d4c614","contract":["suite.Suite stores and returns the active testing context via SetT and T","suite.Suite initializes assert and require helpers bound to the testing context upon SetT","suite.Suite enables direct assertion calls through embedded assert.Assertions","suite.Suite.Run executes a subtest with an isolated child context and restores the parent context","suite.Run executes all exported Test methods on a type embedding suite.Suite"],"goal":"verify github.com/stretchr/testify/suite.Suite in pkg:golang/github.com/stretchr/testify@v1.7.0","kind":"HOW","packages":["pkg:golang/github.com/stretchr/testify@v1.7.0"],"schemaVersion":1,"symbols":["github.com/stretchr/testify/suite.Suite"]},"contractCommand":["go","test","-v","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/stretchr/testify@v1.7.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/stretchr/testify@v1.7.0","symbols":["github.com/stretchr/testify/suite.Suite"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/stretchr/testify v1.7.0

require (
	github.com/davecgh/go-spew v1.1.0 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
main.go
package main

import (
	"fmt"

	"github.com/stretchr/testify/suite"
)

// Counter manages a simple integer value with basic operations.
type Counter struct {
	value int
}

// NewCounter creates a new Counter initialized with a base value.
func NewCounter(initial int) *Counter {
	return &Counter{value: initial}
}

// Increment increments the counter by step.
func (c *Counter) Increment(step int) int {
	c.value += step
	return c.value
}

// Value returns the current counter value.
func (c *Counter) Value() int {
	return c.value
}

// CounterSuite demonstrates test organization using testify's suite.Suite.
type CounterSuite struct {
	suite.Suite
	counter *Counter
}

// SetupTest prepares fresh state before each test in the suite.
func (s *CounterSuite) SetupTest() {
	s.counter = NewCounter(10)
}

// TestIncrement verifies that Increment increases the stored value.
func (s *CounterSuite) TestIncrement() {
	result := s.counter.Increment(5)
	s.Equal(15, result)
	s.Equal(15, s.counter.Value())
}

// TestAccessors verifies Assert and Require accessors on the embedded suite.Suite.
func (s *CounterSuite) TestAccessors() {
	s.Assert().NotNil(s.counter)
	s.Require().Equal(10, s.counter.Value())
}

func main() {
	counter := NewCounter(0)
	counter.Increment(1)
	fmt.Printf("Counter initialized with value: %d\n", counter.Value())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/stretchr/testify/suite.Suite in pkg:golang/github.com/stretchr/testify@v1.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/stretchr/testify@v1.7.0"
  ],
  "symbols": [
    "github.com/stretchr/testify/suite.Suite"
  ]
}
test/contract_test.go
package test

import (
	"testing"

	"github.com/stretchr/testify/suite"
)

type ExecutionSuite struct {
	suite.Suite
	firstRun  bool
	secondRun bool
	helperRun bool
}

func (s *ExecutionSuite) TestFirst() {
	s.firstRun = true
	s.Equal("alpha", "alpha")
}

func (s *ExecutionSuite) TestSecond() {
	s.secondRun = true
	s.NotNil(s.T())
}

func (s *ExecutionSuite) HelperMethod() {
	s.helperRun = true
}

func TestContract(t *testing.T) {
	// Assertion 1: suite.Suite stores and returns the active testing context via SetT and T
	{
		s := new(suite.Suite)
		if s.T() != nil {
			t.Fatalf("assertion 1 failed: initial T() must be nil")
		}
		s.SetT(t)
		if s.T() != t {
			t.Fatalf("assertion 1 failed: T() must return context set by SetT")
		}
		s.SetT(nil)
		if s.T() != nil {
			t.Fatalf("assertion 1 failed: T() must return nil after SetT(nil)")
		}
		s.SetT(t)
	}

	// Assertion 2: suite.Suite initializes assert and require helpers bound to the testing context upon SetT
	{
		s := new(suite.Suite)
		s.SetT(t)
		if s.Assertions == nil {
			t.Fatalf("assertion 2 failed: embedded Assertions must not be nil after SetT")
		}
		if s.Assert() == nil {
			t.Fatalf("assertion 2 failed: Assert() must not be nil")
		}
		if s.Require() == nil {
			t.Fatalf("assertion 2 failed: Require() must not be nil")
		}
		if !s.Assert().True(true) {
			t.Fatalf("assertion 2 failed: Assert().True failed")
		}
	}

	// Assertion 3: suite.Suite enables direct assertion calls through embedded assert.Assertions
	{
		s := new(suite.Suite)
		s.SetT(t)
		if !s.Equal(100, 100) {
			t.Fatalf("assertion 3 failed: s.Equal failed")
		}
		if !s.NotEqual(100, 200) {
			t.Fatalf("assertion 3 failed: s.NotEqual failed")
		}
		if !s.True(true) {
			t.Fatalf("assertion 3 failed: s.True failed")
		}
		if !s.False(false) {
			t.Fatalf("assertion 3 failed: s.False failed")
		}
		if !s.Nil(nil) {
			t.Fatalf("assertion 3 failed: s.Nil failed")
		}
		if !s.NotNil(s.T()) {
			t.Fatalf("assertion 3 failed: s.NotNil failed")
		}
		if !s.NoError(nil) {
			t.Fatalf("assertion 3 failed: s.NoError failed")
		}
		if !s.Len([]int{1, 2, 3}, 3) {
			t.Fatalf("assertion 3 failed: s.Len failed")
		}
	}

	// Assertion 4: suite.Suite.Run executes a subtest with an isolated child context and restores the parent context
	{
		s := new(suite.Suite)
		s.SetT(t)
		parentT := s.T()

		subtestExecuted := false
		var childT *testing.T

		ok := s.Run("ChildSubtest", func() {
			subtestExecuted = true
			childT = s.T()
			if childT == nil {
				t.Fatalf("assertion 4 failed: child context must not be nil")
			}
			if childT == parentT {
				t.Fatalf("assertion 4 failed: child context must differ from parent context")
			}
			s.Equal("nested", "nested")
		})

		if !ok || !subtestExecuted {
			t.Fatalf("assertion 4 failed: subtest execution did not complete successfully")
		}
		if s.T() != parentT {
			t.Fatalf("assertion 4 failed: parent context must be restored after subtest completion")
		}
	}

	// Assertion 5: suite.Run executes all exported Test methods on a type embedding suite.Suite
	{
		execSuite := new(ExecutionSuite)
		suite.Run(t, execSuite)

		if !execSuite.firstRun {
			t.Fatalf("assertion 5 failed: TestFirst was not executed")
		}
		if !execSuite.secondRun {
			t.Fatalf("assertion 5 failed: TestSecond was not executed")
		}
		if execSuite.helperRun {
			t.Fatalf("assertion 5 failed: non-test HelperMethod should not be executed")
		}
	}
}

原始种子者

匿名