{"id":13413862,"url":"https://github.com/percolate/charlatan","last_synced_at":"2025-04-07T15:06:35.716Z","repository":{"id":57480972,"uuid":"106053422","full_name":"percolate/charlatan","owner":"percolate","description":"Go Interface Mocking Tool","archived":false,"fork":false,"pushed_at":"2023-11-19T22:35:01.000Z","size":2885,"stargazers_count":201,"open_issues_count":2,"forks_count":10,"subscribers_count":51,"default_branch":"master","last_synced_at":"2024-07-31T20:52:56.764Z","etag":null,"topics":["code-generation","go","interfaces","mock","testing","testing-tools"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/percolate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-06T21:55:14.000Z","updated_at":"2024-05-31T05:21:20.000Z","dependencies_parsed_at":"2024-06-18T16:50:41.926Z","dependency_job_id":"ac3940e5-3398-4689-a79f-e4b61e444aab","html_url":"https://github.com/percolate/charlatan","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/percolate%2Fcharlatan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/percolate%2Fcharlatan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/percolate%2Fcharlatan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/percolate%2Fcharlatan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/percolate","download_url":"https://codeload.github.com/percolate/charlatan/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247675597,"owners_count":20977376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","go","interfaces","mock","testing","testing-tools"],"created_at":"2024-07-30T20:01:51.440Z","updated_at":"2025-04-07T15:06:35.675Z","avatar_url":"https://github.com/percolate.png","language":"Go","funding_links":[],"categories":["Testing","Testing Frameworks","测试","Template Engines","测试相关","测试相关`测试库和测试数据集生成库`"],"sub_categories":["HTTP Clients","Testing Frameworks","HTTP客户端","交流","查询语"],"readme":"# Charlatan\n\n[![Circle CI](https://circleci.com/gh/percolate/charlatan.svg?style=svg)](https://circleci.com/gh/percolate/charlatan)\n[![codecov.io](https://codecov.io/github/percolate/charlatan/coverage.svg?branch=master)](https://codecov.io/github/percolate/charlatan?branch=master)\n[![BSD](https://img.shields.io/badge/license-BSD-blue.svg)](https://github.com/percolate/charlatan/blob/master/LICENSE)\n[![Go Report Card](https://goreportcard.com/badge/github.com/percolate/charlatan)](https://goreportcard.com/report/github.com/percolate/charlatan)\n\nPercolate's Go Interface Mocking Tool.  Please read our [introductory blog post](https://medium.com/percolate-engineering/introducing-charlatan-df9b5d3d3107).\n\n## Installation\n\n    go get github.com/percolate/charlatan\n\n## Usage\n\n```\n  charlatan [options] \u003cinterface\u003e ...\n  charlatan -h | --help\n\nOptions:\n\n  -dir string\n        input package directory [default: current package directory]\n  -file value\n        name of input file, may be repeated, ignored if -dir is present\n  -output string\n        output file path [default: ./charlatan.go]\n  -package string\n        output package name [default: \"\u003ccurrent package\u003e\"]\n```\n\nIf you would like the mock implementations to live in the same package\nas the interface definition then use the simplest invocation as a\ndirective:\n\n    //go:generate charlatan Interface\n\nor from the command line:\n\n    charlatan -file=path/to/file.go Interface\n\nYou can chose the output path using `-output`, which must include the\nname of the generated source file.  Any intermediate directories in the\npath that don't exist will be created.  The package used in the\ngenerated file's `package` directive can be set using `-package`.\n\n## Example\n\nGiven the following interface:\n\n```go\npackage example\n\n//go:generate charlatan Service\n\ntype Service interface {\n\tQuery(filter *QueryFilter) ([]*Thing, error)\n\tFetch(id string) (*Thing, error)\n}\n```\n\nRunning `go generate ...` for the above package/file should produce\nthe file `charlatan.go`:\n\n```go\npackage example\n\ntype QueryInvocation struct {\n\tParameters struct {\n\t\tFilter *QueryFilter\n\t}\n\tResults struct {\n\t\tIdent1 []*Thing\n\t\tIdent2 error\n\t}\n}\n\ntype FetchInvocation struct {\n\tParameters struct {\n\t\tId string\n\t}\n\tResults struct {\n\t\tIdent3 *Thing\n\t\tIdent4 error\n\t}\n}\n\ntype FakeService struct {\n\tQueryHook func(*QueryFilter) ([]*Thing, error)\n\tFetchHook func(string) (*Thing, error)\n\n\tQueryCalls []*QueryInvocation\n\tFetchCalls []*FetchInvocation\n}\n\nfunc (f *FakeService) Query(filter *QueryFilter) (id1 []*Thing, id2 error) {\n\tinvocation := new(QueryInvocation)\n\tinvocation.Parameters.Filter = filter\n\n\tid1, id2 := f.QueryHook(filter)\n\n\tinvocation.Results.Ident1 = id1\n\tinvocation.Results.Ident2 = id2\n\n\treturn\n}\n\n// other generated code elided ...\n```\n\nNow you can use this in your tests by injecting the `FakeService`\nimplementation instead of the actual one.  A `FakeService` can be used\nanywhere a `Service` interface is expected.\n\n```go\nfunc TestUsingService(t *testing.T) {\n\t// expectedThings := ...\n\t// expectedCriteria := ...\n\tsvc := \u0026example.FakeService{\n\t\tQueryHook: func(filter *QueryFilter) ([]*Thing, error) {\n\t\t\tif filter.Criteria != expectedCriteria {\n\t\t\t\tt.Errorf(\"expected criteria value: %v, have: %v\", filter.Criteria, expectedCriteria)\n\t\t\t\treturn nil, errors.New(\"unexpected criteria\")\n\t\t\t}\n\t\t\treturn expectedThings, nil\n\t\t},\n\t}\n\n\t// use the `svc` instance in the code under test ...\n\n\t// assert state of FakeService ...\n\tsvc.AssertQueryCalledOnce(t)\n}\n```\n\nCreate anonymous function implementations for only those interface\nmethods that should be called in the code under test.  This will force\na panic if any unexpected calls are made to the mock implementation.\n\nThe generated code has `godoc` formatted comments explaining the use\nof the mock and its methods.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpercolate%2Fcharlatan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpercolate%2Fcharlatan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpercolate%2Fcharlatan/lists"}