public Piece(String key, Object value, Syntax syntax) { validatePair(key, value); Preconditions.checkNotNull(syntax, "syntax == null"); this.key = key; this.value = value; this.syntax = syntax; }
public static Kirai from(String string) { return new Kirai(string); }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenValueIsAnEmptyString() { String testKey = "testKey"; String testValue = ""; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
private Kirai(String string) { Preconditions.checkNotEmpty(string, "string is empty"); input = string; if (!isInputBalanced()) { throw new IllegalArgumentException("Braces in provided string are not balanced"); } if (!areTagsValid()) { throw new IllegalArgumentException( "Tags have to start from lower case letter and can contain " + "only lower case letters [a-z] numbers [0-9] and underscore [_]"); } }
public Piece color(String code) { syntax.validateColorCode(code); value = String.format(syntax.getColorFormat(), code, value); return this; }
public Kirai put(Piece piece) { Preconditions.checkNotNull(piece, "piece == null"); if (!tags.contains(piece.getKey())) { throw new IllegalArgumentException( "Tag {" + piece.getKey() + "} was not defined in input string"); } pieces.add(piece); return this; }
@Test(expected = IllegalArgumentException.class) public void testColorCodeShouldBeInValidForZero() { // given String invalidColorCode = "0"; // when syntax.validateColorCode(invalidColorCode); // then an exception is thrown }
@Test(expected = IllegalArgumentException.class) public void testNullStringShouldBeEmpty() { // given String emptyString = null; // when Preconditions.checkNotEmpty(emptyString, "string is empty"); // then // an exception is thrown }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowExceptionWhenKeyIsNotDefinedInInputString() { // given String presentTestTag = "test_tag_present"; String notPresentTestTag = "test_tag_NOT_present"; String testValue = "test value"; String testInputString = "Sample text {" + presentTestTag + "} rest of the sentence."; // when Kirai.from(testInputString).put(notPresentTestTag, testValue); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenStringIsNotBeBalanced() { // given String testTagOne = "tag_one"; String testTagTwo = "tag_two"; String testNotBalancedString = "Sample text } {{ {" + testTagOne + "} and {" + testTagTwo + "} { rest of the sentence."; // when Kirai.from(testNotBalancedString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenValueIsNull() { String testKey = "testKey"; String testValue = null; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testColorCodeShouldBeInValidForNumberLowerThanZero() { // given String invalidColorCode = "-3"; // when syntax.validateColorCode(invalidColorCode); // then an exception is thrown }
@Test(expected = IllegalArgumentException.class) public void testStringShouldBeEmpty() { // given String emptyString = ""; // when Preconditions.checkNotEmpty(emptyString, "string is empty"); // then // an exception is thrown }
@Test(expected = IllegalArgumentException.class) public void testFromShouldThrowExceptionWhenKeyStartsFromNumber() { // given String incorrectTestTag = "1_incorrect_tag"; String testInputString = "Sample text {" + incorrectTestTag + "} rest of the sentence."; // when Kirai.from(testInputString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenKeyIsNull() { // given String testKey = null; String testValue = "test value"; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testColorCodeShouldBeInValidForNumberGreaterThan256() { // given String invalidColorCode = "258"; // when syntax.validateColorCode(invalidColorCode); // then an exception is thrown }
@Test(expected = IllegalArgumentException.class) public void testFromShouldThrowExceptionWhenKeyContainsSpecialCharacters() { // given String incorrectTestTag = "#!@ tag &^*_"; String testInputString = "Sample text {" + incorrectTestTag + "} rest of the sentence."; // when Kirai.from(testInputString); // then // throw an exception }
@Test(expected = IllegalArgumentException.class) public void testPutShouldThrowAnExceptionWhenKeyIsAnEmptyString() { // given String testKey = ""; String testValue = "test value"; String testInputString = "Sample text {" + testKey + "} rest of the sentence."; // when Kirai.from(testInputString).put(testKey, testValue).format(); // then throw an exception }
@Test(expected = IllegalArgumentException.class) public void testShouldThrowExceptionWhenStringHasNoEndingBrace() { // given String testTag = "tag_one"; String testNotBalancedString = "{" + testTag; // when Kirai.from(testNotBalancedString); // then // throw an exception }