Skip to content

Commit 9242a81

Browse files
author
AnsonH
committed
test: update end-to-end test to use README's example
1 parent 7172704 commit 9242a81

6 files changed

Lines changed: 89 additions & 77 deletions

File tree

README.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Neovim plugin to copy the reference path of a Python symbol.
44

55
TODO(Anson): Add video
66

7-
## Features
7+
# Features
88

99
- Supports copying different path formats (see [examples](#path-format-examples)):
1010
- Dotted path (e.g. `some.module.func_1`)
@@ -14,7 +14,7 @@ TODO(Anson): Add video
1414
- Allow copy to user-specified register
1515
- No LSP setup required
1616

17-
## Installation
17+
# Installation
1818

1919
Requires Neovim >=0.7.0.
2020

@@ -32,7 +32,7 @@ With [wbthomason/packer.nvim](https://github.com/wbthomason/packer.nvim):
3232
use {"AnsonH/copy-python-path.nvim", tag = "*" }
3333
```
3434

35-
## Getting started
35+
# Getting started
3636

3737
Open a Python file and place the cursor on the following supported symbols:
3838

@@ -47,14 +47,14 @@ Then, run the command `:CopyPythonPath <format>` to copy to clipboard:
4747
- `:CopyPythonPath dotted` - Copies the dotted path (e.g. `some.module.func_1`)
4848
- `:CopyPythonPath import` - Copies the import path (e.g. `from some.module import func_1`)
4949

50-
### Path format examples
50+
## Path format examples
5151

5252
Let's say we have a file called `app.py`:
5353

5454
```py
5555
""" app.py """
5656
import numpy as np
57-
from some.module import foo
57+
from user.models import User
5858

5959
# (1) 👇
6060
def func_1():
@@ -73,32 +73,32 @@ class MyClass:
7373
# (5) 👇
7474
def method_1(self):
7575
# (6) 👇
76-
foo()
76+
User()
7777
# (7) 👇
7878
return np.array([])
7979

8080
# (8) 👇
8181
MODULE_VAR = 'foo'
8282
```
8383

84-
| Cursor Location | `:CopyPythonPath dotted` | `:CopyPythonPath import` |
85-
| ------------------------------ | ------------------------ | ------------------------------ |
86-
| (1) Function definition | `app.func_1` | `from app import func_1` |
87-
| (2) Async function definition | `app.func_2` | `from app import func_2` |
88-
| (3) Class definition | `app.MyClass` | `from app import MyClass` |
89-
| (4) Inner class | `app.MyClass.Meta` | `from app import MyClass`¹ |
90-
| (5) Class method | `app.MyClass.method_1` | `from app import MyClass`¹ |
91-
| (6) Imported symbol | `some.module.foo` | `from some.module import foo`² |
92-
| (7) Imported symbol with alias | `numpy` | `import numpy` |
93-
| (8) Module-level variable | `app.MODULE_VAR` | `from app import MODULE_VAR` |
94-
| Elsewhere in the file | `app` | `from app import ` |
84+
| Cursor Location | `:CopyPythonPath dotted` | `:CopyPythonPath import` |
85+
| ------------------------------ | ------------------------ | ------------------------------- |
86+
| (1) Function definition | `app.func_1` | `from app import func_1` |
87+
| (2) Async function definition | `app.func_2` | `from app import func_2` |
88+
| (3) Class definition | `app.MyClass` | `from app import MyClass` |
89+
| (4) Inner class | `app.MyClass.Meta` | `from app import MyClass`¹ |
90+
| (5) Class method | `app.MyClass.method_1` | `from app import MyClass`¹ |
91+
| (6) Imported symbol | `user.models.User` | `from user.models import User`² |
92+
| (7) Imported symbol with alias | `numpy` | `import numpy` |
93+
| (8) Module-level variable | `app.MODULE_VAR` | `from app import MODULE_VAR` |
94+
| Elsewhere in the file | `app` | `from app import ` |
9595

9696
Notes:
9797

9898
1. Inner classes and class methods cannot be directly imported, so it only imports the outer class.
9999
2. When the symbol is imported, it copies the original path of where it was imported from.
100100

101-
### Custom keymappings
101+
## Custom keymappings
102102

103103
This plugin does NOT set up any keymappings by default. You can define custom keymappings in your Neovim config, for example:
104104

@@ -107,7 +107,7 @@ vim.api.nvim_set_keymap('n', '<Leader>yd', ':CopyPythonPath dotted<CR>', { norem
107107
vim.api.nvim_set_keymap('n', '<Leader>yi', ':CopyPythonPath import<CR>', { noremap = true, silent = true })
108108
```
109109

110-
## Command
110+
# Command
111111

112112
```
113113
:CopyPythonPath <format> [<register>]
@@ -120,14 +120,27 @@ Copies the reference path of the Python symbol under the cursor.
120120
| `format` | The path format to copy | `dotted`, `import` | N.A. (required) |
121121
| `register` | (optional) The register to copy to | Any valid register name | `+` (clipboard) |
122122

123-
## API
123+
# API
124124

125125
The plugin API is available via:
126126

127127
```lua
128128
local copy_python_path = require('copy-python-path')
129129
```
130130

131+
## `get_path_under_cursor`
132+
133+
Gets the Python path of the symbol underneath the cursor.
134+
135+
```lua
136+
--- Gets the Python path of the symbol underneath the cursor.
137+
---@param format string The Python path format. Accepted values are:
138+
--- - `"dotted"`: Dotted path (e.g. `user.models.User`)
139+
--- - `"import"`: Import statement (e.g. `from user.models import User`)
140+
---@return string path
141+
copy_python_path.get_path_under_cursor(format)
142+
```
143+
131144
Example: Copy the shell command for running a Django test:
132145

133146
```lua
@@ -142,7 +155,7 @@ vim.api.nvim_create_user_command("CopyDjangoTestCommand", function(opts)
142155
end, {})
143156
```
144157

145-
## Similar Work
158+
# Similar Work
146159

147160
- [kawamataryo/copy-python-path](https://github.com/kawamataryo/copy-python-path) - VS Code plugin that inspired this project
148161
- [ranelpadon/python-copy-reference.vim](https://github.com/ranelpadon/python-copy-reference.vim) - Vim Script plugin with similar functionality

tests/fixtures/app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
from user.models import User
3+
4+
5+
def func_1():
6+
pass
7+
8+
9+
async def func_2():
10+
pass
11+
12+
13+
class MyClass:
14+
class Meta:
15+
pass
16+
17+
def method_1(self):
18+
User()
19+
return np.array([])
20+
21+
22+
MODULE_VAR = "foo"

tests/fixtures/layer_one/layer_two/services.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/fixtures/root.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/fixtures/user/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from attrs import define
2+
3+
4+
@define
5+
class User:
6+
id: str

tests/test_command.lua

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ local T = MiniTest.new_set({
1515
},
1616
})
1717

18-
local EDIT_ROOT_FILE_COMMAND = "edit tests/fixtures/root.py"
19-
local EDIT_NESTED_FILE_COMMAND = "edit tests/fixtures/layer_one/layer_two/services.py"
18+
local EDIT_ROOT_FILE_COMMAND = "edit tests/fixtures/app.py"
19+
local EDIT_NESTED_FILE_COMMAND = "edit tests/fixtures/user/models.py"
2020

2121
---@param command string Full command to execute
2222
---@param register string Register to check
@@ -40,16 +40,15 @@ T[":CopyPythonPath"]["dotted"] = MiniTest.new_set()
4040
T[":CopyPythonPath"]["dotted"]["root level Python file"] = function()
4141
child.api.nvim_command(EDIT_ROOT_FILE_COMMAND)
4242
local test_cases = {
43-
-- Importable symbols
44-
{ { 5, 5 }, "root.func" },
45-
{ { 9, 16 }, "root.async_func" },
46-
{ { 13, 7 }, "root.OuterClass" },
47-
{ { 18, 13 }, "root.OuterClass.InnerClass.inner_class_method" },
48-
{ { 22, 10 }, "root.MODULE_LEVEL_CONSTANT" },
49-
{ { 15, 16 }, "numpy" },
50-
{ { 6, 5 }, "layer_one.layer_two.services.some_service" },
51-
-- Non-importable symbol
52-
{ { 15, 26 }, "root" },
43+
{ { 5, 5 }, "app.func_1" },
44+
{ { 9, 11 }, "app.func_2" },
45+
{ { 13, 9 }, "app.MyClass" },
46+
{ { 14, 11 }, "app.MyClass.Meta" },
47+
{ { 17, 9 }, "app.MyClass.method_1" },
48+
{ { 18, 9 }, "user.models.User" },
49+
{ { 19, 16 }, "numpy" },
50+
{ { 22, 1 }, "app.MODULE_VAR" },
51+
{ { 6, 5 }, "app" }, -- non-importable symbol
5352
}
5453
run_test_cases("CopyPythonPath dotted a", "a", test_cases)
5554
end
@@ -58,10 +57,10 @@ T[":CopyPythonPath"]["dotted"]["nested Python file"] = function()
5857
child.api.nvim_command(EDIT_NESTED_FILE_COMMAND)
5958
local test_cases = {
6059
-- Importable symbols
61-
{ { 4, 5 }, "layer_one.layer_two.services.some_service" },
62-
{ { 5, 12 }, "numpy" },
60+
{ { 5, 7 }, "user.models.User" },
61+
{ { 4, 4 }, "attrs.define" },
6362
-- Non-importable symbol
64-
{ { 5, 21 }, "layer_one.layer_two.services" },
63+
{ { 6, 9 }, "user.models" },
6564
}
6665
run_test_cases("CopyPythonPath dotted a", "a", test_cases)
6766
end
@@ -71,16 +70,15 @@ T[":CopyPythonPath"]["import"] = MiniTest.new_set()
7170
T[":CopyPythonPath"]["import"]["root level Python file"] = function()
7271
child.api.nvim_command(EDIT_ROOT_FILE_COMMAND)
7372
local test_cases = {
74-
-- Importable symbols
75-
{ { 5, 5 }, "from root import func" },
76-
{ { 9, 16 }, "from root import async_func" },
77-
{ { 13, 7 }, "from root import OuterClass" },
78-
{ { 18, 13 }, "from root import OuterClass" },
79-
{ { 22, 10 }, "from root import MODULE_LEVEL_CONSTANT" },
80-
{ { 15, 16 }, "import numpy" },
81-
{ { 6, 5 }, "from layer_one.layer_two.services import some_service" },
82-
-- Non-importable symbol
83-
{ { 15, 26 }, "from root import " },
73+
{ { 5, 5 }, "from app import func_1" },
74+
{ { 9, 11 }, "from app import func_2" },
75+
{ { 13, 9 }, "from app import MyClass" },
76+
{ { 14, 11 }, "from app import MyClass" }, -- MyClass.Meta
77+
{ { 17, 9 }, "from app import MyClass" }, -- MyClass.method_1
78+
{ { 18, 9 }, "from user.models import User" },
79+
{ { 19, 16 }, "import numpy" },
80+
{ { 22, 1 }, "from app import MODULE_VAR" },
81+
{ { 6, 5 }, "from app import " }, -- non-importable symbol
8482
}
8583
run_test_cases("CopyPythonPath import a", "a", test_cases)
8684
end
@@ -89,10 +87,10 @@ T[":CopyPythonPath"]["import"]["nested Python file"] = function()
8987
child.api.nvim_command(EDIT_NESTED_FILE_COMMAND)
9088
local test_cases = {
9189
-- Importable symbols
92-
{ { 4, 5 }, "from layer_one.layer_two.services import some_service" },
93-
{ { 5, 12 }, "import numpy" },
90+
{ { 5, 7 }, "from user.models import User" },
91+
{ { 4, 4 }, "from attrs import define" },
9492
-- Non-importable symbol
95-
{ { 5, 21 }, "from layer_one.layer_two.services import " },
93+
{ { 6, 9 }, "from user.models import " },
9694
}
9795
run_test_cases("CopyPythonPath import a", "a", test_cases)
9896
end
@@ -104,7 +102,7 @@ T[":CopyPythonPath"]["copies to clipboard if no register is provided"] = functio
104102
child.api.nvim_command("CopyPythonPath dotted")
105103

106104
local dotted_path = child.fn.getreg("+")
107-
expect.equality(dotted_path, "root.func")
105+
expect.equality(dotted_path, "app.func_1")
108106
end
109107

110108
return T

0 commit comments

Comments
 (0)