@@ -18,21 +18,23 @@ def output
18
18
19
19
context "generating for app" do
20
20
it "generates an operation" do
21
- subject . call ( name : "add_book" )
21
+ subject . call ( name : "operations/ add_book" )
22
22
23
23
operation_file = <<~EXPECTED
24
24
# frozen_string_literal: true
25
25
26
26
module Test
27
- class AddBook < Test::Operation
28
- def call
27
+ module Operations
28
+ class AddBook < Test::Operation
29
+ def call
30
+ end
29
31
end
30
32
end
31
33
end
32
34
EXPECTED
33
35
34
- expect ( fs . read ( "app/add_book.rb" ) ) . to eq ( operation_file )
35
- expect ( output ) . to include ( "Created app/add_book.rb" )
36
+ expect ( fs . read ( "app/operations/ add_book.rb" ) ) . to eq ( operation_file )
37
+ expect ( output ) . to include ( "Created app/operations/ add_book.rb" )
36
38
end
37
39
38
40
it "generates a operation in a deep namespace with default separator" do
@@ -57,7 +59,7 @@ def call
57
59
expect ( output ) . to include ( "Created app/admin/books/add.rb" )
58
60
end
59
61
60
- it "generates an operation in a deep namespace with slash separators " do
62
+ it "generates an operation in a deep namespace with slash separator " do
61
63
subject . call ( name : "admin/books/add" )
62
64
63
65
operation_file = <<~EXPECTED
@@ -78,26 +80,78 @@ def call
78
80
expect ( fs . read ( "app/admin/books/add.rb" ) ) . to eq ( operation_file )
79
81
expect ( output ) . to include ( "Created app/admin/books/add.rb" )
80
82
end
83
+
84
+ it "outputs an error if trying to generate an operation without a separator" do
85
+ expect {
86
+ subject . call ( name : "add_book" )
87
+ } . to raise_error ( Hanami ::CLI ::NameNeedsNamespaceError ) . with_message (
88
+ "Failed to create operation `add_book'. " \
89
+ "This would create the operation directly in the `app/' folder. " \
90
+ "Instead, you should provide a namespace for the folder where this operation will live. " \
91
+ "NOTE: We recommend giving it a name that's specific to your domain, " \
92
+ "but you can also use `operations.add_book' in the meantime if you're unsure."
93
+ )
94
+ expect ( fs . exist? ( "app/add_book.rb" ) ) . to be ( false )
95
+ end
81
96
end
82
97
83
98
context "generating for a slice" do
84
- it "generates a operation in a top-level namespace" do
99
+ it "generates a operation" do
100
+ fs . mkdir ( "slices/main" )
101
+ subject . call ( name : "operations.add_book" , slice : "main" )
102
+
103
+ operation_file = <<~EXPECTED
104
+ # frozen_string_literal: true
105
+
106
+ module Main
107
+ module Operations
108
+ class AddBook < Main::Operation
109
+ def call
110
+ end
111
+ end
112
+ end
113
+ end
114
+ EXPECTED
115
+
116
+ expect ( fs . read ( "slices/main/operations/add_book.rb" ) ) . to eq ( operation_file )
117
+ expect ( output ) . to include ( "Created slices/main/operations/add_book.rb" )
118
+ end
119
+
120
+ it "generates a operation in a deep namespace with default separator" do
85
121
fs . mkdir ( "slices/main" )
86
- subject . call ( name : "add_book " , slice : "main" )
122
+ subject . call ( name : "admin.books.add " , slice : "main" )
87
123
88
124
operation_file = <<~EXPECTED
89
125
# frozen_string_literal: true
90
126
91
127
module Main
92
- class AddBook < Main::Operation
93
- def call
128
+ module Admin
129
+ module Books
130
+ class Add < Main::Operation
131
+ def call
132
+ end
133
+ end
94
134
end
95
135
end
96
136
end
97
137
EXPECTED
98
138
99
- expect ( fs . read ( "slices/main/add_book.rb" ) ) . to eq ( operation_file )
100
- expect ( output ) . to include ( "Created slices/main/add_book.rb" )
139
+ expect ( fs . read ( "slices/main/admin/books/add.rb" ) ) . to eq ( operation_file )
140
+ expect ( output ) . to include ( "Created slices/main/admin/books/add.rb" )
141
+ end
142
+
143
+ it "outputs an error if trying to generate an operation without a separator" do
144
+ fs . mkdir ( "slices/main" )
145
+ expect {
146
+ subject . call ( name : "add_book" , slice : "main" )
147
+ } . to raise_error ( Hanami ::CLI ::NameNeedsNamespaceError ) . with_message (
148
+ "Failed to create operation `add_book'. " \
149
+ "This would create the operation directly in the `slices/main/' folder. " \
150
+ "Instead, you should provide a namespace for the folder where this operation will live. " \
151
+ "NOTE: We recommend giving it a name that's specific to your domain, " \
152
+ "but you can also use `operations.add_book' in the meantime if you're unsure."
153
+ )
154
+ expect ( fs . exist? ( "app/add_book.rb" ) ) . to be ( false )
101
155
end
102
156
end
103
157
end
0 commit comments