This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest-template.stencil
106 lines (97 loc) · 4.3 KB
/
test-template.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{#
Run:
sourcery --sources Sources/TwitterAPIKit/Some/Resource/Requests/ --sources Sources/TwitterAPIKit/TwitterAPIRequest.swift --templates test-template.stencil --output Some/Test/Dir
and edit generated test code.
#}
{% macro argLine param %}
{% if param.typeName.unwrappedTypeName == "String" %}
{{param.name}}: "_{{param.name[0]}}_"{{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName == "Int" %}
{{param.name}}: 1{{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName == "Bool" %}
{{param.name}}: true{{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName == "Date" %}
{{param.name}}: Date(timeIntervalSince1970: 0){{ ',' if not forloop.last }}
{% elif param.type.kind == "enum" %}
{{param.name}}: .{{param.type.cases.first.name}}{{ ',' if not forloop.last }}
{% elif param.typeName.isArray %}
{% if param.typeName.name == "[String]" %}
{{param.name}}: ["a", "b"]{{ ',' if not forloop.last }}
{% else %}
{{param.name}}: []{{ ',' if not forloop.last }}
{% endif %}
{% elif param.typeName.unwrappedTypeName|hasPrefix: "Set<" %}
{{param.name}}: []{{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName|hasPrefix: "TwitterUserIdentifierV1" %}
{{param.name}}: .userID("uid"){{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName|hasPrefix: "TwitterUsersIdentifierV1" %}
{{param.name}}: .userIDs(["uid1", "uid2"]){{ ',' if not forloop.last }}
{% elif param.typeName.unwrappedTypeName|hasPrefix: "TwitterListIdentifierV1" %}
{{param.name}}: .listID("lid"){{ ',' if not forloop.last }}
{% elif param.typeName.isOptional %}
{{param.name}}: {{param.defaultValue}}{{ ',' if not forloop.last }}
{% else %}
{{param.name}}: .init(){{ ',' if not forloop.last }} // Unkown
{% endif %}
{% endmacro %}
{% for type in types.implementing.TwitterAPIRequest %}
// sourcery:file:{{ type.name }}Tests.swift
import TwitterAPIKit
import XCTest
class {{ type.name }}Tests: XCTestCase {
override func setUpWithError() throws {
}
override func tearDownWithError() throws {
}
func test() throws {
let req = {{type.name}}(
{% for param in type.methods[0].parameters %}
{% call argLine param %}
{% endfor %}
)
{% if type.name | hasPrefix: "Post" %}
XCTAssertEqual(req.method, .post)
{% elif type.name | hasPrefix: "Delete" %}
XCTAssertEqual(req.method, .delete)
{% elif type.name | hasPrefix: "Put" %}
XCTAssertEqual(req.method, .put)
{% else %}
XCTAssertEqual(req.method, .get)
{% endif %}
XCTAssertEqual(req.baseURLType, .api)
XCTAssertEqual(req.path, "")
XCTAssertEqual(req.bodyContentType, .wwwFormUrlEncoded)
AssertEqualAnyDict(
req.parameters,
[
{% for variable in type.variables|instance|stored%}
{% if variable.name | hasSuffix: "Fields" %}
"{{variable.name|camelToSnakeCase|replace: "_", "."}}": ""{{ ',' if not forloop.last }}
{% elif variable.typeName.unwrappedTypeName == "Int" %}
"{{variable.name|camelToSnakeCase}}": 1{{ ',' if not forloop.last }}
{% elif variable.typeName.unwrappedTypeName == "String" %}
"{{variable.name|camelToSnakeCase}}": "_{{variable.name[0]}}_"{{ ',' if not forloop.last }}
{% else %}
"{{variable.name|camelToSnakeCase}}": "{{variable.typeName.unwrappedTypeName}}"{{ ',' if not forloop.last }}
{%endif%}
{% endfor %}
]
)
}
{% map type.methods[0].parameters into result %}{{maploop.item.defaultValue}}{% endmap %}
{% if result|join %}
func testDefaultArg() throws {
let req = {{type.name}}(
{% for param in type.methods[0].parameters where param.defaultValue == nil %}
{% call argLine param %}
{% endfor %}
)
AssertEqualAnyDict(
req.parameters,
[:]
)
}
{% endif %}
}
// sourcery:end
{% endfor %}