Skip to content

Commit

Permalink
Merge pull request #9 from Storytel/add-proto-matcher
Browse files Browse the repository at this point in the history
Add proto matcher
  • Loading branch information
niklasembretsen authored Oct 13, 2023
2 parents 105ba3b + f1c7e21 commit a53c27f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 46 deletions.
49 changes: 33 additions & 16 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
version: 2.1
jobs:
build:
docker:
- image: circleci/golang:1.15
environment:
GO111MODULE: "on"
steps:
- checkout
- run: go install github.com/mgechev/revive
- run: revive -config revive.toml
- run: go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
- run:
name: Upload Code Coverage Report
command: |
go get github.com/mattn/goveralls
goveralls -coverprofile=coverage.txt

orbs:
toolkit: storytel/toolkit@1

parameters:
go-version:
type: string
default: "1.19"

workflows:
ci:
jobs:
- toolkit/go-ci:
context: gcloud-storytel-build
version: <<pipeline.parameters.go-version>>
revive: false
test_package: ./...
filters:
branches:
ignore: main
name: ci-pr-branch
sonarqube_mode: pr

- toolkit/go-ci:
context: gcloud-storytel-build
version: <<pipeline.parameters.go-version>>
revive: false
test_package: ./...
filters:
branches:
only: main
name: ci-base-branch
sonarqube_mode: base
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.15
require (
github.com/golang/mock v1.2.1-0.20190311213431-837231f7bb37
github.com/stretchr/testify v1.3.0
google.golang.org/protobuf v1.31.0
)
9 changes: 8 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/golang/mock v1.2.1-0.20190311213431-837231f7bb37 h1:X/VEzS/ARNAir3sehGjZ/MT66dlHovEDk3Cr8BDm4oA=
github.com/golang/mock v1.2.1-0.20190311213431-837231f7bb37/go.mod h1:L3bP22mxdfCUHSUVMs+SPJMx55FrxQew7MSXT11Q86g=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190221204921-83362c3779f5/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
32 changes: 32 additions & 0 deletions proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package matchers

import (
"encoding/json"

"github.com/golang/mock/gomock"
"google.golang.org/protobuf/proto"
)

type ProtoMatcher struct {
data proto.Message
}

func (p *ProtoMatcher) Matches(x interface{}) bool {
pbx, ok := x.(proto.Message)
if !ok {
return false
}
return proto.Equal(p.data, pbx)
}

func (p *ProtoMatcher) String() string {
data, err := json.Marshal(p.data)
if err != nil {
return "<unknown>"
}
return string(data)
}

func Proto(x proto.Message) gomock.Matcher {
return &ProtoMatcher{data: x}
}
42 changes: 42 additions & 0 deletions proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package matchers_test

import (
matchers "github.com/Storytel/gomock-matchers"
"github.com/stretchr/testify/assert"
"testing"

"google.golang.org/protobuf/types/known/structpb"
)

func TestProtoMatcher(t *testing.T) {
data := &structpb.Struct{
Fields: map[string]*structpb.Value{
"Id": {
Kind: &structpb.Value_StringValue{
StringValue: "1234",
},
},
},
}

assert := assert.New(t)
m := matchers.Proto(data)
assert.True(m.Matches(data))
}

func TestProtoMatcherString(t *testing.T) {
assert := assert.New(t)
data := &structpb.Struct{
Fields: map[string]*structpb.Value{
"abc": {
Kind: &structpb.Value_StringValue{
StringValue: "123",
},
},
},
}

expected := "{\"abc\":\"123\"}"
m := matchers.Proto(data)
assert.Equal(expected, m.String())
}
29 changes: 0 additions & 29 deletions revive.toml

This file was deleted.

0 comments on commit a53c27f

Please sign in to comment.