blob: 46a6c5e0509db6e021d2ee45541093772b812281 [file] [log] [blame]
Bob Nystromf14db242014-09-17 16:33:37 -07001// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
Bob Nystromb3cc8102015-06-19 12:36:37 -07005@TestOn("vm")
Bob Nystromf14db242014-09-17 16:33:37 -07006library dart_style.test.formatter_test;
7
Kevin Moore786d4a02015-06-10 21:25:54 +02008import 'package:test/test.dart';
Bob Nystromf14db242014-09-17 16:33:37 -07009
Bob Nystrom76398fc2014-09-29 09:50:01 -070010import 'package:dart_style/dart_style.dart';
Bob Nystrom3a7a05a2014-09-18 09:51:50 -070011
Bob Nystrom9eb5c5d2018-06-08 12:40:17 -070012import 'utils.dart';
13
Bob Nystrom926a41e2014-11-03 16:51:09 -080014void main() {
Bob Nystromf1dbc592014-11-21 13:22:22 -080015 testDirectory("comments");
Bob Nystromf41c5e02014-11-11 11:21:39 -080016 testDirectory("regression");
Bob Nystrom119b00b2015-01-07 16:36:55 -080017 testDirectory("selections");
Bob Nystrom19dd27e2014-12-04 16:16:12 -080018 testDirectory("splitting");
19 testDirectory("whitespace");
Bob Nystrom34002982014-09-28 22:34:32 -070020
Bob Nystrom926a41e2014-11-03 16:51:09 -080021 test("throws a FormatterException on failed parse", () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070022 var formatter = new DartFormatter();
Bob Nystrom926a41e2014-11-03 16:51:09 -080023 expect(() => formatter.format('wat?!'),
Bob Nystrom8d6511e2018-06-18 14:53:10 -070024 throwsA(new isInstanceOf<FormatterException>()));
Bob Nystrom926a41e2014-11-03 16:51:09 -080025 });
Bob Nystromf14db242014-09-17 16:33:37 -070026
Bob Nystrom94bdd6f2015-06-16 09:12:11 -070027 test("FormatterException.message() does not throw", () {
28 // This is a regression test for #358 where an error whose position is
29 // past the end of the source caused FormatterException to throw.
30 try {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070031 new DartFormatter().format("library");
Bob Nystrom94bdd6f2015-06-16 09:12:11 -070032 } on FormatterException catch (err) {
33 var message = err.message();
34 expect(message, contains("Could not format"));
35 }
36 });
37
Bob Nystrom2419d612014-12-05 13:46:21 -080038 test("FormatterException describes parse errors", () {
39 try {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070040 new DartFormatter().format("""
Bob Nystrom2419d612014-12-05 13:46:21 -080041
42 var a = some error;
43
44 var b = another one;
Bob Nystrom9f9efe22017-07-05 17:10:42 -070045 """, uri: "my_file.dart");
Bob Nystrom2419d612014-12-05 13:46:21 -080046
47 fail("Should throw.");
48 } on FormatterException catch (err) {
49 var message = err.message();
50 expect(message, contains("my_file.dart"));
51 expect(message, contains("line 2"));
52 expect(message, contains("line 4"));
53 }
54 });
55
Bob Nystrom926a41e2014-11-03 16:51:09 -080056 test("adds newline to unit", () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070057 expect(new DartFormatter().format("var x = 1;"), equals("var x = 1;\n"));
Bob Nystrom926a41e2014-11-03 16:51:09 -080058 });
Bob Nystromf14db242014-09-17 16:33:37 -070059
Bob Nystrom926a41e2014-11-03 16:51:09 -080060 test("adds newline to unit after trailing comment", () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070061 expect(new DartFormatter().format("library foo; //zamm"),
Bob Nystrom926a41e2014-11-03 16:51:09 -080062 equals("library foo; //zamm\n"));
63 });
64
65 test("removes extra newlines", () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070066 expect(
67 new DartFormatter().format("var x = 1;\n\n\n"), equals("var x = 1;\n"));
Bob Nystrom926a41e2014-11-03 16:51:09 -080068 });
69
70 test("does not add newline to statement", () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070071 expect(new DartFormatter().formatStatement("var x = 1;"),
72 equals("var x = 1;"));
Bob Nystrom926a41e2014-11-03 16:51:09 -080073 });
74
Bob Nystrom04b36842015-06-25 10:08:05 -070075 test("fails if anything is after the statement", () {
76 try {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070077 new DartFormatter().formatStatement("var x = 1;;");
Bob Nystrom04b36842015-06-25 10:08:05 -070078
79 fail("Should throw.");
Bob Nystromeca548c2016-10-19 14:34:45 -070080 } on FormatterException catch (ex) {
81 expect(ex.errors.length, equals(1));
82 expect(ex.errors.first.offset, equals(10));
Bob Nystrom04b36842015-06-25 10:08:05 -070083 }
84 });
85
Bob Nystrom926a41e2014-11-03 16:51:09 -080086 test('preserves initial indent', () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -070087 var formatter = new DartFormatter(indent: 3);
Bob Nystromefd38862015-06-15 14:04:15 -070088 expect(
Bob Nystrom45682252015-07-30 14:19:08 -070089 formatter.formatStatement('if (foo) {bar;}'),
90 equals(' if (foo) {\n'
Bob Nystromefd38862015-06-15 14:04:15 -070091 ' bar;\n'
92 ' }'));
Bob Nystrom926a41e2014-11-03 16:51:09 -080093 });
Bob Nystrom8eb9f962014-11-03 17:23:56 -080094
Jacob Richman2b241cb2017-04-24 14:06:18 -070095 group("line endings", () {
96 test("uses given line ending", () {
97 // Use zero width no-break space character as the line ending. We have
98 // to use a whitespace character for the line ending as the formatter
99 // will throw an error if it accidentally makes non-whitespace changes
100 // as will occur
101 var lineEnding = "\t";
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700102 expect(new DartFormatter(lineEnding: lineEnding).format("var i = 1;"),
Jacob Richman2b241cb2017-04-24 14:06:18 -0700103 equals("var i = 1;\t"));
Bob Nystrom8eb9f962014-11-03 17:23:56 -0800104 });
105
Bob Nystromb3460d42014-11-04 13:16:36 -0800106 test('infers \\r\\n if the first newline uses that', () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700107 expect(new DartFormatter().format("var\r\ni\n=\n1;\n"),
Bob Nystrom81c715b2015-01-08 17:30:51 -0800108 equals("var i = 1;\r\n"));
Bob Nystrom8eb9f962014-11-03 17:23:56 -0800109 });
110
Bob Nystromb3460d42014-11-04 13:16:36 -0800111 test('infers \\n if the first newline uses that', () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700112 expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"),
Bob Nystrom81c715b2015-01-08 17:30:51 -0800113 equals("var i = 1;\n"));
Bob Nystromb3460d42014-11-04 13:16:36 -0800114 });
115
116 test('defaults to \\n if there are no newlines', () {
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700117 expect(new DartFormatter().format("var i =1;"), equals("var i = 1;\n"));
Bob Nystrom81c715b2015-01-08 17:30:51 -0800118 });
119
120 test('handles Windows line endings in multiline strings', () {
Bob Nystromefd38862015-06-15 14:04:15 -0700121 expect(
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700122 new DartFormatter(lineEnding: "\r\n").formatStatement(' """first\r\n'
Bob Nystrom05712e02015-07-23 10:44:47 -0700123 'second\r\n'
Bob Nystrom45682252015-07-30 14:19:08 -0700124 'third""" ;'),
125 equals('"""first\r\n'
Bob Nystromefd38862015-06-15 14:04:15 -0700126 'second\r\n'
127 'third""";'));
Bob Nystrom8eb9f962014-11-03 17:23:56 -0800128 });
129 });
Jacob Richman2b241cb2017-04-24 14:06:18 -0700130
Bob Nystrom23e67202017-04-25 11:26:28 -0700131 test('throws an UnexpectedOutputException on non-whitespace changes', () {
Jacob Richman2b241cb2017-04-24 14:06:18 -0700132 // Use an invalid line ending character to ensure the formatter will
133 // attempt to make non-whitespace changes.
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700134 var formatter = new DartFormatter(lineEnding: '%');
Jacob Richman2b241cb2017-04-24 14:06:18 -0700135 expect(() => formatter.format("var i = 1;"),
Bob Nystrom8d6511e2018-06-18 14:53:10 -0700136 throwsA(new isInstanceOf<UnexpectedOutputException>()));
Jacob Richman2b241cb2017-04-24 14:06:18 -0700137 });
Bob Nystrom926a41e2014-11-03 16:51:09 -0800138}