forked from redpen-cc/redpen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
redpen-core/src/main/java/cc/redpen/validator/section/ListLevelValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* redpen: a text inspection tool | ||
* Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors | ||
* (see CONTRIBUTORS.md) | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
package cc.redpen.validator.section; | ||
|
||
import cc.redpen.model.ListBlock; | ||
import cc.redpen.model.ListElement; | ||
import cc.redpen.model.Section; | ||
import cc.redpen.validator.Validator; | ||
|
||
/** | ||
* Validate that list elements are not deeper than the given level. | ||
*/ | ||
public class ListLevelValidator extends Validator { | ||
|
||
public ListLevelValidator() { | ||
super("max_level", 5); // Default maximum level of list elements | ||
} | ||
|
||
@Override | ||
public void validate(Section section) { | ||
for (ListBlock listBlock : section.getListBlocks()) { | ||
for (ListElement listElement : listBlock.getListElements()) { | ||
final int maxLevel = getInt("max_level"); | ||
if (listElement.getLevel() > maxLevel) { | ||
addLocalizedError(listElement.getSentence(0), maxLevel); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
redpen-core/src/test/java/cc/redpen/validator/section/ListLevelValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* redpen: a text inspection tool | ||
* Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors | ||
* (see CONTRIBUTORS.md) | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
package cc.redpen.validator.section; | ||
|
||
import cc.redpen.RedPenException; | ||
import cc.redpen.config.Configuration; | ||
import cc.redpen.config.ValidatorConfiguration; | ||
import cc.redpen.model.Section; | ||
import cc.redpen.model.Sentence; | ||
import cc.redpen.validator.ValidationError; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ListLevelValidatorTest { | ||
|
||
private ListLevelValidator validator; | ||
|
||
@Before | ||
public void setUp() throws RedPenException { | ||
validator = new ListLevelValidator(); | ||
validator.preInit(new ValidatorConfiguration("ListLevel").addProperty("max_level", 3), Configuration.builder().build()); | ||
} | ||
|
||
@Test | ||
public void testValid() throws Exception { | ||
Section section = new Section(0, "header"); | ||
section.appendListBlock(); | ||
section.appendListElement(1, Arrays.asList(new Sentence("item1", 1))); | ||
section.appendListElement(2, Arrays.asList(new Sentence("item2", 1))); | ||
section.appendListElement(3, Arrays.asList(new Sentence("item3", 1))); | ||
List<ValidationError> errors = new ArrayList<>(); | ||
validator.setErrorList(errors); | ||
validator.validate(section); | ||
assertEquals(0, errors.size()); | ||
} | ||
|
||
@Test | ||
public void testInvalid() throws Exception { | ||
Section section = new Section(0, "header"); | ||
section.appendListBlock(); | ||
section.appendListElement(1, Arrays.asList(new Sentence("item1", 1))); | ||
section.appendListElement(2, Arrays.asList(new Sentence("item2", 1))); | ||
section.appendListElement(3, Arrays.asList(new Sentence("item3", 1))); | ||
section.appendListElement(4, Arrays.asList(new Sentence("item4", 1))); | ||
List<ValidationError> errors = new ArrayList<>(); | ||
validator.setErrorList(errors); | ||
validator.validate(section); | ||
assertEquals(1, errors.size()); | ||
} | ||
|
||
} |