File size: 3,114 Bytes
358dd8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
from server.split import split_snippet


def test_only_imports() -> None:
    code = "import A\nimport Mathlib\nimport B"
    result = split_snippet(code)
    assert result.header.splitlines() == ["import Mathlib", "import A", "import B"]
    assert result.body == ""
    assert result.header_line_count == 3


def test_imports_and_body() -> None:
    code = "import X\n\nimport Mathlib\nimport Y\n\ndef foo():\n    pass"
    result = split_snippet(code)
    assert result.header.splitlines() == ["import Mathlib", "import X", "import Y"]
    assert result.body.splitlines() == ["def foo():", "    pass"]
    assert result.header_line_count == 5


def test_no_imports() -> None:
    code = "def bar():\n    return 1"
    result = split_snippet(code)
    assert result.header == ""
    assert result.body == code
    assert result.header_line_count == 0


def test_duplicate_imports() -> None:
    code = "import Mathlib\nimport Mathlib\nimport Z\nimport Z\nZ"
    result = split_snippet(code)
    assert result.header.splitlines() == ["import Mathlib", "import Z"]
    assert result.body.splitlines() == ["Z"]
    assert result.header_line_count == 4


def test_single_mathlib_import() -> None:
    code = "import Mathlib"
    result = split_snippet(code)
    assert result.header == code
    assert result.body == ""
    assert result.header_line_count == 1


def test_single_mathlib_import_with_trailing_spaces() -> None:
    code = """import Mathlib  

theorem one_plus_one : 1 + 1 = 2 := by rfl"""

    result = split_snippet(code)
    assert result.header == "import Mathlib"
    assert result.body == "theorem one_plus_one : 1 + 1 = 2 := by rfl"
    assert result.header_line_count == 2


def test_multiple_imports() -> None:
    code = """import Mathlib
import Aesop

theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""

    result = split_snippet(code)
    assert result.header == "import Mathlib\nimport Aesop"
    assert (
        result.body
        == """theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""
    )
    assert result.header_line_count == 3


def test_multiple_imports_preceded_by_eols() -> None:
    code = """

import Mathlib
import Aesop
theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""

    result = split_snippet(code)
    assert result.header == "import Mathlib\nimport Aesop"
    assert (
        result.body
        == """theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""
    )
    assert result.header_line_count == 4


def test_multiple_separated_imports() -> None:
    code = """import Mathlib

import Aesop

theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""

    result = split_snippet(code)
    assert result.header == "import Mathlib\nimport Aesop"
    assert (
        result.body
        == """theorem one_plus_one : 1 + 1 = 2 := by
  sorry"""
    )
    assert result.header_line_count == 4


def test_multiple_mathlib_imports() -> None:
    code = """import Mathlib.Tactic
import Aesop
import Mathlib.Data.Nat"""
    result = split_snippet(code)
    assert result.header == "import Mathlib\nimport Aesop"
    assert result.body == ""
    assert result.header_line_count == 3