测试表 Go 泛型函数
在 Go 1.18 中,程序员可以利用其新的泛型功能。在探索这一新功能时,开发人员在执行表测试时可能会遇到挑战。本次讨论探讨了这样一个挑战,特别是在使用表数据测试泛型函数的情况下。
在表测试期间尝试使用不同类型的参数实例化泛型函数时会出现此问题。为了解决这个问题,开发人员通常会重新声明每个函数的测试逻辑,如以下代码片段所示:
package main import ( "testing" "github.com/stretchr/testify/assert" ) type Item interface { int | string } type store[T Item] map[int64]T // add adds an Item to the map if the id of the Item isn't present already func (s store[T]) add(key int64, val T) { _, exists := s[key] if exists { return } s[key] = val } func TestStore(t *testing.T) { t.Run("ints", testInt) t.Run("strings", testString) } type testCase[T Item] struct { name string start store[T] key int64 val T expected store[T] } func testString(t *testing.T) { t.Parallel() tests := []testCase[string]{ { name: "empty map", start: store[string]{}, key: 123, val: "test", expected: store[string]{ 123: "test", }, }, { name: "existing key", start: store[string]{ 123: "test", }, key: 123, val: "newVal", expected: store[string]{ 123: "test", }, }, } for _, tc := range tests { t.Run(tc.name, runTestCase(tc)) } } func testInt(t *testing.T) { t.Parallel() tests := []testCase[int]{ { name: "empty map", start: store[int]{}, key: 123, val: 456, expected: store[int]{ 123: 456, }, }, { name: "existing key", start: store[int]{ 123: 456, }, key: 123, val: 999, expected: store[int]{ 123: 456, }, }, } for _, tc := range tests { t.Run(tc.name, runTestCase(tc)) } } func runTestCase[T Item](tc testCase[T]) func(t *testing.T) { return func(t *testing.T) { tc.start.add(tc.key, tc.val) assert.Equal(t, tc.start, tc.expected) } }
但是,这种方法需要为每个函数提供冗余的测试逻辑。泛型类型的本质在于它们能够处理任意类型,并且约束确保这些类型支持相同的操作。
与其过度测试不同的类型,更谨慎的做法是只专注于测试这些类型使用运算符时表现出不同的行为。例如,“ ”运算符对于数字(求和)和字符串(连接)具有不同的含义,或者“”运算符对数字(更大/更小)和字符串(字典顺序)有不同的解释。
为了进一步说明此问题,开发人员应参考用户尝试使用泛型函数执行表测试的类似讨论。
以上是我们如何有效地对具有不同类型参数的泛型函数进行表测试?的详细内容。更多信息请关注PHP中文网其他相关文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)