In VS Code, what’s an easy way of converting plain text test names into code?

I’m not sure if there’s a proper word for this. I want to be able to write a list of tests in plain text, then convert them into a list of empty test functions.

For example, if I write

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>a user can view all documents
documents can be created
documents can edited
</code>
<code>a user can view all documents documents can be created documents can edited </code>
a user can view all documents

documents can be created

documents can edited

I would like to then be able to do something, like highlight the lines and run an extension, which would transform those lines into

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/** @test */
public function a_user_can_view_all_documents()
{
$this->markTestIncomplete('This test has not been implemented yet.');
}
/** @test */
public function documents_can_be_created()
{
$this->markTestIncomplete('This test has not been implemented yet.');
}
/** @test */
public function documents_can_edited()
{
$this->markTestIncomplete('This test has not been implemented yet.');
}
</code>
<code>/** @test */ public function a_user_can_view_all_documents() { $this->markTestIncomplete('This test has not been implemented yet.'); } /** @test */ public function documents_can_be_created() { $this->markTestIncomplete('This test has not been implemented yet.'); } /** @test */ public function documents_can_edited() { $this->markTestIncomplete('This test has not been implemented yet.'); } </code>
/** @test */
public function a_user_can_view_all_documents()
{
    $this->markTestIncomplete('This test has not been implemented yet.');
}

/** @test */
public function documents_can_be_created()
{
    $this->markTestIncomplete('This test has not been implemented yet.');
}

/** @test */
public function documents_can_edited()
{
    $this->markTestIncomplete('This test has not been implemented yet.');
}

I currently use snippets to generate the test code without a function name. But I still have to do this for each test, AND type out the test names, which is a bit annoying since they’re in snake_case.

Is there a built-in feature of VS Code that can do this?

2

You can create a snippet like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"Create a test": {
"prefix": "t2",
"body": [
"/** @test */",
"public function ${TM_SELECTED_TEXT/\s/_/g}()",
"{",
"t$this-markTextIncomplete('This test has not been implemented yet.')",
"}",
],
"description": ""
}
</code>
<code>"Create a test": { "prefix": "t2", "body": [ "/** @test */", "public function ${TM_SELECTED_TEXT/\s/_/g}()", "{", "t$this-markTextIncomplete('This test has not been implemented yet.')", "}", ], "description": "" } </code>
"Create a test": {
    "prefix": "t2",
    "body": [
        "/** @test */",
        "public function ${TM_SELECTED_TEXT/\s/_/g}()",
        "{",
        "t$this-markTextIncomplete('This test has not been implemented yet.')",
        "}",
        
    ],
    "description": ""
}

and then either trigger it from that prefix – after selecting each line – or to simplify create a keybinding which will select the line for you and also insert the snippet. Use this keybinding (in your keybindings.json file):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"key": "alt+c", // whatever you want
"command": "runCommands",
"args": {
"commands": [
"cursorHomeSelect", // place cursor at end of each line first
{
"command": "editor.action.insertSnippet",
"args": {
"name": "Create a test",
}
},
]
},
}
</code>
<code>{ "key": "alt+c", // whatever you want "command": "runCommands", "args": { "commands": [ "cursorHomeSelect", // place cursor at end of each line first { "command": "editor.action.insertSnippet", "args": { "name": "Create a test", } }, ] }, } </code>
{
  "key": "alt+c",           // whatever you want
  "command": "runCommands",
  "args": {
    "commands": [
      "cursorHomeSelect",   // place cursor at end of each line first
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "name": "Create a test",
        }
      },
    ]
  },
}


Now if you really want to make one big selection of all the lines and trigger it just once that is going to be harder. Might be possible with an extension that examines the selection and ignores the empty lines.

It’s not as straight forward as I would like, but what I’m going with for now is:

  1. Highlight a space between the words of one plain text test name.
  2. Use Ctrl + D to highlight all the spaces in all the test names.
  3. Type ‘_’ to turn all the test names into snake_case.
  4. Press the Home key to have only one cursor per line.
  5. Highlight the full line, and use Ctrl + X to cut.
  6. Use my existing snippet to add the test code. With a cursor for each test, this adds the correct number of empty tests.
  7. Use Ctrl + V to add the snake_case test names in as function names.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật