summaryrefslogtreecommitdiff
path: root/python/testSrc/com/jetbrains/python/PyPathEvaluatorTest.java
blob: 592b820355cd307474c16173efae3eb601679d22 (plain)
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
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python;

import com.intellij.idea.RecordExecution;
import com.intellij.openapi.util.io.FileUtil;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyPathEvaluator;

import java.util.List;


@RecordExecution(includePackages = {"com.jetbrains.python.**"})
public class PyPathEvaluatorTest extends PyTestCase {
  public void testDirName() {
    assertEquals("/foo/bar", doEvaluate("os.path.dirname(__file__)", "/foo/bar/baz.py"));
  }

  public void testOsPathJoin() {
    assertEquals("/foo/bar/db.sqlite3", doEvaluate("os.path.join(__file__, 'db.sqlite3'", "/foo/bar"));
  }

  public void testNormPath() {  // PY-10194
    assertEquals("/foo/bar/baz.py", doEvaluate("os.path.normpath(__file__)", "/foo/bar/baz.py"));
  }

  public void testReplace() {
    assertEquals("/foo/Bar/Baz.py", doEvaluate("__file__.replace('b', 'B')", "/foo/bar/baz.py"));
  }

  public void testConstants() {
    myFixture.configureByText(PythonFileType.INSTANCE, "ROOT_PATH = '/foo'\nTEMPLATES_DIR = os.path.join(ROOT_PATH, 'templates')");
    PyFile file = (PyFile) myFixture.getFile();
    final PyTargetExpression expression = file.findTopLevelAttribute("TEMPLATES_DIR");
    final PyExpression value = expression.findAssignedValue();
    final String result = FileUtil.toSystemIndependentName((String) new PyPathEvaluator("").evaluate(value));
    assertEquals(result, "/foo/templates");
  }

  public void testList() {
    final PyElementGenerator generator = PyElementGenerator.getInstance(myFixture.getProject());
    final PyExpression expression = generator.createExpressionFromText(LanguageLevel.getLatest(), "['a' + 'b'] + ['c']");
    List<Object> result = (List<Object>) new PyPathEvaluator("").evaluate(expression);
    assertEquals(2, result.size());
    assertEquals("ab", result.get(0));
    assertEquals("c", result.get(1));
  }

  public void testParDir() {
    assertEquals("/foo/subfolder/../bar.py", doEvaluate("os.path.abspath(os.path.join(os.path.join('/foo/subfolder',  os.path.pardir, 'bar.py')))", "/foo/bar.py"));
  }

  private String doEvaluate(final String text, final String file) {
    final PyElementGenerator generator = PyElementGenerator.getInstance(myFixture.getProject());
    final PyExpression expression = generator.createExpressionFromText(LanguageLevel.getLatest(), text);
    return FileUtil.toSystemIndependentName((String) new PyPathEvaluator(file).evaluate(expression));
  }
}