diff options
Diffstat (limited to 'clang/unittests')
| -rw-r--r-- | clang/unittests/Format/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | clang/unittests/Format/SortImportsTestJS.cpp | 201 |
2 files changed, 202 insertions, 0 deletions
diff --git a/clang/unittests/Format/CMakeLists.txt b/clang/unittests/Format/CMakeLists.txt index 4cf8b731a0d..240be6ead2a 100644 --- a/clang/unittests/Format/CMakeLists.txt +++ b/clang/unittests/Format/CMakeLists.txt @@ -9,6 +9,7 @@ add_clang_unittest(FormatTests FormatTestJS.cpp FormatTestProto.cpp FormatTestSelective.cpp + SortImportsTestJS.cpp SortIncludesTest.cpp ) diff --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp new file mode 100644 index 00000000000..ad692e600d3 --- /dev/null +++ b/clang/unittests/Format/SortImportsTestJS.cpp @@ -0,0 +1,201 @@ +//===- unittest/Format/SortImportsTestJS.cpp - JS import sort unit tests --===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "FormatTestUtils.h" +#include "clang/Format/Format.h" +#include "llvm/Support/Debug.h" +#include "gtest/gtest.h" + +#define DEBUG_TYPE "format-test" + +namespace clang { +namespace format { +namespace { + +class SortImportsTestJS : public ::testing::Test { +protected: + std::string sort(StringRef Code, unsigned Offset = 0, unsigned Length = 0) { + StringRef FileName = "input.js"; + if (Length == 0U) + Length = Code.size() - Offset; + std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); + std::string Sorted = + applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName)); + return applyAllReplacements(Sorted, + reformat(Style, Sorted, Ranges, FileName)); + } + + void verifySort(llvm::StringRef Expected, llvm::StringRef Code, + unsigned Offset = 0, unsigned Length = 0) { + std::string Result = sort(Code, Offset, Length); + EXPECT_EQ(Expected.str(), Result) << "Expected:\n" + << Expected << "\nActual:\n" + << Result; + } + + FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); +}; + +TEST_F(SortImportsTestJS, AlreadySorted) { + verifySort("import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "import {sym} from 'c';\n" + "\n" + "let x = 1;", + "import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "import {sym} from 'c';\n" + "\n" + "let x = 1;"); +} + +TEST_F(SortImportsTestJS, BasicSorting) { + verifySort("import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "import {sym} from 'c';\n" + "\n" + "let x = 1;", + "import {sym} from 'a';\n" + "import {sym} from 'c';\n" + "import {sym} from 'b';\n" + "let x = 1;"); +} + +TEST_F(SortImportsTestJS, SeparateMainCodeBody) { + verifySort("import {sym} from 'a';" + "\n" + "let x = 1;\n", + "import {sym} from 'a'; let x = 1;\n"); +} + +TEST_F(SortImportsTestJS, Comments) { + verifySort("/** @fileoverview This is a great file. */\n" + "// A very important import follows.\n" + "import {sym} from 'a'; /* more comments */\n" + "import {sym} from 'b'; // from //foo:bar\n", + "/** @fileoverview This is a great file. */\n" + "import {sym} from 'b'; // from //foo:bar\n" + "// A very important import follows.\n" + "import {sym} from 'a'; /* more comments */\n"); +} + +TEST_F(SortImportsTestJS, SortStar) { + verifySort("import * as foo from 'a';\n" + "import {sym} from 'a';\n" + "import * as bar from 'b';\n", + "import {sym} from 'a';\n" + "import * as foo from 'a';\n" + "import * as bar from 'b';\n"); +} + +TEST_F(SortImportsTestJS, AliasesSymbols) { + verifySort("import {sym1 as alias1} from 'b';\n" + "import {sym2 as alias2, sym3 as alias3} from 'c';\n", + "import {sym2 as alias2, sym3 as alias3} from 'c';\n" + "import {sym1 as alias1} from 'b';\n"); +} + +TEST_F(SortImportsTestJS, GroupImports) { + verifySort("import {a} from 'absolute';\n" + "\n" + "import {b} from '../parent';\n" + "import {b} from '../parent/nested';\n" + "\n" + "import {b} from './relative/path';\n" + "import {b} from './relative/path/nested';\n" + "\n" + "let x = 1;\n", + "import {b} from './relative/path/nested';\n" + "import {b} from './relative/path';\n" + "import {b} from '../parent/nested';\n" + "import {b} from '../parent';\n" + "import {a} from 'absolute';\n" + "let x = 1;\n"); +} + +TEST_F(SortImportsTestJS, Exports) { + verifySort("import {S} from 'bpath';\n" + "\n" + "import {T} from './cpath';\n" + "\n" + "export {A, B} from 'apath';\n" + "export {P} from '../parent';\n" + "export {R} from './relative';\n" + "export {S};\n" + "\n" + "let x = 1;\n" + "export y = 1;\n", + "export {R} from './relative';\n" + "import {T} from './cpath';\n" + "export {S};\n" + "export {A, B} from 'apath';\n" + "import {S} from 'bpath';\n" + "export {P} from '../parent';\n" + "let x = 1;\n" + "export y = 1;\n"); + verifySort("import {S} from 'bpath';\n" + "\n" + "export {T} from 'epath';\n", + "export {T} from 'epath';\n" + "import {S} from 'bpath';\n"); +} + +TEST_F(SortImportsTestJS, SideEffectImports) { + verifySort("import 'ZZside-effect';\n" + "import 'AAside-effect';\n" + "\n" + "import {A} from 'absolute';\n" + "\n" + "import {R} from './relative';\n", + "import {R} from './relative';\n" + "import 'ZZside-effect';\n" + "import {A} from 'absolute';\n" + "import 'AAside-effect';\n"); +} + +TEST_F(SortImportsTestJS, AffectedRange) { + // Sort excluding a suffix. + verifySort("import {sym} from 'b';\n" + "import {sym} from 'c';\n" + "import {sym} from 'a';\n" + "let x = 1;", + "import {sym} from 'c';\n" + "import {sym} from 'b';\n" + "import {sym} from 'a';\n" + "let x = 1;", + 0, 30); + // Sort excluding a prefix. + verifySort("import {sym} from 'c';\n" + "import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "\n" + "let x = 1;", + "import {sym} from 'c';\n" + "import {sym} from 'b';\n" + "import {sym} from 'a';\n" + "\n" + "let x = 1;", + 30, 0); + // Sort a range within imports. + verifySort("import {sym} from 'c';\n" + "import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "import {sym} from 'c';\n" + "let x = 1;", + "import {sym} from 'c';\n" + "import {sym} from 'b';\n" + "import {sym} from 'a';\n" + "import {sym} from 'c';\n" + "let x = 1;", + 24, 30); +} + +} // end namespace +} // end namespace format +} // end namespace clang |

