Skip to content

Commit

Permalink
refactor: adapt to arikawa v1
Browse files Browse the repository at this point in the history
  • Loading branch information
mavolin authored Jul 30, 2020
2 parents 2d778ee + becd391 commit 66c1af8
Show file tree
Hide file tree
Showing 36 changed files with 505 additions and 439 deletions.
43 changes: 33 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ We not only use [conventional commits](https://www.conventionalcommits.org/en/v1
If you propose a feature, use `feat(PACKAGE_NAME): TITLE`, for a bug replace the `feat` with a `fix`, for `docs` use `docs`, you get the hang.
Other types are `style`, `refactor` and `test`.
If your change is breaking (in the semantic versioning sense) add an exclamation mark behind the scope, e.g. `feat(package)!: title`.
This however, is not necessary, if this is the first release, as all changes would be considered breaking.

If your issue proposes changes to multiple packages, or you don't know which package is affected, leave the `(PACKAGE_NAME)` part out, e.g. `feat: that one thing that's missing`.

Expand All @@ -35,9 +34,22 @@ Please make small, thoughtful commits, a commit like `feat: add xy` with 20 new
Please use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) for your contributions, once you get the hang of it, you'll see that they are pretty easy to use.
Just have a look at the [quick start guide](https://www.conventionalcommits.org/en/v1.0.0/#summary) on their website.
The scope is typically the package name, but for non-go files appropriate scopes may also be: `git`, `README` or `go.mod`.
If none match what you are doing, just think of something.
The types we use are: `fix`, `feat`, `docs`, `style`, `refactor` and `test`.
Breaking changes are signaled using a `!`, and not by a footer.

##### Types
We use the following types:

- ci: changes to our CI configuration files and scripts (scopes take the name of the file)
- docs: changes to the documentation
- feat: a new feature
- fix: a bug fix
- perf: an improvement to perfomance
- refactor: a code change that neither fixes a bug nor adds a feature
- style: a change that does not affect the meaning of the code
- test: a change to an existing test or a new test

##### Breaking Changes

Breaking changes must have a `!` after the type/scope and a `BREAKING CHANGE:` footer.

### Fixing a Bug

Expand All @@ -47,7 +59,7 @@ This of course only applies if the function is testable.
### Code Style

Make sure all code is `gofmt -s`'ed and passes the golangci-lint checks.
If your code fails a lint task, but the way you did it is justified, add an exception to the `.golangci.yml` file with a comment explaining, why this exception necessary.
If your code fails a lint task, but the way you did it is justified add an exception to the `.golangci.yml` file with a comment explaining, why this exception is necessary.

### Testing

Expand All @@ -68,7 +80,7 @@ func TestSomething(t *testing.T) {
expect := 2 // we declare the expected values second
// use expectX, expectY etc. for multiple returns

a.needed(s) // prerequisites needed before invoking the function, e.g. a http mock
needed(s) // prerequisites needed before invoking the function, e.g. a http mock

actual := Something(s, qty) // get the value computed by the tested function
// again, use actualX, actualY etc. for multiple returns
Expand All @@ -79,16 +91,16 @@ func TestSomething(t *testing.T) {

#### Table-Driven Tests

If there is a single table, it should be called `cases`, multiple use the name `<type>Cases`, e.g. `successCases` and `failureCases`, for tests that test the output at valid input (a success case), and those that aim to provoke an error (a failure case) and therefore work different from a success case.
The same goes if there is a table that's testing only a portion of a function and multiple non-table-driven tests in addition.
If there is a single table, it should be called `cases`, multiple use the name `<type>Cases`, e.g. `successCases` and `failureCases`, for tests that test the output for a valid input (a success case), and those that aim to provoke an error (a failure case) and therefore work different from a success case.
The same goes if there is a table that's only testing a portion of a function and multiple non-table-driven tests in addition.

The struct used in tables is always anonymous.
The structs used in tables are always anonymous.

Every sub-test including table driven ones should have a name that clearly shows what is being done.
For table-driven tests this name is either obtained from a `name` field or computed using the other fields in the table entry.

Every case in a table should run in its own subtest (`t.Run`).
Additionally, if there are multiple tables, each table has its own subtest, in which he calls his cases:
Additionally, if there are multiple tables, each table has its own subtest, in which it calls its cases:

```
TestSomething
Expand All @@ -110,6 +122,17 @@ TestSomething
additionalTest-1
```

```
TestSomething
successCases
successCase-1
successCase-2
failureCases
failureCase-1
failureCase-2
additionalNonTableDriveFailureTest-1
```

### Opening a Pull Request

When opening a pull request, merge against `develop` and use the title of the issue as PR title.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ That's it!

### Meta Requests

Besides, regular calls to the API, you can also mock requests for metadata, i.e. images such as guild icons (`Mocker.GuildIconUrl`).
In order for this to work, you must use the `http.Client` found in the `Mocker` struct, so that the mock server will be called, instead of Discord.
Besides regular calls to the API you can also mock requests for metadata, i.e. images such as guild icons (`Mocker.GuildIcon`).
In order for this to work, you need to use the `http.Client` found in the `Mocker` struct, so that the mock server will be called, instead of Discord.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/mavolin/dismock
go 1.13

require (
github.com/diamondburned/arikawa v0.9.2
github.com/stretchr/testify v1.5.1
github.com/diamondburned/arikawa v1.0.1
github.com/stretchr/testify v1.6.1
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/diamondburned/arikawa v0.9.2 h1:G4W9HpC5NaVnGJKT+J9s/0l1s1Lolv2ogzQqx37KQXc=
github.com/diamondburned/arikawa v0.9.2/go.mod h1:nIhVIatzTQhPUa7NB8w4koG1RF9gYbpAr8Fj8sKq660=
github.com/diamondburned/arikawa v1.0.1 h1:ygctwd8NIwtkzykZRvGZwAbZAAOx7oO/VyNSxyiRt3E=
github.com/diamondburned/arikawa v1.0.1/go.mod h1:nIhVIatzTQhPUa7NB8w4koG1RF9gYbpAr8Fj8sKq660=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
Expand All @@ -12,8 +12,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzhtallWRSm4Q0d09pL6XbQtU=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -27,5 +27,5 @@ golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omN
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions internal/sanitize/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func AuditLog(al discord.AuditLog) discord.AuditLog {
// AuditLogEntry sanitizes an discord.AuditLogEntry.
//
// This function will sanitize AuditLogEntry.ID and AuditLogEntry.UserID.
func AuditLogEntry(e discord.AuditLogEntry, id, userID discord.Snowflake) discord.AuditLogEntry {
if e.ID <= 0 {
func AuditLogEntry(e discord.AuditLogEntry, id discord.AuditLogEntryID, userID discord.UserID) discord.AuditLogEntry {
if e.ID == 0 {
e.ID = id
}

if e.UserID <= 0 {
if e.UserID == 0 {
e.UserID = userID
}

Expand Down
12 changes: 7 additions & 5 deletions internal/sanitize/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "github.com/diamondburned/arikawa/discord"
// Channel sanitizes a discord.Channel.
//
// This function will sanitize Channel.ID.
func Channel(c discord.Channel, id discord.Snowflake) discord.Channel {
if c.ID <= 0 {
func Channel(c discord.Channel, id discord.ChannelID) discord.Channel {
if c.ID == 0 {
c.ID = id
}

Expand All @@ -17,12 +17,14 @@ func Channel(c discord.Channel, id discord.Snowflake) discord.Channel {
//
// This function will sanitize Message.ID, Message.ChannelID and
// Message.Author.ID.
func Message(m discord.Message, id, channelID, authorID discord.Snowflake) discord.Message {
if m.ID <= 0 {
func Message(
m discord.Message, id discord.MessageID, channelID discord.ChannelID, authorID discord.UserID,
) discord.Message {
if m.ID == 0 {
m.ID = id
}

if m.ChannelID <= 0 {
if m.ChannelID == 0 {
m.ChannelID = channelID
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sanitize/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "github.com/diamondburned/arikawa/discord"
// Emoji sanitizes an discord.Emoji.
//
// This function will sanitize Emoji.ID and Emoji.User.ID.
func Emoji(e discord.Emoji, id, userID discord.Snowflake) discord.Emoji {
if e.ID <= 0 {
func Emoji(e discord.Emoji, id discord.EmojiID, userID discord.UserID) discord.Emoji {
if e.ID == 0 {
e.ID = id
}

Expand Down
35 changes: 20 additions & 15 deletions internal/sanitize/guild.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import "github.com/diamondburned/arikawa/discord"
//
// This function will sanitize Guild.ID, Guild.OwnerID, Guild.RulesChannelID,
// Guild.PublicUpdatesChannelID, Guild.Emojis.ID and Guild.Roles.ID.
func Guild(g discord.Guild, id, ownerID, rulesChannelID, publicUpdatesChannelID discord.Snowflake) discord.Guild {
if g.ID <= 0 {
func Guild(
g discord.Guild, id discord.GuildID, ownerID discord.UserID,
rulesChannelID, publicUpdatesChannelID discord.ChannelID,
) discord.Guild {
if g.ID == 0 {
g.ID = id
}

if g.OwnerID <= 0 {
if g.OwnerID == 0 {
g.OwnerID = ownerID
}

if g.RulesChannelID <= 0 {
if g.RulesChannelID == 0 {
g.RulesChannelID = rulesChannelID
}

if g.PublicUpdatesChannelID <= 0 {
if g.PublicUpdatesChannelID == 0 {
g.PublicUpdatesChannelID = publicUpdatesChannelID
}

Expand All @@ -38,8 +41,8 @@ func Guild(g discord.Guild, id, ownerID, rulesChannelID, publicUpdatesChannelID
//
// This function will sanitize GuildPreview.ID, GuildPreview.Emojis.ID and
// GuildPreview.Emojis.User.ID.
func GuildPreview(p discord.GuildPreview, id discord.Snowflake) discord.GuildPreview {
if p.ID <= 0 {
func GuildPreview(p discord.GuildPreview, id discord.GuildID) discord.GuildPreview {
if p.ID == 0 {
p.ID = id
}

Expand All @@ -53,8 +56,8 @@ func GuildPreview(p discord.GuildPreview, id discord.Snowflake) discord.GuildPre
// Role sanitizes a discord.Role.
//
// This function will sanitize Role.ID.
func Role(r discord.Role, id discord.Snowflake) discord.Role {
if r.ID <= 0 {
func Role(r discord.Role, id discord.RoleID) discord.Role {
if r.ID == 0 {
r.ID = id
}

Expand All @@ -64,8 +67,8 @@ func Role(r discord.Role, id discord.Snowflake) discord.Role {
// Member sanitizes a discord.Member.
//
// This function will sanitize Member.User.ID.
func Member(m discord.Member, userID discord.Snowflake) discord.Member {
if m.User.ID <= 0 {
func Member(m discord.Member, userID discord.UserID) discord.Member {
if m.User.ID == 0 {
m.User.ID = userID
}

Expand All @@ -75,7 +78,7 @@ func Member(m discord.Member, userID discord.Snowflake) discord.Member {
// Ban sanitizes a discord.Ban.
//
// This function will sanitize Ban.User.ID.
func Ban(b discord.Ban, userID discord.Snowflake) discord.Ban {
func Ban(b discord.Ban, userID discord.UserID) discord.Ban {
b.User = User(b.User, userID)

return b
Expand All @@ -85,12 +88,14 @@ func Ban(b discord.Ban, userID discord.Snowflake) discord.Ban {
//
// This function will sanitize Integration.ID, Integration.RoleID and
// Integration.User.ID.
func Integration(i discord.Integration, id, roleID, userID discord.Snowflake) discord.Integration {
if i.ID <= 0 {
func Integration(
i discord.Integration, id discord.IntegrationID, roleID discord.RoleID, userID discord.UserID,
) discord.Integration {
if i.ID == 0 {
i.ID = id
}

if i.RoleID <= 0 {
if i.RoleID == 0 {
i.RoleID = roleID
}

Expand Down
4 changes: 2 additions & 2 deletions internal/sanitize/guild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ func TestMember(t *testing.T) {

func TestBan(t *testing.T) {
var (
userID discord.Snowflake = 123
ban = discord.Ban{
userID discord.UserID = 123
ban = discord.Ban{
User: discord.User{
ID: 0,
},
Expand Down
5 changes: 3 additions & 2 deletions internal/sanitize/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import "github.com/diamondburned/arikawa/discord"
// Invite.Guild.Emojis.ID and Invite.Guild.Roles.ID, Invite.Channel.ID and
// Invite.Inviter.ID.
func Invite(
i discord.Invite, guildID, ownerID, rulesChannelID, publicUpdatesChannelID, channelID, inviterID,
targetID discord.Snowflake,
i discord.Invite, guildID discord.GuildID, ownerID discord.UserID,
rulesChannelID, publicUpdatesChannelID, channelID discord.ChannelID, inviterID discord.UserID,
targetID discord.UserID,
) discord.Invite {
if i.Guild != nil {
*i.Guild = Guild(*i.Guild, guildID, ownerID, rulesChannelID, publicUpdatesChannelID)
Expand Down
4 changes: 2 additions & 2 deletions internal/sanitize/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "github.com/diamondburned/arikawa/discord"
// User sanitizes a discord.User.
//
// This function will sanitize User.ID.
func User(u discord.User, id discord.Snowflake) discord.User {
if u.ID <= 0 {
func User(u discord.User, id discord.UserID) discord.User {
if u.ID == 0 {
u.ID = id
}

Expand Down
8 changes: 5 additions & 3 deletions internal/sanitize/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import "github.com/diamondburned/arikawa/discord"
//
// This function will sanitize Webhook.ID, Webhook.User.ID and
// Webhook.ChannelID.
func Webhook(w discord.Webhook, id, userID, channelID discord.Snowflake) discord.Webhook {
if w.ID <= 0 {
func Webhook(
w discord.Webhook, id discord.WebhookID, userID discord.UserID, channelID discord.ChannelID,
) discord.Webhook {
if w.ID == 0 {
w.ID = id
}

w.User = User(w.User, userID)

if w.ChannelID <= 0 {
if w.ChannelID == 0 {
w.ChannelID = channelID
}

Expand Down
Loading

0 comments on commit 66c1af8

Please sign in to comment.