aboutsummaryrefslogtreecommitdiffstats
path: root/modules/unittestbridge
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2021-11-12 22:36:47 +0800
committerGitHub <noreply@github.com>2021-11-12 22:36:47 +0800
commitdf64fa486555de6f403a795fd16c2e9e1d59e535 (patch)
treeb899e9b9e5d57409b1bf0e3afbd606b6a3900235 /modules/unittestbridge
parent7f802631c54d2e91301158380b273b872d62bd80 (diff)
downloadgitea-df64fa486555de6f403a795fd16c2e9e1d59e535.tar.gz
gitea-df64fa486555de6f403a795fd16c2e9e1d59e535.zip
Decouple unit test code from business code (#17623)
Diffstat (limited to 'modules/unittestbridge')
-rw-r--r--modules/unittestbridge/unittestbridge.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/unittestbridge/unittestbridge.go b/modules/unittestbridge/unittestbridge.go
new file mode 100644
index 0000000000..273cf5e70f
--- /dev/null
+++ b/modules/unittestbridge/unittestbridge.go
@@ -0,0 +1,46 @@
+// Copyright 2021 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package unittestbridge
+
+// Usage: generally, non-unit-test code shouldn't depend on unit test code.
+// However, we have some code like models.CheckConsistencyFor, which need to do some unit test works.
+// Now we can not decouple models.CheckConsistencyFor from unit test code easily (cycle-import reasons).
+// So we introduce this `unit test bridge`:
+// * When a release binary is built, no testing/assert framework would be compiled into the binary, and CheckConsistencyFor won't run unit test related code
+// * When a unit test binary is built, the unit test code will init this bridge, then CheckConsistencyFor can run unit test related code
+//
+// Tester/Assert are intermediate interfaces, they should NOT be used in new code.
+// One day, if CheckConsistencyFor is clean enough, we can remove these intermediate interfaces.
+
+// Tester is the same as TestingT in "stretchr/testify/assert"
+// Tester can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
+type Tester interface {
+ Errorf(format string, args ...interface{})
+}
+
+// Asserter can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies
+type Asserter interface {
+ Tester
+ NoError(err error, msgAndArgs ...interface{}) bool
+ EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool
+ Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool
+ True(value bool, msgAndArgs ...interface{}) bool
+ False(value bool, msgAndArgs ...interface{}) bool
+}
+
+var newAsserterFunc func(t Tester) Asserter
+
+// NewAsserter returns a new asserter, only works in unit test
+func NewAsserter(t Tester) Asserter {
+ if newAsserterFunc == nil {
+ panic("the newAsserterFunc is not set. you can only use assert in unit test.")
+ }
+ return newAsserterFunc(t)
+}
+
+// SetNewAsserterFunc in unit test, the asserter must be set first
+func SetNewAsserterFunc(f func(t Tester) Asserter) {
+ newAsserterFunc = f
+}