CodeSampleX

Beispiel

github.com/google/go-cmp v0.6.0: cmp.Option

Verifiziertes Beispiel für golang github.com/google/go-cmp v0.6.0: cmp.Option. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.

sha256:e251407002e57aae04a73d6f944d710efafd7a02d29bfc46750cdfe5fba050a5

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-15

Fall

HOW
Ziel
verify github.com/google/go-cmp/cmp.Option in pkg:golang/github.com/google/go-cmp@v0.6.0
Pakete
Symbole
  • github.com/google/go-cmp/cmp.Option
Erstellt
2026-09-15T07:26:30Z

Contract

  1. cmp.Options composite slice satisfies cmp.Option and applies all contained options
  2. cmp.Comparer returns a cmp.Option configuring custom equality logic
  3. cmp.Transformer returns a cmp.Option transforming values before equality comparison
  4. cmp.Ignore combined with cmp.FilterPath returns a cmp.Option ignoring specified struct fields
  5. cmp.AllowUnexported returns a cmp.Option allowing comparison of structs with unexported fields

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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/google/go-cmp/cmp.Option in pkg:golang/github.com/google/go-cmp@v0.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/go-cmp@v0.6.0
Demonstrate these symbols/APIs:
  - github.com/google/go-cmp/cmp.Option

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:3e3defc6caa2119936059c1343f7db850899552962db0f0ef46c99fb845cdd42","contract":["cmp.Options composite slice satisfies cmp.Option and applies all contained options","cmp.Comparer returns a cmp.Option configuring custom equality logic","cmp.Transformer returns a cmp.Option transforming values before equality comparison","cmp.Ignore combined with cmp.FilterPath returns a cmp.Option ignoring specified struct fields","cmp.AllowUnexported returns a cmp.Option allowing comparison of structs with unexported fields"],"goal":"verify github.com/google/go-cmp/cmp.Option in pkg:golang/github.com/google/go-cmp@v0.6.0","kind":"HOW","packages":["pkg:golang/github.com/google/go-cmp@v0.6.0"],"schemaVersion":1,"symbols":["github.com/google/go-cmp/cmp.Option"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/go-cmp@v0.6.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/go-cmp@v0.6.0","symbols":["github.com/google/go-cmp/cmp.Option"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22.0

require github.com/google/go-cmp v0.6.0
go.sum
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
main.go
package main

import (
	"fmt"
	"math"
	"strings"

	"github.com/google/go-cmp/cmp"
)

type Coordinate struct {
	Label string
	X     float64
	Y     float64
}

type record struct {
	ID    int
	token string
}

func main() {
	// Example 1: cmp.Transformer to normalize strings during comparison
	caseInsensitive := cmp.Transformer("ToLower", strings.ToLower)

	// Example 2: cmp.Comparer for approximate floating point comparison
	approxCoord := cmp.Comparer(func(a, b float64) bool {
		return math.Abs(a-b) < 1e-4
	})

	// Example 3: cmp.Options combines multiple cmp.Option values into a composite Option
	opts := cmp.Options{
		caseInsensitive,
		approxCoord,
	}

	c1 := Coordinate{Label: "ORIGIN", X: 0.00001, Y: 0.0}
	c2 := Coordinate{Label: "origin", X: 0.0, Y: 0.00001}

	if cmp.Equal(c1, c2, opts) {
		fmt.Println("Coordinates match using cmp.Options composite.")
	}

	// Example 4: cmp.AllowUnexported allows comparing unexported fields
	r1 := record{ID: 1, token: "active"}
	r2 := record{ID: 1, token: "active"}
	allowOpt := cmp.AllowUnexported(record{})
	if cmp.Equal(r1, r2, allowOpt) {
		fmt.Println("Records with unexported fields matched successfully.")
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/go-cmp/cmp.Option in pkg:golang/github.com/google/go-cmp@v0.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/go-cmp@v0.6.0"
  ],
  "symbols": [
    "github.com/google/go-cmp/cmp.Option"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"math"
	"os"
	"strings"

	"github.com/google/go-cmp/cmp"
)

type Coordinate struct {
	Label string
	X     float64
	Y     float64
}

type Payload struct {
	ID        int
	Timestamp int64
}

type secretRecord struct {
	ID    int
	token string
}

func main() {
	// Assertion 1: cmp.Options composite slice satisfies cmp.Option and applies all contained options
	{
		var opt1 cmp.Option = cmp.Transformer("ToLower", strings.ToLower)
		var opt2 cmp.Option = cmp.Comparer(func(a, b float64) bool {
			return math.Abs(a-b) < 1e-3
		})
		var composite cmp.Option = cmp.Options{opt1, opt2}

		c1 := Coordinate{Label: "Alpha", X: 10.0001, Y: 20.0}
		c2 := Coordinate{Label: "ALPHA", X: 10.0002, Y: 20.0}
		c3 := Coordinate{Label: "BETA", X: 10.0001, Y: 20.0}

		if !cmp.Equal(c1, c2, composite) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: composite cmp.Options should match c1 and c2\n")
			os.Exit(1)
		}
		if cmp.Equal(c1, c3, composite) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: composite cmp.Options should differentiate c1 and c3\n")
			os.Exit(1)
		}
	}

	// Assertion 2: cmp.Comparer returns a cmp.Option configuring custom equality logic
	{
		type customNum int
		modCompare := cmp.Comparer(func(x, y customNum) bool {
			return x%10 == y%10
		})

		var opt cmp.Option = modCompare
		if !cmp.Equal(customNum(13), customNum(23), opt) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: 13 and 23 should be equal mod 10\n")
			os.Exit(1)
		}
		if cmp.Equal(customNum(13), customNum(25), opt) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: 13 and 25 should not be equal mod 10\n")
			os.Exit(1)
		}
	}

	// Assertion 3: cmp.Transformer returns a cmp.Option transforming values before equality comparison
	{
		trimAndLower := cmp.Transformer("TrimAndLower", func(s string) string {
			return strings.ToLower(strings.TrimSpace(s))
		})

		var opt cmp.Option = trimAndLower
		s1 := "   GoLang   "
		s2 := "golang"
		s3 := "rust"

		if !cmp.Equal(s1, s2, opt) {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: trimmed lower string should match\n")
			os.Exit(1)
		}
		if cmp.Equal(s1, s3, opt) {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: distinct strings should not match\n")
			os.Exit(1)
		}
	}

	// Assertion 4: cmp.Ignore combined with cmp.FilterPath returns a cmp.Option ignoring specified struct fields
	{
		ignoreTime := cmp.FilterPath(func(p cmp.Path) bool {
			return p.Last().String() == ".Timestamp"
		}, cmp.Ignore())

		var opt cmp.Option = ignoreTime
		p1 := Payload{ID: 101, Timestamp: 1000}
		p2 := Payload{ID: 101, Timestamp: 9999}
		p3 := Payload{ID: 202, Timestamp: 1000}

		if !cmp.Equal(p1, p2, opt) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: payload with different timestamp should match when ignored\n")
			os.Exit(1)
		}
		if cmp.Equal(p1, p3, opt) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: payload with different ID should not match\n")
			os.Exit(1)
		}
	}

	// Assertion 5: cmp.AllowUnexported returns a cmp.Option allowing comparison of structs with unexported fields
	{
		r1 := secretRecord{ID: 1, token: "alpha"}
		r2 := secretRecord{ID: 1, token: "alpha"}
		r3 := secretRecord{ID: 1, token: "beta"}

		var panicked bool
		func() {
			defer func() {
				if r := recover(); r != nil {
					panicked = true
				}
			}()
			_ = cmp.Equal(r1, r2)
		}()

		if !panicked {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: expected panic when comparing unexported fields without AllowUnexported\n")
			os.Exit(1)
		}

		var opt cmp.Option = cmp.AllowUnexported(secretRecord{})
		if !cmp.Equal(r1, r2, opt) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: records should match with AllowUnexported\n")
			os.Exit(1)
		}
		if cmp.Equal(r1, r3, opt) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: different records should not match with AllowUnexported\n")
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

Ursprungs-Seeder

anonym