| // Copyright 2020 Garett Tok Ern Liang |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| import 'package:test/test.dart'; |
| import 'package:yaml_edit/yaml_edit.dart'; |
| |
| import 'test_utils.dart'; |
| |
| void main() { |
| group('throws PathError', () { |
| test('if it is a map', () { |
| final doc = YamlEditor('a:1'); |
| expect(() => doc.insertIntoList([], 0, 4), throwsPathError); |
| }); |
| |
| test('if it is a scalar', () { |
| final doc = YamlEditor('1'); |
| expect(() => doc.insertIntoList([], 0, 4), throwsPathError); |
| }); |
| }); |
| |
| test('throws RangeError if index is out of range', () { |
| final doc = YamlEditor('[1, 2]'); |
| expect(() => doc.insertIntoList([], -1, 0), throwsRangeError); |
| expect(() => doc.insertIntoList([], 3, 0), throwsRangeError); |
| }); |
| |
| group('block list', () { |
| test('(1)', () { |
| final doc = YamlEditor(''' |
| - 1 |
| - 2'''); |
| doc.insertIntoList([], 0, 0); |
| expect(doc.toString(), equals(''' |
| - 0 |
| - 1 |
| - 2''')); |
| expectYamlBuilderValue(doc, [0, 1, 2]); |
| }); |
| |
| test('(2)', () { |
| final doc = YamlEditor(''' |
| - 1 |
| - 2'''); |
| doc.insertIntoList([], 1, 3); |
| expect(doc.toString(), equals(''' |
| - 1 |
| - 3 |
| - 2''')); |
| expectYamlBuilderValue(doc, [1, 3, 2]); |
| }); |
| |
| test('(3)', () { |
| final doc = YamlEditor(''' |
| - 1 |
| - 2 |
| '''); |
| doc.insertIntoList([], 2, 3); |
| expect(doc.toString(), equals(''' |
| - 1 |
| - 2 |
| - 3 |
| ''')); |
| expectYamlBuilderValue(doc, [1, 2, 3]); |
| }); |
| |
| test('(4)', () { |
| final doc = YamlEditor(''' |
| - 1 |
| - 3 |
| '''); |
| doc.insertIntoList([], 1, [4, 5, 6]); |
| expect(doc.toString(), equals(''' |
| - 1 |
| - - 4 |
| - 5 |
| - 6 |
| - 3 |
| ''')); |
| expectYamlBuilderValue(doc, [ |
| 1, |
| [4, 5, 6], |
| 3 |
| ]); |
| }); |
| |
| test(' with comments', () { |
| final doc = YamlEditor(''' |
| - 0 # comment a |
| - 2 # comment b |
| '''); |
| doc.insertIntoList([], 1, 1); |
| expect(doc.toString(), equals(''' |
| - 0 # comment a |
| - 1 |
| - 2 # comment b |
| ''')); |
| expectYamlBuilderValue(doc, [0, 1, 2]); |
| }); |
| }); |
| |
| group('flow list', () { |
| test('(1)', () { |
| final doc = YamlEditor('[1, 2]'); |
| doc.insertIntoList([], 0, 0); |
| expect(doc.toString(), equals('[0, 1, 2]')); |
| expectYamlBuilderValue(doc, [0, 1, 2]); |
| }); |
| |
| test('(2)', () { |
| final doc = YamlEditor('[1, 2]'); |
| doc.insertIntoList([], 1, 3); |
| expect(doc.toString(), equals('[1, 3, 2]')); |
| expectYamlBuilderValue(doc, [1, 3, 2]); |
| }); |
| |
| test('(3)', () { |
| final doc = YamlEditor('[1, 2]'); |
| doc.insertIntoList([], 2, 3); |
| expect(doc.toString(), equals('[1, 2, 3]')); |
| expectYamlBuilderValue(doc, [1, 2, 3]); |
| }); |
| |
| test('(4)', () { |
| final doc = YamlEditor('["[],", "[],"]'); |
| doc.insertIntoList([], 1, 'test'); |
| expect(doc.toString(), equals('["[],", test, "[],"]')); |
| expectYamlBuilderValue(doc, ['[],', 'test', '[],']); |
| }); |
| }); |
| } |