Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coding style for mock #806

Merged
merged 30 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e6347f9
feat: add mock document
hlts2 Oct 29, 2020
6bb578b
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
6de917c
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
3b09a7d
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
4a1d831
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
9f86b64
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
895a93b
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
a8794fa
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
fca2121
Update docs/contributing/coding-style.md
hlts2 Nov 4, 2020
7a2f654
fix: apply suggestion
hlts2 Nov 4, 2020
f7ff539
fix: apply suggestion
hlts2 Nov 5, 2020
c5f9b23
fix: apply suggestion
hlts2 Nov 5, 2020
8b96fab
Update docs/contributing/coding-style.md
hlts2 Nov 5, 2020
136b482
Update docs/contributing/coding-style.md
hlts2 Nov 5, 2020
0a730d7
fix: apply suggestion
hlts2 Nov 5, 2020
1ff68ce
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
bc5cd3f
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
9bd038e
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
f162a47
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
08adf78
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
abd53e6
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
593e8a3
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
a6e1b3f
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
39631fd
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
a3a51d9
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
966674b
fix: apply suggestion
hlts2 Nov 10, 2020
7a25996
fix: delete unncessary sentence
hlts2 Nov 10, 2020
2e63b68
fix: apply suggestion
hlts2 Nov 10, 2020
e6eea30
fix: apply suggestion
hlts2 Nov 10, 2020
43f3d6d
Update docs/contributing/coding-style.md
hlts2 Nov 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/reviewdog-markdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
95 changes: 95 additions & 0 deletions docs/contributing/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

In Vald, we use a lot of external library, there are a lot of dependencies between libraries.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

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.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

#### Condition

When a dependent object has the following feature, you can decide to mock the dependent.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

- Incomplete implementation
- IO
- Network access, disk operation, etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Hardware dependent
- CPU, memory usage, disk IO, etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Difficult to create error of dependent object (when we will write error test case)
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- Difficult to initialize
- Random number and time, file IO initialization, environment dependent, etc.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- 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
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

#### Implementation

1. Basic mock example.

For example, we decided to mock the following implementation `Encoder`.

```go
package json

type Encoder struct {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
Encode(interface{}) ([]byte, error)
}

```

```go
type encoder struct {
encoder json.Encoder
}

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
func (e *encoder) Encode(obj interface{}) ([]byte, error) {
kevindiu marked this conversation as resolved.
Show resolved Hide resolved
return e.encoder.Encode(obj)
}
```

And there are 4 rules of mock code.

- File location is same pacakge as mock target.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- File name is `〇〇_mock.go`
- Struct name is `Mock{Interface name}`
- Method injected from test code is `{Method name}Func`
hlts2 marked this conversation as resolved.
Show resolved Hide resolved

The following is an example of mock implementation:

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
```go
package json

type MockEncoder struct {
EncoderFunc func(interface{}) ([]byte, error)
}

hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
func (m *MockEncoder) Encode(obj interface{}) ([]byte, error) {
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
return m.EncodeFunc(obj)
}

```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[LanguageTool] reported by reviewdog 🐶
This sentence does not start with an uppercase letter (UPPERCASE_SENTENCE_START)
Suggestions: Func
Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US
Category: CASING

The following is an example implementation of test code to create the mock object and mock the implementation.

```go

kevindiu marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
tests := []test {
{
name: "returns (byte{}, nil) when encode success"
fields: fields {
encoding: &json.MockEncoder {
EncoderFunc: func(interface{}) ([]byte, error) {
return []byte{}, nil
},
},
}
......
}
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
}
kevindiu marked this conversation as resolved.
Show resolved Hide resolved

......

```

#### Risk

- We don't know if it's really correct.
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
- We don't notice the change in dependency.