├── .circleci └── config.yml ├── .gitattributes ├── .github └── dependabot.yml ├── .gitignore ├── .golangci.yaml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── ast ├── cache.go ├── cache_test.go ├── dsl.go ├── dsl_test.go ├── moq_loadfn_test.go ├── moq_readfilefn_test.go ├── moq_statfn_test.go └── testpkgs │ ├── export │ ├── file1.go │ ├── file2.go │ ├── file3.go │ └── generics.go │ ├── fabricate │ └── silliness.go │ ├── noexport │ ├── file1.go │ ├── file1_test.go │ ├── file2.go │ ├── file2_test.go │ ├── file3.go │ ├── file3_test.go │ └── generics.go │ └── replacebuiltin │ └── holdmybeer.go ├── bulk ├── bulk.go └── internal │ ├── appender.go │ ├── appender_test.go │ ├── finalizer.go │ ├── finalizer_test.go │ ├── initializer.go │ ├── initializer_test.go │ ├── moq_createfn_test.go │ ├── moq_generatefn_test.go │ ├── moq_openfilefn_test.go │ ├── moq_openfn_test.go │ ├── moq_readcloser_test.go │ ├── moq_readwriteseekcloser_test.go │ └── moq_writecloser_test.go ├── cmd ├── finalize.go ├── generate.go ├── initialize.go ├── package.go ├── root.go └── summarizemetrics.go ├── codecov.yml ├── demo ├── demo.go ├── demo_test.go ├── moq_isfavorite_test.go ├── moq_store_test.go └── moq_writer_test.go ├── docs └── anatomy │ ├── README.md │ ├── any-params.svg │ ├── big-picture.svg │ ├── fn.svg │ ├── interface.svg │ ├── params-key.svg │ ├── results.svg │ ├── store-any-params.svg │ └── store-params-key.svg ├── generator ├── converter.go ├── converter_test.go ├── generator.go ├── generator_test.go ├── moq_converterer_test.go ├── moq_getwdfunc_test.go ├── moq_newconverterfunc_test.go ├── moq_testinterface_test.go ├── moq_typecache_test.go ├── moqgen.go ├── moqgen_test.go └── testmoqs │ ├── atomic_test.go │ ├── exported │ └── moq_exported_testmoqs.go │ ├── fnadaptors_test.go │ ├── moq_t_test.go │ ├── moq_testmoqs_test.go │ ├── other │ └── types.go │ ├── testmoqs_test.go │ ├── types.go │ └── usualadaptors_test.go ├── go.mod ├── go.sum ├── metrics ├── metrics.go ├── metrics_test.go ├── moq_isloggingfn_test.go ├── moq_loggingffn_test.go ├── moq_metrics.go ├── moq_reader_test.go ├── summarizer.go └── summarizer_test.go ├── moqueries └── main.go └── pkg ├── generator.go ├── internal ├── generator.go ├── generator_test.go ├── moq_generatewithtypecachefn_test.go └── moq_typecache_test.go └── testmoqs ├── pkgout ├── moq_dosomethingwithparam.go ├── moq_passbyrefsimple.go ├── moq_passbyvaluesimple.go ├── moq_reduced.go └── moq_standalonefunc.go ├── types.go └── types_test.go /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | orbs: 4 | codecov: codecov/codecov@3.2.2 5 | 6 | executors: 7 | go: 8 | docker: 9 | - image: cimg/go:1.21 10 | 11 | jobs: 12 | build: 13 | 14 | environment: 15 | TEST_RESULTS: /tmp/test-results 16 | 17 | executor: go 18 | 19 | steps: 20 | - checkout 21 | - run: mkdir -p $TEST_RESULTS 22 | 23 | - run: 24 | name: Generate code 25 | 26 | command: | 27 | set -ux 28 | find . -name moq_\*.go -delete 29 | go build -o $GOPATH/bin/moqueries moqueries.org/cli/moqueries 30 | export MOQ_BULK_STATE_FILE=$(mktemp --tmpdir= moq-XXXXXX) 31 | moqueries bulk-initialize 32 | go generate ./... 33 | MOQ_DEBUG=true moqueries bulk-finalize 34 | 35 | - run: 36 | name: Run tests 37 | 38 | command: | 39 | set -ux 40 | PACKAGE_NAMES=$(go list ./... | circleci tests split --split-by=timings --timings-type=classname) 41 | gotestsum --junitfile ${TEST_RESULTS}/gotestsum-report.xml -- \ 42 | -v -race -covermode=atomic -coverprofile=coverage.out $PACKAGE_NAMES 43 | # go install golang.org/x/vuln/cmd/govulncheck@latest 44 | # govulncheck ./... 45 | 46 | - run: 47 | name: Check generated code 48 | 49 | command: | 50 | set -ux 51 | # fail if working directory is dirty 52 | # Note: We check after Generate code *and* Run tests because some 53 | # tests generate mocks too. 54 | git status --short 55 | if [[ -n $(git status --short) ]]; then 56 | echo "Working directory dirty" 57 | exit 1 58 | fi 59 | 60 | - codecov/upload 61 | 62 | - run: 63 | name: Run code linters 64 | 65 | command: | 66 | set -ux 67 | # Installs the latest version each time. This may impact build 68 | # repeatability but only mildly. 69 | curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \ 70 | sh -s -- -b $(go env GOPATH)/bin 71 | golangci-lint run 72 | 73 | - run: 74 | name: Check for dirty working directory 75 | 76 | command: | 77 | set -ux 78 | git status 79 | git diff 80 | [[ -z $(git status --short) ]] || (echo 'Untracked changes' && exit 1) 81 | 82 | - store_artifacts: 83 | path: /tmp/test-results 84 | destination: raw-test-output 85 | 86 | - store_test_results: 87 | path: /tmp/test-results 88 | 89 | workflows: 90 | version: 2 91 | build-workflow: 92 | jobs: 93 | - build 94 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | moq_*.go linguist-generated=true 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "gomod" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "daily" 12 | 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | *.coverprofile 15 | /codecov 16 | /codecov.SHA256SUM 17 | /codecov.SHA256SUM.sig 18 | 19 | # AST and DST files used while writing code generation code 20 | moq_*_ast.txt 21 | moq_*_dst.txt 22 | 23 | # macOS 24 | .DS_Store 25 | 26 | # other 27 | .envrc 28 | /.idea 29 | -------------------------------------------------------------------------------- /.golangci.yaml: -------------------------------------------------------------------------------- 1 | linters: 2 | enable-all: true 3 | disable: 4 | - cyclop 5 | - depguard 6 | - dogsled 7 | - errorlint 8 | - exhaustivestruct 9 | - exhaustruct 10 | - funlen 11 | - importas 12 | - interfacer 13 | - ireturn 14 | - gochecknoglobals 15 | - gochecknoinits 16 | - gocognit 17 | - godot 18 | - maintidx 19 | - maligned 20 | - nlreturn 21 | - nosnakecase 22 | - paralleltest 23 | - prealloc 24 | - predeclared 25 | - scopelint 26 | - tagliatelle 27 | - varnamelen 28 | - wsl 29 | 30 | # Consider enabling 31 | - gocyclo 32 | - wrapcheck 33 | 34 | # Usually disabled but useful for checking everything has godoc 35 | - golint 36 | 37 | run: 38 | skip-dirs: 39 | - ^testit.* 40 | 41 | linters-settings: 42 | gci: 43 | sections: 44 | - standard 45 | - default 46 | - prefix(moqueries.org/cli) 47 | revive: 48 | rules: 49 | - name: var-naming 50 | disabled: true 51 | stylecheck: 52 | dot-import-whitelist: 53 | - "moqueries.org/cli/ast" 54 | initialisms: 55 | - "-ID" 56 | 57 | issues: 58 | exclude-use-default: false 59 | exclude-rules: 60 | - path: '(.+)_test.go' 61 | linters: 62 | - err113 63 | - forcetypeassert 64 | - goconst 65 | - gocritic 66 | - goerr113 67 | - nestif 68 | # No easy way to break up go:generate lines 69 | - linters: 70 | - lll 71 | source: "^//go:generate " 72 | - path: 'testpkg' 73 | linters: 74 | - deadcode 75 | - revive 76 | - stylecheck 77 | - unused 78 | - linters: 79 | - unused 80 | path: generator/testmoqs/fnadaptors_test.go 81 | - linters: 82 | - unused 83 | path: generator/testmoqs/usualadaptors_test.go 84 | - linters: 85 | - inamedparam 86 | path: .*/testmoqs/.* 87 | include: 88 | # disable excluding of issues about comments from golint. 89 | - EXC0002 90 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | . 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributions are welcome! Working on code that generates code can be challenging (in a good way!). Since most changes are to generated code, I typically start by modifying one of the generated test mocks [here](generator/testmoqs) (ignore the `DO NOT EDIT!` warning in this case). Then fixup any broken test in the same package, add some tests, rinse and repeat. When that one mock is functioning as expected, next update the code-generation code in the parent package so that it generates identical code. Iterating on the code-generation code typically involve running a command line to generate an example mock: 2 | ```shell 3 | go run moqueries/main.go --debug --destination xxx.go moqueries.org/cli/generator/testmoqs Usual 4 | ``` 5 | 6 | The previous command generates a scratch mock in a `xxx.go` file (don't check this file in!) which can then be compared to your hand-altered mock (assuming you modified the [`moq_usual_test.go`](generator/testmoqs/moq_usual_test.go)). When the scratch mock matches your hand-altered mock, follow the instructions below to regenerate all the mocks and run all the tests before submitting a PR. 7 | 8 | # CI/CD 9 | Moqueries uses CircleCI for CI/CD builds. If you don't see any jobs run when creating or updating a PR, [CircleCI says](https://circleci.com/docs/2.0/oss/#build-pull-requests-from-forked-repositories) that you are probably following your fork which will then trigger jobs under your own account. Please unfollow your fork. 10 | 11 | You can iterate more quickly by locally running some of the [same commands CircleCI is running](.circleci/config.yml) (all the following commands assume you are running them from the root moqueries working directory): 12 | 1. Build the binary: 13 | ```shell 14 | go build -o $GOPATH/bin/moqueries moqueries.org/cli/moqueries 15 | ``` 16 | 2. Generate all the mocks: 17 | ```shell 18 | go generate ./... 19 | ``` 20 | 3. Run all the tests: 21 | ```shell 22 | go test ./... 23 | ``` 24 | 4. Run all the linters (may require installing [`golangci-lint`](https://golangci-lint.run/usage/install/#local-installation)): 25 | ```shell 26 | golangci-lint run 27 | ``` 28 | 29 | # Have fun! 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Dwayne Schultz 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CircleCI](https://circleci.com/gh/moqueries/cli/tree/main.svg?style=shield)](https://circleci.com/gh/moqueries/cli/tree/main) 2 | [![Documentation](https://img.shields.io/badge/godoc-documentation-brightgreen.svg)](https://pkg.go.dev/moqueries.org/runtime/moq) 3 | [![codecov](https://codecov.io/gh/moqueries/cli/branch/main/graph/badge.svg?token=akA6OdarTX)](https://codecov.io/gh/moqueries/cli) 4 | [![Go Report Card](https://goreportcard.com/badge/moqueries/cli)](https://goreportcard.com/report/moqueries/cli) 5 | [![License](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](https://github.com/moqueries/cli/blob/main/LICENSE) 6 | 7 | # Moqueries - Lock-free interface and function mocks for Go 8 | This repository contains the command line interface (CLI) for generating mocks. Please see the official Moqueries documentation at https://moqueries.org/ for more details including the [Generating mocks](https://moqueries.org/generating-mocks/index.html) page. 9 | -------------------------------------------------------------------------------- /ast/moq_loadfn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package ast_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "golang.org/x/tools/go/packages" 9 | "moqueries.org/cli/ast" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqLoadFn holds the state of a moq of the LoadFn type 16 | type moqLoadFn struct { 17 | moq *impl.Moq[ 18 | *moqLoadFn_adaptor, 19 | moqLoadFn_params, 20 | moqLoadFn_paramsKey, 21 | moqLoadFn_results, 22 | ] 23 | 24 | runtime moqLoadFn_runtime 25 | } 26 | 27 | // moqLoadFn_runtime holds runtime configuration for the LoadFn type 28 | type moqLoadFn_runtime struct { 29 | parameterIndexing moqLoadFn_paramIndexing 30 | } 31 | 32 | // moqLoadFn_adaptor adapts moqLoadFn as needed by the runtime 33 | type moqLoadFn_adaptor struct { 34 | moq *moqLoadFn 35 | } 36 | 37 | // moqLoadFn_params holds the params of the LoadFn type 38 | type moqLoadFn_params struct { 39 | cfg *packages.Config 40 | patterns []string 41 | } 42 | 43 | // moqLoadFn_paramsKey holds the map key params of the LoadFn type 44 | type moqLoadFn_paramsKey struct { 45 | params struct{ cfg *packages.Config } 46 | hashes struct { 47 | cfg hash.Hash 48 | patterns hash.Hash 49 | } 50 | } 51 | 52 | // moqLoadFn_results holds the results of the LoadFn type 53 | type moqLoadFn_results struct { 54 | result1 []*packages.Package 55 | result2 error 56 | } 57 | 58 | // moqLoadFn_paramIndexing holds the parameter indexing runtime configuration 59 | // for the LoadFn type 60 | type moqLoadFn_paramIndexing struct { 61 | cfg moq.ParamIndexing 62 | patterns moq.ParamIndexing 63 | } 64 | 65 | // moqLoadFn_doFn defines the type of function needed when calling andDo for 66 | // the LoadFn type 67 | type moqLoadFn_doFn func(cfg *packages.Config, patterns ...string) 68 | 69 | // moqLoadFn_doReturnFn defines the type of function needed when calling 70 | // doReturnResults for the LoadFn type 71 | type moqLoadFn_doReturnFn func(cfg *packages.Config, patterns ...string) ([]*packages.Package, error) 72 | 73 | // moqLoadFn_recorder routes recorded function calls to the moqLoadFn moq 74 | type moqLoadFn_recorder struct { 75 | recorder *impl.Recorder[ 76 | *moqLoadFn_adaptor, 77 | moqLoadFn_params, 78 | moqLoadFn_paramsKey, 79 | moqLoadFn_results, 80 | ] 81 | } 82 | 83 | // moqLoadFn_anyParams isolates the any params functions of the LoadFn type 84 | type moqLoadFn_anyParams struct { 85 | recorder *moqLoadFn_recorder 86 | } 87 | 88 | // newMoqLoadFn creates a new moq of the LoadFn type 89 | func newMoqLoadFn(scene *moq.Scene, config *moq.Config) *moqLoadFn { 90 | adaptor1 := &moqLoadFn_adaptor{} 91 | m := &moqLoadFn{ 92 | moq: impl.NewMoq[ 93 | *moqLoadFn_adaptor, 94 | moqLoadFn_params, 95 | moqLoadFn_paramsKey, 96 | moqLoadFn_results, 97 | ](scene, adaptor1, config), 98 | 99 | runtime: moqLoadFn_runtime{parameterIndexing: moqLoadFn_paramIndexing{ 100 | cfg: moq.ParamIndexByHash, 101 | patterns: moq.ParamIndexByHash, 102 | }}, 103 | } 104 | adaptor1.moq = m 105 | 106 | scene.AddMoq(m) 107 | return m 108 | } 109 | 110 | // mock returns the moq implementation of the LoadFn type 111 | func (m *moqLoadFn) mock() ast.LoadFn { 112 | return func(cfg *packages.Config, patterns ...string) ([]*packages.Package, error) { 113 | m.moq.Scene.T.Helper() 114 | params := moqLoadFn_params{ 115 | cfg: cfg, 116 | patterns: patterns, 117 | } 118 | 119 | var result1 []*packages.Package 120 | var result2 error 121 | if result := m.moq.Function(params); result != nil { 122 | result1 = result.result1 123 | result2 = result.result2 124 | } 125 | return result1, result2 126 | } 127 | } 128 | 129 | func (m *moqLoadFn) onCall(cfg *packages.Config, patterns ...string) *moqLoadFn_recorder { 130 | return &moqLoadFn_recorder{ 131 | recorder: m.moq.OnCall(moqLoadFn_params{ 132 | cfg: cfg, 133 | patterns: patterns, 134 | }), 135 | } 136 | } 137 | 138 | func (r *moqLoadFn_recorder) any() *moqLoadFn_anyParams { 139 | r.recorder.Moq.Scene.T.Helper() 140 | if !r.recorder.IsAnyPermitted(false) { 141 | return nil 142 | } 143 | return &moqLoadFn_anyParams{recorder: r} 144 | } 145 | 146 | func (a *moqLoadFn_anyParams) cfg() *moqLoadFn_recorder { 147 | a.recorder.recorder.AnyParam(1) 148 | return a.recorder 149 | } 150 | 151 | func (a *moqLoadFn_anyParams) patterns() *moqLoadFn_recorder { 152 | a.recorder.recorder.AnyParam(2) 153 | return a.recorder 154 | } 155 | 156 | func (r *moqLoadFn_recorder) seq() *moqLoadFn_recorder { 157 | r.recorder.Moq.Scene.T.Helper() 158 | if !r.recorder.Seq(true, "seq", false) { 159 | return nil 160 | } 161 | return r 162 | } 163 | 164 | func (r *moqLoadFn_recorder) noSeq() *moqLoadFn_recorder { 165 | r.recorder.Moq.Scene.T.Helper() 166 | if !r.recorder.Seq(false, "noSeq", false) { 167 | return nil 168 | } 169 | return r 170 | } 171 | 172 | func (r *moqLoadFn_recorder) returnResults(result1 []*packages.Package, result2 error) *moqLoadFn_recorder { 173 | r.recorder.Moq.Scene.T.Helper() 174 | r.recorder.ReturnResults(moqLoadFn_results{ 175 | result1: result1, 176 | result2: result2, 177 | }) 178 | return r 179 | } 180 | 181 | func (r *moqLoadFn_recorder) andDo(fn moqLoadFn_doFn) *moqLoadFn_recorder { 182 | r.recorder.Moq.Scene.T.Helper() 183 | if !r.recorder.AndDo(func(params moqLoadFn_params) { 184 | fn(params.cfg, params.patterns...) 185 | }, false) { 186 | return nil 187 | } 188 | return r 189 | } 190 | 191 | func (r *moqLoadFn_recorder) doReturnResults(fn moqLoadFn_doReturnFn) *moqLoadFn_recorder { 192 | r.recorder.Moq.Scene.T.Helper() 193 | r.recorder.DoReturnResults(func(params moqLoadFn_params) *moqLoadFn_results { 194 | result1, result2 := fn(params.cfg, params.patterns...) 195 | return &moqLoadFn_results{ 196 | result1: result1, 197 | result2: result2, 198 | } 199 | }) 200 | return r 201 | } 202 | 203 | func (r *moqLoadFn_recorder) repeat(repeaters ...moq.Repeater) *moqLoadFn_recorder { 204 | r.recorder.Moq.Scene.T.Helper() 205 | if !r.recorder.Repeat(repeaters, false) { 206 | return nil 207 | } 208 | return r 209 | } 210 | 211 | func (*moqLoadFn_adaptor) PrettyParams(params moqLoadFn_params) string { 212 | return fmt.Sprintf("LoadFn(%#v, %#v)", params.cfg, params.patterns) 213 | } 214 | 215 | func (a *moqLoadFn_adaptor) ParamsKey(params moqLoadFn_params, anyParams uint64) moqLoadFn_paramsKey { 216 | a.moq.moq.Scene.T.Helper() 217 | cfgUsed, cfgUsedHash := impl.ParamKey( 218 | params.cfg, 1, a.moq.runtime.parameterIndexing.cfg, anyParams) 219 | patternsUsedHash := impl.HashOnlyParamKey(a.moq.moq.Scene.T, 220 | params.patterns, "patterns", 2, a.moq.runtime.parameterIndexing.patterns, anyParams) 221 | return moqLoadFn_paramsKey{ 222 | params: struct{ cfg *packages.Config }{ 223 | cfg: cfgUsed, 224 | }, 225 | hashes: struct { 226 | cfg hash.Hash 227 | patterns hash.Hash 228 | }{ 229 | cfg: cfgUsedHash, 230 | patterns: patternsUsedHash, 231 | }, 232 | } 233 | } 234 | 235 | // Reset resets the state of the moq 236 | func (m *moqLoadFn) Reset() { 237 | m.moq.Reset() 238 | } 239 | 240 | // AssertExpectationsMet asserts that all expectations have been met 241 | func (m *moqLoadFn) AssertExpectationsMet() { 242 | m.moq.Scene.T.Helper() 243 | m.moq.AssertExpectationsMet() 244 | } 245 | -------------------------------------------------------------------------------- /ast/moq_readfilefn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package ast_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/ast" 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // moqReadFileFn holds the state of a moq of the ReadFileFn type 15 | type moqReadFileFn struct { 16 | moq *impl.Moq[ 17 | *moqReadFileFn_adaptor, 18 | moqReadFileFn_params, 19 | moqReadFileFn_paramsKey, 20 | moqReadFileFn_results, 21 | ] 22 | 23 | runtime moqReadFileFn_runtime 24 | } 25 | 26 | // moqReadFileFn_runtime holds runtime configuration for the ReadFileFn type 27 | type moqReadFileFn_runtime struct { 28 | parameterIndexing moqReadFileFn_paramIndexing 29 | } 30 | 31 | // moqReadFileFn_adaptor adapts moqReadFileFn as needed by the runtime 32 | type moqReadFileFn_adaptor struct { 33 | moq *moqReadFileFn 34 | } 35 | 36 | // moqReadFileFn_params holds the params of the ReadFileFn type 37 | type moqReadFileFn_params struct{ name string } 38 | 39 | // moqReadFileFn_paramsKey holds the map key params of the ReadFileFn type 40 | type moqReadFileFn_paramsKey struct { 41 | params struct{ name string } 42 | hashes struct{ name hash.Hash } 43 | } 44 | 45 | // moqReadFileFn_results holds the results of the ReadFileFn type 46 | type moqReadFileFn_results struct { 47 | result1 []byte 48 | result2 error 49 | } 50 | 51 | // moqReadFileFn_paramIndexing holds the parameter indexing runtime 52 | // configuration for the ReadFileFn type 53 | type moqReadFileFn_paramIndexing struct { 54 | name moq.ParamIndexing 55 | } 56 | 57 | // moqReadFileFn_doFn defines the type of function needed when calling andDo 58 | // for the ReadFileFn type 59 | type moqReadFileFn_doFn func(name string) 60 | 61 | // moqReadFileFn_doReturnFn defines the type of function needed when calling 62 | // doReturnResults for the ReadFileFn type 63 | type moqReadFileFn_doReturnFn func(name string) ([]byte, error) 64 | 65 | // moqReadFileFn_recorder routes recorded function calls to the moqReadFileFn 66 | // moq 67 | type moqReadFileFn_recorder struct { 68 | recorder *impl.Recorder[ 69 | *moqReadFileFn_adaptor, 70 | moqReadFileFn_params, 71 | moqReadFileFn_paramsKey, 72 | moqReadFileFn_results, 73 | ] 74 | } 75 | 76 | // moqReadFileFn_anyParams isolates the any params functions of the ReadFileFn 77 | // type 78 | type moqReadFileFn_anyParams struct { 79 | recorder *moqReadFileFn_recorder 80 | } 81 | 82 | // newMoqReadFileFn creates a new moq of the ReadFileFn type 83 | func newMoqReadFileFn(scene *moq.Scene, config *moq.Config) *moqReadFileFn { 84 | adaptor1 := &moqReadFileFn_adaptor{} 85 | m := &moqReadFileFn{ 86 | moq: impl.NewMoq[ 87 | *moqReadFileFn_adaptor, 88 | moqReadFileFn_params, 89 | moqReadFileFn_paramsKey, 90 | moqReadFileFn_results, 91 | ](scene, adaptor1, config), 92 | 93 | runtime: moqReadFileFn_runtime{parameterIndexing: moqReadFileFn_paramIndexing{ 94 | name: moq.ParamIndexByValue, 95 | }}, 96 | } 97 | adaptor1.moq = m 98 | 99 | scene.AddMoq(m) 100 | return m 101 | } 102 | 103 | // mock returns the moq implementation of the ReadFileFn type 104 | func (m *moqReadFileFn) mock() ast.ReadFileFn { 105 | return func(name string) ([]byte, error) { 106 | m.moq.Scene.T.Helper() 107 | params := moqReadFileFn_params{ 108 | name: name, 109 | } 110 | 111 | var result1 []byte 112 | var result2 error 113 | if result := m.moq.Function(params); result != nil { 114 | result1 = result.result1 115 | result2 = result.result2 116 | } 117 | return result1, result2 118 | } 119 | } 120 | 121 | func (m *moqReadFileFn) onCall(name string) *moqReadFileFn_recorder { 122 | return &moqReadFileFn_recorder{ 123 | recorder: m.moq.OnCall(moqReadFileFn_params{ 124 | name: name, 125 | }), 126 | } 127 | } 128 | 129 | func (r *moqReadFileFn_recorder) any() *moqReadFileFn_anyParams { 130 | r.recorder.Moq.Scene.T.Helper() 131 | if !r.recorder.IsAnyPermitted(false) { 132 | return nil 133 | } 134 | return &moqReadFileFn_anyParams{recorder: r} 135 | } 136 | 137 | func (a *moqReadFileFn_anyParams) name() *moqReadFileFn_recorder { 138 | a.recorder.recorder.AnyParam(1) 139 | return a.recorder 140 | } 141 | 142 | func (r *moqReadFileFn_recorder) seq() *moqReadFileFn_recorder { 143 | r.recorder.Moq.Scene.T.Helper() 144 | if !r.recorder.Seq(true, "seq", false) { 145 | return nil 146 | } 147 | return r 148 | } 149 | 150 | func (r *moqReadFileFn_recorder) noSeq() *moqReadFileFn_recorder { 151 | r.recorder.Moq.Scene.T.Helper() 152 | if !r.recorder.Seq(false, "noSeq", false) { 153 | return nil 154 | } 155 | return r 156 | } 157 | 158 | func (r *moqReadFileFn_recorder) returnResults(result1 []byte, result2 error) *moqReadFileFn_recorder { 159 | r.recorder.Moq.Scene.T.Helper() 160 | r.recorder.ReturnResults(moqReadFileFn_results{ 161 | result1: result1, 162 | result2: result2, 163 | }) 164 | return r 165 | } 166 | 167 | func (r *moqReadFileFn_recorder) andDo(fn moqReadFileFn_doFn) *moqReadFileFn_recorder { 168 | r.recorder.Moq.Scene.T.Helper() 169 | if !r.recorder.AndDo(func(params moqReadFileFn_params) { 170 | fn(params.name) 171 | }, false) { 172 | return nil 173 | } 174 | return r 175 | } 176 | 177 | func (r *moqReadFileFn_recorder) doReturnResults(fn moqReadFileFn_doReturnFn) *moqReadFileFn_recorder { 178 | r.recorder.Moq.Scene.T.Helper() 179 | r.recorder.DoReturnResults(func(params moqReadFileFn_params) *moqReadFileFn_results { 180 | result1, result2 := fn(params.name) 181 | return &moqReadFileFn_results{ 182 | result1: result1, 183 | result2: result2, 184 | } 185 | }) 186 | return r 187 | } 188 | 189 | func (r *moqReadFileFn_recorder) repeat(repeaters ...moq.Repeater) *moqReadFileFn_recorder { 190 | r.recorder.Moq.Scene.T.Helper() 191 | if !r.recorder.Repeat(repeaters, false) { 192 | return nil 193 | } 194 | return r 195 | } 196 | 197 | func (*moqReadFileFn_adaptor) PrettyParams(params moqReadFileFn_params) string { 198 | return fmt.Sprintf("ReadFileFn(%#v)", params.name) 199 | } 200 | 201 | func (a *moqReadFileFn_adaptor) ParamsKey(params moqReadFileFn_params, anyParams uint64) moqReadFileFn_paramsKey { 202 | a.moq.moq.Scene.T.Helper() 203 | nameUsed, nameUsedHash := impl.ParamKey( 204 | params.name, 1, a.moq.runtime.parameterIndexing.name, anyParams) 205 | return moqReadFileFn_paramsKey{ 206 | params: struct{ name string }{ 207 | name: nameUsed, 208 | }, 209 | hashes: struct{ name hash.Hash }{ 210 | name: nameUsedHash, 211 | }, 212 | } 213 | } 214 | 215 | // Reset resets the state of the moq 216 | func (m *moqReadFileFn) Reset() { 217 | m.moq.Reset() 218 | } 219 | 220 | // AssertExpectationsMet asserts that all expectations have been met 221 | func (m *moqReadFileFn) AssertExpectationsMet() { 222 | m.moq.Scene.T.Helper() 223 | m.moq.AssertExpectationsMet() 224 | } 225 | -------------------------------------------------------------------------------- /ast/moq_statfn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package ast_test 4 | 5 | import ( 6 | "fmt" 7 | "os" 8 | 9 | "moqueries.org/cli/ast" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqStatFn holds the state of a moq of the StatFn type 16 | type moqStatFn struct { 17 | moq *impl.Moq[ 18 | *moqStatFn_adaptor, 19 | moqStatFn_params, 20 | moqStatFn_paramsKey, 21 | moqStatFn_results, 22 | ] 23 | 24 | runtime moqStatFn_runtime 25 | } 26 | 27 | // moqStatFn_runtime holds runtime configuration for the StatFn type 28 | type moqStatFn_runtime struct { 29 | parameterIndexing moqStatFn_paramIndexing 30 | } 31 | 32 | // moqStatFn_adaptor adapts moqStatFn as needed by the runtime 33 | type moqStatFn_adaptor struct { 34 | moq *moqStatFn 35 | } 36 | 37 | // moqStatFn_params holds the params of the StatFn type 38 | type moqStatFn_params struct{ name string } 39 | 40 | // moqStatFn_paramsKey holds the map key params of the StatFn type 41 | type moqStatFn_paramsKey struct { 42 | params struct{ name string } 43 | hashes struct{ name hash.Hash } 44 | } 45 | 46 | // moqStatFn_results holds the results of the StatFn type 47 | type moqStatFn_results struct { 48 | result1 os.FileInfo 49 | result2 error 50 | } 51 | 52 | // moqStatFn_paramIndexing holds the parameter indexing runtime configuration 53 | // for the StatFn type 54 | type moqStatFn_paramIndexing struct { 55 | name moq.ParamIndexing 56 | } 57 | 58 | // moqStatFn_doFn defines the type of function needed when calling andDo for 59 | // the StatFn type 60 | type moqStatFn_doFn func(name string) 61 | 62 | // moqStatFn_doReturnFn defines the type of function needed when calling 63 | // doReturnResults for the StatFn type 64 | type moqStatFn_doReturnFn func(name string) (os.FileInfo, error) 65 | 66 | // moqStatFn_recorder routes recorded function calls to the moqStatFn moq 67 | type moqStatFn_recorder struct { 68 | recorder *impl.Recorder[ 69 | *moqStatFn_adaptor, 70 | moqStatFn_params, 71 | moqStatFn_paramsKey, 72 | moqStatFn_results, 73 | ] 74 | } 75 | 76 | // moqStatFn_anyParams isolates the any params functions of the StatFn type 77 | type moqStatFn_anyParams struct { 78 | recorder *moqStatFn_recorder 79 | } 80 | 81 | // newMoqStatFn creates a new moq of the StatFn type 82 | func newMoqStatFn(scene *moq.Scene, config *moq.Config) *moqStatFn { 83 | adaptor1 := &moqStatFn_adaptor{} 84 | m := &moqStatFn{ 85 | moq: impl.NewMoq[ 86 | *moqStatFn_adaptor, 87 | moqStatFn_params, 88 | moqStatFn_paramsKey, 89 | moqStatFn_results, 90 | ](scene, adaptor1, config), 91 | 92 | runtime: moqStatFn_runtime{parameterIndexing: moqStatFn_paramIndexing{ 93 | name: moq.ParamIndexByValue, 94 | }}, 95 | } 96 | adaptor1.moq = m 97 | 98 | scene.AddMoq(m) 99 | return m 100 | } 101 | 102 | // mock returns the moq implementation of the StatFn type 103 | func (m *moqStatFn) mock() ast.StatFn { 104 | return func(name string) (os.FileInfo, error) { 105 | m.moq.Scene.T.Helper() 106 | params := moqStatFn_params{ 107 | name: name, 108 | } 109 | 110 | var result1 os.FileInfo 111 | var result2 error 112 | if result := m.moq.Function(params); result != nil { 113 | result1 = result.result1 114 | result2 = result.result2 115 | } 116 | return result1, result2 117 | } 118 | } 119 | 120 | func (m *moqStatFn) onCall(name string) *moqStatFn_recorder { 121 | return &moqStatFn_recorder{ 122 | recorder: m.moq.OnCall(moqStatFn_params{ 123 | name: name, 124 | }), 125 | } 126 | } 127 | 128 | func (r *moqStatFn_recorder) any() *moqStatFn_anyParams { 129 | r.recorder.Moq.Scene.T.Helper() 130 | if !r.recorder.IsAnyPermitted(false) { 131 | return nil 132 | } 133 | return &moqStatFn_anyParams{recorder: r} 134 | } 135 | 136 | func (a *moqStatFn_anyParams) name() *moqStatFn_recorder { 137 | a.recorder.recorder.AnyParam(1) 138 | return a.recorder 139 | } 140 | 141 | func (r *moqStatFn_recorder) seq() *moqStatFn_recorder { 142 | r.recorder.Moq.Scene.T.Helper() 143 | if !r.recorder.Seq(true, "seq", false) { 144 | return nil 145 | } 146 | return r 147 | } 148 | 149 | func (r *moqStatFn_recorder) noSeq() *moqStatFn_recorder { 150 | r.recorder.Moq.Scene.T.Helper() 151 | if !r.recorder.Seq(false, "noSeq", false) { 152 | return nil 153 | } 154 | return r 155 | } 156 | 157 | func (r *moqStatFn_recorder) returnResults(result1 os.FileInfo, result2 error) *moqStatFn_recorder { 158 | r.recorder.Moq.Scene.T.Helper() 159 | r.recorder.ReturnResults(moqStatFn_results{ 160 | result1: result1, 161 | result2: result2, 162 | }) 163 | return r 164 | } 165 | 166 | func (r *moqStatFn_recorder) andDo(fn moqStatFn_doFn) *moqStatFn_recorder { 167 | r.recorder.Moq.Scene.T.Helper() 168 | if !r.recorder.AndDo(func(params moqStatFn_params) { 169 | fn(params.name) 170 | }, false) { 171 | return nil 172 | } 173 | return r 174 | } 175 | 176 | func (r *moqStatFn_recorder) doReturnResults(fn moqStatFn_doReturnFn) *moqStatFn_recorder { 177 | r.recorder.Moq.Scene.T.Helper() 178 | r.recorder.DoReturnResults(func(params moqStatFn_params) *moqStatFn_results { 179 | result1, result2 := fn(params.name) 180 | return &moqStatFn_results{ 181 | result1: result1, 182 | result2: result2, 183 | } 184 | }) 185 | return r 186 | } 187 | 188 | func (r *moqStatFn_recorder) repeat(repeaters ...moq.Repeater) *moqStatFn_recorder { 189 | r.recorder.Moq.Scene.T.Helper() 190 | if !r.recorder.Repeat(repeaters, false) { 191 | return nil 192 | } 193 | return r 194 | } 195 | 196 | func (*moqStatFn_adaptor) PrettyParams(params moqStatFn_params) string { 197 | return fmt.Sprintf("StatFn(%#v)", params.name) 198 | } 199 | 200 | func (a *moqStatFn_adaptor) ParamsKey(params moqStatFn_params, anyParams uint64) moqStatFn_paramsKey { 201 | a.moq.moq.Scene.T.Helper() 202 | nameUsed, nameUsedHash := impl.ParamKey( 203 | params.name, 1, a.moq.runtime.parameterIndexing.name, anyParams) 204 | return moqStatFn_paramsKey{ 205 | params: struct{ name string }{ 206 | name: nameUsed, 207 | }, 208 | hashes: struct{ name hash.Hash }{ 209 | name: nameUsedHash, 210 | }, 211 | } 212 | } 213 | 214 | // Reset resets the state of the moq 215 | func (m *moqStatFn) Reset() { 216 | m.moq.Reset() 217 | } 218 | 219 | // AssertExpectationsMet asserts that all expectations have been met 220 | func (m *moqStatFn) AssertExpectationsMet() { 221 | m.moq.Scene.T.Helper() 222 | m.moq.AssertExpectationsMet() 223 | } 224 | -------------------------------------------------------------------------------- /ast/testpkgs/export/file1.go: -------------------------------------------------------------------------------- 1 | package export 2 | 3 | type Type1 interface { 4 | Funcy1() 5 | } 6 | -------------------------------------------------------------------------------- /ast/testpkgs/export/file2.go: -------------------------------------------------------------------------------- 1 | package export 2 | 3 | type Type2 func() 4 | 5 | func Type4() {} 6 | -------------------------------------------------------------------------------- /ast/testpkgs/export/file3.go: -------------------------------------------------------------------------------- 1 | package export 2 | 3 | type Type3 struct{} 4 | 5 | type Widget struct{} 6 | 7 | func (Widget) Type5() {} 8 | 9 | func (Widget) Type6() {} 10 | 11 | type WidgetByRef struct{} 12 | 13 | func (*WidgetByRef) Type7() {} 14 | 15 | func (*WidgetByRef) Type8() {} 16 | -------------------------------------------------------------------------------- /ast/testpkgs/export/generics.go: -------------------------------------------------------------------------------- 1 | package export 2 | 3 | type GenericByRef[T any] struct{} 4 | 5 | func (g *GenericByRef[T]) DoSomethingPtr() {} 6 | 7 | func (g *GenericByRef[X]) DoSomethingElsePtr() {} 8 | 9 | type Generic[T any] struct{} 10 | 11 | func (g Generic[T]) DoSomething() {} 12 | 13 | func (g Generic[X]) DoSomethingElse() {} 14 | 15 | type GenericListByRef[T any, V any] struct{} 16 | 17 | func (g *GenericListByRef[T, V]) DoSomethingPtr() {} 18 | 19 | func (g *GenericListByRef[X, Y]) DoSomethingElsePtr() {} 20 | 21 | type GenericList[T any, V any] struct{} 22 | 23 | func (g GenericList[T, V]) DoSomething() {} 24 | 25 | func (g GenericList[X, Y]) DoSomethingElse() {} 26 | -------------------------------------------------------------------------------- /ast/testpkgs/fabricate/silliness.go: -------------------------------------------------------------------------------- 1 | package fabricate 2 | 3 | type Mixed int 4 | 5 | func (Mixed) nonExportedByValueRecv() {} 6 | 7 | func (*Mixed) ExportedByRefRecv() {} 8 | 9 | type CantMock int 10 | 11 | func (CantMock) ByValueRecv() {} 12 | 13 | func (*CantMock) ByRefRecv() {} 14 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file1.go: -------------------------------------------------------------------------------- 1 | package noexport 2 | 3 | type type1 interface { 4 | funcy1() 5 | } 6 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file1_test.go: -------------------------------------------------------------------------------- 1 | package noexport_test 2 | 3 | type test_type1 interface{} 4 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file2.go: -------------------------------------------------------------------------------- 1 | package noexport 2 | 3 | type type2 func() 4 | 5 | func type4() {} 6 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file2_test.go: -------------------------------------------------------------------------------- 1 | package noexport_test 2 | 3 | type test_type2 func() 4 | 5 | func test_type4() {} 6 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file3.go: -------------------------------------------------------------------------------- 1 | package noexport 2 | 3 | type type3 struct{} 4 | 5 | type widget struct{} 6 | 7 | func (widget) method1() {} 8 | 9 | func (widget) method2() {} 10 | 11 | type widgetByRef struct{} 12 | 13 | func (*widgetByRef) method3() {} 14 | 15 | func (*widgetByRef) method4() {} 16 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/file3_test.go: -------------------------------------------------------------------------------- 1 | package noexport_test 2 | 3 | type test_type3 struct{} 4 | 5 | type test_widget struct{} 6 | 7 | func (test_widget) test_method1() {} 8 | 9 | func (test_widget) test_method2() {} 10 | 11 | type test_widgetByRef struct{} 12 | 13 | func (*test_widgetByRef) test_method3() {} 14 | 15 | func (*test_widgetByRef) test_method4() {} 16 | -------------------------------------------------------------------------------- /ast/testpkgs/noexport/generics.go: -------------------------------------------------------------------------------- 1 | package noexport 2 | 3 | type genericByRef[T any] struct{} 4 | 5 | func (g *genericByRef[T]) doSomethingPtr() {} 6 | 7 | func (g *genericByRef[X]) doSomethingElsePtr() {} 8 | 9 | type generic[T any] struct{} 10 | 11 | func (g generic[T]) doSomething() {} 12 | 13 | func (g generic[X]) doSomethingElse() {} 14 | 15 | type genericListByRef[T any, V any] struct{} 16 | 17 | func (g *genericListByRef[T, V]) doSomethingPtr() {} 18 | 19 | func (g *genericListByRef[X, Y]) doSomethingElsePtr() {} 20 | 21 | type genericList[T any, V any] struct{} 22 | 23 | func (g genericList[T, V]) doSomething() {} 24 | 25 | func (g genericList[X, Y]) doSomethingElse() {} 26 | -------------------------------------------------------------------------------- /ast/testpkgs/replacebuiltin/holdmybeer.go: -------------------------------------------------------------------------------- 1 | package replacebuiltin 2 | 3 | type string int 4 | 5 | type HoldMyBeer func(xyz string) string 6 | -------------------------------------------------------------------------------- /bulk/bulk.go: -------------------------------------------------------------------------------- 1 | // Package bulk is used to generate several Moqueries mocks at once 2 | package bulk 3 | 4 | import ( 5 | "io" 6 | "os" 7 | 8 | "moqueries.org/cli/bulk/internal" 9 | "moqueries.org/cli/generator" 10 | ) 11 | 12 | // Initialize initializes bulk processing and creates the bulk processing state 13 | // file 14 | func Initialize(stateFile, rootDir string) error { 15 | createFn := func(name string) (io.WriteCloser, error) { 16 | //nolint:gosec // Users can use any file for bulk operations 17 | return os.Create(name) 18 | } 19 | 20 | return internal.Initialize(stateFile, rootDir, createFn) 21 | } 22 | 23 | // Append appends a mock generate request to the bulk state 24 | func Append(stateFile string, request generator.GenerateRequest) error { 25 | openFileFn := func(name string, flag int, perm os.FileMode) (internal.ReadWriteSeekCloser, error) { 26 | //nolint:gosec // Users can use any file for bulk operations 27 | return os.OpenFile(name, flag, perm) 28 | } 29 | 30 | return internal.Append(stateFile, request, openFileFn) 31 | } 32 | 33 | // Finalize complete bulk processing by generating all the requested mocks 34 | func Finalize(stateFile, rootDir string) error { 35 | openFn := func(name string) (io.ReadCloser, error) { 36 | //nolint:gosec // Users can use any file for bulk operations 37 | return os.Open(name) 38 | } 39 | 40 | return internal.Finalize(stateFile, rootDir, openFn, generator.Generate) 41 | } 42 | -------------------------------------------------------------------------------- /bulk/internal/appender.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "bufio" 5 | "encoding/json" 6 | "errors" 7 | "fmt" 8 | "go/build" 9 | "io" 10 | "os" 11 | "path/filepath" 12 | "strings" 13 | 14 | "moqueries.org/runtime/logs" 15 | 16 | "moqueries.org/cli/generator" 17 | ) 18 | 19 | var ( 20 | // ErrBadAppendRequest is returned when a caller passes bad parameters to 21 | // Append 22 | ErrBadAppendRequest = errors.New("bad request") 23 | // ErrBulkState is returned when the bulk state is invalid 24 | ErrBulkState = errors.New("bulk state error") 25 | ) 26 | 27 | //go:generate moqueries OpenFileFn 28 | 29 | // OpenFileFn is the function type of os.OpenFile 30 | type OpenFileFn func(name string, flag int, perm os.FileMode) (ReadWriteSeekCloser, error) 31 | 32 | //go:generate moqueries ReadWriteSeekCloser 33 | 34 | // ReadWriteSeekCloser is the interface that groups the basic Read, Write, 35 | // Seek and Close methods. 36 | type ReadWriteSeekCloser interface { 37 | io.Reader 38 | io.Writer 39 | io.Seeker 40 | io.Closer 41 | } 42 | 43 | // Append appends a mock generate request to the bulk state 44 | func Append(stateFile string, req generator.GenerateRequest, openFileFn OpenFileFn) error { 45 | if !filepath.IsAbs(req.WorkingDir) { 46 | return fmt.Errorf("%w: the request working directory must be absolute: %s", 47 | ErrBadAppendRequest, req.WorkingDir) 48 | } 49 | 50 | f, err := openFileFn(stateFile, os.O_RDWR|os.O_APPEND, 0) 51 | if err != nil { 52 | return fmt.Errorf("error opening state file: %w", err) 53 | } 54 | defer func() { 55 | err := f.Close() 56 | if err != nil { 57 | logs.Error("error closing state file", err) 58 | } 59 | }() 60 | 61 | _, err = verifyState(f, stateFile, req.WorkingDir, false) 62 | if err != nil { 63 | return err 64 | } 65 | 66 | err = appendRequest(f, stateFile, req) 67 | if err != nil { 68 | return err 69 | } 70 | 71 | return nil 72 | } 73 | 74 | func verifyState(f io.ReadCloser, stateFile, workingDir string, rootDirOnly bool) (*bufio.Scanner, error) { 75 | scanner := bufio.NewScanner(f) 76 | if !scanner.Scan() { 77 | return nil, fmt.Errorf("%w: state file %s not initialized properly", 78 | ErrBulkState, stateFile) 79 | } 80 | 81 | txt := scanner.Text() 82 | err := scanner.Err() 83 | if err != nil { 84 | return nil, fmt.Errorf("error reading state file %s: %w", stateFile, err) 85 | } 86 | 87 | var state initialState 88 | err = json.Unmarshal([]byte(txt), &state) 89 | if err != nil { 90 | return nil, fmt.Errorf("error unmarshalling state file %s: %w", stateFile, err) 91 | } 92 | 93 | if state.GoPath != build.Default.GOPATH { 94 | return nil, fmt.Errorf("%w: current GOPATH doesn't match GOPATH from state file %s (%s != %s)", 95 | ErrBulkState, stateFile, build.Default.GOPATH, state.GoPath) 96 | } 97 | 98 | if rootDirOnly { 99 | if state.RootDir != workingDir { 100 | return nil, fmt.Errorf("%w: finalize root directory %s does"+ 101 | " not match root directory %s from state file %s", 102 | ErrBulkState, workingDir, state.RootDir, stateFile) 103 | } 104 | } else { 105 | rel, err := filepath.Rel(state.RootDir, workingDir) 106 | if err != nil { 107 | logs.Panicf("error getting relative path %s from %s: %#v", 108 | state.RootDir, workingDir, err) 109 | } 110 | 111 | if strings.HasPrefix(rel, "..") { 112 | return nil, fmt.Errorf("%w: working directory %s is not a"+ 113 | " child of root directory %s from state file %s", 114 | ErrBulkState, workingDir, state.RootDir, stateFile) 115 | } 116 | } 117 | 118 | return scanner, nil 119 | } 120 | 121 | func appendRequest(f ReadWriteSeekCloser, stateFile string, req generator.GenerateRequest) error { 122 | _, err := f.Seek(0, io.SeekEnd) 123 | if err != nil { 124 | return fmt.Errorf("error seeking end of state file %s: %w", stateFile, err) 125 | } 126 | _, err = f.Write(compact(req)) 127 | if err != nil { 128 | return fmt.Errorf("error writing state file %s: %w", stateFile, err) 129 | } 130 | _, err = f.Write([]byte("\n")) 131 | if err != nil { 132 | return fmt.Errorf("error finishing writing of state file %s: %w", stateFile, err) 133 | } 134 | return nil 135 | } 136 | -------------------------------------------------------------------------------- /bulk/internal/finalizer.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io" 7 | 8 | "moqueries.org/runtime/logs" 9 | 10 | "moqueries.org/cli/generator" 11 | ) 12 | 13 | //go:generate moqueries OpenFn 14 | 15 | // OpenFn is the function type of os.Open 16 | type OpenFn func(name string) (file io.ReadCloser, err error) 17 | 18 | //go:generate moqueries --import io ReadCloser 19 | 20 | //go:generate moqueries GenerateFn 21 | 22 | // GenerateFn is the function type of generator.Generate 23 | type GenerateFn func(reqs ...generator.GenerateRequest) error 24 | 25 | // Finalize complete bulk processing by generating all the requested mocks 26 | func Finalize(stateFile, rootDir string, openFn OpenFn, generateFn GenerateFn) error { 27 | f, err := openFn(stateFile) 28 | if err != nil { 29 | return fmt.Errorf("error opening state file: %w", err) 30 | } 31 | defer func() { 32 | err := f.Close() 33 | if err != nil { 34 | logs.Error("error closing state file", err) 35 | } 36 | }() 37 | scanner, err := verifyState(f, stateFile, rootDir, true) 38 | if err != nil { 39 | return err 40 | } 41 | 42 | var reqs []generator.GenerateRequest 43 | for scanner.Scan() { 44 | txt := scanner.Text() 45 | 46 | var req generator.GenerateRequest 47 | err = json.Unmarshal([]byte(txt), &req) 48 | if err != nil { 49 | return fmt.Errorf("error unmarshalling request from state file %s: %w", stateFile, err) 50 | } 51 | reqs = append(reqs, req) 52 | } 53 | 54 | err = scanner.Err() 55 | if err != nil { 56 | return fmt.Errorf("error reading request from state file %s: %w", stateFile, err) 57 | } 58 | 59 | err = generateFn(reqs...) 60 | if err != nil { 61 | return fmt.Errorf("error generating mocks from state file %s: %w", stateFile, err) 62 | } 63 | 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /bulk/internal/initializer.go: -------------------------------------------------------------------------------- 1 | // Package internal implements the internals for bulk operations 2 | package internal 3 | 4 | import ( 5 | "bytes" 6 | "encoding/json" 7 | "fmt" 8 | "go/build" 9 | "io" 10 | 11 | "moqueries.org/runtime/logs" 12 | ) 13 | 14 | //go:generate moqueries CreateFn 15 | 16 | // CreateFn is the function type of os.Create 17 | type CreateFn func(name string) (file io.WriteCloser, err error) 18 | 19 | //go:generate moqueries --import io WriteCloser 20 | 21 | type initialState struct { 22 | RootDir string `json:"root-dir"` 23 | GoPath string `json:"go-path"` 24 | } 25 | 26 | // Initialize initializes bulk processing and creates the bulk processing state 27 | // file 28 | func Initialize(stateFile, rootDir string, createFn CreateFn) error { 29 | f, err := createFn(stateFile) 30 | if err != nil { 31 | return fmt.Errorf("error creating state file: %w", err) 32 | } 33 | defer func() { 34 | err := f.Close() 35 | if err != nil { 36 | logs.Error("error closing state file", err) 37 | } 38 | }() 39 | 40 | state := initialState{ 41 | RootDir: rootDir, 42 | GoPath: build.Default.GOPATH, 43 | } 44 | _, err = f.Write(compact(state)) 45 | if err != nil { 46 | return fmt.Errorf("error writing state file %s: %w", stateFile, err) 47 | } 48 | _, err = f.Write([]byte("\n")) 49 | if err != nil { 50 | return fmt.Errorf("error finishing writing of state file %s: %w", stateFile, err) 51 | } 52 | 53 | return nil 54 | } 55 | 56 | func compact(v interface{}) []byte { 57 | b, err := json.Marshal(v) 58 | if err != nil { 59 | logs.Panic("error marshalling state info", err) 60 | } 61 | buf := bytes.NewBuffer([]byte{}) 62 | err = json.Compact(buf, b) 63 | if err != nil { 64 | logs.Panic("error compacting state info", err) 65 | } 66 | return buf.Bytes() 67 | } 68 | -------------------------------------------------------------------------------- /bulk/internal/initializer_test.go: -------------------------------------------------------------------------------- 1 | package internal_test 2 | 3 | import ( 4 | "errors" 5 | "go/build" 6 | "testing" 7 | 8 | "moqueries.org/runtime/moq" 9 | 10 | "moqueries.org/cli/bulk/internal" 11 | ) 12 | 13 | var goodInitLine = "{\"root-dir\":\"/my-root-dir\",\"go-path\":\"" + build.Default.GOPATH + "\"}" 14 | 15 | func TestInitialize(t *testing.T) { 16 | var ( 17 | scene *moq.Scene 18 | createFnMoq *moqCreateFn 19 | writeCloserMoq *moqWriteCloser 20 | ) 21 | 22 | beforeEach := func(t *testing.T) { 23 | t.Helper() 24 | if scene != nil { 25 | t.Fatal("afterEach not called") 26 | } 27 | scene = moq.NewScene(t) 28 | config := &moq.Config{Sequence: moq.SeqDefaultOn} 29 | createFnMoq = newMoqCreateFn(scene, config) 30 | writeCloserMoq = newMoqWriteCloser(scene, config) 31 | } 32 | 33 | afterEach := func() { 34 | scene.AssertExpectationsMet() 35 | scene = nil 36 | } 37 | 38 | t.Run("simple", func(t *testing.T) { 39 | // ASSEMBLE 40 | beforeEach(t) 41 | defer afterEach() 42 | createFnMoq.onCall("some-path-to-state"). 43 | returnResults(writeCloserMoq.mock(), nil) 44 | writeCloserMoq.onCall().Write([]byte(goodInitLine)).returnResults(0, nil) 45 | writeCloserMoq.onCall().Write([]byte("\n")).returnResults(0, nil) 46 | writeCloserMoq.onCall().Close().returnResults(errors.New("ignored-error")) 47 | 48 | // ACT 49 | err := internal.Initialize( 50 | "some-path-to-state", "/my-root-dir", createFnMoq.mock()) 51 | // ASSERT 52 | if err != nil { 53 | t.Errorf("got %#v, wanted no error", err) 54 | } 55 | }) 56 | 57 | t.Run("create error", func(t *testing.T) { 58 | // ASSEMBLE 59 | beforeEach(t) 60 | defer afterEach() 61 | createFnMoq.onCall("some-path-to-state"). 62 | returnResults(nil, errors.New("create-error")) 63 | 64 | // ACT 65 | err := internal.Initialize( 66 | "some-path-to-state", "root-dir", createFnMoq.mock()) 67 | 68 | // ASSERT 69 | if err == nil { 70 | t.Fatalf("got %#v, wanted an error", err) 71 | } 72 | expectedErrMsg := "error creating state file: create-error" 73 | if err.Error() != expectedErrMsg { 74 | t.Errorf("got %s, wanted %s", err.Error(), expectedErrMsg) 75 | } 76 | }) 77 | 78 | t.Run("write error", func(t *testing.T) { 79 | // ASSEMBLE 80 | beforeEach(t) 81 | defer afterEach() 82 | createFnMoq.onCall("some-path-to-state"). 83 | returnResults(writeCloserMoq.mock(), nil) 84 | writeCloserMoq.onCall().Write([]byte(goodInitLine)). 85 | returnResults(0, errors.New("write-error")) 86 | writeCloserMoq.onCall().Close().returnResults(nil) 87 | 88 | // ACT 89 | err := internal.Initialize( 90 | "some-path-to-state", "/my-root-dir", createFnMoq.mock()) 91 | 92 | // ASSERT 93 | if err == nil { 94 | t.Fatalf("got %#v, wanted an error", err) 95 | } 96 | expectedErrMsg := "error writing state file some-path-to-state: write-error" 97 | if err.Error() != expectedErrMsg { 98 | t.Errorf("got %s, wanted %s", err.Error(), expectedErrMsg) 99 | } 100 | }) 101 | 102 | t.Run("final write error", func(t *testing.T) { 103 | // ASSEMBLE 104 | beforeEach(t) 105 | defer afterEach() 106 | createFnMoq.onCall("some-path-to-state"). 107 | returnResults(writeCloserMoq.mock(), nil) 108 | writeCloserMoq.onCall().Write([]byte(goodInitLine)). 109 | returnResults(0, nil) 110 | writeCloserMoq.onCall().Write([]byte("\n")). 111 | returnResults(0, errors.New("write-error")) 112 | writeCloserMoq.onCall().Close().returnResults(nil) 113 | 114 | // ACT 115 | err := internal.Initialize( 116 | "some-path-to-state", "/my-root-dir", createFnMoq.mock()) 117 | 118 | // ASSERT 119 | if err == nil { 120 | t.Fatalf("got %#v, wanted an error", err) 121 | } 122 | expectedErrMsg := "error finishing writing of state file some-path-to-state: write-error" 123 | if err.Error() != expectedErrMsg { 124 | t.Errorf("got %s, wanted %s", err.Error(), expectedErrMsg) 125 | } 126 | }) 127 | } 128 | -------------------------------------------------------------------------------- /bulk/internal/moq_createfn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package internal_test 4 | 5 | import ( 6 | "fmt" 7 | "io" 8 | 9 | "moqueries.org/cli/bulk/internal" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqCreateFn holds the state of a moq of the CreateFn type 16 | type moqCreateFn struct { 17 | moq *impl.Moq[ 18 | *moqCreateFn_adaptor, 19 | moqCreateFn_params, 20 | moqCreateFn_paramsKey, 21 | moqCreateFn_results, 22 | ] 23 | 24 | runtime moqCreateFn_runtime 25 | } 26 | 27 | // moqCreateFn_runtime holds runtime configuration for the CreateFn type 28 | type moqCreateFn_runtime struct { 29 | parameterIndexing moqCreateFn_paramIndexing 30 | } 31 | 32 | // moqCreateFn_adaptor adapts moqCreateFn as needed by the runtime 33 | type moqCreateFn_adaptor struct { 34 | moq *moqCreateFn 35 | } 36 | 37 | // moqCreateFn_params holds the params of the CreateFn type 38 | type moqCreateFn_params struct{ name string } 39 | 40 | // moqCreateFn_paramsKey holds the map key params of the CreateFn type 41 | type moqCreateFn_paramsKey struct { 42 | params struct{ name string } 43 | hashes struct{ name hash.Hash } 44 | } 45 | 46 | // moqCreateFn_results holds the results of the CreateFn type 47 | type moqCreateFn_results struct { 48 | file io.WriteCloser 49 | err error 50 | } 51 | 52 | // moqCreateFn_paramIndexing holds the parameter indexing runtime configuration 53 | // for the CreateFn type 54 | type moqCreateFn_paramIndexing struct { 55 | name moq.ParamIndexing 56 | } 57 | 58 | // moqCreateFn_doFn defines the type of function needed when calling andDo for 59 | // the CreateFn type 60 | type moqCreateFn_doFn func(name string) 61 | 62 | // moqCreateFn_doReturnFn defines the type of function needed when calling 63 | // doReturnResults for the CreateFn type 64 | type moqCreateFn_doReturnFn func(name string) (file io.WriteCloser, err error) 65 | 66 | // moqCreateFn_recorder routes recorded function calls to the moqCreateFn moq 67 | type moqCreateFn_recorder struct { 68 | recorder *impl.Recorder[ 69 | *moqCreateFn_adaptor, 70 | moqCreateFn_params, 71 | moqCreateFn_paramsKey, 72 | moqCreateFn_results, 73 | ] 74 | } 75 | 76 | // moqCreateFn_anyParams isolates the any params functions of the CreateFn type 77 | type moqCreateFn_anyParams struct { 78 | recorder *moqCreateFn_recorder 79 | } 80 | 81 | // newMoqCreateFn creates a new moq of the CreateFn type 82 | func newMoqCreateFn(scene *moq.Scene, config *moq.Config) *moqCreateFn { 83 | adaptor1 := &moqCreateFn_adaptor{} 84 | m := &moqCreateFn{ 85 | moq: impl.NewMoq[ 86 | *moqCreateFn_adaptor, 87 | moqCreateFn_params, 88 | moqCreateFn_paramsKey, 89 | moqCreateFn_results, 90 | ](scene, adaptor1, config), 91 | 92 | runtime: moqCreateFn_runtime{parameterIndexing: moqCreateFn_paramIndexing{ 93 | name: moq.ParamIndexByValue, 94 | }}, 95 | } 96 | adaptor1.moq = m 97 | 98 | scene.AddMoq(m) 99 | return m 100 | } 101 | 102 | // mock returns the moq implementation of the CreateFn type 103 | func (m *moqCreateFn) mock() internal.CreateFn { 104 | return func(name string) (io.WriteCloser, error) { 105 | m.moq.Scene.T.Helper() 106 | params := moqCreateFn_params{ 107 | name: name, 108 | } 109 | 110 | var result1 io.WriteCloser 111 | var result2 error 112 | if result := m.moq.Function(params); result != nil { 113 | result1 = result.file 114 | result2 = result.err 115 | } 116 | return result1, result2 117 | } 118 | } 119 | 120 | func (m *moqCreateFn) onCall(name string) *moqCreateFn_recorder { 121 | return &moqCreateFn_recorder{ 122 | recorder: m.moq.OnCall(moqCreateFn_params{ 123 | name: name, 124 | }), 125 | } 126 | } 127 | 128 | func (r *moqCreateFn_recorder) any() *moqCreateFn_anyParams { 129 | r.recorder.Moq.Scene.T.Helper() 130 | if !r.recorder.IsAnyPermitted(false) { 131 | return nil 132 | } 133 | return &moqCreateFn_anyParams{recorder: r} 134 | } 135 | 136 | func (a *moqCreateFn_anyParams) name() *moqCreateFn_recorder { 137 | a.recorder.recorder.AnyParam(1) 138 | return a.recorder 139 | } 140 | 141 | func (r *moqCreateFn_recorder) seq() *moqCreateFn_recorder { 142 | r.recorder.Moq.Scene.T.Helper() 143 | if !r.recorder.Seq(true, "seq", false) { 144 | return nil 145 | } 146 | return r 147 | } 148 | 149 | func (r *moqCreateFn_recorder) noSeq() *moqCreateFn_recorder { 150 | r.recorder.Moq.Scene.T.Helper() 151 | if !r.recorder.Seq(false, "noSeq", false) { 152 | return nil 153 | } 154 | return r 155 | } 156 | 157 | func (r *moqCreateFn_recorder) returnResults(file io.WriteCloser, err error) *moqCreateFn_recorder { 158 | r.recorder.Moq.Scene.T.Helper() 159 | r.recorder.ReturnResults(moqCreateFn_results{ 160 | file: file, 161 | err: err, 162 | }) 163 | return r 164 | } 165 | 166 | func (r *moqCreateFn_recorder) andDo(fn moqCreateFn_doFn) *moqCreateFn_recorder { 167 | r.recorder.Moq.Scene.T.Helper() 168 | if !r.recorder.AndDo(func(params moqCreateFn_params) { 169 | fn(params.name) 170 | }, false) { 171 | return nil 172 | } 173 | return r 174 | } 175 | 176 | func (r *moqCreateFn_recorder) doReturnResults(fn moqCreateFn_doReturnFn) *moqCreateFn_recorder { 177 | r.recorder.Moq.Scene.T.Helper() 178 | r.recorder.DoReturnResults(func(params moqCreateFn_params) *moqCreateFn_results { 179 | file, err := fn(params.name) 180 | return &moqCreateFn_results{ 181 | file: file, 182 | err: err, 183 | } 184 | }) 185 | return r 186 | } 187 | 188 | func (r *moqCreateFn_recorder) repeat(repeaters ...moq.Repeater) *moqCreateFn_recorder { 189 | r.recorder.Moq.Scene.T.Helper() 190 | if !r.recorder.Repeat(repeaters, false) { 191 | return nil 192 | } 193 | return r 194 | } 195 | 196 | func (*moqCreateFn_adaptor) PrettyParams(params moqCreateFn_params) string { 197 | return fmt.Sprintf("CreateFn(%#v)", params.name) 198 | } 199 | 200 | func (a *moqCreateFn_adaptor) ParamsKey(params moqCreateFn_params, anyParams uint64) moqCreateFn_paramsKey { 201 | a.moq.moq.Scene.T.Helper() 202 | nameUsed, nameUsedHash := impl.ParamKey( 203 | params.name, 1, a.moq.runtime.parameterIndexing.name, anyParams) 204 | return moqCreateFn_paramsKey{ 205 | params: struct{ name string }{ 206 | name: nameUsed, 207 | }, 208 | hashes: struct{ name hash.Hash }{ 209 | name: nameUsedHash, 210 | }, 211 | } 212 | } 213 | 214 | // Reset resets the state of the moq 215 | func (m *moqCreateFn) Reset() { 216 | m.moq.Reset() 217 | } 218 | 219 | // AssertExpectationsMet asserts that all expectations have been met 220 | func (m *moqCreateFn) AssertExpectationsMet() { 221 | m.moq.Scene.T.Helper() 222 | m.moq.AssertExpectationsMet() 223 | } 224 | -------------------------------------------------------------------------------- /bulk/internal/moq_generatefn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package internal_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/bulk/internal" 9 | "moqueries.org/cli/generator" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqGenerateFn holds the state of a moq of the GenerateFn type 16 | type moqGenerateFn struct { 17 | moq *impl.Moq[ 18 | *moqGenerateFn_adaptor, 19 | moqGenerateFn_params, 20 | moqGenerateFn_paramsKey, 21 | moqGenerateFn_results, 22 | ] 23 | 24 | runtime moqGenerateFn_runtime 25 | } 26 | 27 | // moqGenerateFn_runtime holds runtime configuration for the GenerateFn type 28 | type moqGenerateFn_runtime struct { 29 | parameterIndexing moqGenerateFn_paramIndexing 30 | } 31 | 32 | // moqGenerateFn_adaptor adapts moqGenerateFn as needed by the runtime 33 | type moqGenerateFn_adaptor struct { 34 | moq *moqGenerateFn 35 | } 36 | 37 | // moqGenerateFn_params holds the params of the GenerateFn type 38 | type moqGenerateFn_params struct{ reqs []generator.GenerateRequest } 39 | 40 | // moqGenerateFn_paramsKey holds the map key params of the GenerateFn type 41 | type moqGenerateFn_paramsKey struct { 42 | params struct{} 43 | hashes struct{ reqs hash.Hash } 44 | } 45 | 46 | // moqGenerateFn_results holds the results of the GenerateFn type 47 | type moqGenerateFn_results struct { 48 | result1 error 49 | } 50 | 51 | // moqGenerateFn_paramIndexing holds the parameter indexing runtime 52 | // configuration for the GenerateFn type 53 | type moqGenerateFn_paramIndexing struct { 54 | reqs moq.ParamIndexing 55 | } 56 | 57 | // moqGenerateFn_doFn defines the type of function needed when calling andDo 58 | // for the GenerateFn type 59 | type moqGenerateFn_doFn func(reqs ...generator.GenerateRequest) 60 | 61 | // moqGenerateFn_doReturnFn defines the type of function needed when calling 62 | // doReturnResults for the GenerateFn type 63 | type moqGenerateFn_doReturnFn func(reqs ...generator.GenerateRequest) error 64 | 65 | // moqGenerateFn_recorder routes recorded function calls to the moqGenerateFn 66 | // moq 67 | type moqGenerateFn_recorder struct { 68 | recorder *impl.Recorder[ 69 | *moqGenerateFn_adaptor, 70 | moqGenerateFn_params, 71 | moqGenerateFn_paramsKey, 72 | moqGenerateFn_results, 73 | ] 74 | } 75 | 76 | // moqGenerateFn_anyParams isolates the any params functions of the GenerateFn 77 | // type 78 | type moqGenerateFn_anyParams struct { 79 | recorder *moqGenerateFn_recorder 80 | } 81 | 82 | // newMoqGenerateFn creates a new moq of the GenerateFn type 83 | func newMoqGenerateFn(scene *moq.Scene, config *moq.Config) *moqGenerateFn { 84 | adaptor1 := &moqGenerateFn_adaptor{} 85 | m := &moqGenerateFn{ 86 | moq: impl.NewMoq[ 87 | *moqGenerateFn_adaptor, 88 | moqGenerateFn_params, 89 | moqGenerateFn_paramsKey, 90 | moqGenerateFn_results, 91 | ](scene, adaptor1, config), 92 | 93 | runtime: moqGenerateFn_runtime{parameterIndexing: moqGenerateFn_paramIndexing{ 94 | reqs: moq.ParamIndexByHash, 95 | }}, 96 | } 97 | adaptor1.moq = m 98 | 99 | scene.AddMoq(m) 100 | return m 101 | } 102 | 103 | // mock returns the moq implementation of the GenerateFn type 104 | func (m *moqGenerateFn) mock() internal.GenerateFn { 105 | return func(reqs ...generator.GenerateRequest) error { 106 | m.moq.Scene.T.Helper() 107 | params := moqGenerateFn_params{ 108 | reqs: reqs, 109 | } 110 | 111 | var result1 error 112 | if result := m.moq.Function(params); result != nil { 113 | result1 = result.result1 114 | } 115 | return result1 116 | } 117 | } 118 | 119 | func (m *moqGenerateFn) onCall(reqs ...generator.GenerateRequest) *moqGenerateFn_recorder { 120 | return &moqGenerateFn_recorder{ 121 | recorder: m.moq.OnCall(moqGenerateFn_params{ 122 | reqs: reqs, 123 | }), 124 | } 125 | } 126 | 127 | func (r *moqGenerateFn_recorder) any() *moqGenerateFn_anyParams { 128 | r.recorder.Moq.Scene.T.Helper() 129 | if !r.recorder.IsAnyPermitted(false) { 130 | return nil 131 | } 132 | return &moqGenerateFn_anyParams{recorder: r} 133 | } 134 | 135 | func (a *moqGenerateFn_anyParams) reqs() *moqGenerateFn_recorder { 136 | a.recorder.recorder.AnyParam(1) 137 | return a.recorder 138 | } 139 | 140 | func (r *moqGenerateFn_recorder) seq() *moqGenerateFn_recorder { 141 | r.recorder.Moq.Scene.T.Helper() 142 | if !r.recorder.Seq(true, "seq", false) { 143 | return nil 144 | } 145 | return r 146 | } 147 | 148 | func (r *moqGenerateFn_recorder) noSeq() *moqGenerateFn_recorder { 149 | r.recorder.Moq.Scene.T.Helper() 150 | if !r.recorder.Seq(false, "noSeq", false) { 151 | return nil 152 | } 153 | return r 154 | } 155 | 156 | func (r *moqGenerateFn_recorder) returnResults(result1 error) *moqGenerateFn_recorder { 157 | r.recorder.Moq.Scene.T.Helper() 158 | r.recorder.ReturnResults(moqGenerateFn_results{ 159 | result1: result1, 160 | }) 161 | return r 162 | } 163 | 164 | func (r *moqGenerateFn_recorder) andDo(fn moqGenerateFn_doFn) *moqGenerateFn_recorder { 165 | r.recorder.Moq.Scene.T.Helper() 166 | if !r.recorder.AndDo(func(params moqGenerateFn_params) { 167 | fn(params.reqs...) 168 | }, false) { 169 | return nil 170 | } 171 | return r 172 | } 173 | 174 | func (r *moqGenerateFn_recorder) doReturnResults(fn moqGenerateFn_doReturnFn) *moqGenerateFn_recorder { 175 | r.recorder.Moq.Scene.T.Helper() 176 | r.recorder.DoReturnResults(func(params moqGenerateFn_params) *moqGenerateFn_results { 177 | result1 := fn(params.reqs...) 178 | return &moqGenerateFn_results{ 179 | result1: result1, 180 | } 181 | }) 182 | return r 183 | } 184 | 185 | func (r *moqGenerateFn_recorder) repeat(repeaters ...moq.Repeater) *moqGenerateFn_recorder { 186 | r.recorder.Moq.Scene.T.Helper() 187 | if !r.recorder.Repeat(repeaters, false) { 188 | return nil 189 | } 190 | return r 191 | } 192 | 193 | func (*moqGenerateFn_adaptor) PrettyParams(params moqGenerateFn_params) string { 194 | return fmt.Sprintf("GenerateFn(%#v)", params.reqs) 195 | } 196 | 197 | func (a *moqGenerateFn_adaptor) ParamsKey(params moqGenerateFn_params, anyParams uint64) moqGenerateFn_paramsKey { 198 | a.moq.moq.Scene.T.Helper() 199 | reqsUsedHash := impl.HashOnlyParamKey(a.moq.moq.Scene.T, 200 | params.reqs, "reqs", 1, a.moq.runtime.parameterIndexing.reqs, anyParams) 201 | return moqGenerateFn_paramsKey{ 202 | params: struct{}{}, 203 | hashes: struct{ reqs hash.Hash }{ 204 | reqs: reqsUsedHash, 205 | }, 206 | } 207 | } 208 | 209 | // Reset resets the state of the moq 210 | func (m *moqGenerateFn) Reset() { 211 | m.moq.Reset() 212 | } 213 | 214 | // AssertExpectationsMet asserts that all expectations have been met 215 | func (m *moqGenerateFn) AssertExpectationsMet() { 216 | m.moq.Scene.T.Helper() 217 | m.moq.AssertExpectationsMet() 218 | } 219 | -------------------------------------------------------------------------------- /bulk/internal/moq_openfilefn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package internal_test 4 | 5 | import ( 6 | "fmt" 7 | "os" 8 | 9 | "moqueries.org/cli/bulk/internal" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqOpenFileFn holds the state of a moq of the OpenFileFn type 16 | type moqOpenFileFn struct { 17 | moq *impl.Moq[ 18 | *moqOpenFileFn_adaptor, 19 | moqOpenFileFn_params, 20 | moqOpenFileFn_paramsKey, 21 | moqOpenFileFn_results, 22 | ] 23 | 24 | runtime moqOpenFileFn_runtime 25 | } 26 | 27 | // moqOpenFileFn_runtime holds runtime configuration for the OpenFileFn type 28 | type moqOpenFileFn_runtime struct { 29 | parameterIndexing moqOpenFileFn_paramIndexing 30 | } 31 | 32 | // moqOpenFileFn_adaptor adapts moqOpenFileFn as needed by the runtime 33 | type moqOpenFileFn_adaptor struct { 34 | moq *moqOpenFileFn 35 | } 36 | 37 | // moqOpenFileFn_params holds the params of the OpenFileFn type 38 | type moqOpenFileFn_params struct { 39 | name string 40 | flag int 41 | perm os.FileMode 42 | } 43 | 44 | // moqOpenFileFn_paramsKey holds the map key params of the OpenFileFn type 45 | type moqOpenFileFn_paramsKey struct { 46 | params struct { 47 | name string 48 | flag int 49 | perm os.FileMode 50 | } 51 | hashes struct { 52 | name hash.Hash 53 | flag hash.Hash 54 | perm hash.Hash 55 | } 56 | } 57 | 58 | // moqOpenFileFn_results holds the results of the OpenFileFn type 59 | type moqOpenFileFn_results struct { 60 | result1 internal.ReadWriteSeekCloser 61 | result2 error 62 | } 63 | 64 | // moqOpenFileFn_paramIndexing holds the parameter indexing runtime 65 | // configuration for the OpenFileFn type 66 | type moqOpenFileFn_paramIndexing struct { 67 | name moq.ParamIndexing 68 | flag moq.ParamIndexing 69 | perm moq.ParamIndexing 70 | } 71 | 72 | // moqOpenFileFn_doFn defines the type of function needed when calling andDo 73 | // for the OpenFileFn type 74 | type moqOpenFileFn_doFn func(name string, flag int, perm os.FileMode) 75 | 76 | // moqOpenFileFn_doReturnFn defines the type of function needed when calling 77 | // doReturnResults for the OpenFileFn type 78 | type moqOpenFileFn_doReturnFn func(name string, flag int, perm os.FileMode) (internal.ReadWriteSeekCloser, error) 79 | 80 | // moqOpenFileFn_recorder routes recorded function calls to the moqOpenFileFn 81 | // moq 82 | type moqOpenFileFn_recorder struct { 83 | recorder *impl.Recorder[ 84 | *moqOpenFileFn_adaptor, 85 | moqOpenFileFn_params, 86 | moqOpenFileFn_paramsKey, 87 | moqOpenFileFn_results, 88 | ] 89 | } 90 | 91 | // moqOpenFileFn_anyParams isolates the any params functions of the OpenFileFn 92 | // type 93 | type moqOpenFileFn_anyParams struct { 94 | recorder *moqOpenFileFn_recorder 95 | } 96 | 97 | // newMoqOpenFileFn creates a new moq of the OpenFileFn type 98 | func newMoqOpenFileFn(scene *moq.Scene, config *moq.Config) *moqOpenFileFn { 99 | adaptor1 := &moqOpenFileFn_adaptor{} 100 | m := &moqOpenFileFn{ 101 | moq: impl.NewMoq[ 102 | *moqOpenFileFn_adaptor, 103 | moqOpenFileFn_params, 104 | moqOpenFileFn_paramsKey, 105 | moqOpenFileFn_results, 106 | ](scene, adaptor1, config), 107 | 108 | runtime: moqOpenFileFn_runtime{parameterIndexing: moqOpenFileFn_paramIndexing{ 109 | name: moq.ParamIndexByValue, 110 | flag: moq.ParamIndexByValue, 111 | perm: moq.ParamIndexByValue, 112 | }}, 113 | } 114 | adaptor1.moq = m 115 | 116 | scene.AddMoq(m) 117 | return m 118 | } 119 | 120 | // mock returns the moq implementation of the OpenFileFn type 121 | func (m *moqOpenFileFn) mock() internal.OpenFileFn { 122 | return func(name string, flag int, perm os.FileMode) (internal.ReadWriteSeekCloser, error) { 123 | m.moq.Scene.T.Helper() 124 | params := moqOpenFileFn_params{ 125 | name: name, 126 | flag: flag, 127 | perm: perm, 128 | } 129 | 130 | var result1 internal.ReadWriteSeekCloser 131 | var result2 error 132 | if result := m.moq.Function(params); result != nil { 133 | result1 = result.result1 134 | result2 = result.result2 135 | } 136 | return result1, result2 137 | } 138 | } 139 | 140 | func (m *moqOpenFileFn) onCall(name string, flag int, perm os.FileMode) *moqOpenFileFn_recorder { 141 | return &moqOpenFileFn_recorder{ 142 | recorder: m.moq.OnCall(moqOpenFileFn_params{ 143 | name: name, 144 | flag: flag, 145 | perm: perm, 146 | }), 147 | } 148 | } 149 | 150 | func (r *moqOpenFileFn_recorder) any() *moqOpenFileFn_anyParams { 151 | r.recorder.Moq.Scene.T.Helper() 152 | if !r.recorder.IsAnyPermitted(false) { 153 | return nil 154 | } 155 | return &moqOpenFileFn_anyParams{recorder: r} 156 | } 157 | 158 | func (a *moqOpenFileFn_anyParams) name() *moqOpenFileFn_recorder { 159 | a.recorder.recorder.AnyParam(1) 160 | return a.recorder 161 | } 162 | 163 | func (a *moqOpenFileFn_anyParams) flag() *moqOpenFileFn_recorder { 164 | a.recorder.recorder.AnyParam(2) 165 | return a.recorder 166 | } 167 | 168 | func (a *moqOpenFileFn_anyParams) perm() *moqOpenFileFn_recorder { 169 | a.recorder.recorder.AnyParam(3) 170 | return a.recorder 171 | } 172 | 173 | func (r *moqOpenFileFn_recorder) seq() *moqOpenFileFn_recorder { 174 | r.recorder.Moq.Scene.T.Helper() 175 | if !r.recorder.Seq(true, "seq", false) { 176 | return nil 177 | } 178 | return r 179 | } 180 | 181 | func (r *moqOpenFileFn_recorder) noSeq() *moqOpenFileFn_recorder { 182 | r.recorder.Moq.Scene.T.Helper() 183 | if !r.recorder.Seq(false, "noSeq", false) { 184 | return nil 185 | } 186 | return r 187 | } 188 | 189 | func (r *moqOpenFileFn_recorder) returnResults(result1 internal.ReadWriteSeekCloser, result2 error) *moqOpenFileFn_recorder { 190 | r.recorder.Moq.Scene.T.Helper() 191 | r.recorder.ReturnResults(moqOpenFileFn_results{ 192 | result1: result1, 193 | result2: result2, 194 | }) 195 | return r 196 | } 197 | 198 | func (r *moqOpenFileFn_recorder) andDo(fn moqOpenFileFn_doFn) *moqOpenFileFn_recorder { 199 | r.recorder.Moq.Scene.T.Helper() 200 | if !r.recorder.AndDo(func(params moqOpenFileFn_params) { 201 | fn(params.name, params.flag, params.perm) 202 | }, false) { 203 | return nil 204 | } 205 | return r 206 | } 207 | 208 | func (r *moqOpenFileFn_recorder) doReturnResults(fn moqOpenFileFn_doReturnFn) *moqOpenFileFn_recorder { 209 | r.recorder.Moq.Scene.T.Helper() 210 | r.recorder.DoReturnResults(func(params moqOpenFileFn_params) *moqOpenFileFn_results { 211 | result1, result2 := fn(params.name, params.flag, params.perm) 212 | return &moqOpenFileFn_results{ 213 | result1: result1, 214 | result2: result2, 215 | } 216 | }) 217 | return r 218 | } 219 | 220 | func (r *moqOpenFileFn_recorder) repeat(repeaters ...moq.Repeater) *moqOpenFileFn_recorder { 221 | r.recorder.Moq.Scene.T.Helper() 222 | if !r.recorder.Repeat(repeaters, false) { 223 | return nil 224 | } 225 | return r 226 | } 227 | 228 | func (*moqOpenFileFn_adaptor) PrettyParams(params moqOpenFileFn_params) string { 229 | return fmt.Sprintf("OpenFileFn(%#v, %#v, %#v)", params.name, params.flag, params.perm) 230 | } 231 | 232 | func (a *moqOpenFileFn_adaptor) ParamsKey(params moqOpenFileFn_params, anyParams uint64) moqOpenFileFn_paramsKey { 233 | a.moq.moq.Scene.T.Helper() 234 | nameUsed, nameUsedHash := impl.ParamKey( 235 | params.name, 1, a.moq.runtime.parameterIndexing.name, anyParams) 236 | flagUsed, flagUsedHash := impl.ParamKey( 237 | params.flag, 2, a.moq.runtime.parameterIndexing.flag, anyParams) 238 | permUsed, permUsedHash := impl.ParamKey( 239 | params.perm, 3, a.moq.runtime.parameterIndexing.perm, anyParams) 240 | return moqOpenFileFn_paramsKey{ 241 | params: struct { 242 | name string 243 | flag int 244 | perm os.FileMode 245 | }{ 246 | name: nameUsed, 247 | flag: flagUsed, 248 | perm: permUsed, 249 | }, 250 | hashes: struct { 251 | name hash.Hash 252 | flag hash.Hash 253 | perm hash.Hash 254 | }{ 255 | name: nameUsedHash, 256 | flag: flagUsedHash, 257 | perm: permUsedHash, 258 | }, 259 | } 260 | } 261 | 262 | // Reset resets the state of the moq 263 | func (m *moqOpenFileFn) Reset() { 264 | m.moq.Reset() 265 | } 266 | 267 | // AssertExpectationsMet asserts that all expectations have been met 268 | func (m *moqOpenFileFn) AssertExpectationsMet() { 269 | m.moq.Scene.T.Helper() 270 | m.moq.AssertExpectationsMet() 271 | } 272 | -------------------------------------------------------------------------------- /bulk/internal/moq_openfn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package internal_test 4 | 5 | import ( 6 | "fmt" 7 | "io" 8 | 9 | "moqueries.org/cli/bulk/internal" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqOpenFn holds the state of a moq of the OpenFn type 16 | type moqOpenFn struct { 17 | moq *impl.Moq[ 18 | *moqOpenFn_adaptor, 19 | moqOpenFn_params, 20 | moqOpenFn_paramsKey, 21 | moqOpenFn_results, 22 | ] 23 | 24 | runtime moqOpenFn_runtime 25 | } 26 | 27 | // moqOpenFn_runtime holds runtime configuration for the OpenFn type 28 | type moqOpenFn_runtime struct { 29 | parameterIndexing moqOpenFn_paramIndexing 30 | } 31 | 32 | // moqOpenFn_adaptor adapts moqOpenFn as needed by the runtime 33 | type moqOpenFn_adaptor struct { 34 | moq *moqOpenFn 35 | } 36 | 37 | // moqOpenFn_params holds the params of the OpenFn type 38 | type moqOpenFn_params struct{ name string } 39 | 40 | // moqOpenFn_paramsKey holds the map key params of the OpenFn type 41 | type moqOpenFn_paramsKey struct { 42 | params struct{ name string } 43 | hashes struct{ name hash.Hash } 44 | } 45 | 46 | // moqOpenFn_results holds the results of the OpenFn type 47 | type moqOpenFn_results struct { 48 | file io.ReadCloser 49 | err error 50 | } 51 | 52 | // moqOpenFn_paramIndexing holds the parameter indexing runtime configuration 53 | // for the OpenFn type 54 | type moqOpenFn_paramIndexing struct { 55 | name moq.ParamIndexing 56 | } 57 | 58 | // moqOpenFn_doFn defines the type of function needed when calling andDo for 59 | // the OpenFn type 60 | type moqOpenFn_doFn func(name string) 61 | 62 | // moqOpenFn_doReturnFn defines the type of function needed when calling 63 | // doReturnResults for the OpenFn type 64 | type moqOpenFn_doReturnFn func(name string) (file io.ReadCloser, err error) 65 | 66 | // moqOpenFn_recorder routes recorded function calls to the moqOpenFn moq 67 | type moqOpenFn_recorder struct { 68 | recorder *impl.Recorder[ 69 | *moqOpenFn_adaptor, 70 | moqOpenFn_params, 71 | moqOpenFn_paramsKey, 72 | moqOpenFn_results, 73 | ] 74 | } 75 | 76 | // moqOpenFn_anyParams isolates the any params functions of the OpenFn type 77 | type moqOpenFn_anyParams struct { 78 | recorder *moqOpenFn_recorder 79 | } 80 | 81 | // newMoqOpenFn creates a new moq of the OpenFn type 82 | func newMoqOpenFn(scene *moq.Scene, config *moq.Config) *moqOpenFn { 83 | adaptor1 := &moqOpenFn_adaptor{} 84 | m := &moqOpenFn{ 85 | moq: impl.NewMoq[ 86 | *moqOpenFn_adaptor, 87 | moqOpenFn_params, 88 | moqOpenFn_paramsKey, 89 | moqOpenFn_results, 90 | ](scene, adaptor1, config), 91 | 92 | runtime: moqOpenFn_runtime{parameterIndexing: moqOpenFn_paramIndexing{ 93 | name: moq.ParamIndexByValue, 94 | }}, 95 | } 96 | adaptor1.moq = m 97 | 98 | scene.AddMoq(m) 99 | return m 100 | } 101 | 102 | // mock returns the moq implementation of the OpenFn type 103 | func (m *moqOpenFn) mock() internal.OpenFn { 104 | return func(name string) (io.ReadCloser, error) { 105 | m.moq.Scene.T.Helper() 106 | params := moqOpenFn_params{ 107 | name: name, 108 | } 109 | 110 | var result1 io.ReadCloser 111 | var result2 error 112 | if result := m.moq.Function(params); result != nil { 113 | result1 = result.file 114 | result2 = result.err 115 | } 116 | return result1, result2 117 | } 118 | } 119 | 120 | func (m *moqOpenFn) onCall(name string) *moqOpenFn_recorder { 121 | return &moqOpenFn_recorder{ 122 | recorder: m.moq.OnCall(moqOpenFn_params{ 123 | name: name, 124 | }), 125 | } 126 | } 127 | 128 | func (r *moqOpenFn_recorder) any() *moqOpenFn_anyParams { 129 | r.recorder.Moq.Scene.T.Helper() 130 | if !r.recorder.IsAnyPermitted(false) { 131 | return nil 132 | } 133 | return &moqOpenFn_anyParams{recorder: r} 134 | } 135 | 136 | func (a *moqOpenFn_anyParams) name() *moqOpenFn_recorder { 137 | a.recorder.recorder.AnyParam(1) 138 | return a.recorder 139 | } 140 | 141 | func (r *moqOpenFn_recorder) seq() *moqOpenFn_recorder { 142 | r.recorder.Moq.Scene.T.Helper() 143 | if !r.recorder.Seq(true, "seq", false) { 144 | return nil 145 | } 146 | return r 147 | } 148 | 149 | func (r *moqOpenFn_recorder) noSeq() *moqOpenFn_recorder { 150 | r.recorder.Moq.Scene.T.Helper() 151 | if !r.recorder.Seq(false, "noSeq", false) { 152 | return nil 153 | } 154 | return r 155 | } 156 | 157 | func (r *moqOpenFn_recorder) returnResults(file io.ReadCloser, err error) *moqOpenFn_recorder { 158 | r.recorder.Moq.Scene.T.Helper() 159 | r.recorder.ReturnResults(moqOpenFn_results{ 160 | file: file, 161 | err: err, 162 | }) 163 | return r 164 | } 165 | 166 | func (r *moqOpenFn_recorder) andDo(fn moqOpenFn_doFn) *moqOpenFn_recorder { 167 | r.recorder.Moq.Scene.T.Helper() 168 | if !r.recorder.AndDo(func(params moqOpenFn_params) { 169 | fn(params.name) 170 | }, false) { 171 | return nil 172 | } 173 | return r 174 | } 175 | 176 | func (r *moqOpenFn_recorder) doReturnResults(fn moqOpenFn_doReturnFn) *moqOpenFn_recorder { 177 | r.recorder.Moq.Scene.T.Helper() 178 | r.recorder.DoReturnResults(func(params moqOpenFn_params) *moqOpenFn_results { 179 | file, err := fn(params.name) 180 | return &moqOpenFn_results{ 181 | file: file, 182 | err: err, 183 | } 184 | }) 185 | return r 186 | } 187 | 188 | func (r *moqOpenFn_recorder) repeat(repeaters ...moq.Repeater) *moqOpenFn_recorder { 189 | r.recorder.Moq.Scene.T.Helper() 190 | if !r.recorder.Repeat(repeaters, false) { 191 | return nil 192 | } 193 | return r 194 | } 195 | 196 | func (*moqOpenFn_adaptor) PrettyParams(params moqOpenFn_params) string { 197 | return fmt.Sprintf("OpenFn(%#v)", params.name) 198 | } 199 | 200 | func (a *moqOpenFn_adaptor) ParamsKey(params moqOpenFn_params, anyParams uint64) moqOpenFn_paramsKey { 201 | a.moq.moq.Scene.T.Helper() 202 | nameUsed, nameUsedHash := impl.ParamKey( 203 | params.name, 1, a.moq.runtime.parameterIndexing.name, anyParams) 204 | return moqOpenFn_paramsKey{ 205 | params: struct{ name string }{ 206 | name: nameUsed, 207 | }, 208 | hashes: struct{ name hash.Hash }{ 209 | name: nameUsedHash, 210 | }, 211 | } 212 | } 213 | 214 | // Reset resets the state of the moq 215 | func (m *moqOpenFn) Reset() { 216 | m.moq.Reset() 217 | } 218 | 219 | // AssertExpectationsMet asserts that all expectations have been met 220 | func (m *moqOpenFn) AssertExpectationsMet() { 221 | m.moq.Scene.T.Helper() 222 | m.moq.AssertExpectationsMet() 223 | } 224 | -------------------------------------------------------------------------------- /cmd/finalize.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/spf13/cobra" 5 | "moqueries.org/runtime/logs" 6 | 7 | "moqueries.org/cli/bulk" 8 | ) 9 | 10 | var finalizeCmd = &cobra.Command{ 11 | Use: "bulk-finalize", 12 | Short: "Finalize bulk processing and generate mocks", 13 | Args: cobra.NoArgs, 14 | Run: finalize, 15 | } 16 | 17 | func finalize(cmd *cobra.Command, _ []string) { 18 | rootInfo := rootSetup(cmd) 19 | 20 | if rootInfo.stateFile == "" { 21 | logs.Panic(stateFileEnvVar+" environment variable is required"+ 22 | " when finalizing bulk processing", nil) 23 | } 24 | 25 | logs.Debugf("Moqueries finalize invoked") 26 | 27 | err := bulk.Finalize(rootInfo.stateFile, rootInfo.workingDir) 28 | if err != nil { 29 | logs.Panicf("Error finalizing bulk processing for %s: %#v", 30 | rootInfo.stateFile, err) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cmd/generate.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/spf13/cobra" 5 | "moqueries.org/runtime/logs" 6 | 7 | "moqueries.org/cli/bulk" 8 | "moqueries.org/cli/generator" 9 | ) 10 | 11 | const ( 12 | exportFlag = "export" 13 | 14 | destinationFlag = "destination" 15 | destinationDirFlag = "destination-dir" 16 | 17 | packageFlag = "package" 18 | importFlag = "import" 19 | testImportFlag = "test-import" 20 | ) 21 | 22 | func addGenerateFlags() { 23 | rootCmd.Flags().Bool(exportFlag, false, 24 | "If true, generated mocks will be exported and accessible from other packages") 25 | 26 | rootCmd.Flags().String(destinationFlag, "", 27 | "The file path where mocks are generated. Relative paths are "+ 28 | "allowed (relative to the directory containing the generate "+ 29 | "directive or relative to the current directory) (defaults to "+ 30 | "./moq_.go when exported or ./moq__test.go when not "+ 31 | "exported)") 32 | addDestinationDirFlag(rootCmd) 33 | 34 | rootCmd.Flags().String(packageFlag, "", 35 | "The package to generate code into (defaults to the test package of "+ 36 | "the destination directory when --export=false or the package of "+ 37 | "the destination directory when --export=true)") 38 | rootCmd.Flags().String(importFlag, ".", 39 | "The package containing the type (interface or function type) to be "+ 40 | "mocked (defaults to the directory containing generate directive)") 41 | rootCmd.Flags().Bool(testImportFlag, false, 42 | "Look for the types to be mocked in the test package") 43 | } 44 | 45 | func addDestinationDirFlag(cmd *cobra.Command) { 46 | cmd.Flags().String(destinationDirFlag, "", 47 | "The file directory where mocks are generated relative. Relative "+ 48 | "paths are allowed (relative to the directory containing the "+ 49 | "generate directive (or relative to the current directory) "+ 50 | "(defaults to .)") 51 | } 52 | 53 | // generate gathers details on the environment and calls the generator 54 | func generate(cmd *cobra.Command, typs []string) { 55 | root := rootSetup(cmd) 56 | 57 | export, err := cmd.Flags().GetBool(exportFlag) 58 | if err != nil { 59 | logs.Panic("Error getting export flag", err) 60 | } 61 | dest, err := cmd.Flags().GetString(destinationFlag) 62 | if err != nil { 63 | logs.Panic("Error getting destination flag", err) 64 | } 65 | destDir, err := cmd.Flags().GetString(destinationDirFlag) 66 | if err != nil { 67 | logs.Panic("Error getting destination dir flag", err) 68 | } 69 | pkg, err := cmd.Flags().GetString(packageFlag) 70 | if err != nil { 71 | logs.Panic("Error getting package flag", err) 72 | } 73 | imp, err := cmd.Flags().GetString(importFlag) 74 | if err != nil { 75 | logs.Panic("Error getting import flag", err) 76 | } 77 | testImp, err := cmd.Flags().GetBool(testImportFlag) 78 | if err != nil { 79 | logs.Panic("Error getting test-import flag", err) 80 | } 81 | 82 | logs.Debugf("Moqueries generate invoked,"+ 83 | " export: %t,"+ 84 | " destination: %s,"+ 85 | " destination-dir: %s,"+ 86 | " package: %s,"+ 87 | " import: %s,"+ 88 | " types: %s", 89 | export, dest, destDir, pkg, imp, typs) 90 | 91 | req := generator.GenerateRequest{ 92 | Types: typs, 93 | Export: export, 94 | Destination: dest, 95 | DestinationDir: destDir, 96 | Package: pkg, 97 | Import: imp, 98 | TestImport: testImp, 99 | WorkingDir: root.workingDir, 100 | } 101 | if root.stateFile == "" { 102 | err = generator.Generate(req) 103 | if err != nil { 104 | logs.Panicf("Error generating mock for %s in %s: %#v", typs, imp, err) 105 | } 106 | } else { 107 | err = bulk.Append(root.stateFile, req) 108 | if err != nil { 109 | logs.Panicf("Error appending mock request for %s in %s: %#v", typs, imp, err) 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /cmd/initialize.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/spf13/cobra" 5 | "moqueries.org/runtime/logs" 6 | 7 | "moqueries.org/cli/bulk" 8 | ) 9 | 10 | var initializeCmd = &cobra.Command{ 11 | Use: "bulk-initialize", 12 | Short: "Initialize state for bulk processing", 13 | Args: cobra.NoArgs, 14 | Run: initialize, 15 | } 16 | 17 | func initialize(cmd *cobra.Command, _ []string) { 18 | rootInfo := rootSetup(cmd) 19 | 20 | if rootInfo.stateFile == "" { 21 | logs.Panic(stateFileEnvVar+" environment variable is required"+ 22 | " when initializing bulk processing", nil) 23 | } 24 | 25 | logs.Debugf("Moqueries initialize invoked") 26 | 27 | err := bulk.Initialize(rootInfo.stateFile, rootInfo.workingDir) 28 | if err != nil { 29 | logs.Panicf("Error initializing state for bulk processing for %s: %#v", 30 | rootInfo.stateFile, err) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cmd/package.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/spf13/cobra" 5 | "moqueries.org/runtime/logs" 6 | 7 | "moqueries.org/cli/pkg" 8 | ) 9 | 10 | const ( 11 | skipPkgDirsFlag = "skip-pkg-dirs" 12 | excludePkgPathRegexFlag = "exclude-pkg-path-regex" 13 | ) 14 | 15 | func packageCmd() *cobra.Command { 16 | cmd := &cobra.Command{ 17 | Use: "package [packages]", 18 | Short: "Generate mocks for a package", 19 | Run: pkgGen, 20 | } 21 | 22 | addDestinationDirFlag(cmd) 23 | 24 | cmd.PersistentFlags().Int(skipPkgDirsFlag, 0, 25 | "Skips specified number of directories in the package path when "+ 26 | "determining the destination directory (defaults to 0)") 27 | cmd.PersistentFlags().String(excludePkgPathRegexFlag, "", 28 | "Specifies a regular expression used to exclude package paths") 29 | 30 | return cmd 31 | } 32 | 33 | func pkgGen(cmd *cobra.Command, pkgPatterns []string) { 34 | destDir, err := cmd.Flags().GetString(destinationDirFlag) 35 | if err != nil { 36 | logs.Panic("Error getting destination dir flag", err) 37 | } 38 | skipPkgDirs, err := cmd.Flags().GetInt(skipPkgDirsFlag) 39 | if err != nil { 40 | logs.Panic("Error getting the skip package dirs flag", err) 41 | } 42 | excludePkgPathRegex, err := cmd.Flags().GetString(excludePkgPathRegexFlag) 43 | if err != nil { 44 | logs.Panic("Error getting the skip package dirs flag", err) 45 | } 46 | 47 | err = pkg.Generate(pkg.PackageGenerateRequest{ 48 | DestinationDir: destDir, 49 | SkipPkgDirs: skipPkgDirs, 50 | PkgPatterns: pkgPatterns, 51 | ExcludePkgPathRegex: excludePkgPathRegex, 52 | }) 53 | if err != nil { 54 | logs.Panicf("Error generating mocks for %s packages: %#v", pkgPatterns, err) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /cmd/root.go: -------------------------------------------------------------------------------- 1 | // Package cmd centralizes the command line interface 2 | package cmd 3 | 4 | import ( 5 | "os" 6 | "strconv" 7 | 8 | "github.com/spf13/cobra" 9 | "moqueries.org/runtime/logs" 10 | ) 11 | 12 | const ( 13 | debugFlag = "debug" 14 | 15 | stateFileEnvVar = "MOQ_BULK_STATE_FILE" 16 | debugEnvVar = "MOQ_DEBUG" 17 | ) 18 | 19 | var rootCmd = &cobra.Command{ 20 | Use: "moqueries [interfaces and/or function types to mock]", 21 | Short: "Moqueries generates lock-free mocks", 22 | Args: cobra.MinimumNArgs(1), 23 | Run: generate, 24 | } 25 | 26 | func init() { 27 | cobra.OnInitialize() 28 | 29 | rootCmd.PersistentFlags().Bool(debugFlag, false, 30 | "If true, debugging output will be logged") 31 | 32 | addGenerateFlags() 33 | 34 | rootCmd.AddCommand(summarizeMetricsCmd) 35 | rootCmd.AddCommand(initializeCmd) 36 | rootCmd.AddCommand(finalizeCmd) 37 | rootCmd.AddCommand(packageCmd()) 38 | } 39 | 40 | // Execute generates one or more mocks 41 | func Execute() { 42 | if err := rootCmd.Execute(); err != nil { 43 | logs.Panic("Error executing command", err) 44 | } 45 | } 46 | 47 | type rootInfo struct { 48 | workingDir string 49 | stateFile string 50 | } 51 | 52 | // rootSetup is called by all subcommands for general setup 53 | func rootSetup(cmd *cobra.Command) rootInfo { 54 | debug, err := cmd.Root().PersistentFlags().GetBool(debugFlag) 55 | if err != nil { 56 | logs.Panic("Error getting debug flag", err) 57 | } 58 | 59 | debugStr, ok := os.LookupEnv(debugEnvVar) 60 | if ok { 61 | envVar, err := strconv.ParseBool(debugStr) 62 | if err != nil { 63 | logs.Panic("Error parsing "+debugEnvVar+" environment variable", err) 64 | } 65 | debug = debug || envVar 66 | } 67 | 68 | logs.Init(debug) 69 | 70 | workingDir, err := os.Getwd() 71 | if err != nil { 72 | logs.Panic("Could not get working directory", err) 73 | } 74 | 75 | stateFile := os.Getenv(stateFileEnvVar) 76 | 77 | logs.Debugf("Moqueries root info,"+ 78 | " bulk processing state file: %s,"+ 79 | " working directory: %s", 80 | stateFile, workingDir) 81 | 82 | return rootInfo{ 83 | workingDir: workingDir, 84 | stateFile: stateFile, 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /cmd/summarizemetrics.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/spf13/cobra" 7 | "moqueries.org/runtime/logs" 8 | 9 | "moqueries.org/cli/metrics" 10 | ) 11 | 12 | var summarizeMetricsCmd = &cobra.Command{ 13 | Use: "summarize-metrics", 14 | Short: "Summarize metrics logging from multiple runs", 15 | Args: cobra.MaximumNArgs(1), 16 | Run: summarizeMetrics, 17 | } 18 | 19 | func summarizeMetrics(cmd *cobra.Command, files []string) { 20 | rootSetup(cmd) 21 | 22 | if len(files) > 1 { 23 | logs.Panicf("Expected one file argument or none (reads stdin)") 24 | } 25 | 26 | var f *os.File 27 | var err error 28 | if len(files) == 0 || files[0] == "-" { 29 | f = os.Stdin 30 | } else { 31 | f, err = os.Open(files[0]) 32 | if err != nil { 33 | logs.Panicf("Error opening file %s: %#v", files[0], err) 34 | } 35 | defer func() { 36 | err := f.Close() 37 | if err != nil { 38 | logs.Error("error closing log file", err) 39 | } 40 | }() 41 | } 42 | 43 | err = metrics.Summarize(f, logs.Infof) 44 | if err != nil { 45 | logs.Panicf("Error summarizing file %s: %#v", files[0], err) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | status: 3 | project: 4 | default: 5 | target: 90% 6 | threshold: 1% 7 | 8 | ignore: 9 | # ignore generated moqs 10 | - "**/moq_*.go" 11 | 12 | comment: 13 | layout: "reach, diff, files" 14 | behavior: new 15 | require_changes: false 16 | -------------------------------------------------------------------------------- /demo/demo.go: -------------------------------------------------------------------------------- 1 | // Package demo provides code examples used in the ../README.md 2 | package demo 3 | 4 | import ( 5 | "fmt" 6 | "io" 7 | "math" 8 | "strconv" 9 | ) 10 | 11 | //go:generate moqueries --import io Writer 12 | 13 | //go:generate moqueries IsFavorite 14 | 15 | // IsFavorite is the function type for a function that determines if a number 16 | // is a favorite 17 | type IsFavorite func(n int) bool 18 | 19 | // FavWriter is used to filter and write only favorite number 20 | type FavWriter struct { 21 | IsFav IsFavorite 22 | W io.Writer 23 | Store Store 24 | } 25 | 26 | // WriteFavorites iterates through a given list of numbers and writes any 27 | // favorite number to the given Writer 28 | func (d *FavWriter) WriteFavorites(nums []int) error { 29 | for _, num := range nums { 30 | if d.IsFav(num) { 31 | _, err := d.W.Write([]byte(strconv.Itoa(num))) 32 | if err != nil { 33 | return fmt.Errorf("that pesky writer says that it '%w'", 34 | err) 35 | } 36 | } 37 | } 38 | return nil 39 | } 40 | 41 | // Widget holds the state of a widget 42 | type Widget struct { 43 | Id int 44 | GadgetsByColor map[string]Gadget 45 | } 46 | 47 | // Gadget holds the state of a gadget 48 | type Gadget struct { 49 | Id int 50 | WidgetId int 51 | Color string 52 | Weight uint32 53 | } 54 | 55 | //go:generate moqueries Store 56 | 57 | // Store is the abstraction for Widget and Gadget types 58 | type Store interface { 59 | AllWidgetsIds() ([]int, error) 60 | GadgetsByWidgetId(widgetId int) ([]Gadget, error) 61 | LightGadgetsByWidgetId(widgetId int, maxWeight uint32) ([]Gadget, error) 62 | } 63 | 64 | // Load retrieves multiple Widget objects 65 | func (d *FavWriter) Load(maxWeight uint32) (map[int]Widget, error) { 66 | wIds, err := d.Store.AllWidgetsIds() 67 | if err != nil { 68 | return nil, fmt.Errorf("failed to load widget ids: %w", err) 69 | } 70 | 71 | widgets := make(map[int]Widget) 72 | for _, wId := range wIds { 73 | w, ok := widgets[wId] 74 | if !ok { 75 | w = Widget{Id: wId, GadgetsByColor: make(map[string]Gadget)} 76 | widgets[wId] = w 77 | } 78 | 79 | var gadgets []Gadget 80 | if maxWeight == math.MaxUint32 { 81 | gadgets, err = d.Store.GadgetsByWidgetId(wId) 82 | } else { 83 | gadgets, err = d.Store.LightGadgetsByWidgetId(wId, maxWeight) 84 | } 85 | if err != nil { 86 | return nil, fmt.Errorf("failed to load gadgets for widget %d ids: %w", wId, err) 87 | } 88 | 89 | for _, g := range gadgets { 90 | w.GadgetsByColor[g.Color] = g 91 | } 92 | } 93 | 94 | return widgets, nil 95 | } 96 | -------------------------------------------------------------------------------- /demo/moq_isfavorite_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package demo_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/demo" 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // moqIsFavorite holds the state of a moq of the IsFavorite type 15 | type moqIsFavorite struct { 16 | moq *impl.Moq[ 17 | *moqIsFavorite_adaptor, 18 | moqIsFavorite_params, 19 | moqIsFavorite_paramsKey, 20 | moqIsFavorite_results, 21 | ] 22 | 23 | runtime moqIsFavorite_runtime 24 | } 25 | 26 | // moqIsFavorite_runtime holds runtime configuration for the IsFavorite type 27 | type moqIsFavorite_runtime struct { 28 | parameterIndexing moqIsFavorite_paramIndexing 29 | } 30 | 31 | // moqIsFavorite_adaptor adapts moqIsFavorite as needed by the runtime 32 | type moqIsFavorite_adaptor struct { 33 | moq *moqIsFavorite 34 | } 35 | 36 | // moqIsFavorite_params holds the params of the IsFavorite type 37 | type moqIsFavorite_params struct{ n int } 38 | 39 | // moqIsFavorite_paramsKey holds the map key params of the IsFavorite type 40 | type moqIsFavorite_paramsKey struct { 41 | params struct{ n int } 42 | hashes struct{ n hash.Hash } 43 | } 44 | 45 | // moqIsFavorite_results holds the results of the IsFavorite type 46 | type moqIsFavorite_results struct { 47 | result1 bool 48 | } 49 | 50 | // moqIsFavorite_paramIndexing holds the parameter indexing runtime 51 | // configuration for the IsFavorite type 52 | type moqIsFavorite_paramIndexing struct { 53 | n moq.ParamIndexing 54 | } 55 | 56 | // moqIsFavorite_doFn defines the type of function needed when calling andDo 57 | // for the IsFavorite type 58 | type moqIsFavorite_doFn func(n int) 59 | 60 | // moqIsFavorite_doReturnFn defines the type of function needed when calling 61 | // doReturnResults for the IsFavorite type 62 | type moqIsFavorite_doReturnFn func(n int) bool 63 | 64 | // moqIsFavorite_recorder routes recorded function calls to the moqIsFavorite 65 | // moq 66 | type moqIsFavorite_recorder struct { 67 | recorder *impl.Recorder[ 68 | *moqIsFavorite_adaptor, 69 | moqIsFavorite_params, 70 | moqIsFavorite_paramsKey, 71 | moqIsFavorite_results, 72 | ] 73 | } 74 | 75 | // moqIsFavorite_anyParams isolates the any params functions of the IsFavorite 76 | // type 77 | type moqIsFavorite_anyParams struct { 78 | recorder *moqIsFavorite_recorder 79 | } 80 | 81 | // newMoqIsFavorite creates a new moq of the IsFavorite type 82 | func newMoqIsFavorite(scene *moq.Scene, config *moq.Config) *moqIsFavorite { 83 | adaptor1 := &moqIsFavorite_adaptor{} 84 | m := &moqIsFavorite{ 85 | moq: impl.NewMoq[ 86 | *moqIsFavorite_adaptor, 87 | moqIsFavorite_params, 88 | moqIsFavorite_paramsKey, 89 | moqIsFavorite_results, 90 | ](scene, adaptor1, config), 91 | 92 | runtime: moqIsFavorite_runtime{parameterIndexing: moqIsFavorite_paramIndexing{ 93 | n: moq.ParamIndexByValue, 94 | }}, 95 | } 96 | adaptor1.moq = m 97 | 98 | scene.AddMoq(m) 99 | return m 100 | } 101 | 102 | // mock returns the moq implementation of the IsFavorite type 103 | func (m *moqIsFavorite) mock() demo.IsFavorite { 104 | return func(n int) bool { 105 | m.moq.Scene.T.Helper() 106 | params := moqIsFavorite_params{ 107 | n: n, 108 | } 109 | 110 | var result1 bool 111 | if result := m.moq.Function(params); result != nil { 112 | result1 = result.result1 113 | } 114 | return result1 115 | } 116 | } 117 | 118 | func (m *moqIsFavorite) onCall(n int) *moqIsFavorite_recorder { 119 | return &moqIsFavorite_recorder{ 120 | recorder: m.moq.OnCall(moqIsFavorite_params{ 121 | n: n, 122 | }), 123 | } 124 | } 125 | 126 | func (r *moqIsFavorite_recorder) any() *moqIsFavorite_anyParams { 127 | r.recorder.Moq.Scene.T.Helper() 128 | if !r.recorder.IsAnyPermitted(false) { 129 | return nil 130 | } 131 | return &moqIsFavorite_anyParams{recorder: r} 132 | } 133 | 134 | func (a *moqIsFavorite_anyParams) n() *moqIsFavorite_recorder { 135 | a.recorder.recorder.AnyParam(1) 136 | return a.recorder 137 | } 138 | 139 | func (r *moqIsFavorite_recorder) seq() *moqIsFavorite_recorder { 140 | r.recorder.Moq.Scene.T.Helper() 141 | if !r.recorder.Seq(true, "seq", false) { 142 | return nil 143 | } 144 | return r 145 | } 146 | 147 | func (r *moqIsFavorite_recorder) noSeq() *moqIsFavorite_recorder { 148 | r.recorder.Moq.Scene.T.Helper() 149 | if !r.recorder.Seq(false, "noSeq", false) { 150 | return nil 151 | } 152 | return r 153 | } 154 | 155 | func (r *moqIsFavorite_recorder) returnResults(result1 bool) *moqIsFavorite_recorder { 156 | r.recorder.Moq.Scene.T.Helper() 157 | r.recorder.ReturnResults(moqIsFavorite_results{ 158 | result1: result1, 159 | }) 160 | return r 161 | } 162 | 163 | func (r *moqIsFavorite_recorder) andDo(fn moqIsFavorite_doFn) *moqIsFavorite_recorder { 164 | r.recorder.Moq.Scene.T.Helper() 165 | if !r.recorder.AndDo(func(params moqIsFavorite_params) { 166 | fn(params.n) 167 | }, false) { 168 | return nil 169 | } 170 | return r 171 | } 172 | 173 | func (r *moqIsFavorite_recorder) doReturnResults(fn moqIsFavorite_doReturnFn) *moqIsFavorite_recorder { 174 | r.recorder.Moq.Scene.T.Helper() 175 | r.recorder.DoReturnResults(func(params moqIsFavorite_params) *moqIsFavorite_results { 176 | result1 := fn(params.n) 177 | return &moqIsFavorite_results{ 178 | result1: result1, 179 | } 180 | }) 181 | return r 182 | } 183 | 184 | func (r *moqIsFavorite_recorder) repeat(repeaters ...moq.Repeater) *moqIsFavorite_recorder { 185 | r.recorder.Moq.Scene.T.Helper() 186 | if !r.recorder.Repeat(repeaters, false) { 187 | return nil 188 | } 189 | return r 190 | } 191 | 192 | func (*moqIsFavorite_adaptor) PrettyParams(params moqIsFavorite_params) string { 193 | return fmt.Sprintf("IsFavorite(%#v)", params.n) 194 | } 195 | 196 | func (a *moqIsFavorite_adaptor) ParamsKey(params moqIsFavorite_params, anyParams uint64) moqIsFavorite_paramsKey { 197 | a.moq.moq.Scene.T.Helper() 198 | nUsed, nUsedHash := impl.ParamKey( 199 | params.n, 1, a.moq.runtime.parameterIndexing.n, anyParams) 200 | return moqIsFavorite_paramsKey{ 201 | params: struct{ n int }{ 202 | n: nUsed, 203 | }, 204 | hashes: struct{ n hash.Hash }{ 205 | n: nUsedHash, 206 | }, 207 | } 208 | } 209 | 210 | // Reset resets the state of the moq 211 | func (m *moqIsFavorite) Reset() { 212 | m.moq.Reset() 213 | } 214 | 215 | // AssertExpectationsMet asserts that all expectations have been met 216 | func (m *moqIsFavorite) AssertExpectationsMet() { 217 | m.moq.Scene.T.Helper() 218 | m.moq.AssertExpectationsMet() 219 | } 220 | -------------------------------------------------------------------------------- /demo/moq_writer_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package demo_test 4 | 5 | import ( 6 | "fmt" 7 | "io" 8 | 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // The following type assertion assures that io.Writer is mocked completely 15 | var _ io.Writer = (*moqWriter_mock)(nil) 16 | 17 | // moqWriter holds the state of a moq of the Writer type 18 | type moqWriter struct { 19 | moq *moqWriter_mock 20 | 21 | moq_Write *impl.Moq[ 22 | *moqWriter_Write_adaptor, 23 | moqWriter_Write_params, 24 | moqWriter_Write_paramsKey, 25 | moqWriter_Write_results, 26 | ] 27 | 28 | runtime moqWriter_runtime 29 | } 30 | 31 | // moqWriter_mock isolates the mock interface of the Writer type 32 | type moqWriter_mock struct { 33 | moq *moqWriter 34 | } 35 | 36 | // moqWriter_recorder isolates the recorder interface of the Writer type 37 | type moqWriter_recorder struct { 38 | moq *moqWriter 39 | } 40 | 41 | // moqWriter_runtime holds runtime configuration for the Writer type 42 | type moqWriter_runtime struct { 43 | parameterIndexing struct { 44 | Write moqWriter_Write_paramIndexing 45 | } 46 | } 47 | 48 | // moqWriter_Write_adaptor adapts moqWriter as needed by the runtime 49 | type moqWriter_Write_adaptor struct { 50 | moq *moqWriter 51 | } 52 | 53 | // moqWriter_Write_params holds the params of the Writer type 54 | type moqWriter_Write_params struct{ p []byte } 55 | 56 | // moqWriter_Write_paramsKey holds the map key params of the Writer type 57 | type moqWriter_Write_paramsKey struct { 58 | params struct{} 59 | hashes struct{ p hash.Hash } 60 | } 61 | 62 | // moqWriter_Write_results holds the results of the Writer type 63 | type moqWriter_Write_results struct { 64 | n int 65 | err error 66 | } 67 | 68 | // moqWriter_Write_paramIndexing holds the parameter indexing runtime 69 | // configuration for the Writer type 70 | type moqWriter_Write_paramIndexing struct { 71 | p moq.ParamIndexing 72 | } 73 | 74 | // moqWriter_Write_doFn defines the type of function needed when calling andDo 75 | // for the Writer type 76 | type moqWriter_Write_doFn func(p []byte) 77 | 78 | // moqWriter_Write_doReturnFn defines the type of function needed when calling 79 | // doReturnResults for the Writer type 80 | type moqWriter_Write_doReturnFn func(p []byte) (n int, err error) 81 | 82 | // moqWriter_Write_recorder routes recorded function calls to the moqWriter moq 83 | type moqWriter_Write_recorder struct { 84 | recorder *impl.Recorder[ 85 | *moqWriter_Write_adaptor, 86 | moqWriter_Write_params, 87 | moqWriter_Write_paramsKey, 88 | moqWriter_Write_results, 89 | ] 90 | } 91 | 92 | // moqWriter_Write_anyParams isolates the any params functions of the Writer 93 | // type 94 | type moqWriter_Write_anyParams struct { 95 | recorder *moqWriter_Write_recorder 96 | } 97 | 98 | // newMoqWriter creates a new moq of the Writer type 99 | func newMoqWriter(scene *moq.Scene, config *moq.Config) *moqWriter { 100 | adaptor1 := &moqWriter_Write_adaptor{} 101 | m := &moqWriter{ 102 | moq: &moqWriter_mock{}, 103 | 104 | moq_Write: impl.NewMoq[ 105 | *moqWriter_Write_adaptor, 106 | moqWriter_Write_params, 107 | moqWriter_Write_paramsKey, 108 | moqWriter_Write_results, 109 | ](scene, adaptor1, config), 110 | 111 | runtime: moqWriter_runtime{parameterIndexing: struct { 112 | Write moqWriter_Write_paramIndexing 113 | }{ 114 | Write: moqWriter_Write_paramIndexing{ 115 | p: moq.ParamIndexByHash, 116 | }, 117 | }}, 118 | } 119 | m.moq.moq = m 120 | 121 | adaptor1.moq = m 122 | 123 | scene.AddMoq(m) 124 | return m 125 | } 126 | 127 | // mock returns the mock implementation of the Writer type 128 | func (m *moqWriter) mock() *moqWriter_mock { return m.moq } 129 | 130 | func (m *moqWriter_mock) Write(p []byte) (int, error) { 131 | m.moq.moq_Write.Scene.T.Helper() 132 | params := moqWriter_Write_params{ 133 | p: p, 134 | } 135 | 136 | var result1 int 137 | var result2 error 138 | if result := m.moq.moq_Write.Function(params); result != nil { 139 | result1 = result.n 140 | result2 = result.err 141 | } 142 | return result1, result2 143 | } 144 | 145 | // onCall returns the recorder implementation of the Writer type 146 | func (m *moqWriter) onCall() *moqWriter_recorder { 147 | return &moqWriter_recorder{ 148 | moq: m, 149 | } 150 | } 151 | 152 | func (m *moqWriter_recorder) Write(p []byte) *moqWriter_Write_recorder { 153 | return &moqWriter_Write_recorder{ 154 | recorder: m.moq.moq_Write.OnCall(moqWriter_Write_params{ 155 | p: p, 156 | }), 157 | } 158 | } 159 | 160 | func (r *moqWriter_Write_recorder) any() *moqWriter_Write_anyParams { 161 | r.recorder.Moq.Scene.T.Helper() 162 | if !r.recorder.IsAnyPermitted(false) { 163 | return nil 164 | } 165 | return &moqWriter_Write_anyParams{recorder: r} 166 | } 167 | 168 | func (a *moqWriter_Write_anyParams) p() *moqWriter_Write_recorder { 169 | a.recorder.recorder.AnyParam(1) 170 | return a.recorder 171 | } 172 | 173 | func (r *moqWriter_Write_recorder) seq() *moqWriter_Write_recorder { 174 | r.recorder.Moq.Scene.T.Helper() 175 | if !r.recorder.Seq(true, "seq", false) { 176 | return nil 177 | } 178 | return r 179 | } 180 | 181 | func (r *moqWriter_Write_recorder) noSeq() *moqWriter_Write_recorder { 182 | r.recorder.Moq.Scene.T.Helper() 183 | if !r.recorder.Seq(false, "noSeq", false) { 184 | return nil 185 | } 186 | return r 187 | } 188 | 189 | func (r *moqWriter_Write_recorder) returnResults(n int, err error) *moqWriter_Write_recorder { 190 | r.recorder.Moq.Scene.T.Helper() 191 | r.recorder.ReturnResults(moqWriter_Write_results{ 192 | n: n, 193 | err: err, 194 | }) 195 | return r 196 | } 197 | 198 | func (r *moqWriter_Write_recorder) andDo(fn moqWriter_Write_doFn) *moqWriter_Write_recorder { 199 | r.recorder.Moq.Scene.T.Helper() 200 | if !r.recorder.AndDo(func(params moqWriter_Write_params) { 201 | fn(params.p) 202 | }, false) { 203 | return nil 204 | } 205 | return r 206 | } 207 | 208 | func (r *moqWriter_Write_recorder) doReturnResults(fn moqWriter_Write_doReturnFn) *moqWriter_Write_recorder { 209 | r.recorder.Moq.Scene.T.Helper() 210 | r.recorder.DoReturnResults(func(params moqWriter_Write_params) *moqWriter_Write_results { 211 | n, err := fn(params.p) 212 | return &moqWriter_Write_results{ 213 | n: n, 214 | err: err, 215 | } 216 | }) 217 | return r 218 | } 219 | 220 | func (r *moqWriter_Write_recorder) repeat(repeaters ...moq.Repeater) *moqWriter_Write_recorder { 221 | r.recorder.Moq.Scene.T.Helper() 222 | if !r.recorder.Repeat(repeaters, false) { 223 | return nil 224 | } 225 | return r 226 | } 227 | 228 | func (*moqWriter_Write_adaptor) PrettyParams(params moqWriter_Write_params) string { 229 | return fmt.Sprintf("Write(%#v)", params.p) 230 | } 231 | 232 | func (a *moqWriter_Write_adaptor) ParamsKey(params moqWriter_Write_params, anyParams uint64) moqWriter_Write_paramsKey { 233 | a.moq.moq_Write.Scene.T.Helper() 234 | pUsedHash := impl.HashOnlyParamKey(a.moq.moq_Write.Scene.T, 235 | params.p, "p", 1, a.moq.runtime.parameterIndexing.Write.p, anyParams) 236 | return moqWriter_Write_paramsKey{ 237 | params: struct{}{}, 238 | hashes: struct{ p hash.Hash }{ 239 | p: pUsedHash, 240 | }, 241 | } 242 | } 243 | 244 | // Reset resets the state of the moq 245 | func (m *moqWriter) Reset() { 246 | m.moq_Write.Reset() 247 | } 248 | 249 | // AssertExpectationsMet asserts that all expectations have been met 250 | func (m *moqWriter) AssertExpectationsMet() { 251 | m.moq_Write.Scene.T.Helper() 252 | m.moq_Write.AssertExpectationsMet() 253 | } 254 | -------------------------------------------------------------------------------- /generator/generator.go: -------------------------------------------------------------------------------- 1 | // Package generator generates Moqueries mocks 2 | package generator 3 | 4 | import ( 5 | "errors" 6 | "fmt" 7 | "os" 8 | "path/filepath" 9 | "time" 10 | 11 | "github.com/dave/dst" 12 | "github.com/dave/dst/decorator" 13 | "github.com/dave/dst/decorator/resolver/gopackages" 14 | "golang.org/x/tools/go/packages" 15 | "moqueries.org/runtime/logs" 16 | 17 | "moqueries.org/cli/ast" 18 | "moqueries.org/cli/metrics" 19 | ) 20 | 21 | // GenerateRequest contains all the parameters needed to call Generate 22 | type GenerateRequest struct { 23 | Types []string `json:"types"` 24 | Export bool `json:"export"` 25 | Destination string `json:"destination"` 26 | DestinationDir string `json:"destination-dir"` 27 | Package string `json:"package"` 28 | Import string `json:"import"` 29 | TestImport bool `json:"test-import"` 30 | // WorkingDir is the current working directory. Optional, in which case 31 | // os.Getwd is used. Useful in cases where a request is serialized then 32 | // rerun in bulk processing from a different working directory. WorkingDir 33 | // is used for relative-path imports and relative path destination 34 | // files/directories. 35 | WorkingDir string `json:"working-dir"` 36 | // ExcludeNonExported causes the generator to exclude non-exported types 37 | // from the generated mock. This includes possibly returning ErrNonExported 38 | // if after exclusion, nothing is left to be written as a mock. 39 | ExcludeNonExported bool `json:"exclude-non-exported"` 40 | } 41 | 42 | // ErrNonExported is returned by Generate when ExcludeNonExported is set to 43 | // true resulting in an empty mock. 44 | var ErrNonExported = errors.New("non-exported types") 45 | 46 | // Generate generates a moq 47 | func Generate(reqs ...GenerateRequest) error { 48 | m := metrics.NewMetrics(logs.IsDebug, logs.Debugf) 49 | cache := ast.NewCache(packages.Load, os.Stat, os.ReadFile, m) 50 | start := time.Now() 51 | for _, req := range reqs { 52 | err := GenerateWithTypeCache(cache, req) 53 | if err != nil { 54 | return err 55 | } 56 | } 57 | m.TotalProcessingTimeInc(time.Since(start)) 58 | m.Finalize() 59 | return nil 60 | } 61 | 62 | //go:generate moqueries TypeCache 63 | 64 | // TypeCache defines the interface to the Cache type 65 | type TypeCache interface { 66 | Type(id dst.Ident, contextPkg string, testImport bool) (ast.TypeInfo, error) 67 | IsComparable(expr dst.Expr, parentType ast.TypeInfo) (bool, error) 68 | IsDefaultComparable(expr dst.Expr, parentType ast.TypeInfo) (bool, error) 69 | FindPackage(dir string) (string, error) 70 | } 71 | 72 | // GenerateWithTypeCache generates a single moq using the provided type cache. 73 | // This function is exposed for use in bulk operations that have already loaded 74 | // a type. 75 | func GenerateWithTypeCache(cache TypeCache, req GenerateRequest) error { 76 | newConverterFn := func(typ Type, export bool) Converterer { 77 | return NewConverter(typ, export, cache) 78 | } 79 | gen := New(cache, os.Getwd, newConverterFn) 80 | 81 | resp, err := gen.Generate(req) 82 | if err != nil { 83 | return err 84 | } 85 | 86 | destDir := filepath.Dir(resp.DestPath) 87 | if _, err = os.Stat(destDir); os.IsNotExist(err) { 88 | //nolint: mnd, gomnd // Unix file permissions are wellknown numbers 89 | err = os.MkdirAll(destDir, 0o750) 90 | if err != nil { 91 | logs.Errorf( 92 | "Error creating destination directory %s from working director %s: %v", 93 | destDir, req.WorkingDir, err) 94 | } 95 | } 96 | 97 | tempFile, err := os.CreateTemp(destDir, "*.go-gen") 98 | if err != nil { 99 | return fmt.Errorf("error creating temp file: %w", err) 100 | } 101 | 102 | defer func() { 103 | err = tempFile.Close() 104 | if err != nil { 105 | logs.Error("Error closing temp file", err) 106 | } 107 | }() 108 | 109 | restorer := decorator.NewRestorerWithImports(resp.OutPkgPath, gopackages.New(destDir)) 110 | err = restorer.Fprint(tempFile, resp.File) 111 | if err != nil { 112 | return fmt.Errorf("invalid moq: %w", err) 113 | } 114 | 115 | err = os.Rename(tempFile.Name(), resp.DestPath) 116 | if err != nil { 117 | logs.Debugf("Error removing destination file: %v", err) 118 | } 119 | logs.Debugf("Wrote file: %s", resp.DestPath) 120 | 121 | return nil 122 | } 123 | -------------------------------------------------------------------------------- /generator/generator_test.go: -------------------------------------------------------------------------------- 1 | package generator_test 2 | 3 | import ( 4 | "bufio" 5 | "go/parser" 6 | "go/token" 7 | "os" 8 | "strings" 9 | "testing" 10 | 11 | "github.com/dave/dst" 12 | "github.com/dave/dst/decorator" 13 | 14 | "moqueries.org/cli/generator" 15 | "moqueries.org/cli/pkg" 16 | ) 17 | 18 | //go:generate moqueries --test-import testInterface 19 | 20 | // testInterface verifies that the generator can access types in the test 21 | // package 22 | type testInterface interface { 23 | something() 24 | } 25 | 26 | func TestGenerating(t *testing.T) { 27 | const imp = "moqueries.org/cli/generator/testmoqs" 28 | 29 | t.Run("generates lots of different types of moqs which are then tested by testmoqs", func(t *testing.T) { 30 | if testing.Short() { 31 | t.Skip("skipping generate test in short mode.") 32 | } 33 | 34 | // NB: Keep in sync with testmoqs/types.go go:generate directives 35 | 36 | // These lines generate the same moqs listed in types.go go:generate 37 | // directives. 38 | 39 | types := []string{ 40 | "UsualFn", 41 | "NoNamesFn", 42 | "NoResultsFn", 43 | "NoParamsFn", 44 | "NothingFn", 45 | "VariadicFn", 46 | "RepeatedIdsFn", 47 | "TimesFn", 48 | "DifficultParamNamesFn", 49 | "DifficultResultNamesFn", 50 | "PassByArrayFn", 51 | "PassByChanFn", 52 | "PassByEllipsisFn", 53 | "PassByMapFn", 54 | "PassByReferenceFn", 55 | "PassBySliceFn", 56 | "PassByValueFn", 57 | "InterfaceParamFn", 58 | "InterfaceResultFn", 59 | "GenericParamsFn", 60 | "PartialGenericParamsFn", 61 | "GenericResultsFn", 62 | "PartialGenericResultsFn", 63 | "GenericInterfaceParamFn", 64 | "GenericInterfaceResultFn", 65 | "Usual", 66 | "GenericParams", 67 | "PartialGenericParams", 68 | "GenericResults", 69 | "PartialGenericResults", 70 | "GenericInterfaceParam", 71 | "GenericInterfaceResult", 72 | "UnsafePointerFn", 73 | } 74 | 75 | err := generator.Generate( 76 | generator.GenerateRequest{ 77 | Destination: "testmoqs/moq_testmoqs_test.go", 78 | Types: types, 79 | Import: imp, 80 | }, 81 | generator.GenerateRequest{ 82 | Destination: "testmoqs/exported/moq_exported_testmoqs.go", 83 | Export: true, 84 | Types: types, 85 | Import: imp, 86 | }, 87 | ) 88 | if err != nil { 89 | t.Errorf("got %#v, wanted no err", err) 90 | } 91 | 92 | typeGoPath := "testmoqs/types.go" 93 | f, err := os.Open(typeGoPath) 94 | if err != nil { 95 | t.Fatalf("got %#v, wanted no err", err) 96 | } 97 | s := bufio.NewScanner(f) 98 | 99 | lineNo := 0 100 | for s.Scan() { 101 | lineNo++ 102 | line := string(s.Bytes()) 103 | 104 | prefix := "//go:generate moqueries" 105 | if !strings.HasPrefix(line, prefix) { 106 | continue 107 | } 108 | 109 | // Fresh copy of checked map; we check multiple lines 110 | checked := map[string]struct{}{} 111 | for _, typ := range types { 112 | checked[typ] = struct{}{} 113 | } 114 | 115 | genTypes := strings.Split(line[len(prefix)+1:], " ") 116 | for _, genType := range genTypes { 117 | if strings.HasPrefix(genType, "--") || strings.HasSuffix(genType, ".go") { 118 | continue 119 | } 120 | 121 | if _, ok := checked[genType]; !ok { 122 | t.Errorf("type %s missing from types array above", genType) 123 | } 124 | 125 | delete(checked, genType) 126 | } 127 | 128 | for c := range checked { 129 | t.Errorf("type %s missing from line %d of %s", c, lineNo, typeGoPath) 130 | } 131 | } 132 | }) 133 | 134 | t.Run("manual package test", func(t *testing.T) { 135 | t.Skip("manual test") 136 | err := pkg.Generate(pkg.PackageGenerateRequest{ 137 | DestinationDir: "../../std", PkgPatterns: []string{"std"}, 138 | }) 139 | if err != nil { 140 | t.Errorf("got %#v, wanted no err", err) 141 | } 142 | }) 143 | 144 | t.Run("dumps the DST of a moq", func(t *testing.T) { 145 | t.SkipNow() 146 | filePath := "./testmoqs/exported/moqusual.go" 147 | outPath := "./dst.txt" 148 | 149 | fSet := token.NewFileSet() 150 | // inFile, err := parser.ParseDir(fSet, filePath, nil, parser.AllErrors) 151 | inFile, err := parser.ParseFile(fSet, filePath, nil, parser.ParseComments) 152 | if err != nil { 153 | t.Errorf("got %#v, want no err", err) 154 | } 155 | 156 | // pkgs, err := packages.Load(&packages.Config{ 157 | // Mode: packages.NeedName | 158 | // packages.NeedFiles | 159 | // packages.NeedCompiledGoFiles | 160 | // packages.NeedImports | 161 | // packages.NeedTypes | 162 | // packages.NeedSyntax | 163 | // packages.NeedTypesInfo | 164 | // packages.NeedTypesSizes, 165 | // Tests: false, 166 | // }, filePath) 167 | dstFile, err := decorator.DecorateFile(fSet, inFile) 168 | if err != nil { 169 | t.Errorf("got %#v, want no err", err) 170 | } 171 | 172 | outFile, err := os.Create(outPath) 173 | if err != nil { 174 | t.Errorf("got %#v, want no err", err) 175 | } 176 | 177 | err = dst.Fprint(outFile, dstFile, dst.NotNilFilter) 178 | if err != nil { 179 | t.Errorf("got %#v, want no err", err) 180 | } 181 | }) 182 | } 183 | -------------------------------------------------------------------------------- /generator/moq_getwdfunc_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package generator_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/generator" 9 | "moqueries.org/runtime/impl" 10 | "moqueries.org/runtime/moq" 11 | ) 12 | 13 | // moqGetwdFunc holds the state of a moq of the GetwdFunc type 14 | type moqGetwdFunc struct { 15 | moq *impl.Moq[ 16 | *moqGetwdFunc_adaptor, 17 | moqGetwdFunc_params, 18 | moqGetwdFunc_paramsKey, 19 | moqGetwdFunc_results, 20 | ] 21 | 22 | runtime moqGetwdFunc_runtime 23 | } 24 | 25 | // moqGetwdFunc_runtime holds runtime configuration for the GetwdFunc type 26 | type moqGetwdFunc_runtime struct { 27 | parameterIndexing moqGetwdFunc_paramIndexing 28 | } 29 | 30 | // moqGetwdFunc_adaptor adapts moqGetwdFunc as needed by the runtime 31 | type moqGetwdFunc_adaptor struct { 32 | moq *moqGetwdFunc 33 | } 34 | 35 | // moqGetwdFunc_params holds the params of the GetwdFunc type 36 | type moqGetwdFunc_params struct{} 37 | 38 | // moqGetwdFunc_paramsKey holds the map key params of the GetwdFunc type 39 | type moqGetwdFunc_paramsKey struct { 40 | params struct{} 41 | hashes struct{} 42 | } 43 | 44 | // moqGetwdFunc_results holds the results of the GetwdFunc type 45 | type moqGetwdFunc_results struct { 46 | result1 string 47 | result2 error 48 | } 49 | 50 | // moqGetwdFunc_paramIndexing holds the parameter indexing runtime 51 | // configuration for the GetwdFunc type 52 | type moqGetwdFunc_paramIndexing struct{} 53 | 54 | // moqGetwdFunc_doFn defines the type of function needed when calling andDo for 55 | // the GetwdFunc type 56 | type moqGetwdFunc_doFn func() 57 | 58 | // moqGetwdFunc_doReturnFn defines the type of function needed when calling 59 | // doReturnResults for the GetwdFunc type 60 | type moqGetwdFunc_doReturnFn func() (string, error) 61 | 62 | // moqGetwdFunc_recorder routes recorded function calls to the moqGetwdFunc moq 63 | type moqGetwdFunc_recorder struct { 64 | recorder *impl.Recorder[ 65 | *moqGetwdFunc_adaptor, 66 | moqGetwdFunc_params, 67 | moqGetwdFunc_paramsKey, 68 | moqGetwdFunc_results, 69 | ] 70 | } 71 | 72 | // moqGetwdFunc_anyParams isolates the any params functions of the GetwdFunc 73 | // type 74 | type moqGetwdFunc_anyParams struct { 75 | recorder *moqGetwdFunc_recorder 76 | } 77 | 78 | // newMoqGetwdFunc creates a new moq of the GetwdFunc type 79 | func newMoqGetwdFunc(scene *moq.Scene, config *moq.Config) *moqGetwdFunc { 80 | adaptor1 := &moqGetwdFunc_adaptor{} 81 | m := &moqGetwdFunc{ 82 | moq: impl.NewMoq[ 83 | *moqGetwdFunc_adaptor, 84 | moqGetwdFunc_params, 85 | moqGetwdFunc_paramsKey, 86 | moqGetwdFunc_results, 87 | ](scene, adaptor1, config), 88 | 89 | runtime: moqGetwdFunc_runtime{parameterIndexing: moqGetwdFunc_paramIndexing{}}, 90 | } 91 | adaptor1.moq = m 92 | 93 | scene.AddMoq(m) 94 | return m 95 | } 96 | 97 | // mock returns the moq implementation of the GetwdFunc type 98 | func (m *moqGetwdFunc) mock() generator.GetwdFunc { 99 | return func() (string, error) { 100 | m.moq.Scene.T.Helper() 101 | params := moqGetwdFunc_params{} 102 | 103 | var result1 string 104 | var result2 error 105 | if result := m.moq.Function(params); result != nil { 106 | result1 = result.result1 107 | result2 = result.result2 108 | } 109 | return result1, result2 110 | } 111 | } 112 | 113 | func (m *moqGetwdFunc) onCall() *moqGetwdFunc_recorder { 114 | return &moqGetwdFunc_recorder{ 115 | recorder: m.moq.OnCall(moqGetwdFunc_params{}), 116 | } 117 | } 118 | 119 | func (r *moqGetwdFunc_recorder) any() *moqGetwdFunc_anyParams { 120 | r.recorder.Moq.Scene.T.Helper() 121 | if !r.recorder.IsAnyPermitted(false) { 122 | return nil 123 | } 124 | return &moqGetwdFunc_anyParams{recorder: r} 125 | } 126 | 127 | func (r *moqGetwdFunc_recorder) seq() *moqGetwdFunc_recorder { 128 | r.recorder.Moq.Scene.T.Helper() 129 | if !r.recorder.Seq(true, "seq", false) { 130 | return nil 131 | } 132 | return r 133 | } 134 | 135 | func (r *moqGetwdFunc_recorder) noSeq() *moqGetwdFunc_recorder { 136 | r.recorder.Moq.Scene.T.Helper() 137 | if !r.recorder.Seq(false, "noSeq", false) { 138 | return nil 139 | } 140 | return r 141 | } 142 | 143 | func (r *moqGetwdFunc_recorder) returnResults(result1 string, result2 error) *moqGetwdFunc_recorder { 144 | r.recorder.Moq.Scene.T.Helper() 145 | r.recorder.ReturnResults(moqGetwdFunc_results{ 146 | result1: result1, 147 | result2: result2, 148 | }) 149 | return r 150 | } 151 | 152 | func (r *moqGetwdFunc_recorder) andDo(fn moqGetwdFunc_doFn) *moqGetwdFunc_recorder { 153 | r.recorder.Moq.Scene.T.Helper() 154 | if !r.recorder.AndDo(func(params moqGetwdFunc_params) { 155 | fn() 156 | }, false) { 157 | return nil 158 | } 159 | return r 160 | } 161 | 162 | func (r *moqGetwdFunc_recorder) doReturnResults(fn moqGetwdFunc_doReturnFn) *moqGetwdFunc_recorder { 163 | r.recorder.Moq.Scene.T.Helper() 164 | r.recorder.DoReturnResults(func(params moqGetwdFunc_params) *moqGetwdFunc_results { 165 | result1, result2 := fn() 166 | return &moqGetwdFunc_results{ 167 | result1: result1, 168 | result2: result2, 169 | } 170 | }) 171 | return r 172 | } 173 | 174 | func (r *moqGetwdFunc_recorder) repeat(repeaters ...moq.Repeater) *moqGetwdFunc_recorder { 175 | r.recorder.Moq.Scene.T.Helper() 176 | if !r.recorder.Repeat(repeaters, false) { 177 | return nil 178 | } 179 | return r 180 | } 181 | 182 | func (*moqGetwdFunc_adaptor) PrettyParams(params moqGetwdFunc_params) string { 183 | return fmt.Sprintf("GetwdFunc()") 184 | } 185 | 186 | func (a *moqGetwdFunc_adaptor) ParamsKey(params moqGetwdFunc_params, anyParams uint64) moqGetwdFunc_paramsKey { 187 | a.moq.moq.Scene.T.Helper() 188 | return moqGetwdFunc_paramsKey{ 189 | params: struct{}{}, 190 | hashes: struct{}{}, 191 | } 192 | } 193 | 194 | // Reset resets the state of the moq 195 | func (m *moqGetwdFunc) Reset() { 196 | m.moq.Reset() 197 | } 198 | 199 | // AssertExpectationsMet asserts that all expectations have been met 200 | func (m *moqGetwdFunc) AssertExpectationsMet() { 201 | m.moq.Scene.T.Helper() 202 | m.moq.AssertExpectationsMet() 203 | } 204 | -------------------------------------------------------------------------------- /generator/moq_newconverterfunc_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package generator_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/generator" 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // moqNewConverterFunc holds the state of a moq of the NewConverterFunc type 15 | type moqNewConverterFunc struct { 16 | moq *impl.Moq[ 17 | *moqNewConverterFunc_adaptor, 18 | moqNewConverterFunc_params, 19 | moqNewConverterFunc_paramsKey, 20 | moqNewConverterFunc_results, 21 | ] 22 | 23 | runtime moqNewConverterFunc_runtime 24 | } 25 | 26 | // moqNewConverterFunc_runtime holds runtime configuration for the 27 | // NewConverterFunc type 28 | type moqNewConverterFunc_runtime struct { 29 | parameterIndexing moqNewConverterFunc_paramIndexing 30 | } 31 | 32 | // moqNewConverterFunc_adaptor adapts moqNewConverterFunc as needed by the 33 | // runtime 34 | type moqNewConverterFunc_adaptor struct { 35 | moq *moqNewConverterFunc 36 | } 37 | 38 | // moqNewConverterFunc_params holds the params of the NewConverterFunc type 39 | type moqNewConverterFunc_params struct { 40 | typ generator.Type 41 | export bool 42 | } 43 | 44 | // moqNewConverterFunc_paramsKey holds the map key params of the 45 | // NewConverterFunc type 46 | type moqNewConverterFunc_paramsKey struct { 47 | params struct{ export bool } 48 | hashes struct { 49 | typ hash.Hash 50 | export hash.Hash 51 | } 52 | } 53 | 54 | // moqNewConverterFunc_results holds the results of the NewConverterFunc type 55 | type moqNewConverterFunc_results struct { 56 | result1 generator.Converterer 57 | } 58 | 59 | // moqNewConverterFunc_paramIndexing holds the parameter indexing runtime 60 | // configuration for the NewConverterFunc type 61 | type moqNewConverterFunc_paramIndexing struct { 62 | typ moq.ParamIndexing 63 | export moq.ParamIndexing 64 | } 65 | 66 | // moqNewConverterFunc_doFn defines the type of function needed when calling 67 | // andDo for the NewConverterFunc type 68 | type moqNewConverterFunc_doFn func(typ generator.Type, export bool) 69 | 70 | // moqNewConverterFunc_doReturnFn defines the type of function needed when 71 | // calling doReturnResults for the NewConverterFunc type 72 | type moqNewConverterFunc_doReturnFn func(typ generator.Type, export bool) generator.Converterer 73 | 74 | // moqNewConverterFunc_recorder routes recorded function calls to the 75 | // moqNewConverterFunc moq 76 | type moqNewConverterFunc_recorder struct { 77 | recorder *impl.Recorder[ 78 | *moqNewConverterFunc_adaptor, 79 | moqNewConverterFunc_params, 80 | moqNewConverterFunc_paramsKey, 81 | moqNewConverterFunc_results, 82 | ] 83 | } 84 | 85 | // moqNewConverterFunc_anyParams isolates the any params functions of the 86 | // NewConverterFunc type 87 | type moqNewConverterFunc_anyParams struct { 88 | recorder *moqNewConverterFunc_recorder 89 | } 90 | 91 | // newMoqNewConverterFunc creates a new moq of the NewConverterFunc type 92 | func newMoqNewConverterFunc(scene *moq.Scene, config *moq.Config) *moqNewConverterFunc { 93 | adaptor1 := &moqNewConverterFunc_adaptor{} 94 | m := &moqNewConverterFunc{ 95 | moq: impl.NewMoq[ 96 | *moqNewConverterFunc_adaptor, 97 | moqNewConverterFunc_params, 98 | moqNewConverterFunc_paramsKey, 99 | moqNewConverterFunc_results, 100 | ](scene, adaptor1, config), 101 | 102 | runtime: moqNewConverterFunc_runtime{parameterIndexing: moqNewConverterFunc_paramIndexing{ 103 | typ: moq.ParamIndexByHash, 104 | export: moq.ParamIndexByValue, 105 | }}, 106 | } 107 | adaptor1.moq = m 108 | 109 | scene.AddMoq(m) 110 | return m 111 | } 112 | 113 | // mock returns the moq implementation of the NewConverterFunc type 114 | func (m *moqNewConverterFunc) mock() generator.NewConverterFunc { 115 | return func(typ generator.Type, export bool) generator.Converterer { 116 | m.moq.Scene.T.Helper() 117 | params := moqNewConverterFunc_params{ 118 | typ: typ, 119 | export: export, 120 | } 121 | 122 | var result1 generator.Converterer 123 | if result := m.moq.Function(params); result != nil { 124 | result1 = result.result1 125 | } 126 | return result1 127 | } 128 | } 129 | 130 | func (m *moqNewConverterFunc) onCall(typ generator.Type, export bool) *moqNewConverterFunc_recorder { 131 | return &moqNewConverterFunc_recorder{ 132 | recorder: m.moq.OnCall(moqNewConverterFunc_params{ 133 | typ: typ, 134 | export: export, 135 | }), 136 | } 137 | } 138 | 139 | func (r *moqNewConverterFunc_recorder) any() *moqNewConverterFunc_anyParams { 140 | r.recorder.Moq.Scene.T.Helper() 141 | if !r.recorder.IsAnyPermitted(false) { 142 | return nil 143 | } 144 | return &moqNewConverterFunc_anyParams{recorder: r} 145 | } 146 | 147 | func (a *moqNewConverterFunc_anyParams) typ() *moqNewConverterFunc_recorder { 148 | a.recorder.recorder.AnyParam(1) 149 | return a.recorder 150 | } 151 | 152 | func (a *moqNewConverterFunc_anyParams) export() *moqNewConverterFunc_recorder { 153 | a.recorder.recorder.AnyParam(2) 154 | return a.recorder 155 | } 156 | 157 | func (r *moqNewConverterFunc_recorder) seq() *moqNewConverterFunc_recorder { 158 | r.recorder.Moq.Scene.T.Helper() 159 | if !r.recorder.Seq(true, "seq", false) { 160 | return nil 161 | } 162 | return r 163 | } 164 | 165 | func (r *moqNewConverterFunc_recorder) noSeq() *moqNewConverterFunc_recorder { 166 | r.recorder.Moq.Scene.T.Helper() 167 | if !r.recorder.Seq(false, "noSeq", false) { 168 | return nil 169 | } 170 | return r 171 | } 172 | 173 | func (r *moqNewConverterFunc_recorder) returnResults(result1 generator.Converterer) *moqNewConverterFunc_recorder { 174 | r.recorder.Moq.Scene.T.Helper() 175 | r.recorder.ReturnResults(moqNewConverterFunc_results{ 176 | result1: result1, 177 | }) 178 | return r 179 | } 180 | 181 | func (r *moqNewConverterFunc_recorder) andDo(fn moqNewConverterFunc_doFn) *moqNewConverterFunc_recorder { 182 | r.recorder.Moq.Scene.T.Helper() 183 | if !r.recorder.AndDo(func(params moqNewConverterFunc_params) { 184 | fn(params.typ, params.export) 185 | }, false) { 186 | return nil 187 | } 188 | return r 189 | } 190 | 191 | func (r *moqNewConverterFunc_recorder) doReturnResults(fn moqNewConverterFunc_doReturnFn) *moqNewConverterFunc_recorder { 192 | r.recorder.Moq.Scene.T.Helper() 193 | r.recorder.DoReturnResults(func(params moqNewConverterFunc_params) *moqNewConverterFunc_results { 194 | result1 := fn(params.typ, params.export) 195 | return &moqNewConverterFunc_results{ 196 | result1: result1, 197 | } 198 | }) 199 | return r 200 | } 201 | 202 | func (r *moqNewConverterFunc_recorder) repeat(repeaters ...moq.Repeater) *moqNewConverterFunc_recorder { 203 | r.recorder.Moq.Scene.T.Helper() 204 | if !r.recorder.Repeat(repeaters, false) { 205 | return nil 206 | } 207 | return r 208 | } 209 | 210 | func (*moqNewConverterFunc_adaptor) PrettyParams(params moqNewConverterFunc_params) string { 211 | return fmt.Sprintf("NewConverterFunc(%#v, %#v)", params.typ, params.export) 212 | } 213 | 214 | func (a *moqNewConverterFunc_adaptor) ParamsKey(params moqNewConverterFunc_params, anyParams uint64) moqNewConverterFunc_paramsKey { 215 | a.moq.moq.Scene.T.Helper() 216 | typUsedHash := impl.HashOnlyParamKey(a.moq.moq.Scene.T, 217 | params.typ, "typ", 1, a.moq.runtime.parameterIndexing.typ, anyParams) 218 | exportUsed, exportUsedHash := impl.ParamKey( 219 | params.export, 2, a.moq.runtime.parameterIndexing.export, anyParams) 220 | return moqNewConverterFunc_paramsKey{ 221 | params: struct{ export bool }{ 222 | export: exportUsed, 223 | }, 224 | hashes: struct { 225 | typ hash.Hash 226 | export hash.Hash 227 | }{ 228 | typ: typUsedHash, 229 | export: exportUsedHash, 230 | }, 231 | } 232 | } 233 | 234 | // Reset resets the state of the moq 235 | func (m *moqNewConverterFunc) Reset() { 236 | m.moq.Reset() 237 | } 238 | 239 | // AssertExpectationsMet asserts that all expectations have been met 240 | func (m *moqNewConverterFunc) AssertExpectationsMet() { 241 | m.moq.Scene.T.Helper() 242 | m.moq.AssertExpectationsMet() 243 | } 244 | -------------------------------------------------------------------------------- /generator/moq_testinterface_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package generator_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/runtime/impl" 9 | "moqueries.org/runtime/moq" 10 | ) 11 | 12 | // The following type assertion assures that generator_test.testInterface is 13 | // mocked completely 14 | var _ testInterface = (*moqTestInterface_mock)(nil) 15 | 16 | // moqTestInterface holds the state of a moq of the testInterface type 17 | type moqTestInterface struct { 18 | moq *moqTestInterface_mock 19 | 20 | moq_something *impl.Moq[ 21 | *moqTestInterface_something_adaptor, 22 | moqTestInterface_something_params, 23 | moqTestInterface_something_paramsKey, 24 | moqTestInterface_something_results, 25 | ] 26 | 27 | runtime moqTestInterface_runtime 28 | } 29 | 30 | // moqTestInterface_mock isolates the mock interface of the testInterface type 31 | type moqTestInterface_mock struct { 32 | moq *moqTestInterface 33 | } 34 | 35 | // moqTestInterface_recorder isolates the recorder interface of the 36 | // testInterface type 37 | type moqTestInterface_recorder struct { 38 | moq *moqTestInterface 39 | } 40 | 41 | // moqTestInterface_runtime holds runtime configuration for the testInterface 42 | // type 43 | type moqTestInterface_runtime struct { 44 | parameterIndexing struct { 45 | something moqTestInterface_something_paramIndexing 46 | } 47 | } 48 | 49 | // moqTestInterface_something_adaptor adapts moqTestInterface as needed by the 50 | // runtime 51 | type moqTestInterface_something_adaptor struct { 52 | moq *moqTestInterface 53 | } 54 | 55 | // moqTestInterface_something_params holds the params of the testInterface type 56 | type moqTestInterface_something_params struct{} 57 | 58 | // moqTestInterface_something_paramsKey holds the map key params of the 59 | // testInterface type 60 | type moqTestInterface_something_paramsKey struct { 61 | params struct{} 62 | hashes struct{} 63 | } 64 | 65 | // moqTestInterface_something_results holds the results of the testInterface 66 | // type 67 | type moqTestInterface_something_results struct{} 68 | 69 | // moqTestInterface_something_paramIndexing holds the parameter indexing 70 | // runtime configuration for the testInterface type 71 | type moqTestInterface_something_paramIndexing struct{} 72 | 73 | // moqTestInterface_something_doFn defines the type of function needed when 74 | // calling andDo for the testInterface type 75 | type moqTestInterface_something_doFn func() 76 | 77 | // moqTestInterface_something_doReturnFn defines the type of function needed 78 | // when calling doReturnResults for the testInterface type 79 | type moqTestInterface_something_doReturnFn func() 80 | 81 | // moqTestInterface_something_recorder routes recorded function calls to the 82 | // moqTestInterface moq 83 | type moqTestInterface_something_recorder struct { 84 | recorder *impl.Recorder[ 85 | *moqTestInterface_something_adaptor, 86 | moqTestInterface_something_params, 87 | moqTestInterface_something_paramsKey, 88 | moqTestInterface_something_results, 89 | ] 90 | } 91 | 92 | // moqTestInterface_something_anyParams isolates the any params functions of 93 | // the testInterface type 94 | type moqTestInterface_something_anyParams struct { 95 | recorder *moqTestInterface_something_recorder 96 | } 97 | 98 | // newMoqtestInterface creates a new moq of the testInterface type 99 | func newMoqtestInterface(scene *moq.Scene, config *moq.Config) *moqTestInterface { 100 | adaptor1 := &moqTestInterface_something_adaptor{} 101 | m := &moqTestInterface{ 102 | moq: &moqTestInterface_mock{}, 103 | 104 | moq_something: impl.NewMoq[ 105 | *moqTestInterface_something_adaptor, 106 | moqTestInterface_something_params, 107 | moqTestInterface_something_paramsKey, 108 | moqTestInterface_something_results, 109 | ](scene, adaptor1, config), 110 | 111 | runtime: moqTestInterface_runtime{parameterIndexing: struct { 112 | something moqTestInterface_something_paramIndexing 113 | }{ 114 | something: moqTestInterface_something_paramIndexing{}, 115 | }}, 116 | } 117 | m.moq.moq = m 118 | 119 | adaptor1.moq = m 120 | 121 | scene.AddMoq(m) 122 | return m 123 | } 124 | 125 | // mock returns the mock implementation of the testInterface type 126 | func (m *moqTestInterface) mock() *moqTestInterface_mock { return m.moq } 127 | 128 | func (m *moqTestInterface_mock) something() { 129 | m.moq.moq_something.Scene.T.Helper() 130 | params := moqTestInterface_something_params{} 131 | 132 | m.moq.moq_something.Function(params) 133 | } 134 | 135 | // onCall returns the recorder implementation of the testInterface type 136 | func (m *moqTestInterface) onCall() *moqTestInterface_recorder { 137 | return &moqTestInterface_recorder{ 138 | moq: m, 139 | } 140 | } 141 | 142 | func (m *moqTestInterface_recorder) something() *moqTestInterface_something_recorder { 143 | return &moqTestInterface_something_recorder{ 144 | recorder: m.moq.moq_something.OnCall(moqTestInterface_something_params{}), 145 | } 146 | } 147 | 148 | func (r *moqTestInterface_something_recorder) any() *moqTestInterface_something_anyParams { 149 | r.recorder.Moq.Scene.T.Helper() 150 | if !r.recorder.IsAnyPermitted(false) { 151 | return nil 152 | } 153 | return &moqTestInterface_something_anyParams{recorder: r} 154 | } 155 | 156 | func (r *moqTestInterface_something_recorder) seq() *moqTestInterface_something_recorder { 157 | r.recorder.Moq.Scene.T.Helper() 158 | if !r.recorder.Seq(true, "seq", false) { 159 | return nil 160 | } 161 | return r 162 | } 163 | 164 | func (r *moqTestInterface_something_recorder) noSeq() *moqTestInterface_something_recorder { 165 | r.recorder.Moq.Scene.T.Helper() 166 | if !r.recorder.Seq(false, "noSeq", false) { 167 | return nil 168 | } 169 | return r 170 | } 171 | 172 | func (r *moqTestInterface_something_recorder) returnResults() *moqTestInterface_something_recorder { 173 | r.recorder.Moq.Scene.T.Helper() 174 | r.recorder.ReturnResults(moqTestInterface_something_results{}) 175 | return r 176 | } 177 | 178 | func (r *moqTestInterface_something_recorder) andDo(fn moqTestInterface_something_doFn) *moqTestInterface_something_recorder { 179 | r.recorder.Moq.Scene.T.Helper() 180 | if !r.recorder.AndDo(func(params moqTestInterface_something_params) { 181 | fn() 182 | }, false) { 183 | return nil 184 | } 185 | return r 186 | } 187 | 188 | func (r *moqTestInterface_something_recorder) doReturnResults(fn moqTestInterface_something_doReturnFn) *moqTestInterface_something_recorder { 189 | r.recorder.Moq.Scene.T.Helper() 190 | r.recorder.DoReturnResults(func(params moqTestInterface_something_params) *moqTestInterface_something_results { 191 | fn() 192 | return &moqTestInterface_something_results{} 193 | }) 194 | return r 195 | } 196 | 197 | func (r *moqTestInterface_something_recorder) repeat(repeaters ...moq.Repeater) *moqTestInterface_something_recorder { 198 | r.recorder.Moq.Scene.T.Helper() 199 | if !r.recorder.Repeat(repeaters, false) { 200 | return nil 201 | } 202 | return r 203 | } 204 | 205 | func (*moqTestInterface_something_adaptor) PrettyParams(params moqTestInterface_something_params) string { 206 | return fmt.Sprintf("something()") 207 | } 208 | 209 | func (a *moqTestInterface_something_adaptor) ParamsKey(params moqTestInterface_something_params, anyParams uint64) moqTestInterface_something_paramsKey { 210 | a.moq.moq_something.Scene.T.Helper() 211 | return moqTestInterface_something_paramsKey{ 212 | params: struct{}{}, 213 | hashes: struct{}{}, 214 | } 215 | } 216 | 217 | // Reset resets the state of the moq 218 | func (m *moqTestInterface) Reset() { 219 | m.moq_something.Reset() 220 | } 221 | 222 | // AssertExpectationsMet asserts that all expectations have been met 223 | func (m *moqTestInterface) AssertExpectationsMet() { 224 | m.moq_something.Scene.T.Helper() 225 | m.moq_something.AssertExpectationsMet() 226 | } 227 | -------------------------------------------------------------------------------- /generator/testmoqs/atomic_test.go: -------------------------------------------------------------------------------- 1 | package testmoqs_test 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | 7 | "moqueries.org/runtime/moq" 8 | ) 9 | 10 | const ( 11 | callsPerRoutine = 100 12 | routines = 10 13 | ) 14 | 15 | func TestAtomicSequences(t *testing.T) { 16 | t.Skip("This test is used to show how sequences and expected " + 17 | "call order are not atomic. This test is rarely successful so " + 18 | "should remain skipped.") 19 | 20 | // ASSEMBLE 21 | scene := moq.NewScene(t) 22 | usualMoq := newMoqUsualFn(scene, &moq.Config{ 23 | Sequence: moq.SeqDefaultOn, 24 | }) 25 | 26 | usualMoq.onCall("Hi", false). 27 | returnResults("Bye", nil). 28 | // We say we're expecting 10 times more results just in case one 29 | // routine iterates more quickly. We don't actually expect all calls 30 | // to be made. 31 | repeat(moq.Times(callsPerRoutine * routines * 10)) 32 | 33 | start := make(chan struct{}) 34 | done := sync.WaitGroup{} 35 | mockFn := usualMoq.mock() 36 | 37 | for n := 0; n < routines; n++ { 38 | done.Add(1) 39 | go func() { 40 | defer done.Done() 41 | 42 | <-start 43 | 44 | for m := 0; m < callsPerRoutine; m++ { 45 | res, err := mockFn("Hi", false) 46 | if err != nil { 47 | t.Errorf("wanted no err, got %#v", err) 48 | } 49 | if res != "Bye" { 50 | t.Errorf("wanted Bye, got %s", res) 51 | } 52 | } 53 | }() 54 | } 55 | 56 | // ACT 57 | close(start) 58 | done.Wait() 59 | 60 | // ASSERT 61 | } 62 | -------------------------------------------------------------------------------- /generator/testmoqs/other/types.go: -------------------------------------------------------------------------------- 1 | // Package other contains multiple test types for use in unit testing 2 | package other 3 | 4 | type Other interface { 5 | Another 6 | } 7 | 8 | // Params encapsulates the parameters for use in various test types 9 | type Params struct { 10 | SParam string 11 | BParam bool 12 | } 13 | 14 | // Results encapsulates the results for use in various test types 15 | type Results struct { 16 | SResult string 17 | Err error 18 | } 19 | 20 | type Another interface { 21 | Other(Params) Results 22 | } 23 | -------------------------------------------------------------------------------- /generator/testmoqs/types.go: -------------------------------------------------------------------------------- 1 | // Package testmoqs contains multiple test mocks and adaptors for use in unit 2 | // testing 3 | package testmoqs 4 | 5 | import ( 6 | "io" 7 | "unsafe" 8 | 9 | "moqueries.org/cli/generator/testmoqs/other" 10 | ) 11 | 12 | // NB: Keep in sync with ../generator_test.go TestGenerating 13 | 14 | //go:generate moqueries --destination moq_testmoqs_test.go UsualFn NoNamesFn NoResultsFn NoParamsFn NothingFn VariadicFn RepeatedIdsFn TimesFn DifficultParamNamesFn DifficultResultNamesFn PassByArrayFn PassByChanFn PassByEllipsisFn PassByMapFn PassByReferenceFn PassBySliceFn PassByValueFn InterfaceParamFn InterfaceResultFn GenericParamsFn PartialGenericParamsFn GenericResultsFn PartialGenericResultsFn GenericInterfaceParamFn GenericInterfaceResultFn Usual GenericParams PartialGenericParams GenericResults PartialGenericResults GenericInterfaceParam GenericInterfaceResult UnsafePointerFn 15 | //go:generate moqueries --destination exported/moq_exported_testmoqs.go --export UsualFn NoNamesFn NoResultsFn NoParamsFn NothingFn VariadicFn RepeatedIdsFn TimesFn DifficultParamNamesFn DifficultResultNamesFn PassByArrayFn PassByChanFn PassByEllipsisFn PassByMapFn PassByReferenceFn PassBySliceFn PassByValueFn InterfaceParamFn InterfaceResultFn GenericParamsFn PartialGenericParamsFn GenericResultsFn PartialGenericResultsFn GenericInterfaceParamFn GenericInterfaceResultFn Usual GenericParams PartialGenericParams GenericResults PartialGenericResults GenericInterfaceParam GenericInterfaceResult UnsafePointerFn 16 | 17 | // UsualFn is a typical function type 18 | type UsualFn func(sParam string, bParam bool) (sResult string, err error) 19 | 20 | // NoNamesFn is a typical function type 21 | type NoNamesFn func(string, bool) (string, error) 22 | 23 | // NoResultsFn is a function with no return values 24 | type NoResultsFn func(sParam string, bParam bool) 25 | 26 | // NoParamsFn is a function with no parameters 27 | type NoParamsFn func() (sResult string, err error) 28 | 29 | // NothingFn is a function with no parameters and no return values 30 | type NothingFn func() 31 | 32 | // VariadicFn is a function with a variable number of arguments 33 | type VariadicFn func(other bool, args ...string) (sResult string, err error) 34 | 35 | // RepeatedIdsFn is a function with multiple arguments of the same type 36 | type RepeatedIdsFn func(sParam1, sParam2 string, bParam bool) (sResult1, sResult2 string, err error) 37 | 38 | // TimesFn takes a parameter called times which should generate a valid moq 39 | type TimesFn func(times string, bParam bool) (sResult string, err error) 40 | 41 | // DifficultParamNamesFn has parameters with names that have been problematic 42 | type DifficultParamNamesFn func(m, r bool, sequence string, param, params, i int, result, results, _ float32) 43 | 44 | // DifficultResultNamesFn has parameters with names that have been problematic 45 | type DifficultResultNamesFn func() (m, r string, sequence error, param, params, i int, result, results, _ float32) 46 | 47 | // Params encapsulates the parameters for use in various test types 48 | type Params struct { 49 | SParam string 50 | BParam bool 51 | } 52 | 53 | // Results encapsulates the results for use in various test types 54 | type Results struct { 55 | SResult string 56 | Err error 57 | } 58 | 59 | // PassByArrayFn tests passing parameters and results by array 60 | type PassByArrayFn func(p [3]Params) [3]Results 61 | 62 | // PassByChanFn tests passing parameters and results by channel 63 | type PassByChanFn func(p chan Params) chan Results 64 | 65 | // PassByEllipsisFn tests passing parameters by ellipsis 66 | type PassByEllipsisFn func(p ...Params) (string, error) 67 | 68 | // PassByMapFn tests passing parameters and results by map 69 | type PassByMapFn func(p map[string]Params) map[string]Results 70 | 71 | // PassByReferenceFn tests passing parameters and results by reference 72 | type PassByReferenceFn func(p *Params) *Results 73 | 74 | // PassBySliceFn tests passing parameters and results by slice 75 | type PassBySliceFn func(p []Params) []Results 76 | 77 | // PassByValueFn tests passing parameters and results by reference 78 | type PassByValueFn func(p Params) Results 79 | 80 | // InterfaceParamWriter is used for testing functions that take an interface as 81 | // a parameter 82 | type InterfaceParamWriter struct { 83 | SParam string 84 | BParam bool 85 | } 86 | 87 | func (w *InterfaceParamWriter) Write([]byte) (int, error) { 88 | return 0, nil 89 | } 90 | 91 | // InterfaceParamFn tests passing interface parameters 92 | type InterfaceParamFn func(w io.Writer) (sResult string, err error) 93 | 94 | // InterfaceResultReader is used for testing functions that return an interface 95 | type InterfaceResultReader struct { 96 | SResult string 97 | Err error 98 | } 99 | 100 | func (r *InterfaceResultReader) Read([]byte) (int, error) { 101 | return 0, nil 102 | } 103 | 104 | // InterfaceResultFn tests returning interface results 105 | type InterfaceResultFn func(sParam string, bParam bool) (r io.Reader) 106 | 107 | // GenericParamsFn has all generic parameters 108 | type GenericParamsFn[S, B any] func(S, B) (string, error) 109 | 110 | // PartialGenericParamsFn has some generic parameters 111 | type PartialGenericParamsFn[S any] func(S, bool) (string, error) 112 | 113 | // GenericResultsFn has all generic results 114 | type GenericResultsFn[S ~string, E error] func(string, bool) (S, E) 115 | 116 | // PartialGenericResultsFn has some generic results 117 | type PartialGenericResultsFn[S ~string] func(string, bool) (S, error) 118 | 119 | type MyWriter io.Writer 120 | 121 | // GenericInterfaceParamFn tests passing generic interface parameters 122 | type GenericInterfaceParamFn[W MyWriter] func(w W) (sResult string, err error) 123 | 124 | type MyReader io.Reader 125 | 126 | // GenericInterfaceResultFn tests returning generic interface results 127 | type GenericInterfaceResultFn[R MyReader] func(sParam string, bParam bool) (r R) 128 | 129 | // Usual combines all the above function types into an interface 130 | // 131 | //nolint:interfacebloat // Test interface with one of every method type 132 | type Usual interface { 133 | Usual(sParam string, bParam bool) (sResult string, err error) 134 | NoNames(string, bool) (string, error) 135 | NoResults(sParam string, bParam bool) 136 | NoParams() (sResult string, err error) 137 | Nothing() 138 | Variadic(other bool, args ...string) (sResult string, err error) 139 | RepeatedIds(sParam1, sParam2 string, bParam bool) (sResult1, sResult2 string, err error) 140 | Times(sParam string, times bool) (sResult string, err error) 141 | DifficultParamNames(m, r bool, sequence string, param, params, i int, result, results, _ float32) 142 | DifficultResultNames() (m, r string, sequence error, param, params, i int, result, results, _ float32) 143 | PassByArray(p [3]Params) [3]Results 144 | PassByChan(p chan Params) chan Results 145 | PassByEllipsis(p ...Params) (string, error) 146 | PassByMap(p map[string]Params) map[string]Results 147 | PassByReference(p *Params) *Results 148 | PassBySlice(p []Params) []Results 149 | PassByValue(p Params) Results 150 | InterfaceParam(w io.Writer) (sResult string, err error) 151 | InterfaceResult(sParam string, bParam bool) (r io.Reader) 152 | FnParam(fn func()) 153 | other.Other 154 | } 155 | 156 | // The following unused structs check that the AST cache doesn't pick up the 157 | // wrong types 158 | type ( 159 | S struct{} 160 | B struct{} 161 | E struct{} 162 | W struct{} 163 | ) 164 | 165 | type GenericParams[S, B any] interface { 166 | Usual(S, B) (string, error) 167 | } 168 | 169 | type PartialGenericParams[S any] interface { 170 | Usual(S, bool) (string, error) 171 | } 172 | 173 | type GenericResults[S ~string, E error] interface { 174 | Usual(string, bool) (S, E) 175 | } 176 | 177 | type PartialGenericResults[S ~string] interface { 178 | Usual(string, bool) (S, error) 179 | } 180 | 181 | type GenericInterfaceParam[W MyWriter] interface { 182 | Usual(w W) (sResult string, err error) 183 | } 184 | 185 | type GenericInterfaceResult[R MyReader] interface { 186 | Usual(sParam string, bParam bool) (r R) 187 | } 188 | 189 | type UnsafePointerFn func() unsafe.Pointer 190 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module moqueries.org/cli 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/dave/dst v0.27.3 7 | github.com/spf13/cobra v1.8.1 8 | golang.org/x/mod v0.20.0 9 | golang.org/x/text v0.17.0 10 | golang.org/x/tools v0.24.0 11 | moqueries.org/runtime v0.4.0 12 | ) 13 | 14 | require ( 15 | github.com/inconshreveable/mousetrap v1.1.0 // indirect 16 | github.com/spf13/pflag v1.0.5 // indirect 17 | golang.org/x/sync v0.8.0 // indirect 18 | moqueries.org/deephash v0.26.0 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 2 | github.com/dave/dst v0.27.3 h1:P1HPoMza3cMEquVf9kKy8yXsFirry4zEnWOdYPOoIzY= 3 | github.com/dave/dst v0.27.3/go.mod h1:jHh6EOibnHgcUW3WjKHisiooEkYwqpHLBSX1iOBhEyc= 4 | github.com/dave/jennifer v1.5.0 h1:HmgPN93bVDpkQyYbqhCHj5QlgvUkvEOzMyEvKLgCRrg= 5 | github.com/dave/jennifer v1.5.0/go.mod h1:4MnyiFIlZS3l5tSDn8VnzE6ffAhYBMB2SZntBsZGUok= 6 | github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= 7 | github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= 8 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 9 | github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= 10 | github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 11 | github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= 12 | github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= 13 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 14 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 15 | golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= 16 | golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 17 | golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= 18 | golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 19 | golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= 20 | golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= 21 | golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= 22 | golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= 23 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 24 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 25 | moqueries.org/deephash v0.26.0 h1:KvMnNvb/N9lId0OOO1X6Yp2BifVuDuJJQH3epAeF178= 26 | moqueries.org/deephash v0.26.0/go.mod h1:brwHe2Ks78ZEA1xIgUTEy0gtZfO7tJTqXT9haTdIRNE= 27 | moqueries.org/runtime v0.4.0 h1:J5azoWrMEo8baBYKoJVlpw0dPjI5ZGJviPwvRJbG6Iw= 28 | moqueries.org/runtime v0.4.0/go.mod h1:VDwSKhTpm8NBxE23Wp0+zCXiV4hvJZ6zbzh9y4LkGeE= 29 | -------------------------------------------------------------------------------- /metrics/metrics.go: -------------------------------------------------------------------------------- 1 | // Package metrics implements a simple metrics mechanism for gathering, 2 | // reporting and aggregating metrics 3 | package metrics 4 | 5 | import ( 6 | "bytes" 7 | "encoding/json" 8 | "time" 9 | 10 | "moqueries.org/runtime/logs" 11 | ) 12 | 13 | const ( 14 | metricsLogKey = "Type cache metrics" 15 | ) 16 | 17 | //go:generate moqueries --export Metrics 18 | 19 | // Metrics is the interface to the metrics system 20 | type Metrics interface { 21 | ASTPkgCacheHitsInc() 22 | ASTPkgCacheMissesInc() 23 | ASTTotalLoadTimeInc(d time.Duration) 24 | ASTTotalDecorationTimeInc(d time.Duration) 25 | 26 | TotalProcessingTimeInc(d time.Duration) 27 | 28 | Finalize() 29 | } 30 | 31 | type metricsState struct { 32 | ASTPkgCacheHits int `json:"ast-pkg-cache-hits"` 33 | ASTPkgCacheMisses int `json:"ast-pkg-cache-misses"` 34 | ASTTypeCacheHits int `json:"ast-type-cache-hits"` 35 | ASTTypeCacheMisses int `json:"ast-type-cache-misses"` 36 | 37 | ASTTotalLoadTime time.Duration `json:"ast-total-load-time"` 38 | ASTTotalLoadTimeStr string `json:"ast-total-load-time-str"` 39 | 40 | ASTTotalDecorationTime time.Duration `json:"ast-total-decoration-time"` 41 | ASTTotalDecorationTimeStr string `json:"ast-total-decoration-time-str"` 42 | 43 | TotalProcessingTime time.Duration `json:"total-processing-time"` 44 | TotalProcessingTimeStr string `json:"total-processing-time-str"` 45 | } 46 | 47 | func add(m1, m2 metricsState) metricsState { 48 | // NOTE: Intentionally close to struct definition as a reminder to update 49 | // this func when the struct is updated 50 | return metricsState{ 51 | ASTPkgCacheHits: m1.ASTPkgCacheHits + m2.ASTPkgCacheHits, 52 | ASTPkgCacheMisses: m1.ASTPkgCacheMisses + m2.ASTPkgCacheMisses, 53 | ASTTypeCacheHits: m1.ASTTypeCacheHits + m2.ASTTypeCacheHits, 54 | ASTTypeCacheMisses: m1.ASTTypeCacheMisses + m2.ASTTypeCacheMisses, 55 | ASTTotalLoadTime: m1.ASTTotalLoadTime + m2.ASTTotalLoadTime, 56 | ASTTotalDecorationTime: m1.ASTTotalDecorationTime + m2.ASTTotalDecorationTime, 57 | TotalProcessingTime: m1.TotalProcessingTime + m2.TotalProcessingTime, 58 | } 59 | } 60 | 61 | // Processor maintains the state of the metrics system 62 | type Processor struct { 63 | isLoggingFn IsLoggingFn 64 | loggingfFn LoggingfFn 65 | 66 | state metricsState 67 | } 68 | 69 | //go:generate moqueries IsLoggingFn 70 | 71 | // IsLoggingFn is the function that determines if logging is on 72 | type IsLoggingFn func() bool 73 | 74 | //go:generate moqueries LoggingfFn 75 | 76 | // LoggingfFn is the logging function to output finalized metrics 77 | type LoggingfFn func(format string, args ...interface{}) 78 | 79 | // NewMetrics returns a new system for gathering metrics 80 | func NewMetrics(isLoggingFn IsLoggingFn, loggingfFn LoggingfFn) *Processor { 81 | return &Processor{ 82 | isLoggingFn: isLoggingFn, 83 | loggingfFn: loggingfFn, 84 | } 85 | } 86 | 87 | // Finalize is called after generating mocks to log metrics 88 | func (m *Processor) Finalize() { 89 | if m.isLoggingFn() { 90 | m.loggingfFn(metricsLogKey+" %s", serializeState(m.state)) 91 | } 92 | } 93 | 94 | // ASTPkgCacheHitsInc increments the ASTPkgCacheHits metric 95 | func (m *Processor) ASTPkgCacheHitsInc() { 96 | m.state.ASTPkgCacheHits++ 97 | } 98 | 99 | // ASTPkgCacheMissesInc increments the ASTPkgCacheMisses metric 100 | func (m *Processor) ASTPkgCacheMissesInc() { 101 | m.state.ASTPkgCacheMisses++ 102 | } 103 | 104 | // ASTTotalLoadTimeInc increments the ASTTotalLoadTime duration metric by the 105 | // d duration specified 106 | func (m *Processor) ASTTotalLoadTimeInc(d time.Duration) { 107 | m.state.ASTTotalLoadTime += d 108 | } 109 | 110 | // ASTTotalDecorationTimeInc increments the ASTTotalDecorationTime duration 111 | // metric by the d duration specified 112 | func (m *Processor) ASTTotalDecorationTimeInc(d time.Duration) { 113 | m.state.ASTTotalDecorationTime += d 114 | } 115 | 116 | // TotalProcessingTimeInc increments the TotalProcessingTime duration metric 117 | // by the d duration specified 118 | func (m *Processor) TotalProcessingTimeInc(d time.Duration) { 119 | m.state.TotalProcessingTime += d 120 | } 121 | 122 | func serializeState(state metricsState) []byte { 123 | state.ASTTotalLoadTimeStr = state.ASTTotalLoadTime.String() 124 | state.ASTTotalDecorationTimeStr = state.ASTTotalDecorationTime.String() 125 | state.TotalProcessingTimeStr = state.TotalProcessingTime.String() 126 | 127 | b, err := json.Marshal(state) 128 | if err != nil { 129 | logs.Panic("Could not marshal metrics to JSON", err) 130 | } 131 | 132 | buf := &bytes.Buffer{} 133 | err = json.Compact(buf, b) 134 | if err != nil { 135 | logs.Panic("Could not compact metrics", err) 136 | } 137 | 138 | return buf.Bytes() 139 | } 140 | -------------------------------------------------------------------------------- /metrics/metrics_test.go: -------------------------------------------------------------------------------- 1 | package metrics_test 2 | 3 | import ( 4 | "encoding/json" 5 | "testing" 6 | "time" 7 | 8 | "moqueries.org/runtime/moq" 9 | 10 | "moqueries.org/cli/metrics" 11 | ) 12 | 13 | func TestMetrics(t *testing.T) { 14 | var ( 15 | scene *moq.Scene 16 | isLoggingFnMoq *moqIsLoggingFn 17 | loggingfFnMoq *moqLoggingfFn 18 | 19 | m *metrics.Processor 20 | ) 21 | 22 | beforeEach := func(t *testing.T) { 23 | t.Helper() 24 | if scene != nil { 25 | t.Fatalf("afterEach not called") 26 | } 27 | scene = moq.NewScene(t) 28 | config := &moq.Config{Sequence: moq.SeqDefaultOn} 29 | isLoggingFnMoq = newMoqIsLoggingFn(scene, config) 30 | loggingfFnMoq = newMoqLoggingfFn(scene, config) 31 | 32 | m = metrics.NewMetrics(isLoggingFnMoq.mock(), loggingfFnMoq.mock()) 33 | } 34 | 35 | afterEach := func() { 36 | scene.AssertExpectationsMet() 37 | scene = nil 38 | } 39 | 40 | t.Run("no-op", func(t *testing.T) { 41 | // ASSEMBLE 42 | beforeEach(t) 43 | defer afterEach() 44 | 45 | m.ASTPkgCacheHitsInc() 46 | m.ASTPkgCacheMissesInc() 47 | m.ASTTotalLoadTimeInc(1234 * time.Millisecond) 48 | m.ASTTotalDecorationTimeInc(9999 * time.Millisecond) 49 | m.TotalProcessingTimeInc(4321 * time.Millisecond) 50 | 51 | isLoggingFnMoq.onCall().returnResults(false) 52 | 53 | // ACT 54 | m.Finalize() 55 | 56 | // ASSERT 57 | }) 58 | 59 | t.Run("single int increment", func(t *testing.T) { 60 | incFuncs := map[string]func(m *metrics.Processor){ 61 | "ast-pkg-cache-hits": func(m *metrics.Processor) { m.ASTPkgCacheHitsInc() }, 62 | "ast-pkg-cache-misses": func(m *metrics.Processor) { m.ASTPkgCacheMissesInc() }, 63 | } 64 | 65 | for name, incFunc := range incFuncs { 66 | t.Run(name, func(t *testing.T) { 67 | // ASSEMBLE 68 | beforeEach(t) 69 | defer afterEach() 70 | 71 | isLoggingFnMoq.onCall().returnResults(true) 72 | var data interface{} 73 | loggingfFnMoq.onCall("Type cache metrics %s", nil). 74 | any().args(). 75 | doReturnResults(func(_ string, args ...interface{}) { 76 | if len(args) != 1 { 77 | t.Fatalf("got %d args, wanted 1", len(args)) 78 | } 79 | data = args[0] 80 | }) 81 | 82 | // ACT 83 | incFunc(m) 84 | m.Finalize() 85 | 86 | // ASSERT 87 | buf, ok := data.([]byte) 88 | if !ok { 89 | t.Fatalf("got %#v, wanted []byte", data) 90 | } 91 | msi := map[string]interface{}{} 92 | err := json.Unmarshal(buf, &msi) 93 | if err != nil { 94 | t.Fatalf("got %#v, wanted no err", err) 95 | } 96 | 97 | found := false 98 | for k, v := range msi { 99 | var i float64 100 | if k == name { 101 | found = true 102 | i = 1 103 | } else { 104 | i = 0 105 | } 106 | switch typ := v.(type) { 107 | case float64: 108 | if typ != i { 109 | t.Errorf("got %f, wanted %f", typ, i) 110 | } 111 | case string: 112 | if typ != "0s" { 113 | t.Errorf("got %s, wanted 0s", typ) 114 | } 115 | default: 116 | t.Errorf("got %#v, wanted float64 or string", typ) 117 | } 118 | } 119 | 120 | if !found { 121 | t.Errorf("got not found, wanted found") 122 | } 123 | }) 124 | } 125 | }) 126 | 127 | t.Run("single duration increment", func(t *testing.T) { 128 | incFuncs := map[string]func(m *metrics.Processor, d time.Duration){ 129 | "ast-total-load-time": func(m *metrics.Processor, d time.Duration) { 130 | m.ASTTotalLoadTimeInc(d) 131 | }, 132 | "ast-total-decoration-time": func(m *metrics.Processor, d time.Duration) { 133 | m.ASTTotalDecorationTimeInc(d) 134 | }, 135 | "total-processing-time": func(m *metrics.Processor, d time.Duration) { 136 | m.TotalProcessingTimeInc(d) 137 | }, 138 | } 139 | 140 | for name, incFunc := range incFuncs { 141 | t.Run(name, func(t *testing.T) { 142 | // ASSEMBLE 143 | beforeEach(t) 144 | defer afterEach() 145 | 146 | isLoggingFnMoq.onCall().returnResults(true) 147 | var data interface{} 148 | loggingfFnMoq.onCall("Type cache metrics %s", nil). 149 | any().args(). 150 | doReturnResults(func(_ string, args ...interface{}) { 151 | if len(args) != 1 { 152 | t.Fatalf("got %d args, wanted 1", len(args)) 153 | } 154 | data = args[0] 155 | }) 156 | 157 | // ACT 158 | incFunc(m, 123*time.Millisecond) 159 | m.Finalize() 160 | 161 | // ASSERT 162 | buf, ok := data.([]byte) 163 | if !ok { 164 | t.Fatalf("got %#v, wanted []byte", data) 165 | } 166 | msi := map[string]interface{}{} 167 | err := json.Unmarshal(buf, &msi) 168 | if err != nil { 169 | t.Fatalf("got %#v, wanted no err", err) 170 | } 171 | 172 | found := false 173 | foundStr := false 174 | for k, v := range msi { 175 | if k == name { 176 | found = true 177 | lTime := float64(123 * time.Millisecond) 178 | if v != lTime { 179 | t.Errorf("got %#v, wanted %f", v, lTime) 180 | } 181 | } 182 | if k == name+"-str" { 183 | foundStr = true 184 | lTime := "123ms" 185 | if v != lTime { 186 | t.Errorf("got %#v, wanted %s", v, lTime) 187 | } 188 | } 189 | } 190 | if !found { 191 | t.Errorf("got not found, wanted found") 192 | } 193 | if !foundStr { 194 | t.Errorf("got not foundStr, wanted foundStr") 195 | } 196 | }) 197 | } 198 | }) 199 | } 200 | -------------------------------------------------------------------------------- /metrics/moq_isloggingfn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package metrics_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/metrics" 9 | "moqueries.org/runtime/impl" 10 | "moqueries.org/runtime/moq" 11 | ) 12 | 13 | // moqIsLoggingFn holds the state of a moq of the IsLoggingFn type 14 | type moqIsLoggingFn struct { 15 | moq *impl.Moq[ 16 | *moqIsLoggingFn_adaptor, 17 | moqIsLoggingFn_params, 18 | moqIsLoggingFn_paramsKey, 19 | moqIsLoggingFn_results, 20 | ] 21 | 22 | runtime moqIsLoggingFn_runtime 23 | } 24 | 25 | // moqIsLoggingFn_runtime holds runtime configuration for the IsLoggingFn type 26 | type moqIsLoggingFn_runtime struct { 27 | parameterIndexing moqIsLoggingFn_paramIndexing 28 | } 29 | 30 | // moqIsLoggingFn_adaptor adapts moqIsLoggingFn as needed by the runtime 31 | type moqIsLoggingFn_adaptor struct { 32 | moq *moqIsLoggingFn 33 | } 34 | 35 | // moqIsLoggingFn_params holds the params of the IsLoggingFn type 36 | type moqIsLoggingFn_params struct{} 37 | 38 | // moqIsLoggingFn_paramsKey holds the map key params of the IsLoggingFn type 39 | type moqIsLoggingFn_paramsKey struct { 40 | params struct{} 41 | hashes struct{} 42 | } 43 | 44 | // moqIsLoggingFn_results holds the results of the IsLoggingFn type 45 | type moqIsLoggingFn_results struct { 46 | result1 bool 47 | } 48 | 49 | // moqIsLoggingFn_paramIndexing holds the parameter indexing runtime 50 | // configuration for the IsLoggingFn type 51 | type moqIsLoggingFn_paramIndexing struct{} 52 | 53 | // moqIsLoggingFn_doFn defines the type of function needed when calling andDo 54 | // for the IsLoggingFn type 55 | type moqIsLoggingFn_doFn func() 56 | 57 | // moqIsLoggingFn_doReturnFn defines the type of function needed when calling 58 | // doReturnResults for the IsLoggingFn type 59 | type moqIsLoggingFn_doReturnFn func() bool 60 | 61 | // moqIsLoggingFn_recorder routes recorded function calls to the moqIsLoggingFn 62 | // moq 63 | type moqIsLoggingFn_recorder struct { 64 | recorder *impl.Recorder[ 65 | *moqIsLoggingFn_adaptor, 66 | moqIsLoggingFn_params, 67 | moqIsLoggingFn_paramsKey, 68 | moqIsLoggingFn_results, 69 | ] 70 | } 71 | 72 | // moqIsLoggingFn_anyParams isolates the any params functions of the 73 | // IsLoggingFn type 74 | type moqIsLoggingFn_anyParams struct { 75 | recorder *moqIsLoggingFn_recorder 76 | } 77 | 78 | // newMoqIsLoggingFn creates a new moq of the IsLoggingFn type 79 | func newMoqIsLoggingFn(scene *moq.Scene, config *moq.Config) *moqIsLoggingFn { 80 | adaptor1 := &moqIsLoggingFn_adaptor{} 81 | m := &moqIsLoggingFn{ 82 | moq: impl.NewMoq[ 83 | *moqIsLoggingFn_adaptor, 84 | moqIsLoggingFn_params, 85 | moqIsLoggingFn_paramsKey, 86 | moqIsLoggingFn_results, 87 | ](scene, adaptor1, config), 88 | 89 | runtime: moqIsLoggingFn_runtime{parameterIndexing: moqIsLoggingFn_paramIndexing{}}, 90 | } 91 | adaptor1.moq = m 92 | 93 | scene.AddMoq(m) 94 | return m 95 | } 96 | 97 | // mock returns the moq implementation of the IsLoggingFn type 98 | func (m *moqIsLoggingFn) mock() metrics.IsLoggingFn { 99 | return func() bool { 100 | m.moq.Scene.T.Helper() 101 | params := moqIsLoggingFn_params{} 102 | 103 | var result1 bool 104 | if result := m.moq.Function(params); result != nil { 105 | result1 = result.result1 106 | } 107 | return result1 108 | } 109 | } 110 | 111 | func (m *moqIsLoggingFn) onCall() *moqIsLoggingFn_recorder { 112 | return &moqIsLoggingFn_recorder{ 113 | recorder: m.moq.OnCall(moqIsLoggingFn_params{}), 114 | } 115 | } 116 | 117 | func (r *moqIsLoggingFn_recorder) any() *moqIsLoggingFn_anyParams { 118 | r.recorder.Moq.Scene.T.Helper() 119 | if !r.recorder.IsAnyPermitted(false) { 120 | return nil 121 | } 122 | return &moqIsLoggingFn_anyParams{recorder: r} 123 | } 124 | 125 | func (r *moqIsLoggingFn_recorder) seq() *moqIsLoggingFn_recorder { 126 | r.recorder.Moq.Scene.T.Helper() 127 | if !r.recorder.Seq(true, "seq", false) { 128 | return nil 129 | } 130 | return r 131 | } 132 | 133 | func (r *moqIsLoggingFn_recorder) noSeq() *moqIsLoggingFn_recorder { 134 | r.recorder.Moq.Scene.T.Helper() 135 | if !r.recorder.Seq(false, "noSeq", false) { 136 | return nil 137 | } 138 | return r 139 | } 140 | 141 | func (r *moqIsLoggingFn_recorder) returnResults(result1 bool) *moqIsLoggingFn_recorder { 142 | r.recorder.Moq.Scene.T.Helper() 143 | r.recorder.ReturnResults(moqIsLoggingFn_results{ 144 | result1: result1, 145 | }) 146 | return r 147 | } 148 | 149 | func (r *moqIsLoggingFn_recorder) andDo(fn moqIsLoggingFn_doFn) *moqIsLoggingFn_recorder { 150 | r.recorder.Moq.Scene.T.Helper() 151 | if !r.recorder.AndDo(func(params moqIsLoggingFn_params) { 152 | fn() 153 | }, false) { 154 | return nil 155 | } 156 | return r 157 | } 158 | 159 | func (r *moqIsLoggingFn_recorder) doReturnResults(fn moqIsLoggingFn_doReturnFn) *moqIsLoggingFn_recorder { 160 | r.recorder.Moq.Scene.T.Helper() 161 | r.recorder.DoReturnResults(func(params moqIsLoggingFn_params) *moqIsLoggingFn_results { 162 | result1 := fn() 163 | return &moqIsLoggingFn_results{ 164 | result1: result1, 165 | } 166 | }) 167 | return r 168 | } 169 | 170 | func (r *moqIsLoggingFn_recorder) repeat(repeaters ...moq.Repeater) *moqIsLoggingFn_recorder { 171 | r.recorder.Moq.Scene.T.Helper() 172 | if !r.recorder.Repeat(repeaters, false) { 173 | return nil 174 | } 175 | return r 176 | } 177 | 178 | func (*moqIsLoggingFn_adaptor) PrettyParams(params moqIsLoggingFn_params) string { 179 | return fmt.Sprintf("IsLoggingFn()") 180 | } 181 | 182 | func (a *moqIsLoggingFn_adaptor) ParamsKey(params moqIsLoggingFn_params, anyParams uint64) moqIsLoggingFn_paramsKey { 183 | a.moq.moq.Scene.T.Helper() 184 | return moqIsLoggingFn_paramsKey{ 185 | params: struct{}{}, 186 | hashes: struct{}{}, 187 | } 188 | } 189 | 190 | // Reset resets the state of the moq 191 | func (m *moqIsLoggingFn) Reset() { 192 | m.moq.Reset() 193 | } 194 | 195 | // AssertExpectationsMet asserts that all expectations have been met 196 | func (m *moqIsLoggingFn) AssertExpectationsMet() { 197 | m.moq.Scene.T.Helper() 198 | m.moq.AssertExpectationsMet() 199 | } 200 | -------------------------------------------------------------------------------- /metrics/moq_loggingffn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package metrics_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/metrics" 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // moqLoggingfFn holds the state of a moq of the LoggingfFn type 15 | type moqLoggingfFn struct { 16 | moq *impl.Moq[ 17 | *moqLoggingfFn_adaptor, 18 | moqLoggingfFn_params, 19 | moqLoggingfFn_paramsKey, 20 | moqLoggingfFn_results, 21 | ] 22 | 23 | runtime moqLoggingfFn_runtime 24 | } 25 | 26 | // moqLoggingfFn_runtime holds runtime configuration for the LoggingfFn type 27 | type moqLoggingfFn_runtime struct { 28 | parameterIndexing moqLoggingfFn_paramIndexing 29 | } 30 | 31 | // moqLoggingfFn_adaptor adapts moqLoggingfFn as needed by the runtime 32 | type moqLoggingfFn_adaptor struct { 33 | moq *moqLoggingfFn 34 | } 35 | 36 | // moqLoggingfFn_params holds the params of the LoggingfFn type 37 | type moqLoggingfFn_params struct { 38 | format string 39 | args []interface{} 40 | } 41 | 42 | // moqLoggingfFn_paramsKey holds the map key params of the LoggingfFn type 43 | type moqLoggingfFn_paramsKey struct { 44 | params struct{ format string } 45 | hashes struct { 46 | format hash.Hash 47 | args hash.Hash 48 | } 49 | } 50 | 51 | // moqLoggingfFn_results holds the results of the LoggingfFn type 52 | type moqLoggingfFn_results struct{} 53 | 54 | // moqLoggingfFn_paramIndexing holds the parameter indexing runtime 55 | // configuration for the LoggingfFn type 56 | type moqLoggingfFn_paramIndexing struct { 57 | format moq.ParamIndexing 58 | args moq.ParamIndexing 59 | } 60 | 61 | // moqLoggingfFn_doFn defines the type of function needed when calling andDo 62 | // for the LoggingfFn type 63 | type moqLoggingfFn_doFn func(format string, args ...interface{}) 64 | 65 | // moqLoggingfFn_doReturnFn defines the type of function needed when calling 66 | // doReturnResults for the LoggingfFn type 67 | type moqLoggingfFn_doReturnFn func(format string, args ...interface{}) 68 | 69 | // moqLoggingfFn_recorder routes recorded function calls to the moqLoggingfFn 70 | // moq 71 | type moqLoggingfFn_recorder struct { 72 | recorder *impl.Recorder[ 73 | *moqLoggingfFn_adaptor, 74 | moqLoggingfFn_params, 75 | moqLoggingfFn_paramsKey, 76 | moqLoggingfFn_results, 77 | ] 78 | } 79 | 80 | // moqLoggingfFn_anyParams isolates the any params functions of the LoggingfFn 81 | // type 82 | type moqLoggingfFn_anyParams struct { 83 | recorder *moqLoggingfFn_recorder 84 | } 85 | 86 | // newMoqLoggingfFn creates a new moq of the LoggingfFn type 87 | func newMoqLoggingfFn(scene *moq.Scene, config *moq.Config) *moqLoggingfFn { 88 | adaptor1 := &moqLoggingfFn_adaptor{} 89 | m := &moqLoggingfFn{ 90 | moq: impl.NewMoq[ 91 | *moqLoggingfFn_adaptor, 92 | moqLoggingfFn_params, 93 | moqLoggingfFn_paramsKey, 94 | moqLoggingfFn_results, 95 | ](scene, adaptor1, config), 96 | 97 | runtime: moqLoggingfFn_runtime{parameterIndexing: moqLoggingfFn_paramIndexing{ 98 | format: moq.ParamIndexByValue, 99 | args: moq.ParamIndexByHash, 100 | }}, 101 | } 102 | adaptor1.moq = m 103 | 104 | scene.AddMoq(m) 105 | return m 106 | } 107 | 108 | // mock returns the moq implementation of the LoggingfFn type 109 | func (m *moqLoggingfFn) mock() metrics.LoggingfFn { 110 | return func(format string, args ...interface{}) { 111 | m.moq.Scene.T.Helper() 112 | params := moqLoggingfFn_params{ 113 | format: format, 114 | args: args, 115 | } 116 | 117 | m.moq.Function(params) 118 | } 119 | } 120 | 121 | func (m *moqLoggingfFn) onCall(format string, args ...interface{}) *moqLoggingfFn_recorder { 122 | return &moqLoggingfFn_recorder{ 123 | recorder: m.moq.OnCall(moqLoggingfFn_params{ 124 | format: format, 125 | args: args, 126 | }), 127 | } 128 | } 129 | 130 | func (r *moqLoggingfFn_recorder) any() *moqLoggingfFn_anyParams { 131 | r.recorder.Moq.Scene.T.Helper() 132 | if !r.recorder.IsAnyPermitted(false) { 133 | return nil 134 | } 135 | return &moqLoggingfFn_anyParams{recorder: r} 136 | } 137 | 138 | func (a *moqLoggingfFn_anyParams) format() *moqLoggingfFn_recorder { 139 | a.recorder.recorder.AnyParam(1) 140 | return a.recorder 141 | } 142 | 143 | func (a *moqLoggingfFn_anyParams) args() *moqLoggingfFn_recorder { 144 | a.recorder.recorder.AnyParam(2) 145 | return a.recorder 146 | } 147 | 148 | func (r *moqLoggingfFn_recorder) seq() *moqLoggingfFn_recorder { 149 | r.recorder.Moq.Scene.T.Helper() 150 | if !r.recorder.Seq(true, "seq", false) { 151 | return nil 152 | } 153 | return r 154 | } 155 | 156 | func (r *moqLoggingfFn_recorder) noSeq() *moqLoggingfFn_recorder { 157 | r.recorder.Moq.Scene.T.Helper() 158 | if !r.recorder.Seq(false, "noSeq", false) { 159 | return nil 160 | } 161 | return r 162 | } 163 | 164 | func (r *moqLoggingfFn_recorder) returnResults() *moqLoggingfFn_recorder { 165 | r.recorder.Moq.Scene.T.Helper() 166 | r.recorder.ReturnResults(moqLoggingfFn_results{}) 167 | return r 168 | } 169 | 170 | func (r *moqLoggingfFn_recorder) andDo(fn moqLoggingfFn_doFn) *moqLoggingfFn_recorder { 171 | r.recorder.Moq.Scene.T.Helper() 172 | if !r.recorder.AndDo(func(params moqLoggingfFn_params) { 173 | fn(params.format, params.args...) 174 | }, false) { 175 | return nil 176 | } 177 | return r 178 | } 179 | 180 | func (r *moqLoggingfFn_recorder) doReturnResults(fn moqLoggingfFn_doReturnFn) *moqLoggingfFn_recorder { 181 | r.recorder.Moq.Scene.T.Helper() 182 | r.recorder.DoReturnResults(func(params moqLoggingfFn_params) *moqLoggingfFn_results { 183 | fn(params.format, params.args...) 184 | return &moqLoggingfFn_results{} 185 | }) 186 | return r 187 | } 188 | 189 | func (r *moqLoggingfFn_recorder) repeat(repeaters ...moq.Repeater) *moqLoggingfFn_recorder { 190 | r.recorder.Moq.Scene.T.Helper() 191 | if !r.recorder.Repeat(repeaters, false) { 192 | return nil 193 | } 194 | return r 195 | } 196 | 197 | func (*moqLoggingfFn_adaptor) PrettyParams(params moqLoggingfFn_params) string { 198 | return fmt.Sprintf("LoggingfFn(%#v, %#v)", params.format, params.args) 199 | } 200 | 201 | func (a *moqLoggingfFn_adaptor) ParamsKey(params moqLoggingfFn_params, anyParams uint64) moqLoggingfFn_paramsKey { 202 | a.moq.moq.Scene.T.Helper() 203 | formatUsed, formatUsedHash := impl.ParamKey( 204 | params.format, 1, a.moq.runtime.parameterIndexing.format, anyParams) 205 | argsUsedHash := impl.HashOnlyParamKey(a.moq.moq.Scene.T, 206 | params.args, "args", 2, a.moq.runtime.parameterIndexing.args, anyParams) 207 | return moqLoggingfFn_paramsKey{ 208 | params: struct{ format string }{ 209 | format: formatUsed, 210 | }, 211 | hashes: struct { 212 | format hash.Hash 213 | args hash.Hash 214 | }{ 215 | format: formatUsedHash, 216 | args: argsUsedHash, 217 | }, 218 | } 219 | } 220 | 221 | // Reset resets the state of the moq 222 | func (m *moqLoggingfFn) Reset() { 223 | m.moq.Reset() 224 | } 225 | 226 | // AssertExpectationsMet asserts that all expectations have been met 227 | func (m *moqLoggingfFn) AssertExpectationsMet() { 228 | m.moq.Scene.T.Helper() 229 | m.moq.AssertExpectationsMet() 230 | } 231 | -------------------------------------------------------------------------------- /metrics/moq_reader_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package metrics_test 4 | 5 | import ( 6 | "fmt" 7 | "io" 8 | 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // The following type assertion assures that io.Reader is mocked completely 15 | var _ io.Reader = (*moqReader_mock)(nil) 16 | 17 | // moqReader holds the state of a moq of the Reader type 18 | type moqReader struct { 19 | moq *moqReader_mock 20 | 21 | moq_Read *impl.Moq[ 22 | *moqReader_Read_adaptor, 23 | moqReader_Read_params, 24 | moqReader_Read_paramsKey, 25 | moqReader_Read_results, 26 | ] 27 | 28 | runtime moqReader_runtime 29 | } 30 | 31 | // moqReader_mock isolates the mock interface of the Reader type 32 | type moqReader_mock struct { 33 | moq *moqReader 34 | } 35 | 36 | // moqReader_recorder isolates the recorder interface of the Reader type 37 | type moqReader_recorder struct { 38 | moq *moqReader 39 | } 40 | 41 | // moqReader_runtime holds runtime configuration for the Reader type 42 | type moqReader_runtime struct { 43 | parameterIndexing struct { 44 | Read moqReader_Read_paramIndexing 45 | } 46 | } 47 | 48 | // moqReader_Read_adaptor adapts moqReader as needed by the runtime 49 | type moqReader_Read_adaptor struct { 50 | moq *moqReader 51 | } 52 | 53 | // moqReader_Read_params holds the params of the Reader type 54 | type moqReader_Read_params struct{ p []byte } 55 | 56 | // moqReader_Read_paramsKey holds the map key params of the Reader type 57 | type moqReader_Read_paramsKey struct { 58 | params struct{} 59 | hashes struct{ p hash.Hash } 60 | } 61 | 62 | // moqReader_Read_results holds the results of the Reader type 63 | type moqReader_Read_results struct { 64 | n int 65 | err error 66 | } 67 | 68 | // moqReader_Read_paramIndexing holds the parameter indexing runtime 69 | // configuration for the Reader type 70 | type moqReader_Read_paramIndexing struct { 71 | p moq.ParamIndexing 72 | } 73 | 74 | // moqReader_Read_doFn defines the type of function needed when calling andDo 75 | // for the Reader type 76 | type moqReader_Read_doFn func(p []byte) 77 | 78 | // moqReader_Read_doReturnFn defines the type of function needed when calling 79 | // doReturnResults for the Reader type 80 | type moqReader_Read_doReturnFn func(p []byte) (n int, err error) 81 | 82 | // moqReader_Read_recorder routes recorded function calls to the moqReader moq 83 | type moqReader_Read_recorder struct { 84 | recorder *impl.Recorder[ 85 | *moqReader_Read_adaptor, 86 | moqReader_Read_params, 87 | moqReader_Read_paramsKey, 88 | moqReader_Read_results, 89 | ] 90 | } 91 | 92 | // moqReader_Read_anyParams isolates the any params functions of the Reader 93 | // type 94 | type moqReader_Read_anyParams struct { 95 | recorder *moqReader_Read_recorder 96 | } 97 | 98 | // newMoqReader creates a new moq of the Reader type 99 | func newMoqReader(scene *moq.Scene, config *moq.Config) *moqReader { 100 | adaptor1 := &moqReader_Read_adaptor{} 101 | m := &moqReader{ 102 | moq: &moqReader_mock{}, 103 | 104 | moq_Read: impl.NewMoq[ 105 | *moqReader_Read_adaptor, 106 | moqReader_Read_params, 107 | moqReader_Read_paramsKey, 108 | moqReader_Read_results, 109 | ](scene, adaptor1, config), 110 | 111 | runtime: moqReader_runtime{parameterIndexing: struct { 112 | Read moqReader_Read_paramIndexing 113 | }{ 114 | Read: moqReader_Read_paramIndexing{ 115 | p: moq.ParamIndexByHash, 116 | }, 117 | }}, 118 | } 119 | m.moq.moq = m 120 | 121 | adaptor1.moq = m 122 | 123 | scene.AddMoq(m) 124 | return m 125 | } 126 | 127 | // mock returns the mock implementation of the Reader type 128 | func (m *moqReader) mock() *moqReader_mock { return m.moq } 129 | 130 | func (m *moqReader_mock) Read(p []byte) (int, error) { 131 | m.moq.moq_Read.Scene.T.Helper() 132 | params := moqReader_Read_params{ 133 | p: p, 134 | } 135 | 136 | var result1 int 137 | var result2 error 138 | if result := m.moq.moq_Read.Function(params); result != nil { 139 | result1 = result.n 140 | result2 = result.err 141 | } 142 | return result1, result2 143 | } 144 | 145 | // onCall returns the recorder implementation of the Reader type 146 | func (m *moqReader) onCall() *moqReader_recorder { 147 | return &moqReader_recorder{ 148 | moq: m, 149 | } 150 | } 151 | 152 | func (m *moqReader_recorder) Read(p []byte) *moqReader_Read_recorder { 153 | return &moqReader_Read_recorder{ 154 | recorder: m.moq.moq_Read.OnCall(moqReader_Read_params{ 155 | p: p, 156 | }), 157 | } 158 | } 159 | 160 | func (r *moqReader_Read_recorder) any() *moqReader_Read_anyParams { 161 | r.recorder.Moq.Scene.T.Helper() 162 | if !r.recorder.IsAnyPermitted(false) { 163 | return nil 164 | } 165 | return &moqReader_Read_anyParams{recorder: r} 166 | } 167 | 168 | func (a *moqReader_Read_anyParams) p() *moqReader_Read_recorder { 169 | a.recorder.recorder.AnyParam(1) 170 | return a.recorder 171 | } 172 | 173 | func (r *moqReader_Read_recorder) seq() *moqReader_Read_recorder { 174 | r.recorder.Moq.Scene.T.Helper() 175 | if !r.recorder.Seq(true, "seq", false) { 176 | return nil 177 | } 178 | return r 179 | } 180 | 181 | func (r *moqReader_Read_recorder) noSeq() *moqReader_Read_recorder { 182 | r.recorder.Moq.Scene.T.Helper() 183 | if !r.recorder.Seq(false, "noSeq", false) { 184 | return nil 185 | } 186 | return r 187 | } 188 | 189 | func (r *moqReader_Read_recorder) returnResults(n int, err error) *moqReader_Read_recorder { 190 | r.recorder.Moq.Scene.T.Helper() 191 | r.recorder.ReturnResults(moqReader_Read_results{ 192 | n: n, 193 | err: err, 194 | }) 195 | return r 196 | } 197 | 198 | func (r *moqReader_Read_recorder) andDo(fn moqReader_Read_doFn) *moqReader_Read_recorder { 199 | r.recorder.Moq.Scene.T.Helper() 200 | if !r.recorder.AndDo(func(params moqReader_Read_params) { 201 | fn(params.p) 202 | }, false) { 203 | return nil 204 | } 205 | return r 206 | } 207 | 208 | func (r *moqReader_Read_recorder) doReturnResults(fn moqReader_Read_doReturnFn) *moqReader_Read_recorder { 209 | r.recorder.Moq.Scene.T.Helper() 210 | r.recorder.DoReturnResults(func(params moqReader_Read_params) *moqReader_Read_results { 211 | n, err := fn(params.p) 212 | return &moqReader_Read_results{ 213 | n: n, 214 | err: err, 215 | } 216 | }) 217 | return r 218 | } 219 | 220 | func (r *moqReader_Read_recorder) repeat(repeaters ...moq.Repeater) *moqReader_Read_recorder { 221 | r.recorder.Moq.Scene.T.Helper() 222 | if !r.recorder.Repeat(repeaters, false) { 223 | return nil 224 | } 225 | return r 226 | } 227 | 228 | func (*moqReader_Read_adaptor) PrettyParams(params moqReader_Read_params) string { 229 | return fmt.Sprintf("Read(%#v)", params.p) 230 | } 231 | 232 | func (a *moqReader_Read_adaptor) ParamsKey(params moqReader_Read_params, anyParams uint64) moqReader_Read_paramsKey { 233 | a.moq.moq_Read.Scene.T.Helper() 234 | pUsedHash := impl.HashOnlyParamKey(a.moq.moq_Read.Scene.T, 235 | params.p, "p", 1, a.moq.runtime.parameterIndexing.Read.p, anyParams) 236 | return moqReader_Read_paramsKey{ 237 | params: struct{}{}, 238 | hashes: struct{ p hash.Hash }{ 239 | p: pUsedHash, 240 | }, 241 | } 242 | } 243 | 244 | // Reset resets the state of the moq 245 | func (m *moqReader) Reset() { 246 | m.moq_Read.Reset() 247 | } 248 | 249 | // AssertExpectationsMet asserts that all expectations have been met 250 | func (m *moqReader) AssertExpectationsMet() { 251 | m.moq_Read.Scene.T.Helper() 252 | m.moq_Read.AssertExpectationsMet() 253 | } 254 | -------------------------------------------------------------------------------- /metrics/summarizer.go: -------------------------------------------------------------------------------- 1 | package metrics 2 | 3 | import ( 4 | "bufio" 5 | "encoding/json" 6 | "fmt" 7 | "io" 8 | "strings" 9 | 10 | "moqueries.org/runtime/logs" 11 | ) 12 | 13 | //go:generate moqueries --import io Reader 14 | 15 | // Summarize reads a log file, selects multiple metrics from multiple generate 16 | // calls, and summarizes the metrics 17 | func Summarize(f io.Reader, loggingfFn LoggingfFn) error { 18 | var totals metricsState 19 | scanner := bufio.NewScanner(f) 20 | runs := 0 21 | for scanner.Scan() { 22 | txt := scanner.Text() 23 | idx := strings.Index(txt, metricsLogKey) 24 | if idx == -1 { 25 | continue 26 | } 27 | 28 | jTxt := txt[idx+len(metricsLogKey)+1:] 29 | var metrics metricsState 30 | err := json.Unmarshal([]byte(jTxt), &metrics) 31 | if err != nil { 32 | logs.Errorf("Ignoring line, error JSON parsing %s: %#v", jTxt, err) 33 | continue 34 | } 35 | totals = add(totals, metrics) 36 | runs++ 37 | } 38 | 39 | if err := scanner.Err(); err != nil { 40 | return fmt.Errorf("error scanning file: %w", err) 41 | } 42 | 43 | loggingfFn("Scanned %d runs, summary: %s", runs, serializeState(totals)) 44 | 45 | return nil 46 | } 47 | -------------------------------------------------------------------------------- /metrics/summarizer_test.go: -------------------------------------------------------------------------------- 1 | package metrics_test 2 | 3 | import ( 4 | "encoding/json" 5 | "io" 6 | "reflect" 7 | "testing" 8 | 9 | "moqueries.org/runtime/moq" 10 | 11 | "moqueries.org/cli/metrics" 12 | ) 13 | 14 | func TestSummarize(t *testing.T) { 15 | type testCase struct { 16 | lines []string 17 | expectedRuns int 18 | expectedState map[string]interface{} 19 | } 20 | testCases := map[string]testCase{ 21 | "nothing to summarize": { 22 | lines: []string{"Hello 1", "Hello 2", "Hello 3"}, 23 | expectedRuns: 0, 24 | expectedState: map[string]interface{}{ 25 | "ast-pkg-cache-hits": 0.0, 26 | "ast-pkg-cache-misses": 0.0, 27 | "ast-type-cache-hits": 0.0, 28 | "ast-type-cache-misses": 0.0, 29 | "ast-total-load-time": 0.0, 30 | "ast-total-load-time-str": "0s", 31 | "ast-total-decoration-time": 0.0, 32 | "ast-total-decoration-time-str": "0s", 33 | "total-processing-time": 0.0, 34 | "total-processing-time-str": "0s", 35 | }, 36 | }, 37 | "sparse": { 38 | lines: []string{ 39 | "Hello 1", 40 | "Type cache metrics bad line", 41 | "Hello 2", 42 | "DEBUG: 2022/06/16 14:39:10 Type cache metrics {\"ast-pkg-cache-hits\":1,\"ast-total-load-time\":145394479," + 43 | "\"ast-total-load-time-str\":\"999.999ms\"}", 44 | "Hello 3", 45 | "Type cache metrics {\"ast-pkg-cache-misses\":2,\"ast-total-load-time\":42,\"ast-total-load-time-str\":\"1234ms\"}", 46 | "hello 4", 47 | "Type cache metrics {\"ast-type-cache-misses\":1,\"total-processing-time\":394392," + 48 | "\"total-processing-time-str\":\"ignored\",\"ast-total-decoration-time\":12345}", 49 | "hello 5", 50 | "Type cache metrics {\"ast-type-cache-misses\":5,\"total-processing-time\":8374583}", 51 | "hello 6", 52 | "Type cache metrics {\"ast-type-cache-misses\":0,\"ast-total-decoration-time\":54321," + 53 | "\"ast-total-decoration-time-str\":\"ignored\"}", 54 | }, 55 | expectedRuns: 5, 56 | expectedState: map[string]interface{}{ 57 | "ast-pkg-cache-hits": 1.0, 58 | "ast-pkg-cache-misses": 2.0, 59 | "ast-type-cache-hits": 0.0, 60 | "ast-type-cache-misses": 6.0, 61 | "ast-total-load-time": 145394521.0, 62 | "ast-total-load-time-str": "145.394521ms", 63 | "ast-total-decoration-time": 66666.0, 64 | "ast-total-decoration-time-str": "66.666µs", 65 | "total-processing-time": 8768975.0, 66 | "total-processing-time-str": "8.768975ms", 67 | }, 68 | }, 69 | } 70 | 71 | for name, tc := range testCases { 72 | t.Run(name, func(t *testing.T) { 73 | // ASSEMBLE 74 | scene := moq.NewScene(t) 75 | config := &moq.Config{Sequence: moq.SeqDefaultOn} 76 | fMoq := newMoqReader(scene, config) 77 | loggingfFnMoq := newMoqLoggingfFn(scene, config) 78 | 79 | for _, line := range tc.lines { 80 | fn := func(line string) func(p []byte) (int, error) { 81 | return func(p []byte) (int, error) { 82 | line += "\n" 83 | copy(p, line) 84 | return len(line), nil 85 | } 86 | } 87 | fMoq.onCall().Read(nil).any().p(). 88 | doReturnResults(fn(line)) 89 | } 90 | fMoq.onCall().Read(nil).any().p(). 91 | doReturnResults(func(p []byte) (int, error) { 92 | return 0, io.EOF 93 | }) 94 | var actualArgs []interface{} 95 | loggingfFnMoq.onCall("Scanned %d runs, summary: %s", nil). 96 | any().args(). 97 | doReturnResults(func(format string, args ...interface{}) { 98 | actualArgs = args 99 | }) 100 | 101 | // ACT 102 | err := metrics.Summarize(fMoq.mock(), loggingfFnMoq.mock()) 103 | // ASSERT 104 | if err != nil { 105 | t.Errorf("got %#v, wanted no error", err) 106 | } 107 | if len(actualArgs) != 2 { 108 | t.Fatalf("got %d args, wanted 2", len(actualArgs)) 109 | } 110 | runs, ok := actualArgs[0].(int) 111 | if !ok { 112 | t.Errorf("got %#v, wanted int", runs) 113 | } 114 | if runs != tc.expectedRuns { 115 | t.Errorf("got %d runs, wanted %d", runs, tc.expectedRuns) 116 | } 117 | buf, ok := actualArgs[1].([]byte) 118 | if !ok { 119 | t.Errorf("got %#v, wanted []byte", buf) 120 | } 121 | msi := map[string]interface{}{} 122 | err = json.Unmarshal(buf, &msi) 123 | if err != nil { 124 | t.Fatalf("got %#v, wanted no err", err) 125 | } 126 | if !reflect.DeepEqual(msi, tc.expectedState) { 127 | t.Errorf("got %#v, wanted %#v", msi, tc.expectedState) 128 | } 129 | }) 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /moqueries/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "moqueries.org/cli/cmd" 4 | 5 | func main() { 6 | cmd.Execute() 7 | } 8 | -------------------------------------------------------------------------------- /pkg/generator.go: -------------------------------------------------------------------------------- 1 | // Package pkg is used to generate mocks for an entire package or module 2 | package pkg 3 | 4 | import ( 5 | "os" 6 | 7 | "golang.org/x/tools/go/packages" 8 | "moqueries.org/runtime/logs" 9 | 10 | "moqueries.org/cli/ast" 11 | "moqueries.org/cli/generator" 12 | "moqueries.org/cli/metrics" 13 | "moqueries.org/cli/pkg/internal" 14 | ) 15 | 16 | type PackageGenerateRequest = internal.PackageGenerateRequest 17 | 18 | // Generate generates mocks for several packages at once 19 | func Generate(req PackageGenerateRequest) error { 20 | m := metrics.NewMetrics(logs.IsDebug, logs.Debugf) 21 | cache := ast.NewCache(packages.Load, os.Stat, os.ReadFile, m) 22 | 23 | err := internal.Generate(cache, m, generator.GenerateWithTypeCache, req) 24 | if err != nil { 25 | return err 26 | } 27 | 28 | return nil 29 | } 30 | -------------------------------------------------------------------------------- /pkg/internal/generator.go: -------------------------------------------------------------------------------- 1 | // Package internal is the internal implementation for generate mocks for an 2 | // entire package or module 3 | package internal 4 | 5 | import ( 6 | "errors" 7 | "fmt" 8 | "path/filepath" 9 | "regexp" 10 | "strings" 11 | "time" 12 | 13 | "github.com/dave/dst" 14 | "moqueries.org/runtime/logs" 15 | 16 | "moqueries.org/cli/ast" 17 | "moqueries.org/cli/generator" 18 | "moqueries.org/cli/metrics" 19 | ) 20 | 21 | // ErrSkipTooManyPackageDirs is returned by Generate when skipPkgDirs requests 22 | // that more directories should be skipped than directories observed 23 | var ErrSkipTooManyPackageDirs = errors.New("skipping too many package dirs") 24 | 25 | //go:generate moqueries TypeCache 26 | 27 | // TypeCache defines the interface to the Cache type 28 | type TypeCache interface { 29 | LoadPackage(pkgPattern string) error 30 | MockableTypes(onlyExported bool) []dst.Ident 31 | generator.TypeCache 32 | } 33 | 34 | //go:generate moqueries GenerateWithTypeCacheFn 35 | 36 | // GenerateWithTypeCacheFn is the function type for generator.GenerateWithTypeCache 37 | type GenerateWithTypeCacheFn func(cache generator.TypeCache, req generator.GenerateRequest) error 38 | 39 | type PackageGenerateRequest struct { 40 | DestinationDir string 41 | SkipPkgDirs int 42 | PkgPatterns []string 43 | ExcludePkgPathRegex string 44 | } 45 | 46 | // Generate generates mocks for several packages at once 47 | func Generate( 48 | cache TypeCache, 49 | mProcessor metrics.Metrics, 50 | genFn GenerateWithTypeCacheFn, 51 | req PackageGenerateRequest, 52 | ) error { 53 | start := time.Now() 54 | 55 | var reg *regexp.Regexp 56 | if req.ExcludePkgPathRegex != "" { 57 | var err error 58 | reg, err = regexp.Compile(req.ExcludePkgPathRegex) 59 | if err != nil { 60 | return fmt.Errorf("%w: could not compile exclude package regex \"%s\"", 61 | err, req.ExcludePkgPathRegex) 62 | } 63 | } 64 | 65 | for _, pkgPattern := range req.PkgPatterns { 66 | err := cache.LoadPackage(pkgPattern) 67 | if err != nil { 68 | return err 69 | } 70 | } 71 | 72 | typs := cache.MockableTypes(true) 73 | logs.Debugf("Mocking %d types", len(typs)) 74 | 75 | for _, id := range typs { 76 | if reg != nil && reg.MatchString(id.Path) { 77 | logs.Warnf("Skipping %s because of package exclusion %s", 78 | id.String(), req.ExcludePkgPathRegex) 79 | continue 80 | } 81 | 82 | pkgDestDir, err := skipDirs(id.Path, req.SkipPkgDirs) 83 | if err != nil { 84 | return err 85 | } 86 | pkgDestDir = filepath.Join(req.DestinationDir, pkgDestDir) 87 | logs.Debugf("Package generating,"+ 88 | " destination-dir: %s,"+ 89 | " type: %s", 90 | pkgDestDir, id.String()) 91 | 92 | err = genFn(cache, generator.GenerateRequest{ 93 | Types: []string{id.Name}, 94 | Export: true, 95 | DestinationDir: pkgDestDir, 96 | Import: id.Path, 97 | ExcludeNonExported: true, 98 | }) 99 | if err != nil { 100 | if errors.Is(err, generator.ErrNonExported) || errors.Is(err, ast.ErrMixedRecvTypes) { 101 | logs.Debugf("Skipping generation of mock for %s, %s", 102 | id.String(), err.Error()) 103 | continue 104 | } 105 | return err 106 | } 107 | } 108 | 109 | mProcessor.TotalProcessingTimeInc(time.Since(start)) 110 | mProcessor.Finalize() 111 | 112 | return nil 113 | } 114 | 115 | func skipDirs(pkgPath string, skipDirs int) (string, error) { 116 | orig := pkgPath 117 | for n := 0; n < skipDirs; n++ { 118 | if pkgPath == "." { 119 | return "", fmt.Errorf("%w: skipping %d directories on package %s", 120 | ErrSkipTooManyPackageDirs, skipDirs, orig) 121 | } 122 | idx := strings.Index(pkgPath, string(filepath.Separator)) 123 | if idx == -1 { 124 | pkgPath = "." 125 | } else { 126 | pkgPath = pkgPath[idx+1:] 127 | } 128 | } 129 | return pkgPath, nil 130 | } 131 | -------------------------------------------------------------------------------- /pkg/internal/moq_generatewithtypecachefn_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package internal_test 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/generator" 9 | "moqueries.org/cli/pkg/internal" 10 | "moqueries.org/runtime/hash" 11 | "moqueries.org/runtime/impl" 12 | "moqueries.org/runtime/moq" 13 | ) 14 | 15 | // moqGenerateWithTypeCacheFn holds the state of a moq of the 16 | // GenerateWithTypeCacheFn type 17 | type moqGenerateWithTypeCacheFn struct { 18 | moq *impl.Moq[ 19 | *moqGenerateWithTypeCacheFn_adaptor, 20 | moqGenerateWithTypeCacheFn_params, 21 | moqGenerateWithTypeCacheFn_paramsKey, 22 | moqGenerateWithTypeCacheFn_results, 23 | ] 24 | 25 | runtime moqGenerateWithTypeCacheFn_runtime 26 | } 27 | 28 | // moqGenerateWithTypeCacheFn_runtime holds runtime configuration for the 29 | // GenerateWithTypeCacheFn type 30 | type moqGenerateWithTypeCacheFn_runtime struct { 31 | parameterIndexing moqGenerateWithTypeCacheFn_paramIndexing 32 | } 33 | 34 | // moqGenerateWithTypeCacheFn_adaptor adapts moqGenerateWithTypeCacheFn as 35 | // needed by the runtime 36 | type moqGenerateWithTypeCacheFn_adaptor struct { 37 | moq *moqGenerateWithTypeCacheFn 38 | } 39 | 40 | // moqGenerateWithTypeCacheFn_params holds the params of the 41 | // GenerateWithTypeCacheFn type 42 | type moqGenerateWithTypeCacheFn_params struct { 43 | cache generator.TypeCache 44 | req generator.GenerateRequest 45 | } 46 | 47 | // moqGenerateWithTypeCacheFn_paramsKey holds the map key params of the 48 | // GenerateWithTypeCacheFn type 49 | type moqGenerateWithTypeCacheFn_paramsKey struct { 50 | params struct{ cache generator.TypeCache } 51 | hashes struct { 52 | cache hash.Hash 53 | req hash.Hash 54 | } 55 | } 56 | 57 | // moqGenerateWithTypeCacheFn_results holds the results of the 58 | // GenerateWithTypeCacheFn type 59 | type moqGenerateWithTypeCacheFn_results struct { 60 | result1 error 61 | } 62 | 63 | // moqGenerateWithTypeCacheFn_paramIndexing holds the parameter indexing 64 | // runtime configuration for the GenerateWithTypeCacheFn type 65 | type moqGenerateWithTypeCacheFn_paramIndexing struct { 66 | cache moq.ParamIndexing 67 | req moq.ParamIndexing 68 | } 69 | 70 | // moqGenerateWithTypeCacheFn_doFn defines the type of function needed when 71 | // calling andDo for the GenerateWithTypeCacheFn type 72 | type moqGenerateWithTypeCacheFn_doFn func(cache generator.TypeCache, req generator.GenerateRequest) 73 | 74 | // moqGenerateWithTypeCacheFn_doReturnFn defines the type of function needed 75 | // when calling doReturnResults for the GenerateWithTypeCacheFn type 76 | type moqGenerateWithTypeCacheFn_doReturnFn func(cache generator.TypeCache, req generator.GenerateRequest) error 77 | 78 | // moqGenerateWithTypeCacheFn_recorder routes recorded function calls to the 79 | // moqGenerateWithTypeCacheFn moq 80 | type moqGenerateWithTypeCacheFn_recorder struct { 81 | recorder *impl.Recorder[ 82 | *moqGenerateWithTypeCacheFn_adaptor, 83 | moqGenerateWithTypeCacheFn_params, 84 | moqGenerateWithTypeCacheFn_paramsKey, 85 | moqGenerateWithTypeCacheFn_results, 86 | ] 87 | } 88 | 89 | // moqGenerateWithTypeCacheFn_anyParams isolates the any params functions of 90 | // the GenerateWithTypeCacheFn type 91 | type moqGenerateWithTypeCacheFn_anyParams struct { 92 | recorder *moqGenerateWithTypeCacheFn_recorder 93 | } 94 | 95 | // newMoqGenerateWithTypeCacheFn creates a new moq of the 96 | // GenerateWithTypeCacheFn type 97 | func newMoqGenerateWithTypeCacheFn(scene *moq.Scene, config *moq.Config) *moqGenerateWithTypeCacheFn { 98 | adaptor1 := &moqGenerateWithTypeCacheFn_adaptor{} 99 | m := &moqGenerateWithTypeCacheFn{ 100 | moq: impl.NewMoq[ 101 | *moqGenerateWithTypeCacheFn_adaptor, 102 | moqGenerateWithTypeCacheFn_params, 103 | moqGenerateWithTypeCacheFn_paramsKey, 104 | moqGenerateWithTypeCacheFn_results, 105 | ](scene, adaptor1, config), 106 | 107 | runtime: moqGenerateWithTypeCacheFn_runtime{parameterIndexing: moqGenerateWithTypeCacheFn_paramIndexing{ 108 | cache: moq.ParamIndexByHash, 109 | req: moq.ParamIndexByHash, 110 | }}, 111 | } 112 | adaptor1.moq = m 113 | 114 | scene.AddMoq(m) 115 | return m 116 | } 117 | 118 | // mock returns the moq implementation of the GenerateWithTypeCacheFn type 119 | func (m *moqGenerateWithTypeCacheFn) mock() internal.GenerateWithTypeCacheFn { 120 | return func(cache generator.TypeCache, req generator.GenerateRequest) error { 121 | m.moq.Scene.T.Helper() 122 | params := moqGenerateWithTypeCacheFn_params{ 123 | cache: cache, 124 | req: req, 125 | } 126 | 127 | var result1 error 128 | if result := m.moq.Function(params); result != nil { 129 | result1 = result.result1 130 | } 131 | return result1 132 | } 133 | } 134 | 135 | func (m *moqGenerateWithTypeCacheFn) onCall(cache generator.TypeCache, req generator.GenerateRequest) *moqGenerateWithTypeCacheFn_recorder { 136 | return &moqGenerateWithTypeCacheFn_recorder{ 137 | recorder: m.moq.OnCall(moqGenerateWithTypeCacheFn_params{ 138 | cache: cache, 139 | req: req, 140 | }), 141 | } 142 | } 143 | 144 | func (r *moqGenerateWithTypeCacheFn_recorder) any() *moqGenerateWithTypeCacheFn_anyParams { 145 | r.recorder.Moq.Scene.T.Helper() 146 | if !r.recorder.IsAnyPermitted(false) { 147 | return nil 148 | } 149 | return &moqGenerateWithTypeCacheFn_anyParams{recorder: r} 150 | } 151 | 152 | func (a *moqGenerateWithTypeCacheFn_anyParams) cache() *moqGenerateWithTypeCacheFn_recorder { 153 | a.recorder.recorder.AnyParam(1) 154 | return a.recorder 155 | } 156 | 157 | func (a *moqGenerateWithTypeCacheFn_anyParams) req() *moqGenerateWithTypeCacheFn_recorder { 158 | a.recorder.recorder.AnyParam(2) 159 | return a.recorder 160 | } 161 | 162 | func (r *moqGenerateWithTypeCacheFn_recorder) seq() *moqGenerateWithTypeCacheFn_recorder { 163 | r.recorder.Moq.Scene.T.Helper() 164 | if !r.recorder.Seq(true, "seq", false) { 165 | return nil 166 | } 167 | return r 168 | } 169 | 170 | func (r *moqGenerateWithTypeCacheFn_recorder) noSeq() *moqGenerateWithTypeCacheFn_recorder { 171 | r.recorder.Moq.Scene.T.Helper() 172 | if !r.recorder.Seq(false, "noSeq", false) { 173 | return nil 174 | } 175 | return r 176 | } 177 | 178 | func (r *moqGenerateWithTypeCacheFn_recorder) returnResults(result1 error) *moqGenerateWithTypeCacheFn_recorder { 179 | r.recorder.Moq.Scene.T.Helper() 180 | r.recorder.ReturnResults(moqGenerateWithTypeCacheFn_results{ 181 | result1: result1, 182 | }) 183 | return r 184 | } 185 | 186 | func (r *moqGenerateWithTypeCacheFn_recorder) andDo(fn moqGenerateWithTypeCacheFn_doFn) *moqGenerateWithTypeCacheFn_recorder { 187 | r.recorder.Moq.Scene.T.Helper() 188 | if !r.recorder.AndDo(func(params moqGenerateWithTypeCacheFn_params) { 189 | fn(params.cache, params.req) 190 | }, false) { 191 | return nil 192 | } 193 | return r 194 | } 195 | 196 | func (r *moqGenerateWithTypeCacheFn_recorder) doReturnResults(fn moqGenerateWithTypeCacheFn_doReturnFn) *moqGenerateWithTypeCacheFn_recorder { 197 | r.recorder.Moq.Scene.T.Helper() 198 | r.recorder.DoReturnResults(func(params moqGenerateWithTypeCacheFn_params) *moqGenerateWithTypeCacheFn_results { 199 | result1 := fn(params.cache, params.req) 200 | return &moqGenerateWithTypeCacheFn_results{ 201 | result1: result1, 202 | } 203 | }) 204 | return r 205 | } 206 | 207 | func (r *moqGenerateWithTypeCacheFn_recorder) repeat(repeaters ...moq.Repeater) *moqGenerateWithTypeCacheFn_recorder { 208 | r.recorder.Moq.Scene.T.Helper() 209 | if !r.recorder.Repeat(repeaters, false) { 210 | return nil 211 | } 212 | return r 213 | } 214 | 215 | func (*moqGenerateWithTypeCacheFn_adaptor) PrettyParams(params moqGenerateWithTypeCacheFn_params) string { 216 | return fmt.Sprintf("GenerateWithTypeCacheFn(%#v, %#v)", params.cache, params.req) 217 | } 218 | 219 | func (a *moqGenerateWithTypeCacheFn_adaptor) ParamsKey(params moqGenerateWithTypeCacheFn_params, anyParams uint64) moqGenerateWithTypeCacheFn_paramsKey { 220 | a.moq.moq.Scene.T.Helper() 221 | cacheUsed, cacheUsedHash := impl.ParamKey( 222 | params.cache, 1, a.moq.runtime.parameterIndexing.cache, anyParams) 223 | reqUsedHash := impl.HashOnlyParamKey(a.moq.moq.Scene.T, 224 | params.req, "req", 2, a.moq.runtime.parameterIndexing.req, anyParams) 225 | return moqGenerateWithTypeCacheFn_paramsKey{ 226 | params: struct{ cache generator.TypeCache }{ 227 | cache: cacheUsed, 228 | }, 229 | hashes: struct { 230 | cache hash.Hash 231 | req hash.Hash 232 | }{ 233 | cache: cacheUsedHash, 234 | req: reqUsedHash, 235 | }, 236 | } 237 | } 238 | 239 | // Reset resets the state of the moq 240 | func (m *moqGenerateWithTypeCacheFn) Reset() { 241 | m.moq.Reset() 242 | } 243 | 244 | // AssertExpectationsMet asserts that all expectations have been met 245 | func (m *moqGenerateWithTypeCacheFn) AssertExpectationsMet() { 246 | m.moq.Scene.T.Helper() 247 | m.moq.AssertExpectationsMet() 248 | } 249 | -------------------------------------------------------------------------------- /pkg/testmoqs/pkgout/moq_dosomethingwithparam.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package pkgout 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/cli/pkg/testmoqs" 9 | "moqueries.org/runtime/hash" 10 | "moqueries.org/runtime/impl" 11 | "moqueries.org/runtime/moq" 12 | ) 13 | 14 | // DoSomethingWithParam_genType is the fabricated implementation type of this 15 | // mock (emitted when mocking functions directly and not from a function type) 16 | type DoSomethingWithParam_genType func(testmoqs.Param) 17 | 18 | // MoqDoSomethingWithParam_genType holds the state of a moq of the 19 | // DoSomethingWithParam_genType type 20 | type MoqDoSomethingWithParam_genType struct { 21 | Moq *impl.Moq[ 22 | *MoqDoSomethingWithParam_genType_adaptor, 23 | MoqDoSomethingWithParam_genType_params, 24 | MoqDoSomethingWithParam_genType_paramsKey, 25 | MoqDoSomethingWithParam_genType_results, 26 | ] 27 | 28 | Runtime MoqDoSomethingWithParam_genType_runtime 29 | } 30 | 31 | // MoqDoSomethingWithParam_genType_runtime holds runtime configuration for the 32 | // DoSomethingWithParam_genType type 33 | type MoqDoSomethingWithParam_genType_runtime struct { 34 | ParameterIndexing MoqDoSomethingWithParam_genType_paramIndexing 35 | } 36 | 37 | // MoqDoSomethingWithParam_genType_adaptor adapts 38 | // MoqDoSomethingWithParam_genType as needed by the runtime 39 | type MoqDoSomethingWithParam_genType_adaptor struct { 40 | Moq *MoqDoSomethingWithParam_genType 41 | } 42 | 43 | // MoqDoSomethingWithParam_genType_params holds the params of the 44 | // DoSomethingWithParam_genType type 45 | type MoqDoSomethingWithParam_genType_params struct{ Param1 testmoqs.Param } 46 | 47 | // MoqDoSomethingWithParam_genType_paramsKey holds the map key params of the 48 | // DoSomethingWithParam_genType type 49 | type MoqDoSomethingWithParam_genType_paramsKey struct { 50 | Params struct{ Param1 testmoqs.Param } 51 | Hashes struct{ Param1 hash.Hash } 52 | } 53 | 54 | // MoqDoSomethingWithParam_genType_results holds the results of the 55 | // DoSomethingWithParam_genType type 56 | type MoqDoSomethingWithParam_genType_results struct{} 57 | 58 | // MoqDoSomethingWithParam_genType_paramIndexing holds the parameter indexing 59 | // runtime configuration for the DoSomethingWithParam_genType type 60 | type MoqDoSomethingWithParam_genType_paramIndexing struct { 61 | Param1 moq.ParamIndexing 62 | } 63 | 64 | // MoqDoSomethingWithParam_genType_doFn defines the type of function needed 65 | // when calling AndDo for the DoSomethingWithParam_genType type 66 | type MoqDoSomethingWithParam_genType_doFn func(testmoqs.Param) 67 | 68 | // MoqDoSomethingWithParam_genType_doReturnFn defines the type of function 69 | // needed when calling DoReturnResults for the DoSomethingWithParam_genType 70 | // type 71 | type MoqDoSomethingWithParam_genType_doReturnFn func(testmoqs.Param) 72 | 73 | // MoqDoSomethingWithParam_genType_recorder routes recorded function calls to 74 | // the MoqDoSomethingWithParam_genType moq 75 | type MoqDoSomethingWithParam_genType_recorder struct { 76 | Recorder *impl.Recorder[ 77 | *MoqDoSomethingWithParam_genType_adaptor, 78 | MoqDoSomethingWithParam_genType_params, 79 | MoqDoSomethingWithParam_genType_paramsKey, 80 | MoqDoSomethingWithParam_genType_results, 81 | ] 82 | } 83 | 84 | // MoqDoSomethingWithParam_genType_anyParams isolates the any params functions 85 | // of the DoSomethingWithParam_genType type 86 | type MoqDoSomethingWithParam_genType_anyParams struct { 87 | Recorder *MoqDoSomethingWithParam_genType_recorder 88 | } 89 | 90 | // NewMoqDoSomethingWithParam_genType creates a new moq of the 91 | // DoSomethingWithParam_genType type 92 | func NewMoqDoSomethingWithParam_genType(scene *moq.Scene, config *moq.Config) *MoqDoSomethingWithParam_genType { 93 | adaptor1 := &MoqDoSomethingWithParam_genType_adaptor{} 94 | m := &MoqDoSomethingWithParam_genType{ 95 | Moq: impl.NewMoq[ 96 | *MoqDoSomethingWithParam_genType_adaptor, 97 | MoqDoSomethingWithParam_genType_params, 98 | MoqDoSomethingWithParam_genType_paramsKey, 99 | MoqDoSomethingWithParam_genType_results, 100 | ](scene, adaptor1, config), 101 | 102 | Runtime: MoqDoSomethingWithParam_genType_runtime{ParameterIndexing: MoqDoSomethingWithParam_genType_paramIndexing{ 103 | Param1: moq.ParamIndexByValue, 104 | }}, 105 | } 106 | adaptor1.Moq = m 107 | 108 | scene.AddMoq(m) 109 | return m 110 | } 111 | 112 | // Mock returns the moq implementation of the DoSomethingWithParam_genType type 113 | func (m *MoqDoSomethingWithParam_genType) Mock() DoSomethingWithParam_genType { 114 | return func(param1 testmoqs.Param) { 115 | m.Moq.Scene.T.Helper() 116 | params := MoqDoSomethingWithParam_genType_params{ 117 | Param1: param1, 118 | } 119 | 120 | m.Moq.Function(params) 121 | } 122 | } 123 | 124 | func (m *MoqDoSomethingWithParam_genType) OnCall(param1 testmoqs.Param) *MoqDoSomethingWithParam_genType_recorder { 125 | return &MoqDoSomethingWithParam_genType_recorder{ 126 | Recorder: m.Moq.OnCall(MoqDoSomethingWithParam_genType_params{ 127 | Param1: param1, 128 | }), 129 | } 130 | } 131 | 132 | func (r *MoqDoSomethingWithParam_genType_recorder) Any() *MoqDoSomethingWithParam_genType_anyParams { 133 | r.Recorder.Moq.Scene.T.Helper() 134 | if !r.Recorder.IsAnyPermitted(true) { 135 | return nil 136 | } 137 | return &MoqDoSomethingWithParam_genType_anyParams{Recorder: r} 138 | } 139 | 140 | func (a *MoqDoSomethingWithParam_genType_anyParams) Param1() *MoqDoSomethingWithParam_genType_recorder { 141 | a.Recorder.Recorder.AnyParam(1) 142 | return a.Recorder 143 | } 144 | 145 | func (r *MoqDoSomethingWithParam_genType_recorder) Seq() *MoqDoSomethingWithParam_genType_recorder { 146 | r.Recorder.Moq.Scene.T.Helper() 147 | if !r.Recorder.Seq(true, "Seq", true) { 148 | return nil 149 | } 150 | return r 151 | } 152 | 153 | func (r *MoqDoSomethingWithParam_genType_recorder) NoSeq() *MoqDoSomethingWithParam_genType_recorder { 154 | r.Recorder.Moq.Scene.T.Helper() 155 | if !r.Recorder.Seq(false, "NoSeq", true) { 156 | return nil 157 | } 158 | return r 159 | } 160 | 161 | func (r *MoqDoSomethingWithParam_genType_recorder) ReturnResults() *MoqDoSomethingWithParam_genType_recorder { 162 | r.Recorder.Moq.Scene.T.Helper() 163 | r.Recorder.ReturnResults(MoqDoSomethingWithParam_genType_results{}) 164 | return r 165 | } 166 | 167 | func (r *MoqDoSomethingWithParam_genType_recorder) AndDo(fn MoqDoSomethingWithParam_genType_doFn) *MoqDoSomethingWithParam_genType_recorder { 168 | r.Recorder.Moq.Scene.T.Helper() 169 | if !r.Recorder.AndDo(func(params MoqDoSomethingWithParam_genType_params) { 170 | fn(params.Param1) 171 | }, true) { 172 | return nil 173 | } 174 | return r 175 | } 176 | 177 | func (r *MoqDoSomethingWithParam_genType_recorder) DoReturnResults(fn MoqDoSomethingWithParam_genType_doReturnFn) *MoqDoSomethingWithParam_genType_recorder { 178 | r.Recorder.Moq.Scene.T.Helper() 179 | r.Recorder.DoReturnResults(func(params MoqDoSomethingWithParam_genType_params) *MoqDoSomethingWithParam_genType_results { 180 | fn(params.Param1) 181 | return &MoqDoSomethingWithParam_genType_results{} 182 | }) 183 | return r 184 | } 185 | 186 | func (r *MoqDoSomethingWithParam_genType_recorder) Repeat(repeaters ...moq.Repeater) *MoqDoSomethingWithParam_genType_recorder { 187 | r.Recorder.Moq.Scene.T.Helper() 188 | if !r.Recorder.Repeat(repeaters, true) { 189 | return nil 190 | } 191 | return r 192 | } 193 | 194 | func (*MoqDoSomethingWithParam_genType_adaptor) PrettyParams(params MoqDoSomethingWithParam_genType_params) string { 195 | return fmt.Sprintf("DoSomethingWithParam_genType(%#v)", params.Param1) 196 | } 197 | 198 | func (a *MoqDoSomethingWithParam_genType_adaptor) ParamsKey(params MoqDoSomethingWithParam_genType_params, anyParams uint64) MoqDoSomethingWithParam_genType_paramsKey { 199 | a.Moq.Moq.Scene.T.Helper() 200 | param1Used, param1UsedHash := impl.ParamKey( 201 | params.Param1, 1, a.Moq.Runtime.ParameterIndexing.Param1, anyParams) 202 | return MoqDoSomethingWithParam_genType_paramsKey{ 203 | Params: struct{ Param1 testmoqs.Param }{ 204 | Param1: param1Used, 205 | }, 206 | Hashes: struct{ Param1 hash.Hash }{ 207 | Param1: param1UsedHash, 208 | }, 209 | } 210 | } 211 | 212 | // Reset resets the state of the moq 213 | func (m *MoqDoSomethingWithParam_genType) Reset() { 214 | m.Moq.Reset() 215 | } 216 | 217 | // AssertExpectationsMet asserts that all expectations have been met 218 | func (m *MoqDoSomethingWithParam_genType) AssertExpectationsMet() { 219 | m.Moq.Scene.T.Helper() 220 | m.Moq.AssertExpectationsMet() 221 | } 222 | -------------------------------------------------------------------------------- /pkg/testmoqs/pkgout/moq_standalonefunc.go: -------------------------------------------------------------------------------- 1 | // Code generated by Moqueries - https://moqueries.org - DO NOT EDIT. 2 | 3 | package pkgout 4 | 5 | import ( 6 | "fmt" 7 | 8 | "moqueries.org/runtime/hash" 9 | "moqueries.org/runtime/impl" 10 | "moqueries.org/runtime/moq" 11 | ) 12 | 13 | // StandaloneFunc_genType is the fabricated implementation type of this mock 14 | // (emitted when mocking functions directly and not from a function type) 15 | type StandaloneFunc_genType func(_ string, bParam bool) (string, error) 16 | 17 | // MoqStandaloneFunc_genType holds the state of a moq of the 18 | // StandaloneFunc_genType type 19 | type MoqStandaloneFunc_genType struct { 20 | Moq *impl.Moq[ 21 | *MoqStandaloneFunc_genType_adaptor, 22 | MoqStandaloneFunc_genType_params, 23 | MoqStandaloneFunc_genType_paramsKey, 24 | MoqStandaloneFunc_genType_results, 25 | ] 26 | 27 | Runtime MoqStandaloneFunc_genType_runtime 28 | } 29 | 30 | // MoqStandaloneFunc_genType_runtime holds runtime configuration for the 31 | // StandaloneFunc_genType type 32 | type MoqStandaloneFunc_genType_runtime struct { 33 | ParameterIndexing MoqStandaloneFunc_genType_paramIndexing 34 | } 35 | 36 | // MoqStandaloneFunc_genType_adaptor adapts MoqStandaloneFunc_genType as needed 37 | // by the runtime 38 | type MoqStandaloneFunc_genType_adaptor struct { 39 | Moq *MoqStandaloneFunc_genType 40 | } 41 | 42 | // MoqStandaloneFunc_genType_params holds the params of the 43 | // StandaloneFunc_genType type 44 | type MoqStandaloneFunc_genType_params struct { 45 | Param1 string 46 | BParam bool 47 | } 48 | 49 | // MoqStandaloneFunc_genType_paramsKey holds the map key params of the 50 | // StandaloneFunc_genType type 51 | type MoqStandaloneFunc_genType_paramsKey struct { 52 | Params struct { 53 | Param1 string 54 | BParam bool 55 | } 56 | Hashes struct { 57 | Param1 hash.Hash 58 | BParam hash.Hash 59 | } 60 | } 61 | 62 | // MoqStandaloneFunc_genType_results holds the results of the 63 | // StandaloneFunc_genType type 64 | type MoqStandaloneFunc_genType_results struct { 65 | Result1 string 66 | Result2 error 67 | } 68 | 69 | // MoqStandaloneFunc_genType_paramIndexing holds the parameter indexing runtime 70 | // configuration for the StandaloneFunc_genType type 71 | type MoqStandaloneFunc_genType_paramIndexing struct { 72 | Param1 moq.ParamIndexing 73 | BParam moq.ParamIndexing 74 | } 75 | 76 | // MoqStandaloneFunc_genType_doFn defines the type of function needed when 77 | // calling AndDo for the StandaloneFunc_genType type 78 | type MoqStandaloneFunc_genType_doFn func(_ string, bParam bool) 79 | 80 | // MoqStandaloneFunc_genType_doReturnFn defines the type of function needed 81 | // when calling DoReturnResults for the StandaloneFunc_genType type 82 | type MoqStandaloneFunc_genType_doReturnFn func(_ string, bParam bool) (string, error) 83 | 84 | // MoqStandaloneFunc_genType_recorder routes recorded function calls to the 85 | // MoqStandaloneFunc_genType moq 86 | type MoqStandaloneFunc_genType_recorder struct { 87 | Recorder *impl.Recorder[ 88 | *MoqStandaloneFunc_genType_adaptor, 89 | MoqStandaloneFunc_genType_params, 90 | MoqStandaloneFunc_genType_paramsKey, 91 | MoqStandaloneFunc_genType_results, 92 | ] 93 | } 94 | 95 | // MoqStandaloneFunc_genType_anyParams isolates the any params functions of the 96 | // StandaloneFunc_genType type 97 | type MoqStandaloneFunc_genType_anyParams struct { 98 | Recorder *MoqStandaloneFunc_genType_recorder 99 | } 100 | 101 | // NewMoqStandaloneFunc_genType creates a new moq of the StandaloneFunc_genType 102 | // type 103 | func NewMoqStandaloneFunc_genType(scene *moq.Scene, config *moq.Config) *MoqStandaloneFunc_genType { 104 | adaptor1 := &MoqStandaloneFunc_genType_adaptor{} 105 | m := &MoqStandaloneFunc_genType{ 106 | Moq: impl.NewMoq[ 107 | *MoqStandaloneFunc_genType_adaptor, 108 | MoqStandaloneFunc_genType_params, 109 | MoqStandaloneFunc_genType_paramsKey, 110 | MoqStandaloneFunc_genType_results, 111 | ](scene, adaptor1, config), 112 | 113 | Runtime: MoqStandaloneFunc_genType_runtime{ParameterIndexing: MoqStandaloneFunc_genType_paramIndexing{ 114 | Param1: moq.ParamIndexByValue, 115 | BParam: moq.ParamIndexByValue, 116 | }}, 117 | } 118 | adaptor1.Moq = m 119 | 120 | scene.AddMoq(m) 121 | return m 122 | } 123 | 124 | // Mock returns the moq implementation of the StandaloneFunc_genType type 125 | func (m *MoqStandaloneFunc_genType) Mock() StandaloneFunc_genType { 126 | return func(param1 string, bParam bool) (string, error) { 127 | m.Moq.Scene.T.Helper() 128 | params := MoqStandaloneFunc_genType_params{ 129 | Param1: param1, 130 | BParam: bParam, 131 | } 132 | 133 | var result1 string 134 | var result2 error 135 | if result := m.Moq.Function(params); result != nil { 136 | result1 = result.Result1 137 | result2 = result.Result2 138 | } 139 | return result1, result2 140 | } 141 | } 142 | 143 | func (m *MoqStandaloneFunc_genType) OnCall(param1 string, bParam bool) *MoqStandaloneFunc_genType_recorder { 144 | return &MoqStandaloneFunc_genType_recorder{ 145 | Recorder: m.Moq.OnCall(MoqStandaloneFunc_genType_params{ 146 | Param1: param1, 147 | BParam: bParam, 148 | }), 149 | } 150 | } 151 | 152 | func (r *MoqStandaloneFunc_genType_recorder) Any() *MoqStandaloneFunc_genType_anyParams { 153 | r.Recorder.Moq.Scene.T.Helper() 154 | if !r.Recorder.IsAnyPermitted(true) { 155 | return nil 156 | } 157 | return &MoqStandaloneFunc_genType_anyParams{Recorder: r} 158 | } 159 | 160 | func (a *MoqStandaloneFunc_genType_anyParams) Param1() *MoqStandaloneFunc_genType_recorder { 161 | a.Recorder.Recorder.AnyParam(1) 162 | return a.Recorder 163 | } 164 | 165 | func (a *MoqStandaloneFunc_genType_anyParams) BParam() *MoqStandaloneFunc_genType_recorder { 166 | a.Recorder.Recorder.AnyParam(2) 167 | return a.Recorder 168 | } 169 | 170 | func (r *MoqStandaloneFunc_genType_recorder) Seq() *MoqStandaloneFunc_genType_recorder { 171 | r.Recorder.Moq.Scene.T.Helper() 172 | if !r.Recorder.Seq(true, "Seq", true) { 173 | return nil 174 | } 175 | return r 176 | } 177 | 178 | func (r *MoqStandaloneFunc_genType_recorder) NoSeq() *MoqStandaloneFunc_genType_recorder { 179 | r.Recorder.Moq.Scene.T.Helper() 180 | if !r.Recorder.Seq(false, "NoSeq", true) { 181 | return nil 182 | } 183 | return r 184 | } 185 | 186 | func (r *MoqStandaloneFunc_genType_recorder) ReturnResults(result1 string, result2 error) *MoqStandaloneFunc_genType_recorder { 187 | r.Recorder.Moq.Scene.T.Helper() 188 | r.Recorder.ReturnResults(MoqStandaloneFunc_genType_results{ 189 | Result1: result1, 190 | Result2: result2, 191 | }) 192 | return r 193 | } 194 | 195 | func (r *MoqStandaloneFunc_genType_recorder) AndDo(fn MoqStandaloneFunc_genType_doFn) *MoqStandaloneFunc_genType_recorder { 196 | r.Recorder.Moq.Scene.T.Helper() 197 | if !r.Recorder.AndDo(func(params MoqStandaloneFunc_genType_params) { 198 | fn(params.Param1, params.BParam) 199 | }, true) { 200 | return nil 201 | } 202 | return r 203 | } 204 | 205 | func (r *MoqStandaloneFunc_genType_recorder) DoReturnResults(fn MoqStandaloneFunc_genType_doReturnFn) *MoqStandaloneFunc_genType_recorder { 206 | r.Recorder.Moq.Scene.T.Helper() 207 | r.Recorder.DoReturnResults(func(params MoqStandaloneFunc_genType_params) *MoqStandaloneFunc_genType_results { 208 | result1, result2 := fn(params.Param1, params.BParam) 209 | return &MoqStandaloneFunc_genType_results{ 210 | Result1: result1, 211 | Result2: result2, 212 | } 213 | }) 214 | return r 215 | } 216 | 217 | func (r *MoqStandaloneFunc_genType_recorder) Repeat(repeaters ...moq.Repeater) *MoqStandaloneFunc_genType_recorder { 218 | r.Recorder.Moq.Scene.T.Helper() 219 | if !r.Recorder.Repeat(repeaters, true) { 220 | return nil 221 | } 222 | return r 223 | } 224 | 225 | func (*MoqStandaloneFunc_genType_adaptor) PrettyParams(params MoqStandaloneFunc_genType_params) string { 226 | return fmt.Sprintf("StandaloneFunc_genType(%#v, %#v)", params.Param1, params.BParam) 227 | } 228 | 229 | func (a *MoqStandaloneFunc_genType_adaptor) ParamsKey(params MoqStandaloneFunc_genType_params, anyParams uint64) MoqStandaloneFunc_genType_paramsKey { 230 | a.Moq.Moq.Scene.T.Helper() 231 | param1Used, param1UsedHash := impl.ParamKey( 232 | params.Param1, 1, a.Moq.Runtime.ParameterIndexing.Param1, anyParams) 233 | bParamUsed, bParamUsedHash := impl.ParamKey( 234 | params.BParam, 2, a.Moq.Runtime.ParameterIndexing.BParam, anyParams) 235 | return MoqStandaloneFunc_genType_paramsKey{ 236 | Params: struct { 237 | Param1 string 238 | BParam bool 239 | }{ 240 | Param1: param1Used, 241 | BParam: bParamUsed, 242 | }, 243 | Hashes: struct { 244 | Param1 hash.Hash 245 | BParam hash.Hash 246 | }{ 247 | Param1: param1UsedHash, 248 | BParam: bParamUsedHash, 249 | }, 250 | } 251 | } 252 | 253 | // Reset resets the state of the moq 254 | func (m *MoqStandaloneFunc_genType) Reset() { 255 | m.Moq.Reset() 256 | } 257 | 258 | // AssertExpectationsMet asserts that all expectations have been met 259 | func (m *MoqStandaloneFunc_genType) AssertExpectationsMet() { 260 | m.Moq.Scene.T.Helper() 261 | m.Moq.AssertExpectationsMet() 262 | } 263 | -------------------------------------------------------------------------------- /pkg/testmoqs/types.go: -------------------------------------------------------------------------------- 1 | // Package testmoqs contains multiple test mocks and adaptors for use in unit 2 | // testing 3 | package testmoqs 4 | 5 | // StandaloneFunc is used to test that standalone functions can be mocked 6 | func StandaloneFunc(_ string, bParam bool) (string, error) { 7 | return "", nil 8 | } 9 | 10 | type PassByValueSimple struct{} 11 | 12 | func (PassByValueSimple) Usual(string, bool) (string, error) { 13 | return "", nil 14 | } 15 | 16 | type PassByRefSimple struct{} 17 | 18 | func (*PassByRefSimple) Usual(string, bool) (string, error) { 19 | return "", nil 20 | } 21 | 22 | // Reduced creates a mock with an embedded reduced interface with only the 23 | // exported methods mocked when using the ExcludeNonExported flag 24 | type Reduced interface { 25 | Usual(sParam string, bParam bool) (sResult string, err error) 26 | notSoUsual() 27 | ReallyUnusualParams(struct{ a string }) 28 | ReallyUnusualResults() struct{ a string } 29 | } 30 | 31 | type Generic[T any, V any] struct{} 32 | 33 | func (g *Generic[T, V]) DoSomethingPtr() {} 34 | 35 | func (g *Generic[X, Y]) DoSomethingElsePtr() {} 36 | 37 | func (g Generic[T, V]) DoSomething() {} 38 | 39 | func (g Generic[X, Y]) DoSomethingElse() {} 40 | 41 | type Param struct{} 42 | 43 | func DoSomethingWithParam(Param) {} 44 | -------------------------------------------------------------------------------- /pkg/testmoqs/types_test.go: -------------------------------------------------------------------------------- 1 | package testmoqs_test 2 | 3 | import ( 4 | "os" 5 | "strings" 6 | "testing" 7 | 8 | "moqueries.org/cli/pkg" 9 | ) 10 | 11 | func TestPackageGeneration(t *testing.T) { 12 | // ASSEMBLE 13 | entries, err := os.ReadDir(".") 14 | if err != nil { 15 | t.Fatalf("got %#v, want no error", err) 16 | } 17 | for _, e := range entries { 18 | if !strings.HasPrefix(e.Name(), "moq_") || !strings.HasSuffix(e.Name(), ".go") { 19 | continue 20 | } 21 | 22 | err = os.Remove(e.Name()) 23 | if err != nil { 24 | t.Errorf("got %#v, want no error", err) 25 | } 26 | } 27 | expectedFiles := []string{ 28 | "moq_dosomethingwithparam.go", 29 | "moq_passbyrefsimple.go", 30 | "moq_passbyvaluesimple.go", 31 | "moq_reduced.go", 32 | "moq_standalonefunc.go", 33 | } 34 | 35 | // ACT 36 | err = pkg.Generate(pkg.PackageGenerateRequest{ 37 | DestinationDir: "pkgout", 38 | SkipPkgDirs: 4, 39 | PkgPatterns: []string{"moqueries.org/cli/pkg/testmoqs"}, 40 | }) 41 | // ASSERT 42 | if err != nil { 43 | t.Fatalf("got %#v, want no error", err) 44 | } 45 | 46 | entries, err = os.ReadDir("pkgout") 47 | if err != nil { 48 | t.Fatalf("got %#v, want no error", err) 49 | } 50 | moqs := map[string]struct{}{} 51 | for _, e := range entries { 52 | if !strings.HasPrefix(e.Name(), "moq_") || !strings.HasSuffix(e.Name(), ".go") { 53 | continue 54 | } 55 | 56 | moqs[e.Name()] = struct{}{} 57 | } 58 | if len(moqs) != len(expectedFiles) { 59 | t.Errorf("got %#v mocks, want length %d", moqs, len(expectedFiles)) 60 | } 61 | for _, f := range expectedFiles { 62 | if _, ok := moqs[f]; !ok { 63 | t.Errorf("got %#v, want to contain %s", moqs, f) 64 | } 65 | } 66 | // Minimal testing here just to make sure the right types were found (full 67 | // mock testing in the generator package) 68 | } 69 | --------------------------------------------------------------------------------