From e6347f920d27c29dc1d2e754fb31711e53e2f277 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Thu, 29 Oct 2020 12:10:25 +0900 Subject: [PATCH 01/30] feat: add mock document Signed-off-by: hlts2 --- .github/workflows/reviewdog-markdown.yml | 2 +- docs/contributing/coding-style.md | 95 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/.github/workflows/reviewdog-markdown.yml b/.github/workflows/reviewdog-markdown.yml index 7e19adbc10..a640d4c860 100644 --- a/.github/workflows/reviewdog-markdown.yml +++ b/.github/workflows/reviewdog-markdown.yml @@ -36,4 +36,4 @@ jobs: level: warning language: en-US disabled_rules: "DOUBLE_PUNCTUATION,WORD_CONTAINS_UNDERSCORE,ARROWS,CURRENCY,DASH_RULE,EN_QUOTES" - disabled_categories: "TYPOS" + disabled_categories: "TYPOS,TYPOGRAPHY" diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index f48c0a9922..41e9a9a758 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -965,3 +965,98 @@ We do not suggest to modify the generated code other than the `tests` variable, type want struct { // generated test code ``` +### Mock + +In Vald, we use a lot of external library, there are a lot of dependencies between libraries. + +As a result, the complexity of the test has increased, and it has become more difficult to determine whether or not to mock dependent objects. + +#### Cases where you can use mock + +When a dependent object has the following feature, you can decide to mock the dependent. + +- Incomplete implementation +- IO + - Network access, disk operation, etc. +- Hardware dependent + - CPU, memory usage, disk IO, etc. +- Difficult to create error of dependent object (when we will write error test case) +- Difficult to initialize + - Random number and time, file IO initialization, environment dependent, etc. +- Test result may change + - Cases where the implementation and test code are not changed but the test result changes + - System call dependent + +#### How to create mock + +1. Basic mock example. + + For example, we decided to mock the following implementation `Encoder`. + + ```go + package json + + type Encoder struct { + Encode(interface{}) ([]byte, error) + } + + ``` + + ```go + type encoder struct { + encoder json.Encoder + } + + func (e *encoder) Encode(obj interface{}) ([]byte, error) { + return e.encoder.Encode(obj) + } + ``` + + And there are 4 rules of mock code. + + - File location is same pacakge as mock target. + - File name is `〇〇_mock.go` + - Structure name are `Mock{Interface name}` + - Method injected from test code is `{Method name}Func` + + The following is an example of mock implementation: + + ```go + package json + + type MockEncoder struct { + EncoderFunc func(interface{}) ([]byte, error) + } + + func (m *MockEncoder) Encode(obj interface{}) ([]byte, error) { + return m.EncodeFunc(obj) + } + + ``` + + The following is an example implementation of test code to create the mock object and mock the implementation. + + ```go + + tests := []test { + { + name: "returns (byte{}, nil) when encode success" + fields: fields { + encoding: &json.MockEncoder { + EncoderFunc: func(interface{}) ([]byte, error) { + return []byte{}, nil + }, + }, + } + ...... + } + } + + ...... + + ``` + +#### Mock rick + +- We don't know if it's really correct. +- We don't notice the change in dependency. \ No newline at end of file From 6bb578bc05ac8f0587423c940435f9cd89aa5e59 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 17:59:26 +0900 Subject: [PATCH 02/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 41e9a9a758..4dd8869a0f 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1056,7 +1056,7 @@ When a dependent object has the following feature, you can decide to mock the de ``` -#### Mock rick +#### Risk - We don't know if it's really correct. -- We don't notice the change in dependency. \ No newline at end of file +- We don't notice the change in dependency. From 6de917cda00c31f04580c0a652b4c91ddf2957c8 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:01:21 +0900 Subject: [PATCH 03/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 4dd8869a0f..edf030d1af 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -983,7 +983,7 @@ When a dependent object has the following feature, you can decide to mock the de - Difficult to create error of dependent object (when we will write error test case) - Difficult to initialize - Random number and time, file IO initialization, environment dependent, etc. -- Test result may change +- Test result may change in runtime - Cases where the implementation and test code are not changed but the test result changes - System call dependent From 3b09a7df64af96197a75bb4f587dfc4c295ddac4 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:01:32 +0900 Subject: [PATCH 04/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index edf030d1af..681876579f 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -987,7 +987,7 @@ When a dependent object has the following feature, you can decide to mock the de - Cases where the implementation and test code are not changed but the test result changes - System call dependent -#### How to create mock +#### Implementation 1. Basic mock example. From 4a1d831654aa92173b3dbcde56153cce924f3f32 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:01:46 +0900 Subject: [PATCH 05/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 681876579f..e0d22081b2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -985,7 +985,7 @@ When a dependent object has the following feature, you can decide to mock the de - Random number and time, file IO initialization, environment dependent, etc. - Test result may change in runtime - Cases where the implementation and test code are not changed but the test result changes - - System call dependent + - System call inside implementation #### Implementation From 9f86b64fdfda7f782ac3bd5d89ec855d302a592e Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:01:57 +0900 Subject: [PATCH 06/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index e0d22081b2..e2892da315 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -971,7 +971,7 @@ In Vald, we use a lot of external library, there are a lot of dependencies betwe As a result, the complexity of the test has increased, and it has become more difficult to determine whether or not to mock dependent objects. -#### Cases where you can use mock +#### Condition When a dependent object has the following feature, you can decide to mock the dependent. From 895a93b8cbbfc3186339487ee2ac39d589efde8a Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:02:08 +0900 Subject: [PATCH 07/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index e2892da315..b71da97785 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1016,7 +1016,7 @@ When a dependent object has the following feature, you can decide to mock the de - File location is same pacakge as mock target. - File name is `〇〇_mock.go` - - Structure name are `Mock{Interface name}` + - Struct name is `Mock{Interface name}` - Method injected from test code is `{Method name}Func` The following is an example of mock implementation: From a8794fa2da283334be3df8fcff2caeca5553a4fe Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:02:18 +0900 Subject: [PATCH 08/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index b71da97785..5d246d1702 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1037,7 +1037,6 @@ When a dependent object has the following feature, you can decide to mock the de The following is an example implementation of test code to create the mock object and mock the implementation. ```go - tests := []test { { name: "returns (byte{}, nil) when encode success" From fca212106990bc87ea0e02d33b414e6ca5cb302b Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Wed, 4 Nov 2020 18:03:03 +0900 Subject: [PATCH 09/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 5d246d1702..7d7e97595c 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1014,7 +1014,7 @@ When a dependent object has the following feature, you can decide to mock the de And there are 4 rules of mock code. - - File location is same pacakge as mock target. + - Same package as the mock target. - File name is `〇〇_mock.go` - Struct name is `Mock{Interface name}` - Method injected from test code is `{Method name}Func` From 7a2f6546d905a9cc124172fb031152ee7bed1dc0 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Wed, 4 Nov 2020 18:12:34 +0900 Subject: [PATCH 10/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 7d7e97595c..1fa7b6cd90 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1012,13 +1012,6 @@ When a dependent object has the following feature, you can decide to mock the de } ``` - And there are 4 rules of mock code. - - - Same package as the mock target. - - File name is `〇〇_mock.go` - - Struct name is `Mock{Interface name}` - - Method injected from test code is `{Method name}Func` - The following is an example of mock implementation: ```go @@ -1055,7 +1048,16 @@ When a dependent object has the following feature, you can decide to mock the de ``` +#### Rules of mock code + +There are 4 rules of mock code. + +- Same package as the mock target. +- File name is `〇〇_mock.go` +- Struct name is `Mock{Interface name}` +- Method injected from test code is `{Method name}Func` + #### Risk -- We don't know if it's really correct. +- We don't know if the dependent object is correct. - We don't notice the change in dependency. From f7ff539229852449387f04b4773d32ceb81fc428 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Thu, 5 Nov 2020 09:35:41 +0900 Subject: [PATCH 11/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 1fa7b6cd90..9d727e5469 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1055,7 +1055,6 @@ There are 4 rules of mock code. - Same package as the mock target. - File name is `〇〇_mock.go` - Struct name is `Mock{Interface name}` -- Method injected from test code is `{Method name}Func` #### Risk From c5f9b2374d8230f1ec919aa7b06bfd08f6e73fbc Mon Sep 17 00:00:00 2001 From: hlts2 Date: Thu, 5 Nov 2020 10:29:50 +0900 Subject: [PATCH 12/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 9d727e5469..77edfeea39 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -989,6 +989,12 @@ When a dependent object has the following feature, you can decide to mock the de #### Implementation +The implementation of mock object should be: + +- Same package as the mock target. +- File name is `〇〇_mock.go` +- Struct name is `Mock{Interface name} + 1. Basic mock example. For example, we decided to mock the following implementation `Encoder`. @@ -996,7 +1002,7 @@ When a dependent object has the following feature, you can decide to mock the de ```go package json - type Encoder struct { + type Encoder interface { Encode(interface{}) ([]byte, error) } @@ -1048,14 +1054,6 @@ When a dependent object has the following feature, you can decide to mock the de ``` -#### Rules of mock code - -There are 4 rules of mock code. - -- Same package as the mock target. -- File name is `〇〇_mock.go` -- Struct name is `Mock{Interface name}` - #### Risk - We don't know if the dependent object is correct. From 8b96fab215c4988415f9c7813eaa0f31844a3891 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Thu, 5 Nov 2020 11:51:31 +0900 Subject: [PATCH 13/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 77edfeea39..74b8d9d325 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -1056,5 +1056,7 @@ The implementation of mock object should be: #### Risk -- We don't know if the dependent object is correct. -- We don't notice the change in dependency. +Before applying mock to the object, you should be aware of the following risks. + +- We do not know if the dependent object is correctly implemented or not. +- We cannot notice the change in dependency. From 136b4823a03f85a5410ad70320110dda5eba0602 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Thu, 5 Nov 2020 11:52:15 +0900 Subject: [PATCH 14/30] Update docs/contributing/coding-style.md Co-authored-by: Kevin Diu --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 74b8d9d325..4f73eb818e 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -973,7 +973,7 @@ As a result, the complexity of the test has increased, and it has become more di #### Condition -When a dependent object has the following feature, you can decide to mock the dependent. +When a dependent object has the following factor, you can decide to mock the dependent object. - Incomplete implementation - IO From 0a730d7b90745536e798cf45f07bf2b3ec01123f Mon Sep 17 00:00:00 2001 From: hlts2 Date: Thu, 5 Nov 2020 11:54:10 +0900 Subject: [PATCH 15/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 4f73eb818e..eae5d54420 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -987,6 +987,13 @@ When a dependent object has the following factor, you can decide to mock the dep - Cases where the implementation and test code are not changed but the test result changes - System call inside implementation +#### Risk + +Before applying mock to the object, you should be aware of the following risks. + +- We do not know if the dependent object is correctly implemented or not. +- We cannot notice the change in dependency. + #### Implementation The implementation of mock object should be: @@ -1052,11 +1059,4 @@ The implementation of mock object should be: ...... - ``` - -#### Risk - -Before applying mock to the object, you should be aware of the following risks. - -- We do not know if the dependent object is correctly implemented or not. -- We cannot notice the change in dependency. + ``` \ No newline at end of file From 1ff68ce8034c87beeb518f4c889796757ce4a975 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:13:26 +0900 Subject: [PATCH 16/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index eae5d54420..9da7e26f9a 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -965,7 +965,7 @@ We do not suggest to modify the generated code other than the `tests` variable, type want struct { // generated test code ``` -### Mock +### Using Mock In Vald, we use a lot of external library, there are a lot of dependencies between libraries. @@ -1059,4 +1059,4 @@ The implementation of mock object should be: ...... - ``` \ No newline at end of file + ``` From bc5cd3f6fa5a88d2ce92110f413040dcf06153a4 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:15:00 +0900 Subject: [PATCH 17/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 9da7e26f9a..91142acf5e 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -969,7 +969,7 @@ We do not suggest to modify the generated code other than the `tests` variable, In Vald, we use a lot of external library, there are a lot of dependencies between libraries. -As a result, the complexity of the test has increased, and it has become more difficult to determine whether or not to mock dependent objects. +As a result, due to the more complexity of the test, it has become more difficult to determine whether or not to mock dependencies. #### Condition From 9bd038e828511e769498bb654e7b962e652aceb6 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:16:25 +0900 Subject: [PATCH 18/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 91142acf5e..6fb6b2f915 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -982,7 +982,7 @@ When a dependent object has the following factor, you can decide to mock the dep - CPU, memory usage, disk IO, etc. - Difficult to create error of dependent object (when we will write error test case) - Difficult to initialize - - Random number and time, file IO initialization, environment dependent, etc. + - e. g. Random number and time, file IO initialization, environment dependent, or etc. - Test result may change in runtime - Cases where the implementation and test code are not changed but the test result changes - System call inside implementation From f162a472371fe90beefa4f7e1e8c5af434d71c97 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:16:52 +0900 Subject: [PATCH 19/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 6fb6b2f915..a1fd0d0dd3 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -979,7 +979,7 @@ When a dependent object has the following factor, you can decide to mock the dep - IO - Network access, disk operation, etc. - Hardware dependent - - CPU, memory usage, disk IO, etc. + - e. g. CPU, Memory usage, disk I/O, or etc. - Difficult to create error of dependent object (when we will write error test case) - Difficult to initialize - e. g. Random number and time, file IO initialization, environment dependent, or etc. From 08adf78bad315c45f20c2a4112c6b78bd87bb500 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:17:43 +0900 Subject: [PATCH 20/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index a1fd0d0dd3..217b2814a4 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -976,8 +976,8 @@ As a result, due to the more complexity of the test, it has become more difficul When a dependent object has the following factor, you can decide to mock the dependent object. - Incomplete implementation -- IO - - Network access, disk operation, etc. +- I/O + - e. g. Network access, disk operation, or etc. - Hardware dependent - e. g. CPU, Memory usage, disk I/O, or etc. - Difficult to create error of dependent object (when we will write error test case) From abd53e6b4bee5b5be344aacd4f7bc407b04a7bca Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:21:10 +0900 Subject: [PATCH 21/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 217b2814a4..cda0e5a366 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -983,9 +983,8 @@ When a dependent object has the following factor, you can decide to mock the dep - Difficult to create error of dependent object (when we will write error test case) - Difficult to initialize - e. g. Random number and time, file IO initialization, environment dependent, or etc. -- Test result may change in runtime - - Cases where the implementation and test code are not changed but the test result changes - - System call inside implementation +- Test result may change in each runtime + - e. g. Only test result may change in each runtime, System call inside implementation, or etc. #### Risk From 593e8a3d72ead06db855e08fb5b9b843422558b9 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:21:58 +0900 Subject: [PATCH 22/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index cda0e5a366..af76c8f55d 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -998,7 +998,7 @@ Before applying mock to the object, you should be aware of the following risks. The implementation of mock object should be: - Same package as the mock target. -- File name is `〇〇_mock.go` +- File name is `xxx_mock.go` - Struct name is `Mock{Interface name} 1. Basic mock example. From a6e1b3fed8af2fce1d7cdc140e7ee1cd183befca Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:23:11 +0900 Subject: [PATCH 23/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index af76c8f55d..2968a9ea06 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -973,7 +973,7 @@ As a result, due to the more complexity of the test, it has become more difficul #### Condition -When a dependent object has the following factor, you can decide to mock the dependent object. +When dependencies have the following factor, you can decide to mock the dependencies. - Incomplete implementation - I/O From 39631fd106b22fcfd97b2b7e7b6784cafe7868bf Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:23:25 +0900 Subject: [PATCH 24/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 2968a9ea06..8beb987d6a 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -991,7 +991,7 @@ When dependencies have the following factor, you can decide to mock the dependen Before applying mock to the object, you should be aware of the following risks. - We do not know if the dependent object is correctly implemented or not. -- We cannot notice the change in dependency. +- We cannot notice the change in dependencies. #### Implementation From a3a51d9768465e616f7847af661a0b8f7b3bc5c0 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 16:25:44 +0900 Subject: [PATCH 25/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 8beb987d6a..ac3d714cc2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -990,7 +990,7 @@ When dependencies have the following factor, you can decide to mock the dependen Before applying mock to the object, you should be aware of the following risks. -- We do not know if the dependent object is correctly implemented or not. +- We **do not** know whether the dependencies is correctly implemented or not. - We cannot notice the change in dependencies. #### Implementation From 966674b651d2c901c942210dd705d98d6c949d6b Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 10 Nov 2020 17:19:00 +0900 Subject: [PATCH 26/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 85 ++++++++++++++----------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index ac3d714cc2..d6fe1e5029 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -999,63 +999,56 @@ The implementation of mock object should be: - Same package as the mock target. - File name is `xxx_mock.go` -- Struct name is `Mock{Interface name} +- Struct name is `Mock{Interface name}` -1. Basic mock example. +For example, we decided to mock the following implementation `Encoder`. - For example, we decided to mock the following implementation `Encoder`. - - ```go - package json - - type Encoder interface { - Encode(interface{}) ([]byte, error) - } - - ``` +```go +package json - ```go - type encoder struct { - encoder json.Encoder - } +type Encoder interface { + Encode(interface{}) ([]byte, error) +} +``` - func (e *encoder) Encode(obj interface{}) ([]byte, error) { - return e.encoder.Encode(obj) - } - ``` +```go +type encoder struct { + encoder json.Encoder +} - The following is an example of mock implementation: +func (e *encoder) Encode(obj interface{}) ([]byte, error) { + return e.encoder.Encode(obj) +} +``` - ```go - package json +The following is an example of mock implementation: - type MockEncoder struct { - EncoderFunc func(interface{}) ([]byte, error) - } +```go +package json - func (m *MockEncoder) Encode(obj interface{}) ([]byte, error) { - return m.EncodeFunc(obj) - } +type MockEncoder struct { + EncoderFunc func(interface{}) ([]byte, error) +} - ``` +func (m *MockEncoder) Encode(obj interface{}) ([]byte, error) { + return m.EncodeFunc(obj) +} +``` - The following is an example implementation of test code to create the mock object and mock the implementation. +The following is an example implementation of test code to create the mock object and mock the implementation. - ```go - tests := []test { - { - name: "returns (byte{}, nil) when encode success" - fields: fields { - encoding: &json.MockEncoder { - EncoderFunc: func(interface{}) ([]byte, error) { - return []byte{}, nil - }, +```go +tests := []test { + { + name: "returns (byte{}, nil) when encode success" + fields: fields { + encoding: &json.MockEncoder { + EncoderFunc: func(interface{}) ([]byte, error) { + return []byte{}, nil }, - } - ...... + }, } + ...... } - - ...... - - ``` +} +``` From 7a259962a7292da995e5122a41eec983cbf02289 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 10 Nov 2020 17:20:57 +0900 Subject: [PATCH 27/30] fix: delete unncessary sentence Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index d6fe1e5029..c1871b87cf 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -980,7 +980,7 @@ When dependencies have the following factor, you can decide to mock the dependen - e. g. Network access, disk operation, or etc. - Hardware dependent - e. g. CPU, Memory usage, disk I/O, or etc. -- Difficult to create error of dependent object (when we will write error test case) +- Difficult to create error of dependencies - Difficult to initialize - e. g. Random number and time, file IO initialization, environment dependent, or etc. - Test result may change in each runtime From 2e63b68887553ade1304651721be21cb21145344 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 10 Nov 2020 17:44:25 +0900 Subject: [PATCH 28/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index c1871b87cf..70ce7cdbc2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -977,25 +977,25 @@ When dependencies have the following factor, you can decide to mock the dependen - Incomplete implementation - I/O - - e. g. Network access, disk operation, or etc. + - e.g. Network access, disk operation, or etc. - Hardware dependent - - e. g. CPU, Memory usage, disk I/O, or etc. + - e.g. CPU, Memory usage, disk I/O, or etc. - Difficult to create error of dependencies - Difficult to initialize - - e. g. Random number and time, file IO initialization, environment dependent, or etc. + - e.g. Random number and time, file I/O initialization, environment dependent, or etc. - Test result may change in each runtime - - e. g. Only test result may change in each runtime, System call inside implementation, or etc. + - e.g. Only test result may change in each runtime, System call inside implementation, etc. #### Risk Before applying mock to the object, you should be aware of the following risks. -- We **do not** know whether the dependencies is correctly implemented or not. -- We cannot notice the change in dependencies. +- We **do not** know whether the dependencies are correctly implemented or not. +- We cannot notice the changes in dependencies. #### Implementation -The implementation of mock object should be: +The implementation of the mock object should be: - Same package as the mock target. - File name is `xxx_mock.go` From e6eea30de00447e18eb57bd50a90cafc054b3f40 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 10 Nov 2020 17:49:05 +0900 Subject: [PATCH 29/30] fix: apply suggestion Signed-off-by: hlts2 --- docs/contributing/coding-style.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 70ce7cdbc2..50e16d9d37 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -977,12 +977,12 @@ When dependencies have the following factor, you can decide to mock the dependen - Incomplete implementation - I/O - - e.g. Network access, disk operation, or etc. + - e.g. Network access, disk operation, etc. - Hardware dependent - - e.g. CPU, Memory usage, disk I/O, or etc. + - e.g. CPU, Memory usage, disk I/O, etc. - Difficult to create error of dependencies - Difficult to initialize - - e.g. Random number and time, file I/O initialization, environment dependent, or etc. + - e.g. Random number and time, file I/O initialization, environment dependent, etc. - Test result may change in each runtime - e.g. Only test result may change in each runtime, System call inside implementation, etc. From 43f3d6d12fb2b55627bc66b103bf126a19cd4b54 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Tue, 10 Nov 2020 18:11:51 +0900 Subject: [PATCH 30/30] Update docs/contributing/coding-style.md Co-authored-by: Kiichiro YUKAWA --- docs/contributing/coding-style.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 50e16d9d37..00ed743ef8 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -967,7 +967,7 @@ We do not suggest to modify the generated code other than the `tests` variable, ``` ### Using Mock -In Vald, we use a lot of external library, there are a lot of dependencies between libraries. +In Vald, we use a lot of external libraries, there are a lot of dependencies between libraries. As a result, due to the more complexity of the test, it has become more difficult to determine whether or not to mock dependencies.