CheckCaptions extends Rule { @Override public List<Advice> evaluate(AnnotatedString s, AnnotatedString original) { List<Advice> out_list = new ArrayList<Advice>(); List<String> lines = s.getLines(); for (int line_cnt = 0; line_cnt < lines.size(); line_cnt++) { String line = lines.get(line_cnt); int start_pos = line.indexOf("\\caption"); if (start_pos < 0) { continue; } if (line.substring(start_pos).matches("\\\\captionsetup.*")) { continue; } boolean period_seen = false; int level = 0; for (int i = start_pos + 1; i < line.length(); i++) { String c = line.substring(i, i + 1); if (c.compareTo("{") == 0) { level++; period_seen = false; } else if (c.compareTo("}") == 0) { level--; if (level == 0 && !period_seen) { Position start_p = s.getSourcePosition(new Position(line_cnt, start_pos)); Position end_p = s.getSourcePosition(new Position(line_cnt, i)); Range r = new Range(start_p, end_p); out_list.add(new Advice(this, r, "A caption should end with a period", original.getResourceName(), original.getLine(line_cnt), original.getOffset(start_p))); break; } period_seen = false; } else if (c.compareTo(".") == 0) { period_seen = true; } else if (c.compareTo(" ") != 0) { period_seen = false; } } } return out_list; } CheckCaptions(); @Override List<Advice> evaluate(AnnotatedString s, AnnotatedString original); @Override String getDescription();  }
@Test public void test1() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\caption{Hello world }")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(1, ad_list.size()); }
@Test public void test2() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\caption{Hello world. }")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(0, ad_list.size()); }
@Test public void test3() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\caption{Hello \textbf{world }}")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(1, ad_list.size()); }
@Test public void test4() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\caption{Hello \textbf{world.}}")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(1, ad_list.size()); }
@Test public void test5() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\caption{Hello \textbf{world }.}")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(0, ad_list.size()); }
@Test public void test6() { CheckCaptions rule = new CheckCaptions(); AnnotatedString as = AnnotatedString.read(new Scanner("\\captionsetup{whatever}")); List<Advice> ad_list = rule.evaluate(as, as); assertEquals(0, ad_list.size()); }