├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── config.yml │ └── feature_request.md ├── dependabot.yaml └── workflows │ ├── release.yaml │ └── test.yaml ├── .gitignore ├── .goreleaser.yml ├── AUTHORS ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── MAINTAINERS ├── README.md ├── bazel ├── .gitignore ├── BUILD.bazel ├── MODULE.bazel ├── WORKSPACE ├── go.mod ├── go.sum ├── rules │ ├── BUILD.bazel │ ├── gomock.bzl │ └── tools.go └── tests │ ├── README.rst │ ├── reflective │ ├── BUILD.bazel │ ├── client.go │ └── client_test.go │ ├── source │ ├── BUILD.bazel │ ├── client.go │ ├── client_test.go │ └── client_wrapper.go │ └── source_with_importpath │ ├── BUILD.bazel │ ├── client.go │ └── client_test.go ├── ci ├── check_panic_handling.sh └── test.sh ├── go.mod ├── go.sum ├── gomock ├── call.go ├── call_test.go ├── callset.go ├── callset_test.go ├── controller.go ├── controller_test.go ├── doc.go ├── example_test.go ├── internal │ └── mock_gomock │ │ └── mock_matcher.go ├── matchers.go ├── matchers_test.go ├── mock_test.go ├── overridable_controller_test.go └── string.go ├── mockgen ├── deprecated.go ├── generic.go ├── gob.go ├── gob_test.go ├── internal │ └── tests │ │ ├── add_generate_directive │ │ ├── import.go │ │ └── mock.go │ │ ├── alias │ │ ├── interfaces.go │ │ ├── mock │ │ │ ├── interfaces.go │ │ │ └── interfaces_test.go │ │ └── subpkg │ │ │ └── interfaces.go │ │ ├── aux_imports_embedded_interface │ │ ├── README.md │ │ ├── bugreport.go │ │ ├── bugreport_mock.go │ │ ├── bugreport_test.go │ │ └── faux │ │ │ └── faux.go │ │ ├── build_constraint │ │ ├── input.go │ │ └── mock.go │ │ ├── build_flags │ │ ├── directive.go │ │ ├── interfaces_1.go │ │ ├── interfaces_2.go │ │ ├── mock1 │ │ │ └── interfaces_mock.go │ │ └── mock2 │ │ │ └── interfaces_mock.go │ │ ├── const_array_length │ │ ├── input.go │ │ └── mock.go │ │ ├── copyright_file │ │ ├── input.go │ │ ├── mock.go │ │ └── mock_copyright_header │ │ ├── custom_package_name │ │ ├── README.md │ │ ├── client │ │ │ └── v1 │ │ │ │ └── client.go │ │ ├── greeter │ │ │ ├── greeter.go │ │ │ ├── greeter_mock_test.go │ │ │ └── greeter_test.go │ │ └── validator │ │ │ └── validate.go │ │ ├── defined_import_local_name │ │ ├── input.go │ │ └── mock.go │ │ ├── dot_imports │ │ ├── input.go │ │ └── mock.go │ │ ├── empty_interface │ │ ├── input.go │ │ └── mock.go │ │ ├── exclude │ │ ├── interfaces.go │ │ └── mock.go │ │ ├── extra_import │ │ ├── import.go │ │ └── mock.go │ │ ├── generated_identifier_conflict │ │ ├── README.md │ │ ├── bugreport.go │ │ ├── bugreport_mock.go │ │ └── bugreport_test.go │ │ ├── generics │ │ ├── external.go │ │ ├── generics.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── other │ │ │ └── other.go │ │ ├── package_mode │ │ │ ├── mock_external_mock.go │ │ │ └── mock_test.go │ │ └── source │ │ │ ├── mock_external_mock.go │ │ │ ├── mock_external_test.go │ │ │ └── mock_generics_mock.go │ │ ├── import_aliased │ │ ├── source.go │ │ └── source_mock.go │ │ ├── import_collision │ │ ├── internalpackage │ │ │ └── foo.go │ │ └── p2 │ │ │ ├── mocks │ │ │ └── mocks.go │ │ │ └── p2.go │ │ ├── import_embedded_interface │ │ ├── bugreport.go │ │ ├── bugreport_mock.go │ │ ├── bugreport_test.go │ │ ├── ersatz │ │ │ └── ersatz.go │ │ ├── faux │ │ │ ├── conflict.go │ │ │ └── faux.go │ │ ├── foo.go │ │ ├── net.go │ │ ├── net_mock.go │ │ ├── net_test.go │ │ └── other │ │ │ ├── ersatz │ │ │ └── ersatz.go │ │ │ └── log │ │ │ └── log.go │ │ ├── import_source │ │ ├── README.md │ │ ├── definition │ │ │ ├── source.go │ │ │ └── source_mock.go │ │ └── source_mock.go │ │ ├── internal_pkg │ │ ├── generate.go │ │ └── subdir │ │ │ └── internal │ │ │ └── pkg │ │ │ ├── input.go │ │ │ ├── reflect_output │ │ │ └── mock.go │ │ │ └── source_output │ │ │ └── mock.go │ │ ├── missing_import │ │ ├── output │ │ │ └── source_mock.go │ │ └── source │ │ │ └── source.go │ │ ├── mock_in_test_package │ │ ├── README.md │ │ ├── mock_test.go │ │ └── user.go │ │ ├── mock_name │ │ ├── mocks.go │ │ ├── mocks │ │ │ ├── post_service.go │ │ │ └── user_service.go │ │ ├── mocks_test.go │ │ ├── post │ │ │ └── service.go │ │ └── user │ │ │ └── service.go │ │ ├── overlapping_methods │ │ ├── interfaces.go │ │ ├── mock.go │ │ ├── overlap.go │ │ └── overlap_test.go │ │ ├── package_comment │ │ ├── input.go │ │ └── mock.go │ │ ├── package_mode │ │ ├── cars │ │ │ └── cars.go │ │ ├── fuel │ │ │ └── fuel.go │ │ ├── interfaces.go │ │ └── mock │ │ │ ├── interfaces.go │ │ │ └── interfaces_test.go │ │ ├── panicing_test │ │ ├── mock_test.go │ │ ├── panic.go │ │ └── panic_test.go │ │ ├── parenthesized_parameter_type │ │ ├── README.md │ │ ├── input.go │ │ └── mock.go │ │ ├── performance │ │ └── big_interface │ │ │ └── big_interface.go │ │ ├── sanitization │ │ ├── any │ │ │ └── any.go │ │ ├── interface.go │ │ ├── mockout │ │ │ └── mock.go │ │ └── sanitization_test.go │ │ ├── self_package │ │ ├── mock.go │ │ └── types.go │ │ ├── test_package │ │ ├── foo.go │ │ ├── mock_test.go │ │ └── user_test.go │ │ ├── typed │ │ ├── bugreport.go │ │ ├── bugreport_mock.go │ │ ├── bugreport_test.go │ │ ├── external.go │ │ ├── faux │ │ │ └── faux.go │ │ ├── generics.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── other │ │ │ └── other.go │ │ └── source │ │ │ ├── mock_external_test.go │ │ │ └── mock_generics_test.go │ │ ├── typed_inorder │ │ ├── input.go │ │ ├── input_test.go │ │ └── mock.go │ │ ├── unexported_method │ │ ├── README.md │ │ ├── bugreport.go │ │ ├── bugreport_mock.go │ │ └── bugreport_test.go │ │ ├── vendor_dep │ │ ├── README.md │ │ ├── doc.go │ │ ├── mock.go │ │ ├── source_mock_package │ │ │ └── mock.go │ │ └── vendor_dep.go │ │ └── vendor_pkg │ │ ├── README.md │ │ ├── doc.go │ │ └── mock.go ├── mockgen.go ├── mockgen_test.go ├── model │ ├── model.go │ └── model_test.go ├── package_mode.go ├── package_mode_test.go ├── parse.go ├── parse_test.go └── version.go ├── sample ├── README.md ├── concurrent │ ├── README.md │ ├── concurrent.go │ ├── concurrent_test.go │ └── mock │ │ └── concurrent_mock.go ├── imp1 │ └── imp1.go ├── imp2 │ └── imp2.go ├── imp3 │ └── imp3.go ├── imp4 │ └── imp4.go ├── mock_user_test.go ├── user.go └── user_test.go └── tools ├── cmd └── extract-changelog │ ├── main.go │ └── main_test.go ├── go.mod └── go.sum /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @codyoss 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Report a bug in gomock 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Actual behavior** A clear and concise description of what the bug is. 11 | 12 | **Expected behavior** A clear and concise description of what you expected to 13 | happen. 14 | 15 | **To Reproduce** Steps to reproduce the behavior 16 | 17 | 1. ... 18 | 2. ... 19 | 20 | **Additional Information** 21 | 22 | - gomock mode (reflect or source): 23 | - gomock version or git ref: 24 | - golang version: 25 | 26 | **Triage Notes for the Maintainers** 27 | 28 | 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | #contact_links: 3 | # - name: GitHub Community Forum 4 | # url: https://github.community/ 5 | # about: Please ask and answer questions here. 6 | # - name: GitHub Security Bug Bounty 7 | # url: https://bounty.github.com/ 8 | # about: Please report security vulnerabilities here. 9 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Propose a new feature for gomock 4 | title: '' 5 | labels: 'type: feature request' 6 | assignees: '' 7 | 8 | --- 9 | 10 | *Requested feature* A clear description of the desired feature and an example of 11 | how it would be used. 12 | 13 | *Why the feature is needed* A clear description of how this feature is not 14 | served by existing functionality in gomock. 15 | 16 | *(Optional) Proposed solution* A clear description of a proposed method for 17 | adding this feature to gomock. 18 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "gomod" 4 | directory: "/tools" 5 | schedule: 6 | interval: "daily" 7 | open-pull-requests-limit: 5 8 | - package-ecosystem: "gomod" 9 | directory: "/mockgen/internal/tests/generics" 10 | schedule: 11 | interval: "daily" 12 | open-pull-requests-limit: 5 13 | - package-ecosystem: "gomod" 14 | directory: "/mockgen/internal/tests/typed" 15 | schedule: 16 | interval: "daily" 17 | open-pull-requests-limit: 5 18 | - package-ecosystem: "github-actions" 19 | directory: "/" 20 | schedule: 21 | interval: "weekly" 22 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | tags: ['v*'] 6 | 7 | permissions: 8 | contents: write 9 | 10 | jobs: 11 | release: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v3 17 | with: 18 | fetch-depth: 0 19 | 20 | - name: Setup Go 21 | uses: actions/setup-go@v4 22 | with: 23 | go-version: 1.23.x 24 | 25 | - name: Prepare release 26 | run: | 27 | set -eou pipefail 28 | make bin/extract-changelog 29 | 30 | # Extract target version number from the git tag and post it to 31 | # GITHUB_ENV to make it accessible via env.VERSION in other steps. 32 | VERSION=${{ github.ref }} 33 | VERSION="${VERSION#refs/tags/}" # refs/tags/v1.2.3 => v1.2.3 34 | echo "VERSION=$VERSION" >> $GITHUB_ENV 35 | 36 | echo "Releasing $VERSION" 37 | 38 | # Place the release notes in changes.v1.2.3.txt to grab them in the 39 | # release step. 40 | echo "Release notes:" 41 | echo "----" 42 | bin/extract-changelog $VERSION | tee changes.$VERSION.txt 43 | echo "----" 44 | 45 | - name: Release 46 | uses: goreleaser/goreleaser-action@v6 47 | with: 48 | distribution: goreleaser 49 | version: latest 50 | args: release --clean --release-notes changes.${{ env.VERSION }}.txt 51 | env: 52 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 53 | GORELEASER_CURRENT_TAG: ${{ env.VERSION }} 54 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Run tests 2 | on: 3 | push: 4 | branches: ['*'] 5 | pull_request: 6 | branches: ['*'] 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | go: 13 | strategy: 14 | matrix: 15 | go-version: [1.23.x, 1.24.x] # oldest version that can build go mock and official supported go versions 16 | os: [ubuntu-latest] 17 | runs-on: ${{ matrix.os }} 18 | steps: 19 | 20 | - name: Checkout code 21 | uses: actions/checkout@v3 22 | 23 | - name: Install Go 24 | uses: actions/setup-go@v4 25 | with: 26 | go-version: ${{ matrix.go-version }} 27 | cache-dependency-path: '**/go.sum' 28 | 29 | - name: Vet and build 30 | run: | 31 | go vet ./... 32 | go build ./... 33 | 34 | - name: Install mockgen 35 | run: | 36 | go install go.uber.org/mock/mockgen 37 | 38 | - name: Run test script 39 | run: | 40 | ./ci/test.sh 41 | ./ci/check_panic_handling.sh 42 | 43 | - name: Run Go Tests 44 | run: | 45 | for i in $(find $PWD -name go.mod ! -path "$PWD/bazel/go.mod"); do 46 | pushd $(dirname $i) 47 | go test ./... 48 | popd 49 | done 50 | 51 | bazel: 52 | strategy: 53 | matrix: 54 | os: 55 | - macos 56 | - ubuntu 57 | runs-on: ${{ matrix.os }}-latest 58 | steps: 59 | - name: Checkout code 60 | uses: actions/checkout@v4 61 | 62 | - name: Setup Bazel 63 | uses: bazel-contrib/setup-bazel@0.10.0 64 | with: 65 | # Avoid downloading Bazel every time. 66 | bazelisk-cache: true 67 | # Store build cache per workflow. 68 | disk-cache: ${{ github.workflow }} 69 | # Share repository cache between workflows. 70 | repository-cache: true 71 | 72 | - name: Run Bazel tests 73 | run: | 74 | cd bazel && bazel test //... 75 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files and binaries from go. 2 | *.[568] 3 | 4 | # Library files. 5 | *.a 6 | 7 | # Any file prefixed by an underscore. 8 | */_* 9 | 10 | # System 11 | .*.swp 12 | .DS_Store 13 | 14 | # The mockgen binary. 15 | mockgen/mockgen 16 | 17 | # Editors 18 | .vscode 19 | .idea 20 | 21 | # vendor directory used for IDEs 22 | /vendor 23 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | # Release automation via GoReleaser (goreleaser.com) 2 | # Requires a valid GITHUB_TOKEN envar prior to running `goreleaser` 3 | # See https://goreleaser.com/environment/ for more info 4 | --- 5 | release: 6 | github: 7 | owner: uber-go 8 | name: mock 9 | 10 | builds: 11 | - binary: mockgen 12 | goos: 13 | - darwin 14 | - windows 15 | - linux 16 | goarch: 17 | - amd64 18 | - arm64 19 | - 386 20 | env: 21 | - CGO_ENABLED=0 22 | - GO111MODULE=on 23 | - GOPROXY=https://proxy.golang.org 24 | - GOSUMDB=sum.golang.org 25 | main: ./mockgen/ 26 | 27 | archives: 28 | - format: tar.gz 29 | wrap_in_directory: true 30 | files: 31 | - LICENSE 32 | - README.md 33 | 34 | checksum: 35 | snapshot: 36 | name_template: "snap-{{ .Commit }}" 37 | 38 | changelog: 39 | sort: asc 40 | filters: 41 | exclude: 42 | - '^docs:' 43 | - '^test:' 44 | - 'README' 45 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the official list of GoMock authors for copyright purposes. 2 | # This file is distinct from the CONTRIBUTORS files. 3 | # See the latter for an explanation. 4 | 5 | # Names should be added to this file as 6 | # Name or Organization 7 | # The email address is not required for organizations. 8 | 9 | # Please keep the list sorted. 10 | 11 | Alex Reece 12 | Google Inc. 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | First off, thank you for taking an interest to contribute to this project! 4 | 5 | > **Tip:** You'll need to sign [Uber's CLA](https://cla-assistant.io/uber-go/mock) 6 | > before we can accept any of your contributions. 7 | > If necessary, a bot will remind 8 | > you to accept the CLA when you open your pull request. 9 | 10 | ## Opening issues 11 | 12 | When opening a [new issue](https://github.com/uber-go/mock/issues/new/choose) 13 | please: 14 | 15 | 1. Make sure there are no other open/closed issues asking/reporting/requesting 16 | the same thing. 17 | 1. Choose one of our provided templates and fill out as much information as 18 | possible. 19 | 20 | ## Opening a Pull Requests 21 | 22 | We gladly accept contributions from the community. Before opening a pull request 23 | please make sure to create an issue for discussion first. This helps us decide 24 | what action should be taken in regard to the issue. 25 | -------------------------------------------------------------------------------- /MAINTAINERS: -------------------------------------------------------------------------------- 1 | # This is the current list of maintainers to Uber's fork of gomock repository. 2 | 3 | Sung Yoon Whang 4 | Ryan Hang 5 | Zhongpeng Lin 6 | -------------------------------------------------------------------------------- /bazel/.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | MODULE.bazel.lock -------------------------------------------------------------------------------- /bazel/BUILD.bazel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uber-go/mock/871d86bb6f8b22d0c4d250ebca1f9d674f6e6d1e/bazel/BUILD.bazel -------------------------------------------------------------------------------- /bazel/MODULE.bazel: -------------------------------------------------------------------------------- 1 | module(name = "bazel_gomock") 2 | 3 | bazel_dep(name = "bazel_skylib", version = "1.7.1") 4 | bazel_dep(name = "rules_go", version = "0.51.0", repo_name = "io_bazel_rules_go") 5 | bazel_dep(name = "gazelle", version = "0.40.0") 6 | 7 | go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk") 8 | 9 | go_sdk.download(version = "1.23.4") 10 | 11 | go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") 12 | go_deps.from_file(go_mod = "//:go.mod") 13 | use_repo( 14 | go_deps, 15 | "org_uber_go_mock", 16 | ) 17 | -------------------------------------------------------------------------------- /bazel/WORKSPACE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uber-go/mock/871d86bb6f8b22d0c4d250ebca1f9d674f6e6d1e/bazel/WORKSPACE -------------------------------------------------------------------------------- /bazel/go.mod: -------------------------------------------------------------------------------- 1 | module go.uber.org/mock/bazel 2 | 3 | go 1.23.0 4 | 5 | require go.uber.org/mock v0.4.0 6 | -------------------------------------------------------------------------------- /bazel/go.sum: -------------------------------------------------------------------------------- 1 | go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= 2 | go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= 3 | -------------------------------------------------------------------------------- /bazel/rules/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library") 2 | 3 | go_library( 4 | name = "rules", 5 | srcs = ["tools.go"], 6 | importpath = "go.uber.org/mock/bazel/rules", 7 | visibility = ["//visibility:public"], 8 | deps = ["@org_uber_go_mock//mockgen/model:go_default_library"], 9 | ) 10 | -------------------------------------------------------------------------------- /bazel/rules/tools.go: -------------------------------------------------------------------------------- 1 | package rules 2 | 3 | import _ "go.uber.org/mock/mockgen/model" 4 | -------------------------------------------------------------------------------- /bazel/tests/README.rst: -------------------------------------------------------------------------------- 1 | gomock 2 | ===================== 3 | 4 | Tests that ensure the gomock rules can be called correctly under different input permutations. 5 | 6 | reflective 7 | ------------------------ 8 | Checks that gomock can be run in "reflective" mode when passed a `GoLibrary` and `interfaces`. 9 | 10 | source 11 | ------------------------ 12 | Checks that gomock can be run in "source" mode when passed a `GoLibrary` and `source`. 13 | 14 | source_with_importpath 15 | ------------------------ 16 | Checks that gomock can be run in "source" mode when passed an `importpath` and `source`. 17 | This test case also demonstrates the circumstance in which `importpath` is necessary to prevent a circular dependency. 18 | -------------------------------------------------------------------------------- /bazel/tests/reflective/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 2 | load("//rules:gomock.bzl", "gomock") 3 | 4 | 5 | go_library( 6 | name = "client", 7 | srcs = [ 8 | "client.go", 9 | ], 10 | importpath = "github.com/bazelbuild/rules_go/gomock/client", 11 | visibility = ["//visibility:public"], 12 | ) 13 | 14 | # Build the mocks using reflective mode (i.e. without passing source) 15 | gomock( 16 | name = "mocks", 17 | out = "client_mock.go", 18 | interfaces = ["Client"], 19 | library = ":client", 20 | package = "client", 21 | visibility = ["//visibility:public"], 22 | ) 23 | 24 | go_test( 25 | name = "client_test", 26 | srcs = [ 27 | "client_mock.go", 28 | "client_test.go", 29 | ], 30 | embed = [":client"], 31 | deps = ["@org_uber_go_mock//gomock"], 32 | ) 33 | -------------------------------------------------------------------------------- /bazel/tests/reflective/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | type Client interface { 4 | Connect(string) int 5 | } 6 | -------------------------------------------------------------------------------- /bazel/tests/reflective/client_test.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | var _ Client = (*MockClient)(nil) 4 | -------------------------------------------------------------------------------- /bazel/tests/source/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 2 | load("//rules:gomock.bzl", "gomock") 3 | 4 | 5 | go_library( 6 | name = "client", 7 | srcs = [ 8 | "client.go", 9 | "client_wrapper.go", 10 | ], 11 | importpath = "go.uber.org/mock/bazel/tests/source", 12 | visibility = ["//visibility:public"], 13 | ) 14 | 15 | gomock( 16 | name = "client_mocks", 17 | out = "client_mock.go", 18 | library = ":client", 19 | package = "client", 20 | source = "client.go", 21 | visibility = ["//visibility:public"], 22 | ) 23 | 24 | gomock( 25 | name = "wrapper_mocks", 26 | out = "wrapper_mock.go", 27 | aux_files = { 28 | "client.go": "go.uber.org/mock/bazel/tests/source", 29 | }, 30 | library = ":client", 31 | package = "client", 32 | self_package = "go.uber.org/mock/bazel/tests/source", 33 | source = "client_wrapper.go", 34 | visibility = ["//visibility:public"], 35 | ) 36 | 37 | go_test( 38 | name = "client_test", 39 | srcs = [ 40 | "client_mock.go", 41 | "client_test.go", 42 | "wrapper_mock.go", 43 | ], 44 | embed = [":client"], 45 | deps = ["@org_uber_go_mock//gomock"], 46 | ) 47 | -------------------------------------------------------------------------------- /bazel/tests/source/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | type Client interface { 4 | Connect(string) int 5 | } 6 | -------------------------------------------------------------------------------- /bazel/tests/source/client_test.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | var _ Client = (*MockClient)(nil) 4 | -------------------------------------------------------------------------------- /bazel/tests/source/client_wrapper.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | type ClientWrapper interface { 4 | Client 5 | Close() error 6 | } 7 | -------------------------------------------------------------------------------- /bazel/tests/source_with_importpath/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 2 | load("//rules:gomock.bzl", "gomock") 3 | 4 | # For this test, the mock is included as part of the library 5 | go_library( 6 | name = "client", 7 | srcs = [ 8 | "client.go", 9 | "client_mock.go", 10 | ], 11 | importpath = "go.uber.org/mock/bazel/tests/source_with_importpath", 12 | visibility = ["//visibility:public"], 13 | deps = [ 14 | "@org_uber_go_mock//gomock", 15 | ], 16 | ) 17 | 18 | # Pass importpath instead of library to the generation step 19 | # Passing library instead of importpath here will cause a circular dependency 20 | gomock( 21 | name = "mocks", 22 | out = "client_mock.go", 23 | package = "client", 24 | source = "client.go", 25 | source_importpath = "go.uber.org/mock/bazel/tests/source_with_importpath", 26 | visibility = ["//visibility:public"], 27 | ) 28 | 29 | # Don't include client_mock.go as a source file, instead use it from the library 30 | go_test( 31 | name = "client_test", 32 | srcs = [ 33 | "client_test.go", 34 | ], 35 | embed = [":client"], 36 | ) 37 | -------------------------------------------------------------------------------- /bazel/tests/source_with_importpath/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | type Client interface { 4 | Connect(string) int 5 | } 6 | -------------------------------------------------------------------------------- /bazel/tests/source_with_importpath/client_test.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | var _ Client = (*MockClient)(nil) 4 | -------------------------------------------------------------------------------- /ci/check_panic_handling.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2010 Google LLC. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # This script is used to ensure that panics are properly reported in tests. 17 | 18 | set -eux 19 | 20 | pushd mockgen/internal/tests/panicing_test 21 | go test -v -tags=panictest -run TestDanger_Panics_Explicit | grep "Danger, Will Robinson!" 22 | go test -v -tags=panictest -run TestDanger_Panics_Implicit | grep "Danger, Will Robinson!" 23 | popd 24 | -------------------------------------------------------------------------------- /ci/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script is used to ensure that the go.mod file is up to date. 3 | 4 | set -euo pipefail 5 | 6 | for i in $(find $PWD -name go.mod); do 7 | pushd $(dirname $i) 8 | go mod tidy 9 | popd 10 | done 11 | 12 | if [ ! -z "$(git status --porcelain)" ]; then 13 | git status 14 | git diff 15 | echo 16 | echo "The go.mod is not up to date." 17 | exit 1 18 | fi 19 | 20 | BASE_DIR="$PWD" 21 | TEMP_DIR=$(mktemp -d) 22 | function cleanup() { 23 | rm -rf "${TEMP_DIR}" 24 | } 25 | trap cleanup EXIT 26 | 27 | cp -r . "${TEMP_DIR}/" 28 | cd $TEMP_DIR 29 | 30 | for i in $(find $PWD -name go.mod); do 31 | pushd $(dirname $i) 32 | go generate ./... 33 | popd 34 | done 35 | 36 | if ! diff -r . "${BASE_DIR}"; then 37 | echo 38 | echo "The generated files aren't up to date." 39 | echo "Update them with the 'go generate ./...' command." 40 | exit 1 41 | fi 42 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module go.uber.org/mock 2 | 3 | go 1.23 4 | 5 | require ( 6 | github.com/stretchr/testify v1.9.0 7 | golang.org/x/mod v0.18.0 8 | golang.org/x/tools v0.22.0 9 | ) 10 | 11 | require ( 12 | github.com/davecgh/go-spew v1.1.1 // indirect 13 | github.com/pmezard/go-difflib v1.0.0 // indirect 14 | github.com/yuin/goldmark v1.4.13 // indirect 15 | golang.org/x/sync v0.7.0 // indirect 16 | gopkg.in/yaml.v3 v3.0.1 // indirect 17 | ) 18 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 6 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 7 | github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= 8 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 9 | golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= 10 | golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 11 | golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= 12 | golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 13 | golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= 14 | golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= 15 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 18 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 19 | -------------------------------------------------------------------------------- /gomock/callset.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package gomock 16 | 17 | import ( 18 | "bytes" 19 | "errors" 20 | "fmt" 21 | "sync" 22 | ) 23 | 24 | // callSet represents a set of expected calls, indexed by receiver and method 25 | // name. 26 | type callSet struct { 27 | // Calls that are still expected. 28 | expected map[callSetKey][]*Call 29 | expectedMu *sync.Mutex 30 | // Calls that have been exhausted. 31 | exhausted map[callSetKey][]*Call 32 | // when set to true, existing call expectations are overridden when new call expectations are made 33 | allowOverride bool 34 | } 35 | 36 | // callSetKey is the key in the maps in callSet 37 | type callSetKey struct { 38 | receiver any 39 | fname string 40 | } 41 | 42 | func newCallSet() *callSet { 43 | return &callSet{ 44 | expected: make(map[callSetKey][]*Call), 45 | expectedMu: &sync.Mutex{}, 46 | exhausted: make(map[callSetKey][]*Call), 47 | } 48 | } 49 | 50 | func newOverridableCallSet() *callSet { 51 | return &callSet{ 52 | expected: make(map[callSetKey][]*Call), 53 | expectedMu: &sync.Mutex{}, 54 | exhausted: make(map[callSetKey][]*Call), 55 | allowOverride: true, 56 | } 57 | } 58 | 59 | // Add adds a new expected call. 60 | func (cs callSet) Add(call *Call) { 61 | key := callSetKey{call.receiver, call.method} 62 | 63 | cs.expectedMu.Lock() 64 | defer cs.expectedMu.Unlock() 65 | 66 | m := cs.expected 67 | if call.exhausted() { 68 | m = cs.exhausted 69 | } 70 | if cs.allowOverride { 71 | m[key] = make([]*Call, 0) 72 | } 73 | 74 | m[key] = append(m[key], call) 75 | } 76 | 77 | // Remove removes an expected call. 78 | func (cs callSet) Remove(call *Call) { 79 | key := callSetKey{call.receiver, call.method} 80 | 81 | cs.expectedMu.Lock() 82 | defer cs.expectedMu.Unlock() 83 | 84 | calls := cs.expected[key] 85 | for i, c := range calls { 86 | if c == call { 87 | // maintain order for remaining calls 88 | cs.expected[key] = append(calls[:i], calls[i+1:]...) 89 | cs.exhausted[key] = append(cs.exhausted[key], call) 90 | break 91 | } 92 | } 93 | } 94 | 95 | // FindMatch searches for a matching call. Returns error with explanation message if no call matched. 96 | func (cs callSet) FindMatch(receiver any, method string, args []any) (*Call, error) { 97 | key := callSetKey{receiver, method} 98 | 99 | cs.expectedMu.Lock() 100 | defer cs.expectedMu.Unlock() 101 | 102 | // Search through the expected calls. 103 | expected := cs.expected[key] 104 | var callsErrors bytes.Buffer 105 | for _, call := range expected { 106 | err := call.matches(args) 107 | if err != nil { 108 | _, _ = fmt.Fprintf(&callsErrors, "\n%v", err) 109 | } else { 110 | return call, nil 111 | } 112 | } 113 | 114 | // If we haven't found a match then search through the exhausted calls so we 115 | // get useful error messages. 116 | exhausted := cs.exhausted[key] 117 | for _, call := range exhausted { 118 | if err := call.matches(args); err != nil { 119 | _, _ = fmt.Fprintf(&callsErrors, "\n%v", err) 120 | continue 121 | } 122 | _, _ = fmt.Fprintf( 123 | &callsErrors, "all expected calls for method %q have been exhausted", method, 124 | ) 125 | } 126 | 127 | if len(expected)+len(exhausted) == 0 { 128 | _, _ = fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method) 129 | } 130 | 131 | return nil, errors.New(callsErrors.String()) 132 | } 133 | 134 | // Failures returns the calls that are not satisfied. 135 | func (cs callSet) Failures() []*Call { 136 | cs.expectedMu.Lock() 137 | defer cs.expectedMu.Unlock() 138 | 139 | failures := make([]*Call, 0, len(cs.expected)) 140 | for _, calls := range cs.expected { 141 | for _, call := range calls { 142 | if !call.satisfied() { 143 | failures = append(failures, call) 144 | } 145 | } 146 | } 147 | return failures 148 | } 149 | 150 | // Satisfied returns true in case all expected calls in this callSet are satisfied. 151 | func (cs callSet) Satisfied() bool { 152 | cs.expectedMu.Lock() 153 | defer cs.expectedMu.Unlock() 154 | 155 | for _, calls := range cs.expected { 156 | for _, call := range calls { 157 | if !call.satisfied() { 158 | return false 159 | } 160 | } 161 | } 162 | 163 | return true 164 | } 165 | -------------------------------------------------------------------------------- /gomock/callset_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package gomock 16 | 17 | import ( 18 | "reflect" 19 | "testing" 20 | ) 21 | 22 | type receiverType struct{} 23 | 24 | func (receiverType) Func() {} 25 | 26 | func TestCallSetAdd(t *testing.T) { 27 | method := "TestMethod" 28 | var receiver any = "TestReceiver" 29 | cs := newCallSet() 30 | 31 | numCalls := 10 32 | for i := 0; i < numCalls; i++ { 33 | cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func))) 34 | } 35 | 36 | call, err := cs.FindMatch(receiver, method, []any{}) 37 | if err != nil { 38 | t.Fatalf("FindMatch: %v", err) 39 | } 40 | if call == nil { 41 | t.Fatalf("FindMatch: Got nil, want non-nil *Call") 42 | } 43 | } 44 | 45 | func TestCallSetAdd_WhenOverridable_ClearsPreviousExpectedAndExhausted(t *testing.T) { 46 | method := "TestMethod" 47 | var receiver any = "TestReceiver" 48 | cs := newOverridableCallSet() 49 | 50 | cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func))) 51 | numExpectedCalls := len(cs.expected[callSetKey{receiver, method}]) 52 | if numExpectedCalls != 1 { 53 | t.Fatalf("Expected 1 expected call in callset, got %d", numExpectedCalls) 54 | } 55 | 56 | cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func))) 57 | newNumExpectedCalls := len(cs.expected[callSetKey{receiver, method}]) 58 | if newNumExpectedCalls != 1 { 59 | t.Fatalf("Expected 1 expected call in callset, got %d", newNumExpectedCalls) 60 | } 61 | } 62 | 63 | func TestCallSetRemove(t *testing.T) { 64 | method := "TestMethod" 65 | var receiver any = "TestReceiver" 66 | 67 | cs := newCallSet() 68 | ourCalls := []*Call{} 69 | 70 | numCalls := 10 71 | for i := 0; i < numCalls; i++ { 72 | // NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls 73 | generatedCall := &Call{receiver: receiver, method: method, numCalls: i} 74 | cs.Add(generatedCall) 75 | ourCalls = append(ourCalls, generatedCall) 76 | } 77 | 78 | // validateOrder validates that the calls in the array are ordered as they were added 79 | validateOrder := func(calls []*Call) { 80 | // lastNum tracks the last `numCalls` (call order) value seen 81 | lastNum := -1 82 | for _, c := range calls { 83 | if lastNum >= c.numCalls { 84 | t.Errorf("found call %d after call %d", c.numCalls, lastNum) 85 | } 86 | lastNum = c.numCalls 87 | } 88 | } 89 | 90 | for _, c := range ourCalls { 91 | validateOrder(cs.expected[callSetKey{receiver, method}]) 92 | cs.Remove(c) 93 | } 94 | } 95 | 96 | func TestCallSetFindMatch(t *testing.T) { 97 | t.Run("call is exhausted", func(t *testing.T) { 98 | cs := newCallSet() 99 | var receiver any = "TestReceiver" 100 | method := "TestMethod" 101 | args := []any{} 102 | 103 | c1 := newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func)) 104 | cs.exhausted = map[callSetKey][]*Call{ 105 | {receiver: receiver, fname: method}: {c1}, 106 | } 107 | 108 | _, err := cs.FindMatch(receiver, method, args) 109 | if err == nil { 110 | t.Fatal("expected error, but was nil") 111 | } 112 | 113 | if err.Error() == "" { 114 | t.Fatal("expected error to have message, but was empty") 115 | } 116 | }) 117 | } 118 | -------------------------------------------------------------------------------- /gomock/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package gomock is a mock framework for Go. 16 | // 17 | // Standard usage: 18 | // 19 | // (1) Define an interface that you wish to mock. 20 | // type MyInterface interface { 21 | // SomeMethod(x int64, y string) 22 | // } 23 | // (2) Use mockgen to generate a mock from the interface. 24 | // (3) Use the mock in a test: 25 | // func TestMyThing(t *testing.T) { 26 | // mockCtrl := gomock.NewController(t) 27 | // mockObj := something.NewMockMyInterface(mockCtrl) 28 | // mockObj.EXPECT().SomeMethod(4, "blah") 29 | // // pass mockObj to a real object and play with it. 30 | // } 31 | // 32 | // By default, expected calls are not enforced to run in any particular order. 33 | // Call order dependency can be enforced by use of InOrder and/or Call.After. 34 | // Call.After can create more varied call order dependencies, but InOrder is 35 | // often more convenient. 36 | // 37 | // The following examples create equivalent call order dependencies. 38 | // 39 | // Example of using Call.After to chain expected call order: 40 | // 41 | // firstCall := mockObj.EXPECT().SomeMethod(1, "first") 42 | // secondCall := mockObj.EXPECT().SomeMethod(2, "second").After(firstCall) 43 | // mockObj.EXPECT().SomeMethod(3, "third").After(secondCall) 44 | // 45 | // Example of using InOrder to declare expected call order: 46 | // 47 | // gomock.InOrder( 48 | // mockObj.EXPECT().SomeMethod(1, "first"), 49 | // mockObj.EXPECT().SomeMethod(2, "second"), 50 | // mockObj.EXPECT().SomeMethod(3, "third"), 51 | // ) 52 | // 53 | // The standard TestReporter most users will pass to `NewController` is a 54 | // `*testing.T` from the context of the test. Note that this will use the 55 | // standard `t.Error` and `t.Fatal` methods to report what happened in the test. 56 | // In some cases this can leave your testing package in a weird state if global 57 | // state is used since `t.Fatal` is like calling panic in the middle of a 58 | // function. In these cases it is recommended that you pass in your own 59 | // `TestReporter`. 60 | package gomock 61 | -------------------------------------------------------------------------------- /gomock/example_test.go: -------------------------------------------------------------------------------- 1 | package gomock_test 2 | 3 | //go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | "go.uber.org/mock/gomock" 11 | ) 12 | 13 | type Foo interface { 14 | Bar(string) string 15 | String() string 16 | } 17 | 18 | func ExampleCall_DoAndReturn_latency() { 19 | t := &testing.T{} // provided by test 20 | ctrl := gomock.NewController(t) 21 | mockIndex := NewMockFoo(ctrl) 22 | 23 | mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn( 24 | func(arg string) string { 25 | time.Sleep(1 * time.Millisecond) 26 | return "I'm sleepy" 27 | }, 28 | ) 29 | 30 | r := mockIndex.Bar("foo") 31 | fmt.Println(r) 32 | // Output: I'm sleepy 33 | } 34 | 35 | func ExampleCall_DoAndReturn_captureArguments() { 36 | t := &testing.T{} // provided by test 37 | ctrl := gomock.NewController(t) 38 | mockIndex := NewMockFoo(ctrl) 39 | var s string 40 | 41 | mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn( 42 | func(arg string) any { 43 | s = arg 44 | return "I'm sleepy" 45 | }, 46 | ) 47 | 48 | r := mockIndex.Bar("foo") 49 | fmt.Printf("%s %s", r, s) 50 | // Output: I'm sleepy foo 51 | } 52 | 53 | func ExampleCall_DoAndReturn_withOverridableExpectations() { 54 | t := &testing.T{} // provided by test 55 | ctrl := gomock.NewController(t, gomock.WithOverridableExpectations()) 56 | mockIndex := NewMockFoo(ctrl) 57 | var s string 58 | 59 | mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn( 60 | func(arg string) any { 61 | s = arg 62 | return "I'm sleepy" 63 | }, 64 | ) 65 | 66 | r := mockIndex.Bar("foo") 67 | fmt.Printf("%s %s", r, s) 68 | // Output: I'm sleepy foo 69 | } 70 | -------------------------------------------------------------------------------- /gomock/internal/mock_gomock/mock_matcher.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/gomock (interfaces: Matcher) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination internal/mock_gomock/mock_matcher.go go.uber.org/mock/gomock Matcher 7 | // 8 | 9 | // Package mock_gomock is a generated GoMock package. 10 | package mock_gomock 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockMatcher is a mock of Matcher interface. 19 | type MockMatcher struct { 20 | ctrl *gomock.Controller 21 | recorder *MockMatcherMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockMatcherMockRecorder is the mock recorder for MockMatcher. 26 | type MockMatcherMockRecorder struct { 27 | mock *MockMatcher 28 | } 29 | 30 | // NewMockMatcher creates a new mock instance. 31 | func NewMockMatcher(ctrl *gomock.Controller) *MockMatcher { 32 | mock := &MockMatcher{ctrl: ctrl} 33 | mock.recorder = &MockMatcherMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockMatcher) EXPECT() *MockMatcherMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Matches mocks base method. 43 | func (m *MockMatcher) Matches(x any) bool { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Matches", x) 46 | ret0, _ := ret[0].(bool) 47 | return ret0 48 | } 49 | 50 | // Matches indicates an expected call of Matches. 51 | func (mr *MockMatcherMockRecorder) Matches(x any) *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Matches", reflect.TypeOf((*MockMatcher)(nil).Matches), x) 54 | } 55 | 56 | // String mocks base method. 57 | func (m *MockMatcher) String() string { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "String") 60 | ret0, _ := ret[0].(string) 61 | return ret0 62 | } 63 | 64 | // String indicates an expected call of String. 65 | func (mr *MockMatcherMockRecorder) String() *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "String", reflect.TypeOf((*MockMatcher)(nil).String)) 68 | } 69 | -------------------------------------------------------------------------------- /gomock/mock_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: example_test.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination mock_test.go -package gomock_test -source example_test.go 7 | // 8 | 9 | // Package gomock_test is a generated GoMock package. 10 | package gomock_test 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockFoo is a mock of Foo interface. 19 | type MockFoo struct { 20 | ctrl *gomock.Controller 21 | recorder *MockFooMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockFooMockRecorder is the mock recorder for MockFoo. 26 | type MockFooMockRecorder struct { 27 | mock *MockFoo 28 | } 29 | 30 | // NewMockFoo creates a new mock instance. 31 | func NewMockFoo(ctrl *gomock.Controller) *MockFoo { 32 | mock := &MockFoo{ctrl: ctrl} 33 | mock.recorder = &MockFooMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockFoo) EXPECT() *MockFooMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Bar mocks base method. 43 | func (m *MockFoo) Bar(arg0 string) string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Bar", arg0) 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // Bar indicates an expected call of Bar. 51 | func (mr *MockFooMockRecorder) Bar(arg0 any) *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), arg0) 54 | } 55 | 56 | // String mocks base method. 57 | func (m *MockFoo) String() string { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "String") 60 | ret0, _ := ret[0].(string) 61 | return ret0 62 | } 63 | 64 | // String indicates an expected call of String. 65 | func (mr *MockFooMockRecorder) String() *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "String", reflect.TypeOf((*MockFoo)(nil).String)) 68 | } 69 | -------------------------------------------------------------------------------- /gomock/overridable_controller_test.go: -------------------------------------------------------------------------------- 1 | package gomock_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | ) 8 | 9 | func TestEcho_NoOverride(t *testing.T) { 10 | ctrl := gomock.NewController(t, gomock.WithOverridableExpectations()) 11 | mockIndex := NewMockFoo(ctrl) 12 | 13 | mockIndex.EXPECT().Bar(gomock.Any()).Return("foo") 14 | res := mockIndex.Bar("input") 15 | 16 | if res != "foo" { 17 | t.Fatalf("expected response to equal 'foo', got %s", res) 18 | } 19 | } 20 | 21 | func TestEcho_WithOverride_BaseCase(t *testing.T) { 22 | ctrl := gomock.NewController(t, gomock.WithOverridableExpectations()) 23 | mockIndex := NewMockFoo(ctrl) 24 | 25 | // initial expectation set 26 | mockIndex.EXPECT().Bar(gomock.Any()).Return("foo") 27 | // override 28 | mockIndex.EXPECT().Bar(gomock.Any()).Return("bar") 29 | res := mockIndex.Bar("input") 30 | 31 | if res != "bar" { 32 | t.Fatalf("expected response to equal 'bar', got %s", res) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /gomock/string.go: -------------------------------------------------------------------------------- 1 | package gomock 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | // getString is a safe way to convert a value to a string for printing results 9 | // If the value is a a mock, getString avoids calling the mocked String() method, 10 | // which avoids potential deadlocks 11 | func getString(x any) string { 12 | if isGeneratedMock(x) { 13 | return fmt.Sprintf("%T", x) 14 | } 15 | if s, ok := x.(fmt.Stringer); ok { 16 | return s.String() 17 | } 18 | return fmt.Sprintf("%v", x) 19 | } 20 | 21 | // isGeneratedMock checks if the given type has a "isgomock" field, 22 | // indicating it is a generated mock. 23 | func isGeneratedMock(x any) bool { 24 | typ := reflect.TypeOf(x) 25 | if typ == nil { 26 | return false 27 | } 28 | if typ.Kind() == reflect.Ptr { 29 | typ = typ.Elem() 30 | } 31 | if typ.Kind() != reflect.Struct { 32 | return false 33 | } 34 | _, isgomock := typ.FieldByName("isgomock") 35 | return isgomock 36 | } 37 | -------------------------------------------------------------------------------- /mockgen/deprecated.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | "os" 7 | ) 8 | 9 | const ( 10 | deprecatedFlagProgOnly = "prog_only" 11 | deprecatedFlagExecOnly = "exec_only" 12 | ) 13 | 14 | var ( 15 | _ = flag.Bool("prog_only", false, "DEPRECATED (reflect mode) Only generate the reflection program; write it to stdout and exit.") 16 | _ = flag.String("exec_only", "", "DEPRECATED (reflect mode) If set, execute this reflection program.") 17 | ) 18 | 19 | // notifyAboutDeprecatedFlags prints a warning message for a deprecated flags if they are set. 20 | func notifyAboutDeprecatedFlags() { 21 | const resetColorPostfix = "\033[0m" 22 | logger := initWarningLogger() 23 | 24 | flag.Visit(func(f *flag.Flag) { 25 | switch f.Name { 26 | case deprecatedFlagProgOnly: 27 | logger.Println("The -prog_only flag is deprecated and has no effect.", resetColorPostfix) 28 | case deprecatedFlagExecOnly: 29 | logger.Println("The -exec_only flag is deprecated and has no effect.", resetColorPostfix) 30 | } 31 | }) 32 | } 33 | 34 | func initWarningLogger() *log.Logger { 35 | const ( 36 | yellowColor = "\033[33m" 37 | warningPrefix = yellowColor + "WARNING: " 38 | ) 39 | 40 | return log.New(os.Stdout, warningPrefix, log.Ldate|log.Ltime) 41 | } 42 | -------------------------------------------------------------------------------- /mockgen/generic.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // See the License for the specific language governing permissions and 6 | // limitations under the License. 7 | 8 | package main 9 | 10 | import ( 11 | "errors" 12 | "fmt" 13 | "go/ast" 14 | "go/token" 15 | 16 | "go.uber.org/mock/mockgen/model" 17 | ) 18 | 19 | func getTypeSpecTypeParams(ts *ast.TypeSpec) []*ast.Field { 20 | if ts == nil || ts.TypeParams == nil { 21 | return nil 22 | } 23 | return ts.TypeParams.List 24 | } 25 | 26 | func (p *fileParser) parseGenericType(pkg string, typ ast.Expr, tps map[string]model.Type) (model.Type, error) { 27 | switch v := typ.(type) { 28 | case *ast.IndexExpr: 29 | m, err := p.parseType(pkg, v.X, tps) 30 | if err != nil { 31 | return nil, err 32 | } 33 | nm, ok := m.(*model.NamedType) 34 | if !ok { 35 | return m, nil 36 | } 37 | t, err := p.parseType(pkg, v.Index, tps) 38 | if err != nil { 39 | return nil, err 40 | } 41 | nm.TypeParams = &model.TypeParametersType{TypeParameters: []model.Type{t}} 42 | return m, nil 43 | case *ast.IndexListExpr: 44 | m, err := p.parseType(pkg, v.X, tps) 45 | if err != nil { 46 | return nil, err 47 | } 48 | nm, ok := m.(*model.NamedType) 49 | if !ok { 50 | return m, nil 51 | } 52 | var ts []model.Type 53 | for _, expr := range v.Indices { 54 | t, err := p.parseType(pkg, expr, tps) 55 | if err != nil { 56 | return nil, err 57 | } 58 | ts = append(ts, t) 59 | } 60 | nm.TypeParams = &model.TypeParametersType{TypeParameters: ts} 61 | return m, nil 62 | } 63 | return nil, nil 64 | } 65 | 66 | func (p *fileParser) parseGenericMethod(field *ast.Field, it *namedInterface, iface *model.Interface, pkg string, tps map[string]model.Type) ([]*model.Method, error) { 67 | var indices []ast.Expr 68 | var typ ast.Expr 69 | switch v := field.Type.(type) { 70 | case *ast.IndexExpr: 71 | indices = []ast.Expr{v.Index} 72 | typ = v.X 73 | case *ast.IndexListExpr: 74 | indices = v.Indices 75 | typ = v.X 76 | case *ast.UnaryExpr: 77 | if v.Op == token.TILDE { 78 | return nil, errConstraintInterface 79 | } 80 | return nil, fmt.Errorf("~T may only appear as constraint for %T", field.Type) 81 | case *ast.BinaryExpr: 82 | if v.Op == token.OR { 83 | return nil, errConstraintInterface 84 | } 85 | return nil, fmt.Errorf("A|B may only appear as constraint for %T", field.Type) 86 | default: 87 | return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type) 88 | } 89 | 90 | nf := &ast.Field{ 91 | Doc: field.Comment, 92 | Names: field.Names, 93 | Type: typ, 94 | Tag: field.Tag, 95 | Comment: field.Comment, 96 | } 97 | 98 | it.embeddedInstTypeParams = indices 99 | 100 | return p.parseMethod(nf, it, iface, pkg, tps) 101 | } 102 | 103 | var errConstraintInterface = errors.New("interface contains constraints") 104 | -------------------------------------------------------------------------------- /mockgen/gob.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/gob" 5 | "os" 6 | 7 | "go.uber.org/mock/mockgen/model" 8 | ) 9 | 10 | func gobMode(path string) (*model.Package, error) { 11 | in, err := os.Open(path) 12 | if err != nil { 13 | return nil, err 14 | } 15 | defer in.Close() 16 | var pkg model.Package 17 | if err := gob.NewDecoder(in).Decode(&pkg); err != nil { 18 | return nil, err 19 | } 20 | return &pkg, nil 21 | } 22 | -------------------------------------------------------------------------------- /mockgen/gob_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/gob" 5 | "os" 6 | "path/filepath" 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | "github.com/stretchr/testify/require" 11 | ) 12 | 13 | func TestGobMode(t *testing.T) { 14 | 15 | // Encode a package to a temporary gob. 16 | parser := packageModeParser{} 17 | want, err := parser.parsePackage( 18 | "go.uber.org/mock/mockgen/internal/tests/package_mode" /* package name */, 19 | []string{ "Human", "Earth" } /* ifaces */, 20 | ) 21 | path := filepath.Join(t.TempDir(), "model.gob") 22 | outfile, err := os.Create(path) 23 | require.NoError(t, err) 24 | require.NoError(t, gob.NewEncoder(outfile).Encode(want)) 25 | outfile.Close() 26 | 27 | // Ensure gobMode loads it correctly. 28 | got, err := gobMode(path) 29 | require.NoError(t, err) 30 | assert.Equal(t, want, got) 31 | } 32 | -------------------------------------------------------------------------------- /mockgen/internal/tests/add_generate_directive/import.go: -------------------------------------------------------------------------------- 1 | // Package add_generate_directive makes sure output places the go:generate command as a directive in the generated code. 2 | package add_generate_directive 3 | 4 | type Message struct { 5 | Text string 6 | } 7 | 8 | type Foo interface { 9 | Bar(channels []string, message chan<- Message) 10 | } 11 | -------------------------------------------------------------------------------- /mockgen/internal/tests/add_generate_directive/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/add_generate_directive (interfaces: Foo) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -write_generate_directive -destination mock.go -package add_generate_directive . Foo 7 | // 8 | 9 | // Package add_generate_directive is a generated GoMock package. 10 | package add_generate_directive 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | //go:generate mockgen -write_generate_directive -destination mock.go -package add_generate_directive . Foo 19 | 20 | // MockFoo is a mock of Foo interface. 21 | type MockFoo struct { 22 | ctrl *gomock.Controller 23 | recorder *MockFooMockRecorder 24 | isgomock struct{} 25 | } 26 | 27 | // MockFooMockRecorder is the mock recorder for MockFoo. 28 | type MockFooMockRecorder struct { 29 | mock *MockFoo 30 | } 31 | 32 | // NewMockFoo creates a new mock instance. 33 | func NewMockFoo(ctrl *gomock.Controller) *MockFoo { 34 | mock := &MockFoo{ctrl: ctrl} 35 | mock.recorder = &MockFooMockRecorder{mock} 36 | return mock 37 | } 38 | 39 | // EXPECT returns an object that allows the caller to indicate expected use. 40 | func (m *MockFoo) EXPECT() *MockFooMockRecorder { 41 | return m.recorder 42 | } 43 | 44 | // Bar mocks base method. 45 | func (m *MockFoo) Bar(channels []string, message chan<- Message) { 46 | m.ctrl.T.Helper() 47 | m.ctrl.Call(m, "Bar", channels, message) 48 | } 49 | 50 | // Bar indicates an expected call of Bar. 51 | func (mr *MockFooMockRecorder) Bar(channels, message any) *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), channels, message) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/alias/interfaces.go: -------------------------------------------------------------------------------- 1 | package alias 2 | 3 | //go:generate mockgen -typed -package=mock -destination=mock/interfaces.go . Fooer,FooerAlias,Barer,BarerAlias,Bazer,QuxerConsumer,QuuxerConsumer 4 | 5 | import "go.uber.org/mock/mockgen/internal/tests/alias/subpkg" 6 | 7 | // Case 1: A interface that has alias references in this package 8 | // should still be generated for its underlying name, i.e., MockFooer, 9 | // even though we have the alias replacement logic. 10 | type Fooer interface { 11 | Foo() 12 | } 13 | 14 | // Case 2: Generating a mock for an alias type. 15 | type FooerAlias = Fooer 16 | 17 | // Case 3: Generate mock for an interface that takes in alias parameters 18 | // and returns alias results. 19 | type Barer interface{ 20 | Bar(FooerAlias) FooerAlias 21 | } 22 | 23 | // Case 4: Combination of cases 2 & 3. 24 | type BarerAlias = Barer 25 | 26 | // Case 5: Generate mock for an interface that actually returns 27 | // the underlying type. This will generate mocks that use the alias, 28 | // but that should be fine since they should be interchangeable. 29 | type Bazer interface{ 30 | Baz(Fooer) Fooer 31 | } 32 | 33 | // Case 6: Generate mock for a type that refers to an alias defined in this package 34 | // for a type from another package. 35 | // The generated methods should use the alias defined here. 36 | type QuxerAlias = subpkg.Quxer 37 | 38 | type QuxerConsumer interface{ 39 | Consume(QuxerAlias) QuxerAlias 40 | } 41 | 42 | // Case 7: Generate mock for a type that refers to an alias defined in another package 43 | // for an unexported type in that other package. 44 | // The generated method should only use the alias, not the unexported underlying name. 45 | type QuuxerConsumer interface{ 46 | Consume(subpkg.Quuxer) subpkg.Quuxer 47 | } 48 | -------------------------------------------------------------------------------- /mockgen/internal/tests/alias/mock/interfaces_test.go: -------------------------------------------------------------------------------- 1 | package mock 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/alias" 5 | ) 6 | 7 | // This checks for type-checking equivalent of mock types. 8 | // If something does not resolve, the tests will not compile. 9 | 10 | var ( 11 | _ alias.Fooer = &MockFooer{} 12 | _ alias.FooerAlias = &MockFooer{} 13 | _ alias.Fooer = &MockFooerAlias{} 14 | _ alias.FooerAlias = &MockFooerAlias{} 15 | _ alias.Barer = &MockBarer{} 16 | _ alias.BarerAlias = &MockBarer{} 17 | _ alias.Barer = &MockBarerAlias{} 18 | _ alias.BarerAlias = &MockBarerAlias{} 19 | _ alias.Bazer = &MockBazer{} 20 | _ alias.QuxerConsumer = &MockQuxerConsumer{} 21 | _ alias.QuuxerConsumer = &MockQuuxerConsumer{} 22 | ) 23 | -------------------------------------------------------------------------------- /mockgen/internal/tests/alias/subpkg/interfaces.go: -------------------------------------------------------------------------------- 1 | package subpkg 2 | 3 | type Quxer interface { 4 | Qux() 5 | } 6 | 7 | type quuxerUnexported interface{ 8 | Quux(Quxer) Quxer 9 | } 10 | 11 | type Quuxer = quuxerUnexported 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/aux_imports_embedded_interface/README.md: -------------------------------------------------------------------------------- 1 | # Embedded Interfaces in aux_files 2 | 3 | Embedded interfaces in `aux_files` generate `unknown embedded interface XXX` errors. 4 | See below for example of the problem: 5 | 6 | ```go 7 | // source 8 | import ( 9 | alias "some.org/package/imported" 10 | ) 11 | 12 | type Source interface { 13 | alias.Foreign 14 | } 15 | ``` 16 | 17 | ```go 18 | // some.org/package/imported 19 | type Foreign interface { 20 | Embedded 21 | } 22 | 23 | type Embedded interface {} 24 | ``` 25 | 26 | Attempting to generate a mock will result in an `unknown embedded interface Embedded`. 27 | The issue is that the `fileParser` stores `auxInterfaces` underneath the package name 28 | explicitly specified in the `aux_files` flag. 29 | 30 | In the `parseInterface` method, there is an incorrect assumption about an embedded interface 31 | always being in the source file. 32 | 33 | ```go 34 | case *ast.Ident: 35 | // Embedded interface in this package. 36 | ei := p.auxInterfaces[""][v.String()] 37 | if ei == nil { 38 | return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String()) 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | // Reproduce the issue described in the README.md with 4 | // $ go:generate mockgen -aux_files faux=faux/faux.go -destination bugreport_mock.go -package bugreport -source=bugreport.go Example 5 | 6 | import ( 7 | "log" 8 | 9 | "go.uber.org/mock/mockgen/internal/tests/aux_imports_embedded_interface/faux" 10 | ) 11 | 12 | // Source is an interface w/ an embedded foreign interface 13 | type Source interface { 14 | faux.Foreign 15 | } 16 | 17 | func CallForeignMethod(s Source) { 18 | log.Println(s.Method()) 19 | } 20 | -------------------------------------------------------------------------------- /mockgen/internal/tests/aux_imports_embedded_interface/bugreport_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: bugreport.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -aux_files faux=faux/faux.go -destination bugreport_mock.go -package bugreport -source=bugreport.go Example 7 | // 8 | 9 | // Package bugreport is a generated GoMock package. 10 | package bugreport 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | faux "go.uber.org/mock/mockgen/internal/tests/aux_imports_embedded_interface/faux" 17 | ) 18 | 19 | // MockSource is a mock of Source interface. 20 | type MockSource struct { 21 | ctrl *gomock.Controller 22 | recorder *MockSourceMockRecorder 23 | } 24 | 25 | // MockSourceMockRecorder is the mock recorder for MockSource. 26 | type MockSourceMockRecorder struct { 27 | mock *MockSource 28 | } 29 | 30 | // NewMockSource creates a new mock instance. 31 | func NewMockSource(ctrl *gomock.Controller) *MockSource { 32 | mock := &MockSource{ctrl: ctrl} 33 | mock.recorder = &MockSourceMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockSource) EXPECT() *MockSourceMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Error mocks base method. 43 | func (m *MockSource) Error() string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Error") 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // Error indicates an expected call of Error. 51 | func (mr *MockSourceMockRecorder) Error() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockSource)(nil).Error)) 54 | } 55 | 56 | // Method mocks base method. 57 | func (m *MockSource) Method() faux.Return { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "Method") 60 | ret0, _ := ret[0].(faux.Return) 61 | return ret0 62 | } 63 | 64 | // Method indicates an expected call of Method. 65 | func (mr *MockSourceMockRecorder) Method() *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method", reflect.TypeOf((*MockSource)(nil).Method)) 68 | } 69 | -------------------------------------------------------------------------------- /mockgen/internal/tests/aux_imports_embedded_interface/bugreport_test.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | ) 8 | 9 | // TestValidInterface assesses whether or not the generated mock is valid 10 | func TestValidInterface(t *testing.T) { 11 | ctrl := gomock.NewController(t) 12 | 13 | s := NewMockSource(ctrl) 14 | s.EXPECT().Method().Return("") 15 | 16 | CallForeignMethod(s) 17 | } 18 | -------------------------------------------------------------------------------- /mockgen/internal/tests/aux_imports_embedded_interface/faux/faux.go: -------------------------------------------------------------------------------- 1 | package faux 2 | 3 | type Foreign interface { 4 | Method() Return 5 | Embedded 6 | error 7 | } 8 | 9 | type Embedded any 10 | 11 | type Return any 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_constraint/input.go: -------------------------------------------------------------------------------- 1 | package empty_interface 2 | 3 | //go:generate mockgen -package empty_interface -destination mock.go -source input.go "-build_constraint=(linux && 386) || (darwin && !cgo) || usertag" 4 | 5 | type Empty interface{} 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_constraint/mock.go: -------------------------------------------------------------------------------- 1 | //go:build (linux && 386) || (darwin && !cgo) || usertag 2 | 3 | // Code generated by MockGen. DO NOT EDIT. 4 | // Source: input.go 5 | // 6 | // Generated by this command: 7 | // 8 | // mockgen -package empty_interface -destination mock.go -source input.go -build_constraint=(linux && 386) || (darwin && !cgo) || usertag 9 | // 10 | 11 | // Package empty_interface is a generated GoMock package. 12 | package empty_interface 13 | 14 | import ( 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockEmpty is a mock of Empty interface. 19 | type MockEmpty struct { 20 | ctrl *gomock.Controller 21 | recorder *MockEmptyMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockEmptyMockRecorder is the mock recorder for MockEmpty. 26 | type MockEmptyMockRecorder struct { 27 | mock *MockEmpty 28 | } 29 | 30 | // NewMockEmpty creates a new mock instance. 31 | func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { 32 | mock := &MockEmpty{ctrl: ctrl} 33 | mock.recorder = &MockEmptyMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { 39 | return m.recorder 40 | } 41 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_flags/directive.go: -------------------------------------------------------------------------------- 1 | package build_flags 2 | 3 | // one build flag 4 | //go:generate mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface 5 | // multiple build flags 6 | //go:generate mockgen -destination=mock2/interfaces_mock.go "-build_flags=-race -tags=tag2" . Interface 7 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_flags/interfaces_1.go: -------------------------------------------------------------------------------- 1 | //go:build tag1 2 | 3 | package build_flags 4 | 5 | type Interface interface { 6 | HelloWorld() string 7 | } 8 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_flags/interfaces_2.go: -------------------------------------------------------------------------------- 1 | //go:build tag2 2 | 3 | package build_flags 4 | 5 | type Interface interface { 6 | Foo() 7 | } 8 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_flags/mock1/interfaces_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/build_flags (interfaces: Interface) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination=mock1/interfaces_mock.go -build_flags=-tags=tag1 . Interface 7 | // 8 | 9 | // Package mock_build_flags is a generated GoMock package. 10 | package mock_build_flags 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockInterface is a mock of Interface interface. 19 | type MockInterface struct { 20 | ctrl *gomock.Controller 21 | recorder *MockInterfaceMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockInterfaceMockRecorder is the mock recorder for MockInterface. 26 | type MockInterfaceMockRecorder struct { 27 | mock *MockInterface 28 | } 29 | 30 | // NewMockInterface creates a new mock instance. 31 | func NewMockInterface(ctrl *gomock.Controller) *MockInterface { 32 | mock := &MockInterface{ctrl: ctrl} 33 | mock.recorder = &MockInterfaceMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // HelloWorld mocks base method. 43 | func (m *MockInterface) HelloWorld() string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "HelloWorld") 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // HelloWorld indicates an expected call of HelloWorld. 51 | func (mr *MockInterfaceMockRecorder) HelloWorld() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HelloWorld", reflect.TypeOf((*MockInterface)(nil).HelloWorld)) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/build_flags/mock2/interfaces_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/build_flags (interfaces: Interface) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination=mock2/interfaces_mock.go -build_flags=-race -tags=tag2 . Interface 7 | // 8 | 9 | // Package mock_build_flags is a generated GoMock package. 10 | package mock_build_flags 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockInterface is a mock of Interface interface. 19 | type MockInterface struct { 20 | ctrl *gomock.Controller 21 | recorder *MockInterfaceMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockInterfaceMockRecorder is the mock recorder for MockInterface. 26 | type MockInterfaceMockRecorder struct { 27 | mock *MockInterface 28 | } 29 | 30 | // NewMockInterface creates a new mock instance. 31 | func NewMockInterface(ctrl *gomock.Controller) *MockInterface { 32 | mock := &MockInterface{ctrl: ctrl} 33 | mock.recorder = &MockInterfaceMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Foo mocks base method. 43 | func (m *MockInterface) Foo() { 44 | m.ctrl.T.Helper() 45 | m.ctrl.Call(m, "Foo") 46 | } 47 | 48 | // Foo indicates an expected call of Foo. 49 | func (mr *MockInterfaceMockRecorder) Foo() *gomock.Call { 50 | mr.mock.ctrl.T.Helper() 51 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockInterface)(nil).Foo)) 52 | } 53 | -------------------------------------------------------------------------------- /mockgen/internal/tests/const_array_length/input.go: -------------------------------------------------------------------------------- 1 | package const_length 2 | 3 | import "math" 4 | 5 | //go:generate mockgen -package const_length -destination mock.go -source input.go 6 | 7 | const C = 2 8 | 9 | type I interface { 10 | Foo() [C]int 11 | Bar() [2]int 12 | Baz() [math.MaxInt8]int 13 | Qux() [1 + 2]int 14 | Quux() [(1 + 2)]int 15 | Corge() [math.MaxInt8 - 120]int 16 | } 17 | -------------------------------------------------------------------------------- /mockgen/internal/tests/const_array_length/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package const_length -destination mock.go -source input.go 7 | // 8 | 9 | // Package const_length is a generated GoMock package. 10 | package const_length 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockI is a mock of I interface. 19 | type MockI struct { 20 | ctrl *gomock.Controller 21 | recorder *MockIMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockIMockRecorder is the mock recorder for MockI. 26 | type MockIMockRecorder struct { 27 | mock *MockI 28 | } 29 | 30 | // NewMockI creates a new mock instance. 31 | func NewMockI(ctrl *gomock.Controller) *MockI { 32 | mock := &MockI{ctrl: ctrl} 33 | mock.recorder = &MockIMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockI) EXPECT() *MockIMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Bar mocks base method. 43 | func (m *MockI) Bar() [2]int { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Bar") 46 | ret0, _ := ret[0].([2]int) 47 | return ret0 48 | } 49 | 50 | // Bar indicates an expected call of Bar. 51 | func (mr *MockIMockRecorder) Bar() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockI)(nil).Bar)) 54 | } 55 | 56 | // Baz mocks base method. 57 | func (m *MockI) Baz() [127]int { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "Baz") 60 | ret0, _ := ret[0].([127]int) 61 | return ret0 62 | } 63 | 64 | // Baz indicates an expected call of Baz. 65 | func (mr *MockIMockRecorder) Baz() *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Baz", reflect.TypeOf((*MockI)(nil).Baz)) 68 | } 69 | 70 | // Corge mocks base method. 71 | func (m *MockI) Corge() [7]int { 72 | m.ctrl.T.Helper() 73 | ret := m.ctrl.Call(m, "Corge") 74 | ret0, _ := ret[0].([7]int) 75 | return ret0 76 | } 77 | 78 | // Corge indicates an expected call of Corge. 79 | func (mr *MockIMockRecorder) Corge() *gomock.Call { 80 | mr.mock.ctrl.T.Helper() 81 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Corge", reflect.TypeOf((*MockI)(nil).Corge)) 82 | } 83 | 84 | // Foo mocks base method. 85 | func (m *MockI) Foo() [2]int { 86 | m.ctrl.T.Helper() 87 | ret := m.ctrl.Call(m, "Foo") 88 | ret0, _ := ret[0].([2]int) 89 | return ret0 90 | } 91 | 92 | // Foo indicates an expected call of Foo. 93 | func (mr *MockIMockRecorder) Foo() *gomock.Call { 94 | mr.mock.ctrl.T.Helper() 95 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockI)(nil).Foo)) 96 | } 97 | 98 | // Quux mocks base method. 99 | func (m *MockI) Quux() [3]int { 100 | m.ctrl.T.Helper() 101 | ret := m.ctrl.Call(m, "Quux") 102 | ret0, _ := ret[0].([3]int) 103 | return ret0 104 | } 105 | 106 | // Quux indicates an expected call of Quux. 107 | func (mr *MockIMockRecorder) Quux() *gomock.Call { 108 | mr.mock.ctrl.T.Helper() 109 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Quux", reflect.TypeOf((*MockI)(nil).Quux)) 110 | } 111 | 112 | // Qux mocks base method. 113 | func (m *MockI) Qux() [3]int { 114 | m.ctrl.T.Helper() 115 | ret := m.ctrl.Call(m, "Qux") 116 | ret0, _ := ret[0].([3]int) 117 | return ret0 118 | } 119 | 120 | // Qux indicates an expected call of Qux. 121 | func (mr *MockIMockRecorder) Qux() *gomock.Call { 122 | mr.mock.ctrl.T.Helper() 123 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Qux", reflect.TypeOf((*MockI)(nil).Qux)) 124 | } 125 | -------------------------------------------------------------------------------- /mockgen/internal/tests/copyright_file/input.go: -------------------------------------------------------------------------------- 1 | package empty_interface 2 | 3 | //go:generate mockgen -package empty_interface -destination mock.go -source input.go -copyright_file=mock_copyright_header 4 | 5 | type Empty interface{} // migrating interface{} -> any does not resolve to an interface type dropping test generation added in b391ab3 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/copyright_file/mock.go: -------------------------------------------------------------------------------- 1 | // This is a mock copyright header. 2 | // 3 | // Lorem ipsum dolor sit amet, consectetur adipiscing elit, 4 | // sed do eiusmod tempor incididunt ut labore et dolore magna 5 | // aliqua. Velit ut tortor pretium viverra suspendisse potenti. 6 | // 7 | 8 | // Code generated by MockGen. DO NOT EDIT. 9 | // Source: input.go 10 | // 11 | // Generated by this command: 12 | // 13 | // mockgen -package empty_interface -destination mock.go -source input.go -copyright_file=mock_copyright_header 14 | // 15 | 16 | // Package empty_interface is a generated GoMock package. 17 | package empty_interface 18 | 19 | import ( 20 | gomock "go.uber.org/mock/gomock" 21 | ) 22 | 23 | // MockEmpty is a mock of Empty interface. 24 | type MockEmpty struct { 25 | ctrl *gomock.Controller 26 | recorder *MockEmptyMockRecorder 27 | isgomock struct{} 28 | } 29 | 30 | // MockEmptyMockRecorder is the mock recorder for MockEmpty. 31 | type MockEmptyMockRecorder struct { 32 | mock *MockEmpty 33 | } 34 | 35 | // NewMockEmpty creates a new mock instance. 36 | func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { 37 | mock := &MockEmpty{ctrl: ctrl} 38 | mock.recorder = &MockEmptyMockRecorder{mock} 39 | return mock 40 | } 41 | 42 | // EXPECT returns an object that allows the caller to indicate expected use. 43 | func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { 44 | return m.recorder 45 | } 46 | -------------------------------------------------------------------------------- /mockgen/internal/tests/copyright_file/mock_copyright_header: -------------------------------------------------------------------------------- 1 | This is a mock copyright header. 2 | 3 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, 4 | sed do eiusmod tempor incididunt ut labore et dolore magna 5 | aliqua. Velit ut tortor pretium viverra suspendisse potenti. 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/README.md: -------------------------------------------------------------------------------- 1 | # Tests for custom package names 2 | 3 | This directory contains test for mockgen generating mocks when imported package 4 | name does not match import path suffix. For example, package with name "client" 5 | is located under import path "github.com/golang/mock/mockgen/internal/tests/custom_package_name/client/v1". 6 | 7 | Prior to this patch: 8 | 9 | ```bash 10 | $ go generate greeter/greeter.go 11 | 2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client" 12 | greeter/greeter.go:1: running "mockgen": exit status 1 13 | ``` 14 | 15 | This can be fixed by manually providing `-imports` flag, like `-imports client=github.com/golang/mock/mockgen/internal/tests/custom_package_name/client/v1`. 16 | But, mockgen should be able to automatically resolve package names in such situations. 17 | 18 | With this patch applied: 19 | 20 | ```bash 21 | $ go generate greeter/greeter.go 22 | $ echo $? 23 | 0 24 | ``` 25 | 26 | Mockgen runs successfully, produced output is equal to [greeter_mock_test.go](greeter/greeter_mock_test.go) content. 27 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/client/v1/client.go: -------------------------------------------------------------------------------- 1 | package client 2 | 3 | import "fmt" 4 | 5 | type Client struct{} 6 | 7 | func (c *Client) Greet(in GreetInput) string { 8 | return fmt.Sprintf("Hello, %s!", in.Name) 9 | } 10 | 11 | type GreetInput struct { 12 | Name string 13 | } 14 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/greeter/greeter.go: -------------------------------------------------------------------------------- 1 | package greeter 2 | 3 | //go:generate mockgen -source greeter.go -destination greeter_mock_test.go -package greeter 4 | 5 | import ( 6 | // stdlib import 7 | "fmt" 8 | 9 | // non-matching import suffix and package name 10 | "go.uber.org/mock/mockgen/internal/tests/custom_package_name/client/v1" 11 | 12 | // matching import suffix and package name 13 | "go.uber.org/mock/mockgen/internal/tests/custom_package_name/validator" 14 | ) 15 | 16 | type InputMaker interface { 17 | MakeInput() client.GreetInput 18 | } 19 | 20 | type Greeter struct { 21 | InputMaker InputMaker 22 | Client *client.Client 23 | } 24 | 25 | func (g *Greeter) Greet() (string, error) { 26 | in := g.InputMaker.MakeInput() 27 | if err := validator.Validate(in.Name); err != nil { 28 | return "", fmt.Errorf("validation failed: %v", err) 29 | } 30 | return g.Client.Greet(in), nil 31 | } 32 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: greeter.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -source greeter.go -destination greeter_mock_test.go -package greeter 7 | // 8 | 9 | // Package greeter is a generated GoMock package. 10 | package greeter 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | client "go.uber.org/mock/mockgen/internal/tests/custom_package_name/client/v1" 17 | ) 18 | 19 | // MockInputMaker is a mock of InputMaker interface. 20 | type MockInputMaker struct { 21 | ctrl *gomock.Controller 22 | recorder *MockInputMakerMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockInputMakerMockRecorder is the mock recorder for MockInputMaker. 27 | type MockInputMakerMockRecorder struct { 28 | mock *MockInputMaker 29 | } 30 | 31 | // NewMockInputMaker creates a new mock instance. 32 | func NewMockInputMaker(ctrl *gomock.Controller) *MockInputMaker { 33 | mock := &MockInputMaker{ctrl: ctrl} 34 | mock.recorder = &MockInputMakerMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockInputMaker) EXPECT() *MockInputMakerMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // MakeInput mocks base method. 44 | func (m *MockInputMaker) MakeInput() client.GreetInput { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "MakeInput") 47 | ret0, _ := ret[0].(client.GreetInput) 48 | return ret0 49 | } 50 | 51 | // MakeInput indicates an expected call of MakeInput. 52 | func (mr *MockInputMakerMockRecorder) MakeInput() *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MakeInput", reflect.TypeOf((*MockInputMaker)(nil).MakeInput)) 55 | } 56 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/greeter/greeter_test.go: -------------------------------------------------------------------------------- 1 | package greeter 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | "go.uber.org/mock/mockgen/internal/tests/custom_package_name/client/v1" 8 | ) 9 | 10 | func TestGreeter_Greet(t *testing.T) { 11 | ctrl := gomock.NewController(t) 12 | 13 | input := client.GreetInput{ 14 | Name: "Foo", 15 | } 16 | 17 | inputMaker := NewMockInputMaker(ctrl) 18 | inputMaker.EXPECT(). 19 | MakeInput(). 20 | Return(input) 21 | 22 | g := &Greeter{ 23 | InputMaker: inputMaker, 24 | Client: &client.Client{}, 25 | } 26 | 27 | greeting, err := g.Greet() 28 | if err != nil { 29 | t.Fatalf("Unexpected error: %v", err) 30 | } 31 | 32 | expected := "Hello, Foo!" 33 | if greeting != expected { 34 | t.Fatalf("Expected greeting to be %v but got %v", expected, greeting) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /mockgen/internal/tests/custom_package_name/validator/validate.go: -------------------------------------------------------------------------------- 1 | package validator 2 | 3 | func Validate(s string) error { 4 | return nil 5 | } 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/defined_import_local_name/input.go: -------------------------------------------------------------------------------- 1 | package defined_import_local_name 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | ) 7 | 8 | //go:generate mockgen -package defined_import_local_name -destination mock.go -source input.go -imports b_mock=bytes,c_mock=context 9 | 10 | type WithImports interface { 11 | Method1() bytes.Buffer 12 | Method2() context.Context 13 | } 14 | -------------------------------------------------------------------------------- /mockgen/internal/tests/defined_import_local_name/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package defined_import_local_name -destination mock.go -source input.go -imports b_mock=bytes,c_mock=context 7 | // 8 | 9 | // Package defined_import_local_name is a generated GoMock package. 10 | package defined_import_local_name 11 | 12 | import ( 13 | b_mock "bytes" 14 | c_mock "context" 15 | reflect "reflect" 16 | 17 | gomock "go.uber.org/mock/gomock" 18 | ) 19 | 20 | // MockWithImports is a mock of WithImports interface. 21 | type MockWithImports struct { 22 | ctrl *gomock.Controller 23 | recorder *MockWithImportsMockRecorder 24 | isgomock struct{} 25 | } 26 | 27 | // MockWithImportsMockRecorder is the mock recorder for MockWithImports. 28 | type MockWithImportsMockRecorder struct { 29 | mock *MockWithImports 30 | } 31 | 32 | // NewMockWithImports creates a new mock instance. 33 | func NewMockWithImports(ctrl *gomock.Controller) *MockWithImports { 34 | mock := &MockWithImports{ctrl: ctrl} 35 | mock.recorder = &MockWithImportsMockRecorder{mock} 36 | return mock 37 | } 38 | 39 | // EXPECT returns an object that allows the caller to indicate expected use. 40 | func (m *MockWithImports) EXPECT() *MockWithImportsMockRecorder { 41 | return m.recorder 42 | } 43 | 44 | // Method1 mocks base method. 45 | func (m *MockWithImports) Method1() b_mock.Buffer { 46 | m.ctrl.T.Helper() 47 | ret := m.ctrl.Call(m, "Method1") 48 | ret0, _ := ret[0].(b_mock.Buffer) 49 | return ret0 50 | } 51 | 52 | // Method1 indicates an expected call of Method1. 53 | func (mr *MockWithImportsMockRecorder) Method1() *gomock.Call { 54 | mr.mock.ctrl.T.Helper() 55 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method1", reflect.TypeOf((*MockWithImports)(nil).Method1)) 56 | } 57 | 58 | // Method2 mocks base method. 59 | func (m *MockWithImports) Method2() c_mock.Context { 60 | m.ctrl.T.Helper() 61 | ret := m.ctrl.Call(m, "Method2") 62 | ret0, _ := ret[0].(c_mock.Context) 63 | return ret0 64 | } 65 | 66 | // Method2 indicates an expected call of Method2. 67 | func (mr *MockWithImportsMockRecorder) Method2() *gomock.Call { 68 | mr.mock.ctrl.T.Helper() 69 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method2", reflect.TypeOf((*MockWithImports)(nil).Method2)) 70 | } 71 | -------------------------------------------------------------------------------- /mockgen/internal/tests/dot_imports/input.go: -------------------------------------------------------------------------------- 1 | package dot_imports 2 | 3 | //go:generate mockgen -package dot_imports -destination mock.go -source input.go 4 | 5 | import ( 6 | "bytes" 7 | . "context" 8 | . "net/http" 9 | ) 10 | 11 | type WithDotImports interface { 12 | Method1() Request 13 | Method2() *bytes.Buffer 14 | Method3() Context 15 | } 16 | -------------------------------------------------------------------------------- /mockgen/internal/tests/dot_imports/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package dot_imports -destination mock.go -source input.go 7 | // 8 | 9 | // Package dot_imports is a generated GoMock package. 10 | package dot_imports 11 | 12 | import ( 13 | bytes "bytes" 14 | . "context" 15 | . "net/http" 16 | reflect "reflect" 17 | 18 | gomock "go.uber.org/mock/gomock" 19 | ) 20 | 21 | // MockWithDotImports is a mock of WithDotImports interface. 22 | type MockWithDotImports struct { 23 | ctrl *gomock.Controller 24 | recorder *MockWithDotImportsMockRecorder 25 | isgomock struct{} 26 | } 27 | 28 | // MockWithDotImportsMockRecorder is the mock recorder for MockWithDotImports. 29 | type MockWithDotImportsMockRecorder struct { 30 | mock *MockWithDotImports 31 | } 32 | 33 | // NewMockWithDotImports creates a new mock instance. 34 | func NewMockWithDotImports(ctrl *gomock.Controller) *MockWithDotImports { 35 | mock := &MockWithDotImports{ctrl: ctrl} 36 | mock.recorder = &MockWithDotImportsMockRecorder{mock} 37 | return mock 38 | } 39 | 40 | // EXPECT returns an object that allows the caller to indicate expected use. 41 | func (m *MockWithDotImports) EXPECT() *MockWithDotImportsMockRecorder { 42 | return m.recorder 43 | } 44 | 45 | // Method1 mocks base method. 46 | func (m *MockWithDotImports) Method1() Request { 47 | m.ctrl.T.Helper() 48 | ret := m.ctrl.Call(m, "Method1") 49 | ret0, _ := ret[0].(Request) 50 | return ret0 51 | } 52 | 53 | // Method1 indicates an expected call of Method1. 54 | func (mr *MockWithDotImportsMockRecorder) Method1() *gomock.Call { 55 | mr.mock.ctrl.T.Helper() 56 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method1", reflect.TypeOf((*MockWithDotImports)(nil).Method1)) 57 | } 58 | 59 | // Method2 mocks base method. 60 | func (m *MockWithDotImports) Method2() *bytes.Buffer { 61 | m.ctrl.T.Helper() 62 | ret := m.ctrl.Call(m, "Method2") 63 | ret0, _ := ret[0].(*bytes.Buffer) 64 | return ret0 65 | } 66 | 67 | // Method2 indicates an expected call of Method2. 68 | func (mr *MockWithDotImportsMockRecorder) Method2() *gomock.Call { 69 | mr.mock.ctrl.T.Helper() 70 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method2", reflect.TypeOf((*MockWithDotImports)(nil).Method2)) 71 | } 72 | 73 | // Method3 mocks base method. 74 | func (m *MockWithDotImports) Method3() Context { 75 | m.ctrl.T.Helper() 76 | ret := m.ctrl.Call(m, "Method3") 77 | ret0, _ := ret[0].(Context) 78 | return ret0 79 | } 80 | 81 | // Method3 indicates an expected call of Method3. 82 | func (mr *MockWithDotImportsMockRecorder) Method3() *gomock.Call { 83 | mr.mock.ctrl.T.Helper() 84 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method3", reflect.TypeOf((*MockWithDotImports)(nil).Method3)) 85 | } 86 | -------------------------------------------------------------------------------- /mockgen/internal/tests/empty_interface/input.go: -------------------------------------------------------------------------------- 1 | package empty_interface 2 | 3 | //go:generate mockgen -package empty_interface -destination mock.go -source input.go 4 | 5 | type Empty interface{} // migrating interface{} -> any does not resolve to an interface type. 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/empty_interface/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package empty_interface -destination mock.go -source input.go 7 | // 8 | 9 | // Package empty_interface is a generated GoMock package. 10 | package empty_interface 11 | 12 | import ( 13 | gomock "go.uber.org/mock/gomock" 14 | ) 15 | 16 | // MockEmpty is a mock of Empty interface. 17 | type MockEmpty struct { 18 | ctrl *gomock.Controller 19 | recorder *MockEmptyMockRecorder 20 | isgomock struct{} 21 | } 22 | 23 | // MockEmptyMockRecorder is the mock recorder for MockEmpty. 24 | type MockEmptyMockRecorder struct { 25 | mock *MockEmpty 26 | } 27 | 28 | // NewMockEmpty creates a new mock instance. 29 | func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { 30 | mock := &MockEmpty{ctrl: ctrl} 31 | mock.recorder = &MockEmptyMockRecorder{mock} 32 | return mock 33 | } 34 | 35 | // EXPECT returns an object that allows the caller to indicate expected use. 36 | func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { 37 | return m.recorder 38 | } 39 | -------------------------------------------------------------------------------- /mockgen/internal/tests/exclude/interfaces.go: -------------------------------------------------------------------------------- 1 | package exclude 2 | 3 | //go:generate mockgen -source=interfaces.go -destination=mock.go -package=exclude -exclude_interfaces=IgnoreMe,IgnoreMe2 4 | 5 | type IgnoreMe interface { 6 | A() bool 7 | } 8 | 9 | type IgnoreMe2 interface { 10 | ~int 11 | } 12 | 13 | type GenerateMockForMe interface { 14 | B() int 15 | } 16 | -------------------------------------------------------------------------------- /mockgen/internal/tests/exclude/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: interfaces.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -source=interfaces.go -destination=mock.go -package=exclude -exclude_interfaces=IgnoreMe,IgnoreMe2 7 | // 8 | 9 | // Package exclude is a generated GoMock package. 10 | package exclude 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockGenerateMockForMe is a mock of GenerateMockForMe interface. 19 | type MockGenerateMockForMe struct { 20 | ctrl *gomock.Controller 21 | recorder *MockGenerateMockForMeMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockGenerateMockForMeMockRecorder is the mock recorder for MockGenerateMockForMe. 26 | type MockGenerateMockForMeMockRecorder struct { 27 | mock *MockGenerateMockForMe 28 | } 29 | 30 | // NewMockGenerateMockForMe creates a new mock instance. 31 | func NewMockGenerateMockForMe(ctrl *gomock.Controller) *MockGenerateMockForMe { 32 | mock := &MockGenerateMockForMe{ctrl: ctrl} 33 | mock.recorder = &MockGenerateMockForMeMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockGenerateMockForMe) EXPECT() *MockGenerateMockForMeMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // B mocks base method. 43 | func (m *MockGenerateMockForMe) B() int { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "B") 46 | ret0, _ := ret[0].(int) 47 | return ret0 48 | } 49 | 50 | // B indicates an expected call of B. 51 | func (mr *MockGenerateMockForMeMockRecorder) B() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "B", reflect.TypeOf((*MockGenerateMockForMe)(nil).B)) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/extra_import/import.go: -------------------------------------------------------------------------------- 1 | // Package extra_import makes sure output does not import it. See #515. 2 | package extra_import 3 | 4 | //go:generate mockgen -destination mock.go -package extra_import . Foo 5 | 6 | type Message struct { 7 | Text string 8 | } 9 | 10 | type Foo interface { 11 | Bar(channels []string, message chan<- Message) 12 | } 13 | -------------------------------------------------------------------------------- /mockgen/internal/tests/extra_import/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/extra_import (interfaces: Foo) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination mock.go -package extra_import . Foo 7 | // 8 | 9 | // Package extra_import is a generated GoMock package. 10 | package extra_import 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockFoo is a mock of Foo interface. 19 | type MockFoo struct { 20 | ctrl *gomock.Controller 21 | recorder *MockFooMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockFooMockRecorder is the mock recorder for MockFoo. 26 | type MockFooMockRecorder struct { 27 | mock *MockFoo 28 | } 29 | 30 | // NewMockFoo creates a new mock instance. 31 | func NewMockFoo(ctrl *gomock.Controller) *MockFoo { 32 | mock := &MockFoo{ctrl: ctrl} 33 | mock.recorder = &MockFooMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockFoo) EXPECT() *MockFooMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Bar mocks base method. 43 | func (m *MockFoo) Bar(channels []string, message chan<- Message) { 44 | m.ctrl.T.Helper() 45 | m.ctrl.Call(m, "Bar", channels, message) 46 | } 47 | 48 | // Bar indicates an expected call of Bar. 49 | func (mr *MockFooMockRecorder) Bar(channels, message any) *gomock.Call { 50 | mr.mock.ctrl.T.Helper() 51 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), channels, message) 52 | } 53 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generated_identifier_conflict/README.md: -------------------------------------------------------------------------------- 1 | # Generated Identifier Conflict 2 | 3 | The generated mock methods use some hardcoded variable/receiver names that can 4 | have conflicts with the argument names that are defined by the code for which 5 | the mock is generated when using the source generation method. 6 | 7 | Example: 8 | 9 | ```go 10 | type Example interface { 11 | Method(_m, _mr, m, mr int) 12 | } 13 | ``` 14 | 15 | ```go 16 | // Method mocks base method 17 | func (_m *MockExample) Method(_m int, _mr int, m int, mr int) { 18 | _m.ctrl.Call(_m, "Method", _m, _mr, m, mr) 19 | } 20 | ``` 21 | 22 | In the above example one of the interface method parameters is called `_m` 23 | but unfortunately the generated receiver name is also called `_m` so the 24 | mock code won't compile. 25 | 26 | The generator has to make sure that generated identifiers (e.g.: the receiver 27 | names) are always different from the arg names that might come from external 28 | sources. 29 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generated_identifier_conflict/bugreport.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | //go:generate mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go 4 | 5 | type Example interface { 6 | // _m and _mr were used by the buggy code: the '_' prefix was there hoping 7 | // that no one will use method argument names starting with '_' reducing 8 | // the chance of collision with generated identifiers. 9 | // m and mr are used by the bugfixed new code, the '_' prefix has been 10 | // removed because the new code generator changes the names of the 11 | // generated identifiers in case they would collide with identifiers 12 | // coming from argument names. 13 | Method(_m, _mr, m, mr int) 14 | 15 | VarargMethod(_s, _x, a, ret int, varargs ...int) 16 | } 17 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generated_identifier_conflict/bugreport_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: bugreport.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go 7 | // 8 | 9 | // Package bugreport is a generated GoMock package. 10 | package bugreport 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockExample is a mock of Example interface. 19 | type MockExample struct { 20 | ctrl *gomock.Controller 21 | recorder *MockExampleMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockExampleMockRecorder is the mock recorder for MockExample. 26 | type MockExampleMockRecorder struct { 27 | mock *MockExample 28 | } 29 | 30 | // NewMockExample creates a new mock instance. 31 | func NewMockExample(ctrl *gomock.Controller) *MockExample { 32 | mock := &MockExample{ctrl: ctrl} 33 | mock.recorder = &MockExampleMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockExample) EXPECT() *MockExampleMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Method mocks base method. 43 | func (m_2 *MockExample) Method(_m, _mr, m, mr int) { 44 | m_2.ctrl.T.Helper() 45 | m_2.ctrl.Call(m_2, "Method", _m, _mr, m, mr) 46 | } 47 | 48 | // Method indicates an expected call of Method. 49 | func (mr_2 *MockExampleMockRecorder) Method(_m, _mr, m, mr any) *gomock.Call { 50 | mr_2.mock.ctrl.T.Helper() 51 | return mr_2.mock.ctrl.RecordCallWithMethodType(mr_2.mock, "Method", reflect.TypeOf((*MockExample)(nil).Method), _m, _mr, m, mr) 52 | } 53 | 54 | // VarargMethod mocks base method. 55 | func (m *MockExample) VarargMethod(_s, _x, a, ret int, varargs ...int) { 56 | m.ctrl.T.Helper() 57 | varargs_2 := []any{_s, _x, a, ret} 58 | for _, a_2 := range varargs { 59 | varargs_2 = append(varargs_2, a_2) 60 | } 61 | m.ctrl.Call(m, "VarargMethod", varargs_2...) 62 | } 63 | 64 | // VarargMethod indicates an expected call of VarargMethod. 65 | func (mr *MockExampleMockRecorder) VarargMethod(_s, _x, a, ret any, varargs ...any) *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | varargs_2 := append([]any{_s, _x, a, ret}, varargs...) 68 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "VarargMethod", reflect.TypeOf((*MockExample)(nil).VarargMethod), varargs_2...) 69 | } 70 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generated_identifier_conflict/bugreport_test.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | ) 8 | 9 | func TestExample_Method(t *testing.T) { 10 | ctrl := gomock.NewController(t) 11 | m := NewMockExample(ctrl) 12 | m.EXPECT().Method(1, 2, 3, 4) 13 | 14 | m.Method(1, 2, 3, 4) 15 | } 16 | 17 | func TestExample_VarargMethod(t *testing.T) { 18 | ctrl := gomock.NewController(t) 19 | m := NewMockExample(ctrl) 20 | m.EXPECT().VarargMethod(1, 2, 3, 4, 6, 7) 21 | 22 | m.VarargMethod(1, 2, 3, 4, 6, 7) 23 | } 24 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/external.go: -------------------------------------------------------------------------------- 1 | package generics 2 | 3 | import ( 4 | "context" 5 | "io" 6 | 7 | "go.uber.org/mock/mockgen/internal/tests/generics/other" 8 | "golang.org/x/exp/constraints" 9 | ) 10 | 11 | //go:generate mockgen --source=external.go --destination=source/mock_external_mock.go --package source 12 | //go:generate mockgen --destination=package_mode/mock_external_mock.go -package=package_mode . ExternalConstraint,EmbeddingIface,Generator,Group 13 | 14 | type ExternalConstraint[I constraints.Integer, F any] interface { 15 | One(string) string 16 | Two(I) string 17 | Three(I) F 18 | Four(I) Foo[I, F] 19 | Five(I) Baz[F] 20 | Six(I) *Baz[F] 21 | Seven(I) other.One[I] 22 | Eight(F) other.Two[I, F] 23 | Nine(Iface[I]) 24 | Ten(*I) 25 | Eleven() map[string]I 26 | Twelve(ctx context.Context) <-chan []I 27 | Thirteen(...I) *F 28 | } 29 | 30 | type EmbeddingIface[T constraints.Integer, R constraints.Float] interface { 31 | io.Reader 32 | Generator[R] 33 | Earth[Generator[T]] 34 | other.Either[R, StructType, other.Five, Generator[T]] 35 | ExternalConstraint[T, R] 36 | } 37 | 38 | type Generator[T any] interface { 39 | Generate() T 40 | } 41 | 42 | type Group[T Generator[any]] interface { 43 | Join(ctx context.Context) []T 44 | } 45 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/generics.go: -------------------------------------------------------------------------------- 1 | package generics 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/generics/other" 5 | "golang.org/x/exp/constraints" 6 | ) 7 | 8 | //go:generate mockgen --source=generics.go --destination=source/mock_generics_mock.go --package source 9 | //go:generate mockgen --destination=package_mode/mock_test.go --package=package_mode . Bar,Universe,MilkyWay,SolarSystem,Earth,Water 10 | 11 | type Bar[T any, R any] interface { 12 | One(string) string 13 | Two(T) string 14 | Three(T) R 15 | Four(T) Foo[T, R] 16 | Five(T) Baz[T] 17 | Six(T) *Baz[T] 18 | Seven(T) other.One[T] 19 | Eight(T) other.Two[T, R] 20 | Nine(Iface[T]) 21 | Ten(*T) 22 | Eleven() (*other.One[T], error) 23 | Twelve() (*other.Two[T, R], error) 24 | Thirteen() (Baz[StructType], error) 25 | Fourteen() (*Foo[StructType, StructType2], error) 26 | Fifteen() (Iface[StructType], error) 27 | Sixteen() (Baz[other.Three], error) 28 | Seventeen() (*Foo[other.Three, other.Four], error) 29 | Eighteen() (Iface[*other.Five], error) 30 | Nineteen() AliasType 31 | } 32 | 33 | type Foo[T any, R any] struct{} 34 | 35 | type Baz[T any] struct{} 36 | 37 | type Iface[T any] any 38 | 39 | type StructType struct{} 40 | 41 | type StructType2 struct{} 42 | 43 | type AliasType Baz[other.Three] 44 | 45 | type Universe[T constraints.Signed] interface { 46 | MilkyWay[T] 47 | } 48 | 49 | type MilkyWay[R constraints.Integer] interface { 50 | SolarSystem[R] 51 | } 52 | 53 | type SolarSystem[T constraints.Ordered] interface { 54 | Earth[T] 55 | } 56 | 57 | type Earth[R any] interface { 58 | Water(R) []R 59 | } 60 | 61 | type Water[R any, C UnsignedInteger] interface { 62 | Fish(R) []C 63 | } 64 | 65 | type UnsignedInteger interface { 66 | ~uint | ~uint32 | ~uint64 67 | } 68 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/go.mod: -------------------------------------------------------------------------------- 1 | module go.uber.org/mock/mockgen/internal/tests/generics 2 | 3 | go 1.23 4 | 5 | replace go.uber.org/mock => ../../../.. 6 | 7 | require ( 8 | go.uber.org/mock v0.0.0-00010101000000-000000000000 9 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 10 | ) 11 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= 2 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= 3 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/other/other.go: -------------------------------------------------------------------------------- 1 | package other 2 | 3 | type One[T any] struct{} 4 | 5 | type Two[T any, R any] struct{} 6 | 7 | type Three struct{} 8 | 9 | type Four struct{} 10 | 11 | type Five any 12 | 13 | type Either[T, R, K, V any] interface { 14 | First() T 15 | Second() R 16 | Third() K 17 | Fourth() V 18 | } 19 | -------------------------------------------------------------------------------- /mockgen/internal/tests/generics/source/mock_external_test.go: -------------------------------------------------------------------------------- 1 | package source 2 | 3 | import ( 4 | "context" 5 | "testing" 6 | 7 | "go.uber.org/mock/gomock" 8 | "go.uber.org/mock/mockgen/internal/tests/generics" 9 | ) 10 | 11 | func TestMockEmbeddingIface_One(t *testing.T) { 12 | ctrl := gomock.NewController(t) 13 | 14 | m := NewMockEmbeddingIface[int, float64](ctrl) 15 | m.EXPECT().One("foo").Return("bar") 16 | if v := m.One("foo"); v != "bar" { 17 | t.Errorf("One() = %v, want %v", v, "bar") 18 | } 19 | } 20 | 21 | func TestMockUniverse_Water(t *testing.T) { 22 | ctrl := gomock.NewController(t) 23 | 24 | m := NewMockUniverse[int](ctrl) 25 | m.EXPECT().Water(1024) 26 | m.Water(1024) 27 | } 28 | 29 | func TestNewMockGroup_Join(t *testing.T) { 30 | ctrl := gomock.NewController(t) 31 | 32 | m := NewMockGroup[generics.Generator[any]](ctrl) 33 | ctx := context.TODO() 34 | m.EXPECT().Join(ctx).Return(nil) 35 | if v := m.Join(ctx); v != nil { 36 | t.Errorf("Join() = %v, want %v", v, nil) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_aliased/source.go: -------------------------------------------------------------------------------- 1 | package import_aliased 2 | 3 | import ( 4 | definitionAlias "context" 5 | ) 6 | 7 | //go:generate mockgen -package import_aliased -destination source_mock.go -source=source.go -imports definitionAlias=context 8 | 9 | type S interface { 10 | M(ctx definitionAlias.Context) 11 | } 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_aliased/source_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: source.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package import_aliased -destination source_mock.go -source=source.go -imports definitionAlias=context 7 | // 8 | 9 | // Package import_aliased is a generated GoMock package. 10 | package import_aliased 11 | 12 | import ( 13 | definitionAlias "context" 14 | reflect "reflect" 15 | 16 | gomock "go.uber.org/mock/gomock" 17 | ) 18 | 19 | // MockS is a mock of S interface. 20 | type MockS struct { 21 | ctrl *gomock.Controller 22 | recorder *MockSMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockSMockRecorder is the mock recorder for MockS. 27 | type MockSMockRecorder struct { 28 | mock *MockS 29 | } 30 | 31 | // NewMockS creates a new mock instance. 32 | func NewMockS(ctrl *gomock.Controller) *MockS { 33 | mock := &MockS{ctrl: ctrl} 34 | mock.recorder = &MockSMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockS) EXPECT() *MockSMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // M mocks base method. 44 | func (m *MockS) M(ctx definitionAlias.Context) { 45 | m.ctrl.T.Helper() 46 | m.ctrl.Call(m, "M", ctx) 47 | } 48 | 49 | // M indicates an expected call of M. 50 | func (mr *MockSMockRecorder) M(ctx any) *gomock.Call { 51 | mr.mock.ctrl.T.Helper() 52 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "M", reflect.TypeOf((*MockS)(nil).M), ctx) 53 | } 54 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_collision/internalpackage/foo.go: -------------------------------------------------------------------------------- 1 | package internalpackage 2 | 3 | type FooExported struct{} 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_collision/p2/mocks/mocks.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/import_collision/p2 (interfaces: Mything) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination=mocks/mocks.go -package=internalpackage . Mything 7 | // 8 | 9 | // Package internalpackage is a generated GoMock package. 10 | package internalpackage 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | internalpackage "go.uber.org/mock/mockgen/internal/tests/import_collision/internalpackage" 17 | ) 18 | 19 | // MockMything is a mock of Mything interface. 20 | type MockMything struct { 21 | ctrl *gomock.Controller 22 | recorder *MockMythingMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockMythingMockRecorder is the mock recorder for MockMything. 27 | type MockMythingMockRecorder struct { 28 | mock *MockMything 29 | } 30 | 31 | // NewMockMything creates a new mock instance. 32 | func NewMockMything(ctrl *gomock.Controller) *MockMything { 33 | mock := &MockMything{ctrl: ctrl} 34 | mock.recorder = &MockMythingMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockMything) EXPECT() *MockMythingMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // DoThat mocks base method. 44 | func (m *MockMything) DoThat(arg0 int) internalpackage.FooExported { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "DoThat", arg0) 47 | ret0, _ := ret[0].(internalpackage.FooExported) 48 | return ret0 49 | } 50 | 51 | // DoThat indicates an expected call of DoThat. 52 | func (mr *MockMythingMockRecorder) DoThat(arg0 any) *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoThat", reflect.TypeOf((*MockMything)(nil).DoThat), arg0) 55 | } 56 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_collision/p2/p2.go: -------------------------------------------------------------------------------- 1 | package p2 2 | 3 | //go:generate mockgen -destination=mocks/mocks.go -package=internalpackage . Mything 4 | 5 | import ( 6 | "go.uber.org/mock/mockgen/internal/tests/import_collision/internalpackage" 7 | ) 8 | 9 | type Mything interface { 10 | // issue here, is that the variable has the same name as an imported package. 11 | DoThat(internalpackage int) internalpackage.FooExported 12 | } 13 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/bugreport.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bugreport 16 | 17 | //go:generate mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go 18 | 19 | import ( 20 | "log" 21 | 22 | "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/ersatz" 23 | "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/faux" 24 | ) 25 | 26 | // Source is an interface w/ an embedded foreign interface 27 | type Source interface { 28 | ersatz.Embedded 29 | faux.Foreign 30 | error 31 | Foo 32 | } 33 | 34 | func CallForeignMethod(s Source) { 35 | log.Println(s.Ersatz()) 36 | log.Println(s.OtherErsatz()) 37 | } 38 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/bugreport_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: bugreport.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go 7 | // 8 | 9 | // Package bugreport is a generated GoMock package. 10 | package bugreport 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ersatz "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/ersatz" 17 | ersatz0 "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/other/ersatz" 18 | ) 19 | 20 | // MockSource is a mock of Source interface. 21 | type MockSource struct { 22 | ctrl *gomock.Controller 23 | recorder *MockSourceMockRecorder 24 | isgomock struct{} 25 | } 26 | 27 | // MockSourceMockRecorder is the mock recorder for MockSource. 28 | type MockSourceMockRecorder struct { 29 | mock *MockSource 30 | } 31 | 32 | // NewMockSource creates a new mock instance. 33 | func NewMockSource(ctrl *gomock.Controller) *MockSource { 34 | mock := &MockSource{ctrl: ctrl} 35 | mock.recorder = &MockSourceMockRecorder{mock} 36 | return mock 37 | } 38 | 39 | // EXPECT returns an object that allows the caller to indicate expected use. 40 | func (m *MockSource) EXPECT() *MockSourceMockRecorder { 41 | return m.recorder 42 | } 43 | 44 | // Bar mocks base method. 45 | func (m *MockSource) Bar() Baz { 46 | m.ctrl.T.Helper() 47 | ret := m.ctrl.Call(m, "Bar") 48 | ret0, _ := ret[0].(Baz) 49 | return ret0 50 | } 51 | 52 | // Bar indicates an expected call of Bar. 53 | func (mr *MockSourceMockRecorder) Bar() *gomock.Call { 54 | mr.mock.ctrl.T.Helper() 55 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockSource)(nil).Bar)) 56 | } 57 | 58 | // Error mocks base method. 59 | func (m *MockSource) Error() string { 60 | m.ctrl.T.Helper() 61 | ret := m.ctrl.Call(m, "Error") 62 | ret0, _ := ret[0].(string) 63 | return ret0 64 | } 65 | 66 | // Error indicates an expected call of Error. 67 | func (mr *MockSourceMockRecorder) Error() *gomock.Call { 68 | mr.mock.ctrl.T.Helper() 69 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockSource)(nil).Error)) 70 | } 71 | 72 | // Ersatz mocks base method. 73 | func (m *MockSource) Ersatz() ersatz.Return { 74 | m.ctrl.T.Helper() 75 | ret := m.ctrl.Call(m, "Ersatz") 76 | ret0, _ := ret[0].(ersatz.Return) 77 | return ret0 78 | } 79 | 80 | // Ersatz indicates an expected call of Ersatz. 81 | func (mr *MockSourceMockRecorder) Ersatz() *gomock.Call { 82 | mr.mock.ctrl.T.Helper() 83 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ersatz", reflect.TypeOf((*MockSource)(nil).Ersatz)) 84 | } 85 | 86 | // OtherErsatz mocks base method. 87 | func (m *MockSource) OtherErsatz() ersatz0.Return { 88 | m.ctrl.T.Helper() 89 | ret := m.ctrl.Call(m, "OtherErsatz") 90 | ret0, _ := ret[0].(ersatz0.Return) 91 | return ret0 92 | } 93 | 94 | // OtherErsatz indicates an expected call of OtherErsatz. 95 | func (mr *MockSourceMockRecorder) OtherErsatz() *gomock.Call { 96 | mr.mock.ctrl.T.Helper() 97 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OtherErsatz", reflect.TypeOf((*MockSource)(nil).OtherErsatz)) 98 | } 99 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/bugreport_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bugreport 16 | 17 | import ( 18 | "testing" 19 | 20 | "go.uber.org/mock/gomock" 21 | ) 22 | 23 | // TestValidInterface assesses whether or not the generated mock is valid 24 | func TestValidInterface(t *testing.T) { 25 | ctrl := gomock.NewController(t) 26 | 27 | s := NewMockSource(ctrl) 28 | s.EXPECT().Ersatz().Return("") 29 | s.EXPECT().OtherErsatz().Return("") 30 | CallForeignMethod(s) 31 | } 32 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/ersatz/ersatz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package ersatz 16 | 17 | type Embedded interface { 18 | Ersatz() Return 19 | } 20 | 21 | type Return any 22 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/faux/conflict.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package faux 16 | 17 | import "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/other/log" 18 | 19 | func Conflict1() { 20 | log.Foo() 21 | } 22 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/faux/faux.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package faux 16 | 17 | import ( 18 | "log" 19 | 20 | "go.uber.org/mock/mockgen/internal/tests/import_embedded_interface/other/ersatz" 21 | ) 22 | 23 | type Foreign interface { 24 | ersatz.Embedded 25 | } 26 | 27 | func Conflict0() { 28 | log.Println() 29 | } 30 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/foo.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | type Foo interface { 4 | Bar() Baz 5 | } 6 | type Baz any 7 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/net.go: -------------------------------------------------------------------------------- 1 | // 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | // 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | // 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | package bugreport 15 | 16 | //go:generate mockgen -destination net_mock.go -package bugreport -source=net.go 17 | 18 | import "net/http" 19 | 20 | type Net interface { 21 | http.ResponseWriter 22 | } 23 | 24 | func CallResponseWriterMethods(n Net) { 25 | n.WriteHeader(10) 26 | } 27 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/net_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: net.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination net_mock.go -package bugreport -source=net.go 7 | // 8 | 9 | // Package bugreport is a generated GoMock package. 10 | package bugreport 11 | 12 | import ( 13 | http "net/http" 14 | reflect "reflect" 15 | 16 | gomock "go.uber.org/mock/gomock" 17 | ) 18 | 19 | // MockNet is a mock of Net interface. 20 | type MockNet struct { 21 | ctrl *gomock.Controller 22 | recorder *MockNetMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockNetMockRecorder is the mock recorder for MockNet. 27 | type MockNetMockRecorder struct { 28 | mock *MockNet 29 | } 30 | 31 | // NewMockNet creates a new mock instance. 32 | func NewMockNet(ctrl *gomock.Controller) *MockNet { 33 | mock := &MockNet{ctrl: ctrl} 34 | mock.recorder = &MockNetMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockNet) EXPECT() *MockNetMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Header mocks base method. 44 | func (m *MockNet) Header() http.Header { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Header") 47 | ret0, _ := ret[0].(http.Header) 48 | return ret0 49 | } 50 | 51 | // Header indicates an expected call of Header. 52 | func (mr *MockNetMockRecorder) Header() *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Header", reflect.TypeOf((*MockNet)(nil).Header)) 55 | } 56 | 57 | // Write mocks base method. 58 | func (m *MockNet) Write(arg0 []byte) (int, error) { 59 | m.ctrl.T.Helper() 60 | ret := m.ctrl.Call(m, "Write", arg0) 61 | ret0, _ := ret[0].(int) 62 | ret1, _ := ret[1].(error) 63 | return ret0, ret1 64 | } 65 | 66 | // Write indicates an expected call of Write. 67 | func (mr *MockNetMockRecorder) Write(arg0 any) *gomock.Call { 68 | mr.mock.ctrl.T.Helper() 69 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockNet)(nil).Write), arg0) 70 | } 71 | 72 | // WriteHeader mocks base method. 73 | func (m *MockNet) WriteHeader(statusCode int) { 74 | m.ctrl.T.Helper() 75 | m.ctrl.Call(m, "WriteHeader", statusCode) 76 | } 77 | 78 | // WriteHeader indicates an expected call of WriteHeader. 79 | func (mr *MockNetMockRecorder) WriteHeader(statusCode any) *gomock.Call { 80 | mr.mock.ctrl.T.Helper() 81 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteHeader", reflect.TypeOf((*MockNet)(nil).WriteHeader), statusCode) 82 | } 83 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/net_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bugreport 16 | 17 | import ( 18 | "testing" 19 | 20 | "go.uber.org/mock/gomock" 21 | ) 22 | 23 | // TestValidInterface assesses whether or not the generated mock is valid 24 | func TestValidNetInterface(t *testing.T) { 25 | ctrl := gomock.NewController(t) 26 | 27 | s := NewMockNet(ctrl) 28 | s.EXPECT().WriteHeader(10) 29 | CallResponseWriterMethods(s) 30 | } 31 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/other/ersatz/ersatz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package ersatz 16 | 17 | type Embedded interface { 18 | OtherErsatz() Return 19 | } 20 | 21 | type Return any 22 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_embedded_interface/other/log/log.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package log 16 | 17 | func Foo() {} 18 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_source/README.md: -------------------------------------------------------------------------------- 1 | # Import Source 2 | 3 | Test the case where the generated code uses a type defined in the source package (in source mode). There are two test cases: 4 | 5 | - the output is in a new package 6 | - the output is in the same package as the input 7 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_source/definition/source.go: -------------------------------------------------------------------------------- 1 | package source 2 | 3 | //go:generate mockgen -destination ../source_mock.go -source=source.go 4 | //go:generate mockgen -package source -destination source_mock.go -source=source.go 5 | 6 | type X struct{} 7 | 8 | type S interface { 9 | F(X) 10 | } 11 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_source/definition/source_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: source.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package source -destination source_mock.go -source=source.go 7 | // 8 | 9 | // Package source is a generated GoMock package. 10 | package source 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockS is a mock of S interface. 19 | type MockS struct { 20 | ctrl *gomock.Controller 21 | recorder *MockSMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockSMockRecorder is the mock recorder for MockS. 26 | type MockSMockRecorder struct { 27 | mock *MockS 28 | } 29 | 30 | // NewMockS creates a new mock instance. 31 | func NewMockS(ctrl *gomock.Controller) *MockS { 32 | mock := &MockS{ctrl: ctrl} 33 | mock.recorder = &MockSMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockS) EXPECT() *MockSMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // F mocks base method. 43 | func (m *MockS) F(arg0 X) { 44 | m.ctrl.T.Helper() 45 | m.ctrl.Call(m, "F", arg0) 46 | } 47 | 48 | // F indicates an expected call of F. 49 | func (mr *MockSMockRecorder) F(arg0 any) *gomock.Call { 50 | mr.mock.ctrl.T.Helper() 51 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockS)(nil).F), arg0) 52 | } 53 | -------------------------------------------------------------------------------- /mockgen/internal/tests/import_source/source_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: source.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination ../source_mock.go -source=source.go 7 | // 8 | 9 | // Package mock_source is a generated GoMock package. 10 | package mock_source 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | source "go.uber.org/mock/mockgen/internal/tests/import_source/definition" 17 | ) 18 | 19 | // MockS is a mock of S interface. 20 | type MockS struct { 21 | ctrl *gomock.Controller 22 | recorder *MockSMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockSMockRecorder is the mock recorder for MockS. 27 | type MockSMockRecorder struct { 28 | mock *MockS 29 | } 30 | 31 | // NewMockS creates a new mock instance. 32 | func NewMockS(ctrl *gomock.Controller) *MockS { 33 | mock := &MockS{ctrl: ctrl} 34 | mock.recorder = &MockSMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockS) EXPECT() *MockSMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // F mocks base method. 44 | func (m *MockS) F(arg0 source.X) { 45 | m.ctrl.T.Helper() 46 | m.ctrl.Call(m, "F", arg0) 47 | } 48 | 49 | // F indicates an expected call of F. 50 | func (mr *MockSMockRecorder) F(arg0 any) *gomock.Call { 51 | mr.mock.ctrl.T.Helper() 52 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockS)(nil).F), arg0) 53 | } 54 | -------------------------------------------------------------------------------- /mockgen/internal/tests/internal_pkg/generate.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | //go:generate mockgen -source subdir/internal/pkg/input.go -destination subdir/internal/pkg/source_output/mock.go 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/internal_pkg/subdir/internal/pkg/input.go: -------------------------------------------------------------------------------- 1 | package pkg 2 | 3 | type Arg interface { 4 | Foo() int 5 | } 6 | 7 | type Intf interface { 8 | F() Arg 9 | } 10 | -------------------------------------------------------------------------------- /mockgen/internal/tests/internal_pkg/subdir/internal/pkg/reflect_output/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/internal_pkg/subdir/internal/pkg (interfaces: Intf) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination subdir/internal/pkg/reflect_output/mock.go go.uber.org/mock/mockgen/internal/tests/internal_pkg/subdir/internal/pkg Intf 7 | // 8 | 9 | // Package mock_pkg is a generated GoMock package. 10 | package mock_pkg 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | pkg "go.uber.org/mock/mockgen/internal/tests/internal_pkg/subdir/internal/pkg" 17 | ) 18 | 19 | // MockIntf is a mock of Intf interface. 20 | type MockIntf struct { 21 | ctrl *gomock.Controller 22 | recorder *MockIntfMockRecorder 23 | } 24 | 25 | // MockIntfMockRecorder is the mock recorder for MockIntf. 26 | type MockIntfMockRecorder struct { 27 | mock *MockIntf 28 | } 29 | 30 | // NewMockIntf creates a new mock instance. 31 | func NewMockIntf(ctrl *gomock.Controller) *MockIntf { 32 | mock := &MockIntf{ctrl: ctrl} 33 | mock.recorder = &MockIntfMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockIntf) EXPECT() *MockIntfMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // F mocks base method. 43 | func (m *MockIntf) F() pkg.Arg { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "F") 46 | ret0, _ := ret[0].(pkg.Arg) 47 | return ret0 48 | } 49 | 50 | // F indicates an expected call of F. 51 | func (mr *MockIntfMockRecorder) F() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockIntf)(nil).F)) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/internal_pkg/subdir/internal/pkg/source_output/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: subdir/internal/pkg/input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -source subdir/internal/pkg/input.go -destination subdir/internal/pkg/source_output/mock.go 7 | // 8 | 9 | // Package mock_pkg is a generated GoMock package. 10 | package mock_pkg 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | pkg "go.uber.org/mock/mockgen/internal/tests/internal_pkg/subdir/internal/pkg" 17 | ) 18 | 19 | // MockArg is a mock of Arg interface. 20 | type MockArg struct { 21 | ctrl *gomock.Controller 22 | recorder *MockArgMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockArgMockRecorder is the mock recorder for MockArg. 27 | type MockArgMockRecorder struct { 28 | mock *MockArg 29 | } 30 | 31 | // NewMockArg creates a new mock instance. 32 | func NewMockArg(ctrl *gomock.Controller) *MockArg { 33 | mock := &MockArg{ctrl: ctrl} 34 | mock.recorder = &MockArgMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockArg) EXPECT() *MockArgMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Foo mocks base method. 44 | func (m *MockArg) Foo() int { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Foo") 47 | ret0, _ := ret[0].(int) 48 | return ret0 49 | } 50 | 51 | // Foo indicates an expected call of Foo. 52 | func (mr *MockArgMockRecorder) Foo() *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockArg)(nil).Foo)) 55 | } 56 | 57 | // MockIntf is a mock of Intf interface. 58 | type MockIntf struct { 59 | ctrl *gomock.Controller 60 | recorder *MockIntfMockRecorder 61 | isgomock struct{} 62 | } 63 | 64 | // MockIntfMockRecorder is the mock recorder for MockIntf. 65 | type MockIntfMockRecorder struct { 66 | mock *MockIntf 67 | } 68 | 69 | // NewMockIntf creates a new mock instance. 70 | func NewMockIntf(ctrl *gomock.Controller) *MockIntf { 71 | mock := &MockIntf{ctrl: ctrl} 72 | mock.recorder = &MockIntfMockRecorder{mock} 73 | return mock 74 | } 75 | 76 | // EXPECT returns an object that allows the caller to indicate expected use. 77 | func (m *MockIntf) EXPECT() *MockIntfMockRecorder { 78 | return m.recorder 79 | } 80 | 81 | // F mocks base method. 82 | func (m *MockIntf) F() pkg.Arg { 83 | m.ctrl.T.Helper() 84 | ret := m.ctrl.Call(m, "F") 85 | ret0, _ := ret[0].(pkg.Arg) 86 | return ret0 87 | } 88 | 89 | // F indicates an expected call of F. 90 | func (mr *MockIntfMockRecorder) F() *gomock.Call { 91 | mr.mock.ctrl.T.Helper() 92 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockIntf)(nil).F)) 93 | } 94 | -------------------------------------------------------------------------------- /mockgen/internal/tests/missing_import/output/source_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: source.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package source -destination=../output/source_mock.go -source=source.go 7 | // 8 | 9 | // Package source is a generated GoMock package. 10 | package source 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | source "go.uber.org/mock/mockgen/internal/tests/missing_import/source" 17 | ) 18 | 19 | // MockBar is a mock of Bar interface. 20 | type MockBar struct { 21 | ctrl *gomock.Controller 22 | recorder *MockBarMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockBarMockRecorder is the mock recorder for MockBar. 27 | type MockBarMockRecorder struct { 28 | mock *MockBar 29 | } 30 | 31 | // NewMockBar creates a new mock instance. 32 | func NewMockBar(ctrl *gomock.Controller) *MockBar { 33 | mock := &MockBar{ctrl: ctrl} 34 | mock.recorder = &MockBarMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockBar) EXPECT() *MockBarMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Baz mocks base method. 44 | func (m *MockBar) Baz(arg0 source.Foo) { 45 | m.ctrl.T.Helper() 46 | m.ctrl.Call(m, "Baz", arg0) 47 | } 48 | 49 | // Baz indicates an expected call of Baz. 50 | func (mr *MockBarMockRecorder) Baz(arg0 any) *gomock.Call { 51 | mr.mock.ctrl.T.Helper() 52 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Baz", reflect.TypeOf((*MockBar)(nil).Baz), arg0) 53 | } 54 | -------------------------------------------------------------------------------- /mockgen/internal/tests/missing_import/source/source.go: -------------------------------------------------------------------------------- 1 | // Package source makes sure output imports its. See #505. 2 | package source 3 | 4 | //go:generate mockgen -package source -destination=../output/source_mock.go -source=source.go 5 | 6 | type Foo struct{} 7 | 8 | type Bar interface { 9 | Baz(Foo) 10 | } 11 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_in_test_package/README.md: -------------------------------------------------------------------------------- 1 | # Mock in Test Package 2 | 3 | Test the case where the package has the `_test` suffix. 4 | 5 | Prior to patch: 6 | 7 | ```bash 8 | $ go generate 9 | $ go test 10 | # github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test] 11 | ./mock_test.go:36:44: undefined: User 12 | ./mock_test.go:38:21: undefined: User 13 | FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed] 14 | ``` 15 | 16 | With this patch applied: 17 | 18 | ```bash 19 | $ go generate 20 | $ go test 21 | ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s 22 | ``` 23 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_in_test_package/mock_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: user.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen --source=user.go --destination=mock_test.go --package=users_test 7 | // 8 | 9 | // Package users_test is a generated GoMock package. 10 | package users_test 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | users "go.uber.org/mock/mockgen/internal/tests/mock_in_test_package" 17 | ) 18 | 19 | // MockFinder is a mock of Finder interface. 20 | type MockFinder struct { 21 | ctrl *gomock.Controller 22 | recorder *MockFinderMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockFinderMockRecorder is the mock recorder for MockFinder. 27 | type MockFinderMockRecorder struct { 28 | mock *MockFinder 29 | } 30 | 31 | // NewMockFinder creates a new mock instance. 32 | func NewMockFinder(ctrl *gomock.Controller) *MockFinder { 33 | mock := &MockFinder{ctrl: ctrl} 34 | mock.recorder = &MockFinderMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockFinder) EXPECT() *MockFinderMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Add mocks base method. 44 | func (m *MockFinder) Add(u users.User) { 45 | m.ctrl.T.Helper() 46 | m.ctrl.Call(m, "Add", u) 47 | } 48 | 49 | // Add indicates an expected call of Add. 50 | func (mr *MockFinderMockRecorder) Add(u any) *gomock.Call { 51 | mr.mock.ctrl.T.Helper() 52 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockFinder)(nil).Add), u) 53 | } 54 | 55 | // FindUser mocks base method. 56 | func (m *MockFinder) FindUser(name string) users.User { 57 | m.ctrl.T.Helper() 58 | ret := m.ctrl.Call(m, "FindUser", name) 59 | ret0, _ := ret[0].(users.User) 60 | return ret0 61 | } 62 | 63 | // FindUser indicates an expected call of FindUser. 64 | func (mr *MockFinderMockRecorder) FindUser(name any) *gomock.Call { 65 | mr.mock.ctrl.T.Helper() 66 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindUser", reflect.TypeOf((*MockFinder)(nil).FindUser), name) 67 | } 68 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_in_test_package/user.go: -------------------------------------------------------------------------------- 1 | package users 2 | 3 | //go:generate mockgen --source=user.go --destination=mock_test.go --package=users_test 4 | 5 | type User struct { 6 | Name string 7 | } 8 | 9 | type Finder interface { 10 | FindUser(name string) User 11 | Add(u User) 12 | } 13 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/mocks.go: -------------------------------------------------------------------------------- 1 | package mock_names 2 | 3 | //go:generate mockgen -mock_names=Service=UserServiceMock -package mocks -typed -destination mocks/user_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/user Service 4 | //go:generate mockgen -mock_names=Service=PostServiceMock -package mocks -typed -destination mocks/post_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/post Service 5 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/mocks/post_service.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/mock_name/post (interfaces: Service) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -mock_names=Service=PostServiceMock -package mocks -typed -destination mocks/post_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/post Service 7 | // 8 | 9 | // Package mocks is a generated GoMock package. 10 | package mocks 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | post "go.uber.org/mock/mockgen/internal/tests/mock_name/post" 17 | user "go.uber.org/mock/mockgen/internal/tests/mock_name/user" 18 | ) 19 | 20 | // PostServiceMock is a mock of Service interface. 21 | type PostServiceMock struct { 22 | ctrl *gomock.Controller 23 | recorder *PostServiceMockMockRecorder 24 | isgomock struct{} 25 | } 26 | 27 | // PostServiceMockMockRecorder is the mock recorder for PostServiceMock. 28 | type PostServiceMockMockRecorder struct { 29 | mock *PostServiceMock 30 | } 31 | 32 | // NewPostServiceMock creates a new mock instance. 33 | func NewPostServiceMock(ctrl *gomock.Controller) *PostServiceMock { 34 | mock := &PostServiceMock{ctrl: ctrl} 35 | mock.recorder = &PostServiceMockMockRecorder{mock} 36 | return mock 37 | } 38 | 39 | // EXPECT returns an object that allows the caller to indicate expected use. 40 | func (m *PostServiceMock) EXPECT() *PostServiceMockMockRecorder { 41 | return m.recorder 42 | } 43 | 44 | // Create mocks base method. 45 | func (m *PostServiceMock) Create(title, body string, author *user.User) (*post.Post, error) { 46 | m.ctrl.T.Helper() 47 | ret := m.ctrl.Call(m, "Create", title, body, author) 48 | ret0, _ := ret[0].(*post.Post) 49 | ret1, _ := ret[1].(error) 50 | return ret0, ret1 51 | } 52 | 53 | // Create indicates an expected call of Create. 54 | func (mr *PostServiceMockMockRecorder) Create(title, body, author any) *PostServiceMockCreateCall { 55 | mr.mock.ctrl.T.Helper() 56 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*PostServiceMock)(nil).Create), title, body, author) 57 | return &PostServiceMockCreateCall{Call: call} 58 | } 59 | 60 | // PostServiceMockCreateCall wrap *gomock.Call 61 | type PostServiceMockCreateCall struct { 62 | *gomock.Call 63 | } 64 | 65 | // Return rewrite *gomock.Call.Return 66 | func (c *PostServiceMockCreateCall) Return(arg0 *post.Post, arg1 error) *PostServiceMockCreateCall { 67 | c.Call = c.Call.Return(arg0, arg1) 68 | return c 69 | } 70 | 71 | // Do rewrite *gomock.Call.Do 72 | func (c *PostServiceMockCreateCall) Do(f func(string, string, *user.User) (*post.Post, error)) *PostServiceMockCreateCall { 73 | c.Call = c.Call.Do(f) 74 | return c 75 | } 76 | 77 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 78 | func (c *PostServiceMockCreateCall) DoAndReturn(f func(string, string, *user.User) (*post.Post, error)) *PostServiceMockCreateCall { 79 | c.Call = c.Call.DoAndReturn(f) 80 | return c 81 | } 82 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/mocks/user_service.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/mock_name/user (interfaces: Service) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -mock_names=Service=UserServiceMock -package mocks -typed -destination mocks/user_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/user Service 7 | // 8 | 9 | // Package mocks is a generated GoMock package. 10 | package mocks 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | user "go.uber.org/mock/mockgen/internal/tests/mock_name/user" 17 | ) 18 | 19 | // UserServiceMock is a mock of Service interface. 20 | type UserServiceMock struct { 21 | ctrl *gomock.Controller 22 | recorder *UserServiceMockMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // UserServiceMockMockRecorder is the mock recorder for UserServiceMock. 27 | type UserServiceMockMockRecorder struct { 28 | mock *UserServiceMock 29 | } 30 | 31 | // NewUserServiceMock creates a new mock instance. 32 | func NewUserServiceMock(ctrl *gomock.Controller) *UserServiceMock { 33 | mock := &UserServiceMock{ctrl: ctrl} 34 | mock.recorder = &UserServiceMockMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *UserServiceMock) EXPECT() *UserServiceMockMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Create mocks base method. 44 | func (m *UserServiceMock) Create(name string) (*user.User, error) { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Create", name) 47 | ret0, _ := ret[0].(*user.User) 48 | ret1, _ := ret[1].(error) 49 | return ret0, ret1 50 | } 51 | 52 | // Create indicates an expected call of Create. 53 | func (mr *UserServiceMockMockRecorder) Create(name any) *UserServiceMockCreateCall { 54 | mr.mock.ctrl.T.Helper() 55 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*UserServiceMock)(nil).Create), name) 56 | return &UserServiceMockCreateCall{Call: call} 57 | } 58 | 59 | // UserServiceMockCreateCall wrap *gomock.Call 60 | type UserServiceMockCreateCall struct { 61 | *gomock.Call 62 | } 63 | 64 | // Return rewrite *gomock.Call.Return 65 | func (c *UserServiceMockCreateCall) Return(arg0 *user.User, arg1 error) *UserServiceMockCreateCall { 66 | c.Call = c.Call.Return(arg0, arg1) 67 | return c 68 | } 69 | 70 | // Do rewrite *gomock.Call.Do 71 | func (c *UserServiceMockCreateCall) Do(f func(string) (*user.User, error)) *UserServiceMockCreateCall { 72 | c.Call = c.Call.Do(f) 73 | return c 74 | } 75 | 76 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 77 | func (c *UserServiceMockCreateCall) DoAndReturn(f func(string) (*user.User, error)) *UserServiceMockCreateCall { 78 | c.Call = c.Call.DoAndReturn(f) 79 | return c 80 | } 81 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/mocks_test.go: -------------------------------------------------------------------------------- 1 | package mock_names 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | "go.uber.org/mock/mockgen/internal/tests/mock_name/mocks" 8 | "go.uber.org/mock/mockgen/internal/tests/mock_name/post" 9 | "go.uber.org/mock/mockgen/internal/tests/mock_name/user" 10 | ) 11 | 12 | func TestMockNames(t *testing.T) { 13 | ctrl := gomock.NewController(t) 14 | 15 | userService := mocks.NewUserServiceMock(ctrl) 16 | postService := mocks.NewPostServiceMock(ctrl) 17 | 18 | gomock.InOrder( 19 | userService.EXPECT(). 20 | Create("John Doe"). 21 | Return(&user.User{Name: "John Doe"}, nil), 22 | postService.EXPECT(). 23 | Create(gomock.Eq("test title"), gomock.Eq("test body"), gomock.Eq(&user.User{Name: "John Doe"})). 24 | Return(&post.Post{ 25 | Title: "test title", 26 | Body: "test body", 27 | Author: &user.User{ 28 | Name: "John Doe", 29 | }, 30 | }, nil)) 31 | u, err := userService.Create("John Doe") 32 | if err != nil { 33 | t.Fatal("unexpected error") 34 | } 35 | 36 | p, err := postService.Create("test title", "test body", u) 37 | if err != nil { 38 | t.Fatal("unexpected error") 39 | } 40 | 41 | if p.Title != "test title" || p.Body != "test body" || p.Author.Name != u.Name { 42 | t.Fatal("unexpected postService.Create result") 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/post/service.go: -------------------------------------------------------------------------------- 1 | package post 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/mock_name/user" 5 | ) 6 | 7 | type Post struct { 8 | Title string 9 | Body string 10 | Author *user.User 11 | } 12 | 13 | type Service interface { 14 | Create(title, body string, author *user.User) (*Post, error) 15 | } 16 | -------------------------------------------------------------------------------- /mockgen/internal/tests/mock_name/user/service.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | type User struct { 4 | Name string 5 | } 6 | 7 | type Service interface { 8 | Create(name string) (*User, error) 9 | } 10 | -------------------------------------------------------------------------------- /mockgen/internal/tests/overlapping_methods/interfaces.go: -------------------------------------------------------------------------------- 1 | package overlap 2 | 3 | type ReadCloser interface { 4 | Read([]byte) (int, error) 5 | Close() error 6 | } 7 | 8 | type WriteCloser interface { 9 | Write([]byte) (int, error) 10 | Close() error 11 | } 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/overlapping_methods/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: overlap.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package overlap -destination mock.go -source overlap.go -aux_files go.uber.org/mock/mockgen/internal/tests/overlapping_methods=interfaces.go 7 | // 8 | 9 | // Package overlap is a generated GoMock package. 10 | package overlap 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockReadWriteCloser is a mock of ReadWriteCloser interface. 19 | type MockReadWriteCloser struct { 20 | ctrl *gomock.Controller 21 | recorder *MockReadWriteCloserMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockReadWriteCloserMockRecorder is the mock recorder for MockReadWriteCloser. 26 | type MockReadWriteCloserMockRecorder struct { 27 | mock *MockReadWriteCloser 28 | } 29 | 30 | // NewMockReadWriteCloser creates a new mock instance. 31 | func NewMockReadWriteCloser(ctrl *gomock.Controller) *MockReadWriteCloser { 32 | mock := &MockReadWriteCloser{ctrl: ctrl} 33 | mock.recorder = &MockReadWriteCloserMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockReadWriteCloser) EXPECT() *MockReadWriteCloserMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Close mocks base method. 43 | func (m *MockReadWriteCloser) Close() error { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Close") 46 | ret0, _ := ret[0].(error) 47 | return ret0 48 | } 49 | 50 | // Close indicates an expected call of Close. 51 | func (mr *MockReadWriteCloserMockRecorder) Close() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockReadWriteCloser)(nil).Close)) 54 | } 55 | 56 | // Read mocks base method. 57 | func (m *MockReadWriteCloser) Read(arg0 []byte) (int, error) { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "Read", arg0) 60 | ret0, _ := ret[0].(int) 61 | ret1, _ := ret[1].(error) 62 | return ret0, ret1 63 | } 64 | 65 | // Read indicates an expected call of Read. 66 | func (mr *MockReadWriteCloserMockRecorder) Read(arg0 any) *gomock.Call { 67 | mr.mock.ctrl.T.Helper() 68 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockReadWriteCloser)(nil).Read), arg0) 69 | } 70 | 71 | // Write mocks base method. 72 | func (m *MockReadWriteCloser) Write(arg0 []byte) (int, error) { 73 | m.ctrl.T.Helper() 74 | ret := m.ctrl.Call(m, "Write", arg0) 75 | ret0, _ := ret[0].(int) 76 | ret1, _ := ret[1].(error) 77 | return ret0, ret1 78 | } 79 | 80 | // Write indicates an expected call of Write. 81 | func (mr *MockReadWriteCloserMockRecorder) Write(arg0 any) *gomock.Call { 82 | mr.mock.ctrl.T.Helper() 83 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockReadWriteCloser)(nil).Write), arg0) 84 | } 85 | -------------------------------------------------------------------------------- /mockgen/internal/tests/overlapping_methods/overlap.go: -------------------------------------------------------------------------------- 1 | package overlap 2 | 3 | //go:generate mockgen -package overlap -destination mock.go -source overlap.go -aux_files go.uber.org/mock/mockgen/internal/tests/overlapping_methods=interfaces.go 4 | 5 | type ReadWriteCloser interface { 6 | ReadCloser 7 | WriteCloser 8 | } 9 | -------------------------------------------------------------------------------- /mockgen/internal/tests/overlapping_methods/overlap_test.go: -------------------------------------------------------------------------------- 1 | package overlap 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | gomock "go.uber.org/mock/gomock" 8 | ) 9 | 10 | // TestValidInterface assesses whether or not the generated mock is valid 11 | func TestValidInterface(t *testing.T) { 12 | ctrl := gomock.NewController(t) 13 | 14 | s := NewMockReadWriteCloser(ctrl) 15 | s.EXPECT().Close().Return(errors.New("test")) 16 | 17 | s.Close() 18 | } 19 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_comment/input.go: -------------------------------------------------------------------------------- 1 | package empty_interface 2 | 3 | //go:generate mockgen -package empty_interface -destination mock.go -source input.go -write_package_comment=false 4 | 5 | type Empty interface{} 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_comment/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package empty_interface -destination mock.go -source input.go -write_package_comment=false 7 | // 8 | 9 | package empty_interface 10 | 11 | import ( 12 | gomock "go.uber.org/mock/gomock" 13 | ) 14 | 15 | // MockEmpty is a mock of Empty interface. 16 | type MockEmpty struct { 17 | ctrl *gomock.Controller 18 | recorder *MockEmptyMockRecorder 19 | isgomock struct{} 20 | } 21 | 22 | // MockEmptyMockRecorder is the mock recorder for MockEmpty. 23 | type MockEmptyMockRecorder struct { 24 | mock *MockEmpty 25 | } 26 | 27 | // NewMockEmpty creates a new mock instance. 28 | func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { 29 | mock := &MockEmpty{ctrl: ctrl} 30 | mock.recorder = &MockEmptyMockRecorder{mock} 31 | return mock 32 | } 33 | 34 | // EXPECT returns an object that allows the caller to indicate expected use. 35 | func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { 36 | return m.recorder 37 | } 38 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_mode/cars/cars.go: -------------------------------------------------------------------------------- 1 | package cars 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/package_mode/fuel" 5 | ) 6 | 7 | type FuelTank[FuelType fuel.Fuel] struct { 8 | Fuel FuelType 9 | Capacity int 10 | } 11 | 12 | type HyundaiSolaris struct{} 13 | 14 | func (HyundaiSolaris) Refuel(fuel.Gasoline, int) error { 15 | return nil 16 | } 17 | 18 | func (HyundaiSolaris) Brand() string { 19 | return "Hyundai" 20 | } 21 | 22 | func (HyundaiSolaris) FuelTank() FuelTank[fuel.Gasoline] { 23 | return FuelTank[fuel.Gasoline]{ 24 | Fuel: fuel.Gasoline{}, 25 | Capacity: 50, 26 | } 27 | } 28 | 29 | type FordF150 struct{} 30 | 31 | func (FordF150) Brand() string { 32 | return "Ford" 33 | } 34 | 35 | func (FordF150) Refuel(fuel.Diesel, int) error { 36 | return nil 37 | } 38 | 39 | func (FordF150) FuelTank() FuelTank[fuel.Diesel] { 40 | return FuelTank[fuel.Diesel]{ 41 | Fuel: fuel.Diesel{}, 42 | Capacity: 136, 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_mode/fuel/fuel.go: -------------------------------------------------------------------------------- 1 | package fuel 2 | 3 | type Fuel interface { 4 | EnergyCapacity() int 5 | Diesel | Gasoline 6 | } 7 | 8 | type Diesel struct{} 9 | 10 | func (Diesel) EnergyCapacity() int { 11 | return 48 12 | } 13 | 14 | type Gasoline struct{} 15 | 16 | func (Gasoline) EnergyCapacity() int { 17 | return 46 18 | } 19 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_mode/interfaces.go: -------------------------------------------------------------------------------- 1 | package package_mode 2 | 3 | //go:generate mockgen -typed -package=mock -destination=mock/interfaces.go . Food,Eater,Animal,Human,Primate,Car,Driver,UrbanResident,Farmer,Earth 4 | 5 | import ( 6 | "time" 7 | 8 | "go.uber.org/mock/mockgen/internal/tests/package_mode/cars" 9 | "go.uber.org/mock/mockgen/internal/tests/package_mode/fuel" 10 | ) 11 | 12 | type Food interface { 13 | Calories() int 14 | } 15 | 16 | type Eater interface { 17 | Eat(foods ...Food) 18 | } 19 | 20 | type Animal interface { 21 | Eater 22 | Breathe() 23 | Sleep(duration time.Duration) 24 | } 25 | 26 | type Primate Animal 27 | 28 | type Human = Primate 29 | 30 | type Car[FuelType fuel.Fuel] interface { 31 | Brand() string 32 | FuelTank() cars.FuelTank[FuelType] 33 | Refuel(fuel FuelType, volume int) error 34 | } 35 | 36 | type Driver[FuelType fuel.Fuel, CarType Car[FuelType]] interface { 37 | Wroom() error 38 | Drive(car CarType) 39 | } 40 | 41 | type UrbanResident interface { 42 | Human 43 | Driver[fuel.Gasoline, cars.HyundaiSolaris] 44 | Do(work *Work) error 45 | LivesInACity() 46 | } 47 | 48 | type Farmer interface { 49 | Human 50 | Driver[fuel.Diesel, cars.FordF150] 51 | Do(work *Work) error 52 | LivesInAVillage() 53 | } 54 | 55 | type Work struct { 56 | Name string 57 | } 58 | 59 | type Counter interface { 60 | int 61 | } 62 | 63 | type HumansCount = int 64 | 65 | type Earth interface { 66 | AddHumans(HumansCount) []Human 67 | HumanPopulation() HumansCount 68 | } 69 | -------------------------------------------------------------------------------- /mockgen/internal/tests/package_mode/mock/interfaces_test.go: -------------------------------------------------------------------------------- 1 | package mock 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | "go.uber.org/mock/mockgen/internal/tests/package_mode" 8 | "go.uber.org/mock/mockgen/internal/tests/package_mode/cars" 9 | "go.uber.org/mock/mockgen/internal/tests/package_mode/fuel" 10 | ) 11 | 12 | // checks, that mocks implement interfaces in compile-time. 13 | // If something breaks, the tests will not be compiled. 14 | 15 | var food package_mode.Food = &MockFood{} 16 | 17 | var eater package_mode.Eater = &MockEater{} 18 | 19 | var animal package_mode.Animal = &MockAnimal{} 20 | 21 | var human package_mode.Human = &MockHuman{} 22 | 23 | var primate package_mode.Primate = &MockPrimate{} 24 | 25 | var car package_mode.Car[fuel.Gasoline] = &MockCar[fuel.Gasoline]{} 26 | 27 | var driver package_mode.Driver[fuel.Gasoline, cars.HyundaiSolaris] = &MockDriver[fuel.Gasoline, cars.HyundaiSolaris]{} 28 | 29 | var urbanResident package_mode.UrbanResident = &MockUrbanResident{} 30 | 31 | var farmer package_mode.Farmer = &MockFarmer{} 32 | 33 | func TestInterfaces(t *testing.T) { 34 | ctrl := gomock.NewController(t) 35 | 36 | mock := NewMockFarmer(ctrl) 37 | mock.EXPECT().Breathe() 38 | 39 | farmer := package_mode.Farmer(mock) 40 | farmer.Breathe() 41 | } 42 | -------------------------------------------------------------------------------- /mockgen/internal/tests/panicing_test/mock_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: panic.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen --source=panic.go --destination=mock_test.go --package=paniccode 7 | // 8 | 9 | // Package paniccode is a generated GoMock package. 10 | package paniccode 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockFoo is a mock of Foo interface. 19 | type MockFoo struct { 20 | ctrl *gomock.Controller 21 | recorder *MockFooMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockFooMockRecorder is the mock recorder for MockFoo. 26 | type MockFooMockRecorder struct { 27 | mock *MockFoo 28 | } 29 | 30 | // NewMockFoo creates a new mock instance. 31 | func NewMockFoo(ctrl *gomock.Controller) *MockFoo { 32 | mock := &MockFoo{ctrl: ctrl} 33 | mock.recorder = &MockFooMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockFoo) EXPECT() *MockFooMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Bar mocks base method. 43 | func (m *MockFoo) Bar() string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Bar") 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // Bar indicates an expected call of Bar. 51 | func (mr *MockFooMockRecorder) Bar() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar)) 54 | } 55 | 56 | // Baz mocks base method. 57 | func (m *MockFoo) Baz() string { 58 | m.ctrl.T.Helper() 59 | ret := m.ctrl.Call(m, "Baz") 60 | ret0, _ := ret[0].(string) 61 | return ret0 62 | } 63 | 64 | // Baz indicates an expected call of Baz. 65 | func (mr *MockFooMockRecorder) Baz() *gomock.Call { 66 | mr.mock.ctrl.T.Helper() 67 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Baz", reflect.TypeOf((*MockFoo)(nil).Baz)) 68 | } 69 | -------------------------------------------------------------------------------- /mockgen/internal/tests/panicing_test/panic.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package paniccode 16 | 17 | //go:generate mockgen --source=panic.go --destination=mock_test.go --package=paniccode 18 | 19 | type Foo interface { 20 | Bar() string 21 | Baz() string 22 | } 23 | 24 | func Danger(f Foo) { 25 | if f.Bar() == "Bar" { 26 | panic("Danger, Will Robinson!") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /mockgen/internal/tests/panicing_test/panic_test.go: -------------------------------------------------------------------------------- 1 | //go:build panictest 2 | // +build panictest 3 | 4 | // Copyright 2020 Google LLC 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | 18 | package paniccode 19 | 20 | import ( 21 | "testing" 22 | 23 | "go.uber.org/mock/gomock" 24 | ) 25 | 26 | func TestDanger_Panics_Explicit(t *testing.T) { 27 | ctrl := gomock.NewController(t) 28 | mock := NewMockFoo(ctrl) 29 | mock.EXPECT().Bar().Return("Bar") 30 | mock.EXPECT().Bar().Return("Baz") 31 | Danger(mock) 32 | } 33 | 34 | func TestDanger_Panics_Implicit(t *testing.T) { 35 | ctrl := gomock.NewController(t) 36 | mock := NewMockFoo(ctrl) 37 | mock.EXPECT().Bar().Return("Bar") 38 | mock.EXPECT().Bar().Return("Baz") 39 | Danger(mock) 40 | } 41 | -------------------------------------------------------------------------------- /mockgen/internal/tests/parenthesized_parameter_type/README.md: -------------------------------------------------------------------------------- 1 | # Parenthesized Parameter Type 2 | 3 | Test for [Issue#416](https://github.com/golang/mock/issues/416). 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/parenthesized_parameter_type/input.go: -------------------------------------------------------------------------------- 1 | package parenthesized_parameter_type 2 | 3 | type Example interface { 4 | ParenthesizedParameterType(param *(int)) 5 | } 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/parenthesized_parameter_type/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: ./mockgen/internal/tests/parenthesized_parameter_type/input.go 3 | 4 | // Package parenthesized_parameter_type is a generated GoMock package. 5 | package parenthesized_parameter_type 6 | 7 | import ( 8 | gomock "go.uber.org/mock/gomock" 9 | reflect "reflect" 10 | ) 11 | 12 | // MockExample is a mock of Example interface. 13 | type MockExample struct { 14 | ctrl *gomock.Controller 15 | recorder *MockExampleMockRecorder 16 | } 17 | 18 | // MockExampleMockRecorder is the mock recorder for MockExample. 19 | type MockExampleMockRecorder struct { 20 | mock *MockExample 21 | } 22 | 23 | // NewMockExample creates a new mock instance. 24 | func NewMockExample(ctrl *gomock.Controller) *MockExample { 25 | mock := &MockExample{ctrl: ctrl} 26 | mock.recorder = &MockExampleMockRecorder{mock} 27 | return mock 28 | } 29 | 30 | // EXPECT returns an object that allows the caller to indicate expected use. 31 | func (m *MockExample) EXPECT() *MockExampleMockRecorder { 32 | return m.recorder 33 | } 34 | 35 | // ParenthesizedParameterType mocks base method. 36 | func (m *MockExample) ParenthesizedParameterType(param *int) { 37 | m.ctrl.T.Helper() 38 | m.ctrl.Call(m, "ParenthesizedParameterType", param) 39 | } 40 | 41 | // ParenthesizedParameterType indicates an expected call of ParenthesizedParameterType. 42 | func (mr *MockExampleMockRecorder) ParenthesizedParameterType(param any) *gomock.Call { 43 | mr.mock.ctrl.T.Helper() 44 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ParenthesizedParameterType", reflect.TypeOf((*MockExample)(nil).ParenthesizedParameterType), param) 45 | } 46 | -------------------------------------------------------------------------------- /mockgen/internal/tests/performance/big_interface/big_interface.go: -------------------------------------------------------------------------------- 1 | package big_interface 2 | 3 | type Bar struct{} 4 | 5 | type BigInterface interface { 6 | Foo1(bool, bool, bool, bool, bool, bool, bool, bool) Bar 7 | Foo2(bool, bool, bool, bool, bool, bool, bool, bool) Bar 8 | Foo3(bool, bool, bool, bool, bool, bool, bool, bool) Bar 9 | Foo4(bool, bool, bool, bool, bool, bool, bool, bool) Bar 10 | Foo5(bool, bool, bool, bool, bool, bool, bool, bool) Bar 11 | Foo6(bool, bool, bool, bool, bool, bool, bool, bool) Bar 12 | Foo7(bool, bool, bool, bool, bool, bool, bool, bool) Bar 13 | Foo8(bool, bool, bool, bool, bool, bool, bool, bool) Bar 14 | Foo9(bool, bool, bool, bool, bool, bool, bool, bool) Bar 15 | Foo10(bool, bool, bool, bool, bool, bool, bool, bool) Bar 16 | Foo11(bool, bool, bool, bool, bool, bool, bool, bool) Bar 17 | Foo12(bool, bool, bool, bool, bool, bool, bool, bool) Bar 18 | Foo13(bool, bool, bool, bool, bool, bool, bool, bool) Bar 19 | Foo14(bool, bool, bool, bool, bool, bool, bool, bool) Bar 20 | Foo15(bool, bool, bool, bool, bool, bool, bool, bool) Bar 21 | Foo16(bool, bool, bool, bool, bool, bool, bool, bool) Bar 22 | Foo17(bool, bool, bool, bool, bool, bool, bool, bool) Bar 23 | Foo18(bool, bool, bool, bool, bool, bool, bool, bool) Bar 24 | Foo19(bool, bool, bool, bool, bool, bool, bool, bool) Bar 25 | Foo20(bool, bool, bool, bool, bool, bool, bool, bool) Bar 26 | Foo21(bool, bool, bool, bool, bool, bool, bool, bool) Bar 27 | Foo22(bool, bool, bool, bool, bool, bool, bool, bool) Bar 28 | Foo23(bool, bool, bool, bool, bool, bool, bool, bool) Bar 29 | Foo24(bool, bool, bool, bool, bool, bool, bool, bool) Bar 30 | Foo25(bool, bool, bool, bool, bool, bool, bool, bool) Bar 31 | Foo26(bool, bool, bool, bool, bool, bool, bool, bool) Bar 32 | Foo27(bool, bool, bool, bool, bool, bool, bool, bool) Bar 33 | Foo28(bool, bool, bool, bool, bool, bool, bool, bool) Bar 34 | Foo29(bool, bool, bool, bool, bool, bool, bool, bool) Bar 35 | Foo30(bool, bool, bool, bool, bool, bool, bool, bool) Bar 36 | Foo31(bool, bool, bool, bool, bool, bool, bool, bool) Bar 37 | Foo32(bool, bool, bool, bool, bool, bool, bool, bool) Bar 38 | Foo33(bool, bool, bool, bool, bool, bool, bool, bool) Bar 39 | Foo34(bool, bool, bool, bool, bool, bool, bool, bool) Bar 40 | Foo35(bool, bool, bool, bool, bool, bool, bool, bool) Bar 41 | Foo36(bool, bool, bool, bool, bool, bool, bool, bool) Bar 42 | Foo37(bool, bool, bool, bool, bool, bool, bool, bool) Bar 43 | Foo38(bool, bool, bool, bool, bool, bool, bool, bool) Bar 44 | Foo39(bool, bool, bool, bool, bool, bool, bool, bool) Bar 45 | Foo40(bool, bool, bool, bool, bool, bool, bool, bool) Bar 46 | Foo41(bool, bool, bool, bool, bool, bool, bool, bool) Bar 47 | Foo42(bool, bool, bool, bool, bool, bool, bool, bool) Bar 48 | Foo43(bool, bool, bool, bool, bool, bool, bool, bool) Bar 49 | Foo44(bool, bool, bool, bool, bool, bool, bool, bool) Bar 50 | Foo45(bool, bool, bool, bool, bool, bool, bool, bool) Bar 51 | Foo46(bool, bool, bool, bool, bool, bool, bool, bool) Bar 52 | Foo47(bool, bool, bool, bool, bool, bool, bool, bool) Bar 53 | Foo48(bool, bool, bool, bool, bool, bool, bool, bool) Bar 54 | Foo49(bool, bool, bool, bool, bool, bool, bool, bool) Bar 55 | Foo50(bool, bool, bool, bool, bool, bool, bool, bool) Bar 56 | Foo51(bool, bool, bool, bool, bool, bool, bool, bool) Bar 57 | Foo52(bool, bool, bool, bool, bool, bool, bool, bool) Bar 58 | Foo53(bool, bool, bool, bool, bool, bool, bool, bool) Bar 59 | Foo54(bool, bool, bool, bool, bool, bool, bool, bool) Bar 60 | Foo55(bool, bool, bool, bool, bool, bool, bool, bool) Bar 61 | Foo56(bool, bool, bool, bool, bool, bool, bool, bool) Bar 62 | Foo57(bool, bool, bool, bool, bool, bool, bool, bool) Bar 63 | Foo58(bool, bool, bool, bool, bool, bool, bool, bool) Bar 64 | Foo59(bool, bool, bool, bool, bool, bool, bool, bool) Bar 65 | Foo60(bool, bool, bool, bool, bool, bool, bool, bool) Bar 66 | } 67 | -------------------------------------------------------------------------------- /mockgen/internal/tests/sanitization/any/any.go: -------------------------------------------------------------------------------- 1 | package any 2 | 3 | // Any is a type of a package that tests the sanitization of imported packages 4 | // named any. 5 | type Any struct{} 6 | -------------------------------------------------------------------------------- /mockgen/internal/tests/sanitization/interface.go: -------------------------------------------------------------------------------- 1 | package sanitization 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/sanitization/any" 5 | ) 6 | 7 | //go:generate mockgen -destination mockout/mock.go -package mockout . AnyMock 8 | 9 | type AnyMock interface { 10 | Do(a *any.Any, b int) 11 | } 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/sanitization/mockout/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/sanitization (interfaces: AnyMock) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination mockout/mock.go -package mockout . AnyMock 7 | // 8 | 9 | // Package mockout is a generated GoMock package. 10 | package mockout 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | any0 "go.uber.org/mock/mockgen/internal/tests/sanitization/any" 17 | ) 18 | 19 | // MockAnyMock is a mock of AnyMock interface. 20 | type MockAnyMock struct { 21 | ctrl *gomock.Controller 22 | recorder *MockAnyMockMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockAnyMockMockRecorder is the mock recorder for MockAnyMock. 27 | type MockAnyMockMockRecorder struct { 28 | mock *MockAnyMock 29 | } 30 | 31 | // NewMockAnyMock creates a new mock instance. 32 | func NewMockAnyMock(ctrl *gomock.Controller) *MockAnyMock { 33 | mock := &MockAnyMock{ctrl: ctrl} 34 | mock.recorder = &MockAnyMockMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockAnyMock) EXPECT() *MockAnyMockMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Do mocks base method. 44 | func (m *MockAnyMock) Do(a *any0.Any, b int) { 45 | m.ctrl.T.Helper() 46 | m.ctrl.Call(m, "Do", a, b) 47 | } 48 | 49 | // Do indicates an expected call of Do. 50 | func (mr *MockAnyMockMockRecorder) Do(a, b any) *gomock.Call { 51 | mr.mock.ctrl.T.Helper() 52 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockAnyMock)(nil).Do), a, b) 53 | } 54 | -------------------------------------------------------------------------------- /mockgen/internal/tests/sanitization/sanitization_test.go: -------------------------------------------------------------------------------- 1 | package sanitization 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | any0 "go.uber.org/mock/mockgen/internal/tests/sanitization/any" 8 | "go.uber.org/mock/mockgen/internal/tests/sanitization/mockout" 9 | ) 10 | 11 | func TestSanitization(t *testing.T) { 12 | ctrl := gomock.NewController(t) 13 | m := mockout.NewMockAnyMock(ctrl) 14 | m.EXPECT().Do(gomock.Any(), 1) 15 | m.Do(&any0.Any{}, 1) 16 | } 17 | -------------------------------------------------------------------------------- /mockgen/internal/tests/self_package/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/self_package (interfaces: Methods) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package core -self_package go.uber.org/mock/mockgen/internal/tests/self_package -destination mock.go go.uber.org/mock/mockgen/internal/tests/self_package Methods 7 | // 8 | 9 | // Package core is a generated GoMock package. 10 | package core 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockMethods is a mock of Methods interface. 19 | type MockMethods struct { 20 | ctrl *gomock.Controller 21 | recorder *MockMethodsMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockMethodsMockRecorder is the mock recorder for MockMethods. 26 | type MockMethodsMockRecorder struct { 27 | mock *MockMethods 28 | } 29 | 30 | // NewMockMethods creates a new mock instance. 31 | func NewMockMethods(ctrl *gomock.Controller) *MockMethods { 32 | mock := &MockMethods{ctrl: ctrl} 33 | mock.recorder = &MockMethodsMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockMethods) EXPECT() *MockMethodsMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // getInfo mocks base method. 43 | func (m *MockMethods) getInfo() Info { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "getInfo") 46 | ret0, _ := ret[0].(Info) 47 | return ret0 48 | } 49 | 50 | // getInfo indicates an expected call of getInfo. 51 | func (mr *MockMethodsMockRecorder) getInfo() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "getInfo", reflect.TypeOf((*MockMethods)(nil).getInfo)) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/self_package/types.go: -------------------------------------------------------------------------------- 1 | package core 2 | 3 | //go:generate mockgen -package core -self_package go.uber.org/mock/mockgen/internal/tests/self_package -destination mock.go go.uber.org/mock/mockgen/internal/tests/self_package Methods 4 | 5 | type Info struct{} 6 | 7 | type Methods interface { 8 | getInfo() Info 9 | } 10 | -------------------------------------------------------------------------------- /mockgen/internal/tests/test_package/foo.go: -------------------------------------------------------------------------------- 1 | package users 2 | -------------------------------------------------------------------------------- /mockgen/internal/tests/test_package/mock_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: user_test.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen --source=user_test.go --destination=mock_test.go --package=users_test 7 | // 8 | 9 | // Package users_test is a generated GoMock package. 10 | package users_test 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockFinder is a mock of Finder interface. 19 | type MockFinder struct { 20 | ctrl *gomock.Controller 21 | recorder *MockFinderMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockFinderMockRecorder is the mock recorder for MockFinder. 26 | type MockFinderMockRecorder struct { 27 | mock *MockFinder 28 | } 29 | 30 | // NewMockFinder creates a new mock instance. 31 | func NewMockFinder(ctrl *gomock.Controller) *MockFinder { 32 | mock := &MockFinder{ctrl: ctrl} 33 | mock.recorder = &MockFinderMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockFinder) EXPECT() *MockFinderMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Add mocks base method. 43 | func (m *MockFinder) Add(u User) { 44 | m.ctrl.T.Helper() 45 | m.ctrl.Call(m, "Add", u) 46 | } 47 | 48 | // Add indicates an expected call of Add. 49 | func (mr *MockFinderMockRecorder) Add(u any) *gomock.Call { 50 | mr.mock.ctrl.T.Helper() 51 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockFinder)(nil).Add), u) 52 | } 53 | 54 | // FindUser mocks base method. 55 | func (m *MockFinder) FindUser(name string) User { 56 | m.ctrl.T.Helper() 57 | ret := m.ctrl.Call(m, "FindUser", name) 58 | ret0, _ := ret[0].(User) 59 | return ret0 60 | } 61 | 62 | // FindUser indicates an expected call of FindUser. 63 | func (mr *MockFinderMockRecorder) FindUser(name any) *gomock.Call { 64 | mr.mock.ctrl.T.Helper() 65 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindUser", reflect.TypeOf((*MockFinder)(nil).FindUser), name) 66 | } 67 | -------------------------------------------------------------------------------- /mockgen/internal/tests/test_package/user_test.go: -------------------------------------------------------------------------------- 1 | package users_test 2 | 3 | //go:generate mockgen --source=user_test.go --destination=mock_test.go --package=users_test 4 | 5 | type User struct { 6 | Name string 7 | } 8 | 9 | type Finder interface { 10 | FindUser(name string) User 11 | Add(u User) 12 | } 13 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/bugreport.go: -------------------------------------------------------------------------------- 1 | package typed 2 | 3 | //go:generate mockgen -typed -aux_files faux=faux/faux.go -destination bugreport_mock.go -package typed -source=bugreport.go Example 4 | 5 | import ( 6 | "log" 7 | 8 | "go.uber.org/mock/mockgen/internal/tests/typed/faux" 9 | ) 10 | 11 | // Source is an interface w/ an embedded foreign interface 12 | type Source interface { 13 | faux.Foreign 14 | } 15 | 16 | func CallForeignMethod(s Source) { 17 | log.Println(s.Method()) 18 | } 19 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/bugreport_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: bugreport.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -typed -aux_files faux=faux/faux.go -destination bugreport_mock.go -package typed -source=bugreport.go Example 7 | // 8 | 9 | // Package typed is a generated GoMock package. 10 | package typed 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | faux "go.uber.org/mock/mockgen/internal/tests/typed/faux" 17 | ) 18 | 19 | // MockSource is a mock of Source interface. 20 | type MockSource struct { 21 | ctrl *gomock.Controller 22 | recorder *MockSourceMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockSourceMockRecorder is the mock recorder for MockSource. 27 | type MockSourceMockRecorder struct { 28 | mock *MockSource 29 | } 30 | 31 | // NewMockSource creates a new mock instance. 32 | func NewMockSource(ctrl *gomock.Controller) *MockSource { 33 | mock := &MockSource{ctrl: ctrl} 34 | mock.recorder = &MockSourceMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockSource) EXPECT() *MockSourceMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Error mocks base method. 44 | func (m *MockSource) Error() string { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Error") 47 | ret0, _ := ret[0].(string) 48 | return ret0 49 | } 50 | 51 | // Error indicates an expected call of Error. 52 | func (mr *MockSourceMockRecorder) Error() *MockSourceErrorCall { 53 | mr.mock.ctrl.T.Helper() 54 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockSource)(nil).Error)) 55 | return &MockSourceErrorCall{Call: call} 56 | } 57 | 58 | // MockSourceErrorCall wrap *gomock.Call 59 | type MockSourceErrorCall struct { 60 | *gomock.Call 61 | } 62 | 63 | // Return rewrite *gomock.Call.Return 64 | func (c *MockSourceErrorCall) Return(arg0 string) *MockSourceErrorCall { 65 | c.Call = c.Call.Return(arg0) 66 | return c 67 | } 68 | 69 | // Do rewrite *gomock.Call.Do 70 | func (c *MockSourceErrorCall) Do(f func() string) *MockSourceErrorCall { 71 | c.Call = c.Call.Do(f) 72 | return c 73 | } 74 | 75 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 76 | func (c *MockSourceErrorCall) DoAndReturn(f func() string) *MockSourceErrorCall { 77 | c.Call = c.Call.DoAndReturn(f) 78 | return c 79 | } 80 | 81 | // Method mocks base method. 82 | func (m *MockSource) Method() faux.Return { 83 | m.ctrl.T.Helper() 84 | ret := m.ctrl.Call(m, "Method") 85 | ret0, _ := ret[0].(faux.Return) 86 | return ret0 87 | } 88 | 89 | // Method indicates an expected call of Method. 90 | func (mr *MockSourceMockRecorder) Method() *MockSourceMethodCall { 91 | mr.mock.ctrl.T.Helper() 92 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Method", reflect.TypeOf((*MockSource)(nil).Method)) 93 | return &MockSourceMethodCall{Call: call} 94 | } 95 | 96 | // MockSourceMethodCall wrap *gomock.Call 97 | type MockSourceMethodCall struct { 98 | *gomock.Call 99 | } 100 | 101 | // Return rewrite *gomock.Call.Return 102 | func (c *MockSourceMethodCall) Return(arg0 faux.Return) *MockSourceMethodCall { 103 | c.Call = c.Call.Return(arg0) 104 | return c 105 | } 106 | 107 | // Do rewrite *gomock.Call.Do 108 | func (c *MockSourceMethodCall) Do(f func() faux.Return) *MockSourceMethodCall { 109 | c.Call = c.Call.Do(f) 110 | return c 111 | } 112 | 113 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 114 | func (c *MockSourceMethodCall) DoAndReturn(f func() faux.Return) *MockSourceMethodCall { 115 | c.Call = c.Call.DoAndReturn(f) 116 | return c 117 | } 118 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/bugreport_test.go: -------------------------------------------------------------------------------- 1 | package typed 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | ) 8 | 9 | // TestValidInterface assesses whether or not the generated mock is valid 10 | func TestValidInterface(t *testing.T) { 11 | ctrl := gomock.NewController(t) 12 | 13 | s := NewMockSource(ctrl) 14 | s.EXPECT().Method().Return("") 15 | 16 | CallForeignMethod(s) 17 | } 18 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/external.go: -------------------------------------------------------------------------------- 1 | package typed 2 | 3 | import ( 4 | "go.uber.org/mock/mockgen/internal/tests/typed/other" 5 | "golang.org/x/exp/constraints" 6 | ) 7 | 8 | //go:generate mockgen --source=external.go --destination=source/mock_external_test.go --package source -typed 9 | 10 | type ExternalConstraint[I constraints.Integer, F constraints.Float] interface { 11 | One(string) string 12 | Two(I) string 13 | Three(I) F 14 | Four(I) Foo[I, F] 15 | Five(I) Baz[F] 16 | Six(I) *Baz[F] 17 | Seven(I) other.One[I] 18 | Eight(F) other.Two[I, F] 19 | Nine(Iface[I]) 20 | Ten(*I) 21 | } 22 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/faux/faux.go: -------------------------------------------------------------------------------- 1 | package faux 2 | 3 | type Foreign interface { 4 | Method() Return 5 | Embedded 6 | error 7 | } 8 | 9 | type Embedded interface{} 10 | 11 | type Return interface{} 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/generics.go: -------------------------------------------------------------------------------- 1 | package typed 2 | 3 | import "go.uber.org/mock/mockgen/internal/tests/typed/other" 4 | 5 | //go:generate mockgen --source=generics.go --destination=source/mock_generics_test.go --package source -typed 6 | ////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2 7 | 8 | type Bar[T any, R any] interface { 9 | One(string) string 10 | Two(T) string 11 | Three(T) R 12 | Four(T) Foo[T, R] 13 | Five(T) Baz[T] 14 | Six(T) *Baz[T] 15 | Seven(T) other.One[T] 16 | Eight(T) other.Two[T, R] 17 | Nine(Iface[T]) 18 | Ten(*T) 19 | Eleven() (*other.One[T], error) 20 | Twelve() (*other.Two[T, R], error) 21 | Thirteen() (Baz[StructType], error) 22 | Fourteen() (*Foo[StructType, StructType2], error) 23 | Fifteen() (Iface[StructType], error) 24 | Sixteen() (Baz[other.Three], error) 25 | Seventeen() (*Foo[other.Three, other.Four], error) 26 | Eighteen() (Iface[*other.Five], error) 27 | Nineteen() AliasType 28 | } 29 | 30 | type Foo[T any, R any] struct{} 31 | 32 | type Baz[T any] struct{} 33 | 34 | type Iface[T any] any 35 | 36 | type StructType struct{} 37 | 38 | type StructType2 struct{} 39 | 40 | type AliasType Baz[other.Three] 41 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/go.mod: -------------------------------------------------------------------------------- 1 | module go.uber.org/mock/mockgen/internal/tests/typed 2 | 3 | go 1.23 4 | 5 | replace go.uber.org/mock => ../../../.. 6 | 7 | require ( 8 | go.uber.org/mock v0.0.0-00010101000000-000000000000 9 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 10 | ) 11 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= 2 | golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= 3 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed/other/other.go: -------------------------------------------------------------------------------- 1 | package other 2 | 3 | type One[T any] struct{} 4 | 5 | type Two[T any, R any] struct{} 6 | 7 | type Three struct{} 8 | 9 | type Four struct{} 10 | 11 | type Five any 12 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed_inorder/input.go: -------------------------------------------------------------------------------- 1 | package typed_inorder 2 | 3 | //go:generate mockgen -package typed_inorder -source=input.go -destination=mock.go -typed 4 | type Animal interface { 5 | GetSound() string 6 | Feed(string) error 7 | } 8 | 9 | func Interact(a Animal, food string) (string, error) { 10 | if err := a.Feed(food); err != nil { 11 | return "", err 12 | } 13 | return a.GetSound(), nil 14 | } 15 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed_inorder/input_test.go: -------------------------------------------------------------------------------- 1 | package typed_inorder 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "go.uber.org/mock/gomock" 8 | ) 9 | 10 | func TestInteract(t *testing.T) { 11 | ctrl := gomock.NewController(t) 12 | 13 | mockAnimal := NewMockAnimal(ctrl) 14 | gomock.InOrder( 15 | mockAnimal.EXPECT().Feed("burguir").DoAndReturn(func(s string) error { 16 | if s != "chocolate" { 17 | return nil 18 | } 19 | return fmt.Errorf("Dogs can't eat chocolate!") 20 | }), 21 | mockAnimal.EXPECT().GetSound().Return("Woof!"), 22 | ) 23 | _, err := Interact(mockAnimal, "burguir") 24 | if err != nil { 25 | t.Fatalf("sad") 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /mockgen/internal/tests/typed_inorder/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: input.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package typed_inorder -source=input.go -destination=mock.go -typed 7 | // 8 | 9 | // Package typed_inorder is a generated GoMock package. 10 | package typed_inorder 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockAnimal is a mock of Animal interface. 19 | type MockAnimal struct { 20 | ctrl *gomock.Controller 21 | recorder *MockAnimalMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockAnimalMockRecorder is the mock recorder for MockAnimal. 26 | type MockAnimalMockRecorder struct { 27 | mock *MockAnimal 28 | } 29 | 30 | // NewMockAnimal creates a new mock instance. 31 | func NewMockAnimal(ctrl *gomock.Controller) *MockAnimal { 32 | mock := &MockAnimal{ctrl: ctrl} 33 | mock.recorder = &MockAnimalMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockAnimal) EXPECT() *MockAnimalMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Feed mocks base method. 43 | func (m *MockAnimal) Feed(arg0 string) error { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Feed", arg0) 46 | ret0, _ := ret[0].(error) 47 | return ret0 48 | } 49 | 50 | // Feed indicates an expected call of Feed. 51 | func (mr *MockAnimalMockRecorder) Feed(arg0 any) *MockAnimalFeedCall { 52 | mr.mock.ctrl.T.Helper() 53 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Feed", reflect.TypeOf((*MockAnimal)(nil).Feed), arg0) 54 | return &MockAnimalFeedCall{Call: call} 55 | } 56 | 57 | // MockAnimalFeedCall wrap *gomock.Call 58 | type MockAnimalFeedCall struct { 59 | *gomock.Call 60 | } 61 | 62 | // Return rewrite *gomock.Call.Return 63 | func (c *MockAnimalFeedCall) Return(arg0 error) *MockAnimalFeedCall { 64 | c.Call = c.Call.Return(arg0) 65 | return c 66 | } 67 | 68 | // Do rewrite *gomock.Call.Do 69 | func (c *MockAnimalFeedCall) Do(f func(string) error) *MockAnimalFeedCall { 70 | c.Call = c.Call.Do(f) 71 | return c 72 | } 73 | 74 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 75 | func (c *MockAnimalFeedCall) DoAndReturn(f func(string) error) *MockAnimalFeedCall { 76 | c.Call = c.Call.DoAndReturn(f) 77 | return c 78 | } 79 | 80 | // GetSound mocks base method. 81 | func (m *MockAnimal) GetSound() string { 82 | m.ctrl.T.Helper() 83 | ret := m.ctrl.Call(m, "GetSound") 84 | ret0, _ := ret[0].(string) 85 | return ret0 86 | } 87 | 88 | // GetSound indicates an expected call of GetSound. 89 | func (mr *MockAnimalMockRecorder) GetSound() *MockAnimalGetSoundCall { 90 | mr.mock.ctrl.T.Helper() 91 | call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSound", reflect.TypeOf((*MockAnimal)(nil).GetSound)) 92 | return &MockAnimalGetSoundCall{Call: call} 93 | } 94 | 95 | // MockAnimalGetSoundCall wrap *gomock.Call 96 | type MockAnimalGetSoundCall struct { 97 | *gomock.Call 98 | } 99 | 100 | // Return rewrite *gomock.Call.Return 101 | func (c *MockAnimalGetSoundCall) Return(arg0 string) *MockAnimalGetSoundCall { 102 | c.Call = c.Call.Return(arg0) 103 | return c 104 | } 105 | 106 | // Do rewrite *gomock.Call.Do 107 | func (c *MockAnimalGetSoundCall) Do(f func() string) *MockAnimalGetSoundCall { 108 | c.Call = c.Call.Do(f) 109 | return c 110 | } 111 | 112 | // DoAndReturn rewrite *gomock.Call.DoAndReturn 113 | func (c *MockAnimalGetSoundCall) DoAndReturn(f func() string) *MockAnimalGetSoundCall { 114 | c.Call = c.Call.DoAndReturn(f) 115 | return c 116 | } 117 | -------------------------------------------------------------------------------- /mockgen/internal/tests/unexported_method/README.md: -------------------------------------------------------------------------------- 1 | # Unexported Method 2 | 3 | From #52, this tests an unexported method in the mocked interface. 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/unexported_method/bugreport.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | //go:generate mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go Example 4 | 5 | import "fmt" 6 | 7 | // Example is an interface with a non exported method 8 | type Example interface { 9 | someMethod(string) string 10 | } 11 | 12 | // CallExample is a simple function that uses the interface 13 | func CallExample(e Example) { 14 | fmt.Println(e.someMethod("test")) 15 | } 16 | -------------------------------------------------------------------------------- /mockgen/internal/tests/unexported_method/bugreport_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: bugreport.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go Example 7 | // 8 | 9 | // Package bugreport is a generated GoMock package. 10 | package bugreport 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockExample is a mock of Example interface. 19 | type MockExample struct { 20 | ctrl *gomock.Controller 21 | recorder *MockExampleMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockExampleMockRecorder is the mock recorder for MockExample. 26 | type MockExampleMockRecorder struct { 27 | mock *MockExample 28 | } 29 | 30 | // NewMockExample creates a new mock instance. 31 | func NewMockExample(ctrl *gomock.Controller) *MockExample { 32 | mock := &MockExample{ctrl: ctrl} 33 | mock.recorder = &MockExampleMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockExample) EXPECT() *MockExampleMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // someMethod mocks base method. 43 | func (m *MockExample) someMethod(arg0 string) string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "someMethod", arg0) 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // someMethod indicates an expected call of someMethod. 51 | func (mr *MockExampleMockRecorder) someMethod(arg0 any) *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "someMethod", reflect.TypeOf((*MockExample)(nil).someMethod), arg0) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/internal/tests/unexported_method/bugreport_test.go: -------------------------------------------------------------------------------- 1 | package bugreport 2 | 3 | import ( 4 | "testing" 5 | 6 | "go.uber.org/mock/gomock" 7 | ) 8 | 9 | func TestCallExample(t *testing.T) { 10 | ctrl := gomock.NewController(t) 11 | 12 | e := NewMockExample(ctrl) 13 | e.EXPECT().someMethod(gomock.Any()).Return("it works!") 14 | 15 | CallExample(e) 16 | } 17 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_dep/README.md: -------------------------------------------------------------------------------- 1 | # Vendor Dep 2 | 3 | Test for [Issue#4](https://github.com/golang/mock/issues/4). 4 | Also see discussion on [#28](https://github.com/golang/mock/pull/28). 5 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_dep/doc.go: -------------------------------------------------------------------------------- 1 | package vendor_dep 2 | 3 | //go:generate mockgen -package vendor_dep -destination mock.go go.uber.org/mock/mockgen/internal/tests/vendor_dep VendorsDep 4 | //go:generate mockgen -destination source_mock_package/mock.go -source=vendor_dep.go 5 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_dep/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/mockgen/internal/tests/vendor_dep (interfaces: VendorsDep) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -package vendor_dep -destination mock.go go.uber.org/mock/mockgen/internal/tests/vendor_dep VendorsDep 7 | // 8 | 9 | // Package vendor_dep is a generated GoMock package. 10 | package vendor_dep 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | present "golang.org/x/tools/present" 17 | ) 18 | 19 | // MockVendorsDep is a mock of VendorsDep interface. 20 | type MockVendorsDep struct { 21 | ctrl *gomock.Controller 22 | recorder *MockVendorsDepMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockVendorsDepMockRecorder is the mock recorder for MockVendorsDep. 27 | type MockVendorsDepMockRecorder struct { 28 | mock *MockVendorsDep 29 | } 30 | 31 | // NewMockVendorsDep creates a new mock instance. 32 | func NewMockVendorsDep(ctrl *gomock.Controller) *MockVendorsDep { 33 | mock := &MockVendorsDep{ctrl: ctrl} 34 | mock.recorder = &MockVendorsDepMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockVendorsDep) EXPECT() *MockVendorsDepMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Foo mocks base method. 44 | func (m *MockVendorsDep) Foo() present.Elem { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Foo") 47 | ret0, _ := ret[0].(present.Elem) 48 | return ret0 49 | } 50 | 51 | // Foo indicates an expected call of Foo. 52 | func (mr *MockVendorsDepMockRecorder) Foo() *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockVendorsDep)(nil).Foo)) 55 | } 56 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_dep/source_mock_package/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: vendor_dep.go 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination source_mock_package/mock.go -source=vendor_dep.go 7 | // 8 | 9 | // Package mock_vendor_dep is a generated GoMock package. 10 | package mock_vendor_dep 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | present "golang.org/x/tools/present" 17 | ) 18 | 19 | // MockVendorsDep is a mock of VendorsDep interface. 20 | type MockVendorsDep struct { 21 | ctrl *gomock.Controller 22 | recorder *MockVendorsDepMockRecorder 23 | isgomock struct{} 24 | } 25 | 26 | // MockVendorsDepMockRecorder is the mock recorder for MockVendorsDep. 27 | type MockVendorsDepMockRecorder struct { 28 | mock *MockVendorsDep 29 | } 30 | 31 | // NewMockVendorsDep creates a new mock instance. 32 | func NewMockVendorsDep(ctrl *gomock.Controller) *MockVendorsDep { 33 | mock := &MockVendorsDep{ctrl: ctrl} 34 | mock.recorder = &MockVendorsDepMockRecorder{mock} 35 | return mock 36 | } 37 | 38 | // EXPECT returns an object that allows the caller to indicate expected use. 39 | func (m *MockVendorsDep) EXPECT() *MockVendorsDepMockRecorder { 40 | return m.recorder 41 | } 42 | 43 | // Foo mocks base method. 44 | func (m *MockVendorsDep) Foo() present.Elem { 45 | m.ctrl.T.Helper() 46 | ret := m.ctrl.Call(m, "Foo") 47 | ret0, _ := ret[0].(present.Elem) 48 | return ret0 49 | } 50 | 51 | // Foo indicates an expected call of Foo. 52 | func (mr *MockVendorsDepMockRecorder) Foo() *gomock.Call { 53 | mr.mock.ctrl.T.Helper() 54 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockVendorsDep)(nil).Foo)) 55 | } 56 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_dep/vendor_dep.go: -------------------------------------------------------------------------------- 1 | package vendor_dep 2 | 3 | import "golang.org/x/tools/present" 4 | 5 | type VendorsDep interface { 6 | Foo() present.Elem 7 | } 8 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_pkg/README.md: -------------------------------------------------------------------------------- 1 | # Vendor Pkg 2 | 3 | Test for [Issue#4](https://github.com/golang/mock/issues/4). 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_pkg/doc.go: -------------------------------------------------------------------------------- 1 | package vendor_pkg 2 | 3 | //go:generate mockgen -destination mock.go -package vendor_pkg golang.org/x/tools/present Elem 4 | -------------------------------------------------------------------------------- /mockgen/internal/tests/vendor_pkg/mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: golang.org/x/tools/present (interfaces: Elem) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination mock.go -package vendor_pkg golang.org/x/tools/present Elem 7 | // 8 | 9 | // Package vendor_pkg is a generated GoMock package. 10 | package vendor_pkg 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockElem is a mock of Elem interface. 19 | type MockElem struct { 20 | ctrl *gomock.Controller 21 | recorder *MockElemMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockElemMockRecorder is the mock recorder for MockElem. 26 | type MockElemMockRecorder struct { 27 | mock *MockElem 28 | } 29 | 30 | // NewMockElem creates a new mock instance. 31 | func NewMockElem(ctrl *gomock.Controller) *MockElem { 32 | mock := &MockElem{ctrl: ctrl} 33 | mock.recorder = &MockElemMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockElem) EXPECT() *MockElemMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // TemplateName mocks base method. 43 | func (m *MockElem) TemplateName() string { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "TemplateName") 46 | ret0, _ := ret[0].(string) 47 | return ret0 48 | } 49 | 50 | // TemplateName indicates an expected call of TemplateName. 51 | func (mr *MockElemMockRecorder) TemplateName() *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TemplateName", reflect.TypeOf((*MockElem)(nil).TemplateName)) 54 | } 55 | -------------------------------------------------------------------------------- /mockgen/model/model_test.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestImpPath(t *testing.T) { 9 | nonVendor := "github.com/foo/bar" 10 | if nonVendor != impPath(nonVendor) { 11 | t.Errorf("") 12 | } 13 | testCases := []struct { 14 | input string 15 | want string 16 | }{ 17 | {"foo/bar", "foo/bar"}, 18 | {"vendor/foo/bar", "foo/bar"}, 19 | {"vendor/foo/vendor/bar", "bar"}, 20 | {"/vendor/foo/bar", "foo/bar"}, 21 | {"qux/vendor/foo/bar", "foo/bar"}, 22 | {"qux/vendor/foo/vendor/bar", "bar"}, 23 | {"govendor/foo", "govendor/foo"}, 24 | {"foo/govendor/bar", "foo/govendor/bar"}, 25 | {"vendors/foo", "vendors/foo"}, 26 | {"foo/vendors/bar", "foo/vendors/bar"}, 27 | } 28 | for _, tc := range testCases { 29 | t.Run(fmt.Sprintf("input %s", tc.input), func(t *testing.T) { 30 | if got := impPath(tc.input); got != tc.want { 31 | t.Errorf("got %s; want %s", got, tc.want) 32 | } 33 | }) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /mockgen/parse_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "go/parser" 5 | "go/token" 6 | "testing" 7 | ) 8 | 9 | func TestFileParser_ParseFile(t *testing.T) { 10 | fs := token.NewFileSet() 11 | file, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0) 12 | if err != nil { 13 | t.Fatalf("Unexpected error: %v", err) 14 | } 15 | 16 | p := fileParser{ 17 | fileSet: fs, 18 | imports: make(map[string]importedPackage), 19 | importedInterfaces: newInterfaceCache(), 20 | } 21 | 22 | pkg, err := p.parseFile("", file) 23 | if err != nil { 24 | t.Fatalf("Unexpected error: %v", err) 25 | } 26 | 27 | checkGreeterImports(t, p.imports) 28 | 29 | expectedName := "greeter" 30 | if pkg.Name != expectedName { 31 | t.Fatalf("Expected name to be %v but got %v", expectedName, pkg.Name) 32 | } 33 | 34 | expectedInterfaceName := "InputMaker" 35 | if pkg.Interfaces[0].Name != expectedInterfaceName { 36 | t.Fatalf("Expected interface name to be %v but got %v", expectedInterfaceName, pkg.Interfaces[0].Name) 37 | } 38 | } 39 | 40 | func TestFileParser_ParsePackage(t *testing.T) { 41 | fs := token.NewFileSet() 42 | _, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0) 43 | if err != nil { 44 | t.Fatalf("Unexpected error: %v", err) 45 | } 46 | 47 | p := fileParser{ 48 | fileSet: fs, 49 | imports: make(map[string]importedPackage), 50 | importedInterfaces: newInterfaceCache(), 51 | } 52 | 53 | newP, err := p.parsePackage("go.uber.org/mock/mockgen/internal/tests/custom_package_name/greeter") 54 | if err != nil { 55 | t.Fatalf("Unexpected error: %v", err) 56 | } 57 | 58 | checkGreeterImports(t, newP.imports) 59 | } 60 | 61 | func TestImportsOfFile(t *testing.T) { 62 | fs := token.NewFileSet() 63 | file, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0) 64 | if err != nil { 65 | t.Fatalf("Unexpected error: %v", err) 66 | } 67 | 68 | imports, _ := importsOfFile(file) 69 | checkGreeterImports(t, imports) 70 | } 71 | 72 | func checkGreeterImports(t *testing.T, imports map[string]importedPackage) { 73 | // check that imports have stdlib package "fmt" 74 | if fmtPackage, ok := imports["fmt"]; !ok { 75 | t.Errorf("Expected imports to have key \"fmt\"") 76 | } else { 77 | expectedFmtPackage := "fmt" 78 | if fmtPackage.Path() != expectedFmtPackage { 79 | t.Errorf("Expected fmt key to have value %s but got %s", expectedFmtPackage, fmtPackage.Path()) 80 | } 81 | } 82 | 83 | // check that imports have package named "validator" 84 | if validatorPackage, ok := imports["validator"]; !ok { 85 | t.Errorf("Expected imports to have key \"fmt\"") 86 | } else { 87 | expectedValidatorPackage := "go.uber.org/mock/mockgen/internal/tests/custom_package_name/validator" 88 | if validatorPackage.Path() != expectedValidatorPackage { 89 | t.Errorf("Expected validator key to have value %s but got %s", expectedValidatorPackage, validatorPackage.Path()) 90 | } 91 | } 92 | 93 | // check that imports have package named "client" 94 | if clientPackage, ok := imports["client"]; !ok { 95 | t.Errorf("Expected imports to have key \"client\"") 96 | } else { 97 | expectedClientPackage := "go.uber.org/mock/mockgen/internal/tests/custom_package_name/client/v1" 98 | if clientPackage.Path() != expectedClientPackage { 99 | t.Errorf("Expected client key to have value %s but got %s", expectedClientPackage, clientPackage.Path()) 100 | } 101 | } 102 | 103 | // check that imports don't have package named "v1" 104 | if _, ok := imports["v1"]; ok { 105 | t.Errorf("Expected import not to have key \"v1\"") 106 | } 107 | } 108 | 109 | func Benchmark_parseFile(b *testing.B) { 110 | source := "internal/tests/performance/big_interface/big_interface.go" 111 | for n := 0; n < b.N; n++ { 112 | sourceMode(source) 113 | } 114 | } 115 | 116 | func TestParseArrayWithConstLength(t *testing.T) { 117 | fs := token.NewFileSet() 118 | srcDir := "internal/tests/const_array_length/input.go" 119 | 120 | file, err := parser.ParseFile(fs, srcDir, nil, 0) 121 | if err != nil { 122 | t.Fatalf("Unexpected error: %v", err) 123 | } 124 | 125 | p := fileParser{ 126 | fileSet: fs, 127 | imports: make(map[string]importedPackage), 128 | importedInterfaces: newInterfaceCache(), 129 | auxInterfaces: newInterfaceCache(), 130 | srcDir: srcDir, 131 | } 132 | 133 | pkg, err := p.parseFile("", file) 134 | if err != nil { 135 | t.Fatalf("Unexpected error: %v", err) 136 | } 137 | 138 | expects := []string{"[2]int", "[2]int", "[127]int", "[3]int", "[3]int", "[7]int"} 139 | for i, e := range expects { 140 | got := pkg.Interfaces[0].Methods[i].Out[0].Type.String(nil, "") 141 | if got != e { 142 | t.Fatalf("got %v; expected %v", got, e) 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /mockgen/version.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "fmt" 19 | "log" 20 | "runtime/debug" 21 | ) 22 | 23 | func printModuleVersion() { 24 | if bi, exists := debug.ReadBuildInfo(); exists { 25 | fmt.Println(bi.Main.Version) 26 | } else { 27 | log.Printf("No version information found. Make sure to use " + 28 | "GO111MODULE=on when running 'go get' in order to use specific " + 29 | "version of the binary.") 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /sample/README.md: -------------------------------------------------------------------------------- 1 | # Samples 2 | 3 | This directory contains an example of a package containing a non-trivial 4 | interface that can be mocked with GoMock. The interesting files are: 5 | 6 | * `user.go`: Source code for the sample package, containing interfaces to be 7 | mocked. This file depends on the packages named imp[1-4] for various things. 8 | 9 | * `user_test.go`: A test for the sample package, in which mocks of the 10 | interfaces from `user.go` are used. This demonstrates how to create mock 11 | objects, set up expectations, and so on. 12 | 13 | * `mock_user_test.go`: The generated mock code. See ../gomock/matchers.go 14 | for the `go:generate` command used to generate it. 15 | 16 | To run the test, 17 | 18 | ```bash 19 | go test go.uber.org/mock/sample 20 | ``` 21 | -------------------------------------------------------------------------------- /sample/concurrent/README.md: -------------------------------------------------------------------------------- 1 | # Concurrent 2 | 3 | This directory contains an example of executing mock calls concurrently. 4 | 5 | To run the test, 6 | 7 | ```bash 8 | go test -race go.uber.org/mock/sample/concurrent 9 | ``` 10 | -------------------------------------------------------------------------------- /sample/concurrent/concurrent.go: -------------------------------------------------------------------------------- 1 | // Package concurrent demonstrates how to use gomock with goroutines. 2 | package concurrent 3 | 4 | //go:generate mockgen -destination mock/concurrent_mock.go go.uber.org/mock/sample/concurrent Math 5 | 6 | type Math interface { 7 | Sum(a, b int) int 8 | } 9 | -------------------------------------------------------------------------------- /sample/concurrent/concurrent_test.go: -------------------------------------------------------------------------------- 1 | package concurrent 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "testing" 7 | "time" 8 | 9 | "go.uber.org/mock/gomock" 10 | mock "go.uber.org/mock/sample/concurrent/mock" 11 | ) 12 | 13 | func call(ctx context.Context, m Math) (int, error) { 14 | result := make(chan int) 15 | go func() { 16 | result <- m.Sum(1, 2) 17 | close(result) 18 | }() 19 | select { 20 | case r := <-result: 21 | return r, nil 22 | case <-ctx.Done(): 23 | return 0, ctx.Err() 24 | } 25 | } 26 | 27 | func waitForMocks(ctx context.Context, ctrl *gomock.Controller) error { 28 | ticker := time.NewTicker(1 * time.Millisecond) 29 | defer ticker.Stop() 30 | 31 | timeout := time.After(3 * time.Millisecond) 32 | 33 | for { 34 | select { 35 | case <-ticker.C: 36 | if ctrl.Satisfied() { 37 | return nil 38 | } 39 | case <-timeout: 40 | return fmt.Errorf("timeout waiting for mocks to be satisfied") 41 | case <-ctx.Done(): 42 | return fmt.Errorf("context cancelled") 43 | } 44 | } 45 | } 46 | 47 | // TestConcurrentFails is expected to fail (and is disabled). It 48 | // demonstrates how to use gomock.WithContext to interrupt the test 49 | // from a different goroutine. 50 | func TestConcurrentFails(t *testing.T) { 51 | t.Skip("Test is expected to fail, remove skip to trying running yourself.") 52 | ctrl, ctx := gomock.WithContext(context.Background(), t) 53 | m := mock.NewMockMath(ctrl) 54 | if _, err := call(ctx, m); err != nil { 55 | t.Error("call failed:", err) 56 | } 57 | } 58 | 59 | func TestConcurrentWorks(t *testing.T) { 60 | ctrl, ctx := gomock.WithContext(context.Background(), t) 61 | m := mock.NewMockMath(ctrl) 62 | m.EXPECT().Sum(1, 2).Return(3) 63 | if _, err := call(ctx, m); err != nil { 64 | t.Error("call failed:", err) 65 | } 66 | } 67 | 68 | func TestCancelWhenMocksSatisfied(t *testing.T) { 69 | ctrl, ctx := gomock.WithContext(context.Background(), t) 70 | m := mock.NewMockMath(ctrl) 71 | m.EXPECT().Sum(1, 2).Return(3).MinTimes(1) 72 | 73 | // This goroutine calls the mock and then waits for the context to be done. 74 | go func() { 75 | for { 76 | m.Sum(1, 2) 77 | select { 78 | case <-ctx.Done(): 79 | return 80 | } 81 | } 82 | }() 83 | 84 | // waitForMocks spawns another goroutine which blocks until ctrl.Satisfied() is true. 85 | if err := waitForMocks(ctx, ctrl); err != nil { 86 | t.Error("call failed:", err) 87 | } 88 | ctrl.Finish() 89 | } 90 | -------------------------------------------------------------------------------- /sample/concurrent/mock/concurrent_mock.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: go.uber.org/mock/sample/concurrent (interfaces: Math) 3 | // 4 | // Generated by this command: 5 | // 6 | // mockgen -destination mock/concurrent_mock.go go.uber.org/mock/sample/concurrent Math 7 | // 8 | 9 | // Package mock_concurrent is a generated GoMock package. 10 | package mock_concurrent 11 | 12 | import ( 13 | reflect "reflect" 14 | 15 | gomock "go.uber.org/mock/gomock" 16 | ) 17 | 18 | // MockMath is a mock of Math interface. 19 | type MockMath struct { 20 | ctrl *gomock.Controller 21 | recorder *MockMathMockRecorder 22 | isgomock struct{} 23 | } 24 | 25 | // MockMathMockRecorder is the mock recorder for MockMath. 26 | type MockMathMockRecorder struct { 27 | mock *MockMath 28 | } 29 | 30 | // NewMockMath creates a new mock instance. 31 | func NewMockMath(ctrl *gomock.Controller) *MockMath { 32 | mock := &MockMath{ctrl: ctrl} 33 | mock.recorder = &MockMathMockRecorder{mock} 34 | return mock 35 | } 36 | 37 | // EXPECT returns an object that allows the caller to indicate expected use. 38 | func (m *MockMath) EXPECT() *MockMathMockRecorder { 39 | return m.recorder 40 | } 41 | 42 | // Sum mocks base method. 43 | func (m *MockMath) Sum(a, b int) int { 44 | m.ctrl.T.Helper() 45 | ret := m.ctrl.Call(m, "Sum", a, b) 46 | ret0, _ := ret[0].(int) 47 | return ret0 48 | } 49 | 50 | // Sum indicates an expected call of Sum. 51 | func (mr *MockMathMockRecorder) Sum(a, b any) *gomock.Call { 52 | mr.mock.ctrl.T.Helper() 53 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sum", reflect.TypeOf((*MockMath)(nil).Sum), a, b) 54 | } 55 | -------------------------------------------------------------------------------- /sample/imp1/imp1.go: -------------------------------------------------------------------------------- 1 | package imp1 2 | 3 | import "bufio" 4 | 5 | type Imp1 struct{} 6 | 7 | type ImpT int 8 | 9 | type ForeignEmbedded interface { 10 | // The return value here also makes sure that 11 | // the generated mock picks up the "bufio" import. 12 | ForeignEmbeddedMethod() *bufio.Reader 13 | 14 | // This method uses a type in this package, 15 | // which should be qualified when this interface is embedded. 16 | ImplicitPackage(s string, t ImpT, st []ImpT, pt *ImpT, ct chan ImpT) 17 | } 18 | -------------------------------------------------------------------------------- /sample/imp2/imp2.go: -------------------------------------------------------------------------------- 1 | package imp2 2 | 3 | type Imp2 struct{} 4 | -------------------------------------------------------------------------------- /sample/imp3/imp3.go: -------------------------------------------------------------------------------- 1 | package imp3 2 | 3 | type Imp3 struct{} 4 | -------------------------------------------------------------------------------- /sample/imp4/imp4.go: -------------------------------------------------------------------------------- 1 | package imp_four 2 | 3 | type Imp4 struct { 4 | Field string 5 | } 6 | -------------------------------------------------------------------------------- /sample/user.go: -------------------------------------------------------------------------------- 1 | // Package user is an example package with an interface. 2 | package user 3 | 4 | //go:generate mockgen -destination mock_user_test.go -package user_test go.uber.org/mock/sample Index,Embed,Embedded 5 | 6 | // Random bunch of imports to test mockgen. 7 | import ( 8 | "hash" 9 | "io" 10 | "log" 11 | "net" 12 | "net/http" 13 | 14 | btz "bytes" 15 | 16 | // Two imports with the same base name. 17 | t1 "html/template" 18 | 19 | t2 "text/template" 20 | 21 | "go.uber.org/mock/sample/imp1" 22 | 23 | // Dependencies outside the standard library. 24 | 25 | renamed2 "go.uber.org/mock/sample/imp2" 26 | 27 | . "go.uber.org/mock/sample/imp3" 28 | 29 | imp_four "go.uber.org/mock/sample/imp4" 30 | ) 31 | 32 | // calls itself "imp_four" 33 | 34 | // A bizarre interface to test corner cases in mockgen. 35 | // This would normally be in its own file or package, 36 | // separate from the user of it (e.g. io.Reader). 37 | type Index interface { 38 | Get(key string) any 39 | GetTwo(key1, key2 string) (v1, v2 any) 40 | Put(key string, value any) 41 | 42 | // Check that imports are handled correctly. 43 | Summary(buf *btz.Buffer, w io.Writer) 44 | Other() hash.Hash 45 | Templates(a t1.CSS, b t2.FuncMap) 46 | 47 | // A method with an anonymous argument. 48 | Anon(string) 49 | 50 | // Methods using foreign types outside the standard library. 51 | ForeignOne(imp1.Imp1) 52 | ForeignTwo(renamed2.Imp2) 53 | ForeignThree(Imp3) 54 | ForeignFour(imp_four.Imp4) 55 | 56 | // A method that returns a nillable type. 57 | NillableRet() error 58 | // A method that returns a non-interface type. 59 | ConcreteRet() chan<- bool 60 | 61 | // Methods with an ellipsis argument. 62 | Ellip(fmt string, args ...any) 63 | EllipOnly(...string) 64 | 65 | // A method with a pointer argument that we will set. 66 | Ptr(arg *int) 67 | 68 | // A method with a slice argument and an array return. 69 | Slice(a []int, b []byte) [3]int 70 | 71 | // A method with channel arguments. 72 | Chan(a chan int, b chan<- hash.Hash) 73 | 74 | // A method with a function argument. 75 | Func(f func(http.Request) (int, bool)) 76 | 77 | // A method with a map argument. 78 | Map(a map[int]hash.Hash) 79 | 80 | // Methods with an unnamed empty struct argument. 81 | Struct(a struct{}) // not so likely 82 | StructChan(a chan struct{}) // a bit more common 83 | } 84 | 85 | // An interface with an embedded interface. 86 | type Embed interface { 87 | RegularMethod() 88 | Embedded 89 | imp1.ForeignEmbedded 90 | } 91 | 92 | type Embedded interface { 93 | EmbeddedMethod() 94 | } 95 | 96 | // some random use of another package that isn't needed by the interface. 97 | var _ net.Addr 98 | 99 | // A function that we will test that uses the above interface. 100 | // It takes a list of keys and values, and puts them in the index. 101 | func Remember(index Index, keys []string, values []any) { 102 | for i, k := range keys { 103 | index.Put(k, values[i]) 104 | } 105 | err := index.NillableRet() 106 | if err != nil { 107 | log.Fatalf("Woah! %v", err) 108 | } 109 | if len(keys) > 0 && keys[0] == "a" { 110 | index.Ellip("%d", 0, 1, 1, 2, 3) 111 | index.Ellip("%d", 1, 3, 6, 10, 15) 112 | index.EllipOnly("arg") 113 | } 114 | } 115 | 116 | func GrabPointer(index Index) int { 117 | var a int 118 | index.Ptr(&a) 119 | return a 120 | } 121 | -------------------------------------------------------------------------------- /tools/cmd/extract-changelog/main.go: -------------------------------------------------------------------------------- 1 | // extract-changelog extracts the release notes for a specific version from a 2 | // file matching the format prescribed by https://keepachangelog.com/en/1.0.0/. 3 | package main 4 | 5 | import ( 6 | "bufio" 7 | "bytes" 8 | "errors" 9 | "flag" 10 | "fmt" 11 | "io" 12 | "os" 13 | "strings" 14 | ) 15 | 16 | func main() { 17 | cmd := mainCmd{ 18 | Stdout: os.Stdout, 19 | Stderr: os.Stderr, 20 | } 21 | if err := cmd.Run(os.Args[1:]); err != nil && err != flag.ErrHelp { 22 | fmt.Fprintln(cmd.Stderr, err) 23 | os.Exit(1) 24 | } 25 | } 26 | 27 | type mainCmd struct { 28 | Stdout io.Writer 29 | Stderr io.Writer 30 | } 31 | 32 | const _usage = `USAGE 33 | 34 | %v [OPTIONS] VERSION 35 | 36 | Retrieves the release notes for VERSION from a CHANGELOG.md file and prints 37 | them to stdout. 38 | 39 | EXAMPLES 40 | 41 | extract-changelog -i CHANGELOG.md v1.2.3 42 | extract-changelog 0.2.5 43 | 44 | OPTIONS 45 | ` 46 | 47 | func (cmd *mainCmd) Run(args []string) error { 48 | flag := flag.NewFlagSet("extract-changelog", flag.ContinueOnError) 49 | flag.SetOutput(cmd.Stderr) 50 | flag.Usage = func() { 51 | fmt.Fprintf(flag.Output(), _usage, flag.Name()) 52 | flag.PrintDefaults() 53 | } 54 | 55 | file := flag.String("i", "CHANGELOG.md", "input file") 56 | 57 | if err := flag.Parse(args); err != nil { 58 | return err 59 | } 60 | 61 | var version string 62 | if args := flag.Args(); len(args) > 0 { 63 | version = args[0] 64 | } 65 | version = strings.TrimPrefix(version, "v") 66 | 67 | if len(version) == 0 { 68 | return errors.New("please provide a version") 69 | } 70 | 71 | f, err := os.Open(*file) 72 | if err != nil { 73 | return fmt.Errorf("open changelog: %v", err) 74 | } 75 | defer f.Close() 76 | 77 | s, err := extract(f, version) 78 | if err != nil { 79 | return err 80 | } 81 | _, err = io.WriteString(cmd.Stdout, s) 82 | return err 83 | } 84 | 85 | func extract(r io.Reader, version string) (string, error) { 86 | type _state int 87 | 88 | const ( 89 | initial _state = iota 90 | foundHeader 91 | ) 92 | 93 | var ( 94 | state _state 95 | buff bytes.Buffer 96 | scanner = bufio.NewScanner(r) 97 | ) 98 | 99 | scan: 100 | for scanner.Scan() { 101 | line := scanner.Text() 102 | 103 | switch state { 104 | case initial: 105 | // Version headers take one of the following forms: 106 | // 107 | // ## 0.1.3 - 2021-08-18 108 | // ## [0.1.3] - 2021-08-18 109 | switch { 110 | case strings.HasPrefix(line, "## "+version+" "), 111 | strings.HasPrefix(line, "## ["+version+"]"): 112 | fmt.Fprintln(&buff, line) 113 | state = foundHeader 114 | } 115 | 116 | case foundHeader: 117 | // Found a new version header. Stop extracting. 118 | if strings.HasPrefix(line, "## ") { 119 | break scan 120 | } 121 | fmt.Fprintln(&buff, line) 122 | 123 | default: 124 | // unreachable but guard against it. 125 | return "", fmt.Errorf("unexpected state %v at %q", state, line) 126 | } 127 | } 128 | 129 | if err := scanner.Err(); err != nil { 130 | return "", err 131 | } 132 | 133 | if state < foundHeader { 134 | return "", fmt.Errorf("changelog for %q not found", version) 135 | } 136 | 137 | out := buff.String() 138 | out = strings.TrimSpace(out) + "\n" // always end with a single newline 139 | return out, nil 140 | } 141 | -------------------------------------------------------------------------------- /tools/cmd/extract-changelog/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "os" 6 | "path/filepath" 7 | "strings" 8 | "testing" 9 | 10 | "github.com/stretchr/testify/assert" 11 | "github.com/stretchr/testify/require" 12 | ) 13 | 14 | const _changelog = ` 15 | # Changelog 16 | 17 | All notable changes to this project will be documented in this file. 18 | 19 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 20 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 21 | 22 | ## Unreleased 23 | ### Added 24 | - Upcoming feature 25 | 26 | ## [1.0.0] - 2021-08-18 27 | Initial stable release. 28 | 29 | [1.0.0]: http://example.com/1.0.0 30 | 31 | ## 0.3.0 - 2020-09-01 32 | ### Removed 33 | - deprecated functionality 34 | 35 | ### Fixed 36 | - bug 37 | 38 | ## [0.2.0] - 2020-08-19 39 | ### Added 40 | - Fancy new feature. 41 | 42 | [0.2.0]: http://example.com/0.2.0 43 | 44 | ## 0.1.0 - 2020-08-18 45 | 46 | Initial release. 47 | ` 48 | 49 | func TestMain(t *testing.T) { 50 | t.Parallel() 51 | 52 | changelog := filepath.Join(t.TempDir(), "CHANGELOG.md") 53 | require.NoError(t, 54 | os.WriteFile(changelog, []byte(_changelog), 0o644)) 55 | 56 | tests := []struct { 57 | desc string 58 | 59 | version string 60 | want string // expected changelog 61 | wantErr string // expected error, if any 62 | }{ 63 | { 64 | desc: "not found", 65 | version: "0.1.1", 66 | wantErr: `changelog for "0.1.1" not found`, 67 | }, 68 | { 69 | desc: "missing version", 70 | wantErr: "please provide a version", 71 | }, 72 | { 73 | desc: "non-standard body", 74 | version: "1.0.0", 75 | want: joinLines( 76 | "## [1.0.0] - 2021-08-18", 77 | "Initial stable release.", 78 | "", 79 | "[1.0.0]: http://example.com/1.0.0", 80 | ), 81 | }, 82 | { 83 | desc: "unlinked", 84 | version: "0.3.0", 85 | want: joinLines( 86 | "## 0.3.0 - 2020-09-01", 87 | "### Removed", 88 | "- deprecated functionality", 89 | "", 90 | "### Fixed", 91 | "- bug", 92 | ), 93 | }, 94 | { 95 | desc: "end of file", 96 | version: "0.1.0", 97 | want: joinLines( 98 | "## 0.1.0 - 2020-08-18", 99 | "", 100 | "Initial release.", 101 | ), 102 | }, 103 | { 104 | desc: "linked", 105 | version: "0.2.0", 106 | want: joinLines( 107 | "## [0.2.0] - 2020-08-19", 108 | "### Added", 109 | "- Fancy new feature.", 110 | "", 111 | "[0.2.0]: http://example.com/0.2.0", 112 | ), 113 | }, 114 | } 115 | 116 | for _, tt := range tests { 117 | tt := tt 118 | t.Run(tt.desc, func(t *testing.T) { 119 | t.Parallel() 120 | 121 | var stdout, stderr bytes.Buffer 122 | defer func() { 123 | assert.Empty(t, stderr.String(), "stderr should be empty") 124 | }() 125 | 126 | err := (&mainCmd{ 127 | Stdout: &stdout, 128 | Stderr: &stderr, 129 | }).Run([]string{"-i", changelog, tt.version}) 130 | 131 | if len(tt.wantErr) > 0 { 132 | require.Error(t, err) 133 | assert.Contains(t, err.Error(), tt.wantErr) 134 | return 135 | } 136 | 137 | require.NoError(t, err) 138 | assert.Equal(t, tt.want, stdout.String()) 139 | }) 140 | } 141 | } 142 | 143 | // Join a bunch of lines with a trailing newline. 144 | func joinLines(lines ...string) string { 145 | return strings.Join(lines, "\n") + "\n" 146 | } 147 | -------------------------------------------------------------------------------- /tools/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/uber-go/mock/tools 2 | 3 | go 1.23 4 | 5 | require github.com/stretchr/testify v1.9.0 6 | 7 | require ( 8 | github.com/davecgh/go-spew v1.1.1 // indirect 9 | github.com/pmezard/go-difflib v1.0.0 // indirect 10 | gopkg.in/yaml.v3 v3.0.1 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /tools/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 6 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 7 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 8 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 9 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 10 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 11 | --------------------------------------------------------------------------------