001/** 002 * Copyright (c) 2008-2014 Ardor Labs, Inc. 003 * 004 * This file is part of Ardor3D. 005 * 006 * Ardor3D is free software: you can redistribute it and/or modify it 007 * under the terms of its license which may be found in the accompanying 008 * LICENSE file or at <http://www.ardor3d.com/LICENSE>. 009 */ 010 011package com.ardor3d.extension.ui.text.parser; 012 013import java.util.ArrayList; 014import java.util.Iterator; 015import java.util.List; 016import java.util.SortedSet; 017import java.util.TreeSet; 018 019import org.junit.Assert; 020import org.junit.Before; 021import org.junit.Test; 022 023import com.ardor3d.extension.ui.text.StyleConstants; 024import com.ardor3d.extension.ui.text.StyleSpan; 025 026public class ForumLikeMarkupParserTest { 027 028 private ForumLikeMarkupParser parser; 029 030 @Before 031 public void setUp() throws Exception { 032 parser = new ForumLikeMarkupParser(); 033 } 034 035 @Test 036 public void parseWithoutMarkup() { 037 final String text = "A text without any markup what so ever [13] dum di dum [/23]"; 038 final List<StyleSpan> spans = new ArrayList<>(); 039 final String result = parser.parseStyleSpans(text, spans); 040 041 Assert.assertEquals(text, result); 042 043 Assert.assertTrue(spans.isEmpty()); 044 } 045 046 @Test 047 public void parseWithSimpleStyle() throws Exception { 048 final String text = "A text with [size=30]simple markup[/size] dum di dum"; 049 final List<StyleSpan> spans = new ArrayList<>(); 050 final String result = parser.parseStyleSpans(text, spans); 051 052 Assert.assertEquals("A text with simple markup dum di dum", result); 053 054 Assert.assertEquals(1, spans.size()); 055 final StyleSpan span = spans.get(0); 056 Assert.assertEquals(12, span.getSpanStart()); 057 Assert.assertEquals(13, span.getSpanLength()); 058 Assert.assertEquals(StyleConstants.KEY_SIZE, span.getStyle()); 059 Assert.assertEquals(30, span.getValue()); 060 } 061 062 @Test 063 public void parseWithNestedStyle() throws Exception { 064 final String text = "A text [size=30]with [f=arial]simple markup[/f][/size] dum di dum"; 065 final List<StyleSpan> spans = new ArrayList<>(); 066 final String result = parser.parseStyleSpans(text, spans); 067 068 Assert.assertEquals("A text with simple markup dum di dum", result); 069 070 Assert.assertEquals(2, spans.size()); 071 final SortedSet<StyleSpan> sortedSpans = new TreeSet<>(spans); 072 final Iterator<StyleSpan> spanIterator = sortedSpans.iterator(); 073 final StyleSpan span1 = spanIterator.next(); 074 Assert.assertEquals(7, span1.getSpanStart()); 075 Assert.assertEquals(18, span1.getSpanLength()); 076 Assert.assertEquals(StyleConstants.KEY_SIZE, span1.getStyle()); 077 final StyleSpan span2 = spanIterator.next(); 078 Assert.assertEquals(12, span2.getSpanStart()); 079 Assert.assertEquals(13, span2.getSpanLength()); 080 Assert.assertEquals(StyleConstants.KEY_FAMILY, span2.getStyle()); 081 } 082 083 @Test 084 public void parseWithNestedSameStyleBackToBackTags() throws Exception { 085 final String text = "[size=10][size=20]A text [/size]with simple markup[/size] dum di dum"; 086 final List<StyleSpan> spans = new ArrayList<>(); 087 final String result = parser.parseStyleSpans(text, spans); 088 089 Assert.assertEquals("A text with simple markup dum di dum", result); 090 091 Assert.assertEquals(2, spans.size()); 092 final SortedSet<StyleSpan> sortedSpans = new TreeSet<>(spans); 093 final Iterator<StyleSpan> spanIterator = sortedSpans.iterator(); 094 final StyleSpan span1 = spanIterator.next(); 095 Assert.assertEquals(0, span1.getSpanStart()); 096 Assert.assertEquals(25, span1.getSpanLength()); 097 Assert.assertEquals(StyleConstants.KEY_SIZE, span1.getStyle()); 098 Assert.assertEquals(10, span1.getValue()); 099 100 final StyleSpan span2 = spanIterator.next(); 101 Assert.assertEquals(0, span2.getSpanStart()); 102 Assert.assertEquals(7, span2.getSpanLength()); 103 Assert.assertEquals(StyleConstants.KEY_SIZE, span2.getStyle()); 104 Assert.assertEquals(20, span2.getValue()); 105 } 106 107}